A stealth-based 2D platformer where you don't have to kill anyone unless you want to. https://www.semicolin.games
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

215 lines
7.3 KiB

  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using Microsoft.Xna.Framework.Input;
  4. using System;
  5. using System.Collections.Generic;
  6. namespace SemiColinGames {
  7. public class SneakGame : Game {
  8. const int TARGET_FPS = 60;
  9. const double TARGET_FRAME_TIME = 1.0 / TARGET_FPS;
  10. readonly GraphicsDeviceManager graphics;
  11. RenderTarget2D sceneTarget;
  12. RenderTarget2D lightingTarget;
  13. BasicEffect lightingEffect;
  14. SpriteBatch spriteBatch;
  15. SpriteFont font;
  16. bool fullScreen = false;
  17. bool paused = false;
  18. IDisplay display;
  19. readonly History<Input> input = new History<Input>(2);
  20. readonly FpsCounter fpsCounter = new FpsCounter();
  21. readonly Timer updateTimer = new Timer(TARGET_FRAME_TIME / 2.0, "UpdateTimer");
  22. readonly Timer drawTimer = new Timer(TARGET_FRAME_TIME / 2.0, "DrawTimer");
  23. // Draw() needs to be called without IsRunningSlowly this many times before we actually
  24. // attempt to draw the scene. This is a workaround for the fact that otherwise the first few
  25. // frames can be really slow to draw.
  26. int framesToSuppress = 2;
  27. Texture2D grasslandBg1;
  28. Texture2D grasslandBg2;
  29. Player player;
  30. World world;
  31. LinesOfSight linesOfSight;
  32. readonly Camera camera = new Camera();
  33. public SneakGame() {
  34. graphics = new GraphicsDeviceManager(this) {
  35. SynchronizeWithVerticalRetrace = true,
  36. GraphicsProfile = GraphicsProfile.HiDef
  37. };
  38. IsFixedTimeStep = true;
  39. TargetElapsedTime = TimeSpan.FromSeconds(TARGET_FRAME_TIME);
  40. IsMouseVisible = true;
  41. Content.RootDirectory = "Content";
  42. }
  43. // Performs initialization that's needed before starting to run.
  44. protected override void Initialize() {
  45. display = (IDisplay) Services.GetService(typeof(IDisplay));
  46. display.Initialize(Window, graphics);
  47. display.SetFullScreen(fullScreen);
  48. Debug.Initialize(GraphicsDevice);
  49. sceneTarget = new RenderTarget2D(
  50. GraphicsDevice, camera.Width, camera.Height, false /* mipmap */,
  51. GraphicsDevice.PresentationParameters.BackBufferFormat, DepthFormat.Depth24);
  52. lightingTarget = new RenderTarget2D(
  53. GraphicsDevice, camera.Width, camera.Height, false /* mipmap */,
  54. GraphicsDevice.PresentationParameters.BackBufferFormat, DepthFormat.Depth24);
  55. lightingEffect = new BasicEffect(GraphicsDevice);
  56. lightingEffect.World = Matrix.CreateTranslation(0, 0, 0);
  57. lightingEffect.View = Matrix.CreateLookAt(Vector3.Backward, Vector3.Zero, Vector3.Up);
  58. lightingEffect.VertexColorEnabled = true;
  59. RasterizerState rasterizerState = new RasterizerState() {
  60. CullMode = CullMode.None
  61. };
  62. GraphicsDevice.RasterizerState = rasterizerState;
  63. base.Initialize();
  64. }
  65. // Called once per game. Loads all game content.
  66. protected override void LoadContent() {
  67. base.LoadContent();
  68. spriteBatch = new SpriteBatch(GraphicsDevice);
  69. font = Content.Load<SpriteFont>("font");
  70. player = new Player(Content.Load<Texture2D>("Ninja_Female"));
  71. world = new World(Content.Load<Texture2D>("grassland"), Levels.ONE_ONE);
  72. linesOfSight = new LinesOfSight(GraphicsDevice);
  73. grasslandBg1 = Content.Load<Texture2D>("grassland_bg1");
  74. grasslandBg2 = Content.Load<Texture2D>("grassland_bg2");
  75. }
  76. // Called once per game. Unloads all game content.
  77. protected override void UnloadContent() {
  78. base.UnloadContent();
  79. updateTimer.DumpStats();
  80. drawTimer.DumpStats();
  81. }
  82. // Updates the game world.
  83. protected override void Update(GameTime gameTime) {
  84. updateTimer.Start();
  85. input.Add(new Input(GamePad.GetState(PlayerIndex.One), Keyboard.GetState()));
  86. if (input[0].Exit) {
  87. Exit();
  88. }
  89. if (input[0].Pause && !input[1].Pause) {
  90. paused = !paused;
  91. }
  92. if (input[0].FullScreen && !input[1].FullScreen) {
  93. fullScreen = !fullScreen;
  94. display.SetFullScreen(fullScreen);
  95. }
  96. Debug.Clear(paused);
  97. if (input[0].Debug && !input[1].Debug) {
  98. Debug.Enabled = !Debug.Enabled;
  99. }
  100. if (!paused) {
  101. float modelTime = (float) gameTime.ElapsedGameTime.TotalSeconds;
  102. Clock.AddModelTime(modelTime);
  103. player.Update(modelTime, input, world.CollisionTargets);
  104. linesOfSight.Update(player, world.CollisionTargets);
  105. camera.Update(player.Position, world.Width);
  106. }
  107. base.Update(gameTime);
  108. updateTimer.Stop();
  109. }
  110. // Called when the game should draw itself.
  111. protected override void Draw(GameTime gameTime) {
  112. drawTimer.Start();
  113. if (framesToSuppress > 0 && !gameTime.IsRunningSlowly) {
  114. framesToSuppress--;
  115. }
  116. // We need to update the FPS counter in Draw() since Update() might get called more
  117. // frequently, especially when gameTime.IsRunningSlowly.
  118. fpsCounter.Update();
  119. string fpsText = $"{GraphicsDevice.Viewport.Width}x{GraphicsDevice.Viewport.Height}, " +
  120. $"{fpsCounter.Fps} FPS";
  121. if (paused) {
  122. fpsText += " (paused)";
  123. }
  124. Debug.SetFpsText(fpsText);
  125. // Draw scene to sceneTarget.
  126. GraphicsDevice.SetRenderTarget(sceneTarget);
  127. GraphicsDevice.Clear(Color.CornflowerBlue);
  128. spriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.LinearWrap, null, null);
  129. // Draw background.
  130. Rectangle bgSource = new Rectangle(
  131. (int) (camera.Left * 0.25), 0, camera.Width, camera.Height);
  132. Rectangle bgTarget = new Rectangle(0, 0, camera.Width, camera.Height);
  133. spriteBatch.Draw(grasslandBg2, bgTarget, bgSource, Color.White);
  134. bgSource = new Rectangle(
  135. (int) (camera.Left * 0.5), 0, camera.Width, camera.Height);
  136. spriteBatch.Draw(grasslandBg1, bgTarget, bgSource, Color.White);
  137. spriteBatch.End();
  138. // Set up transformation matrix for drawing world objects.
  139. Matrix transform = Matrix.CreateTranslation(-camera.Left, -camera.Top, 0);
  140. spriteBatch.Begin(
  141. SpriteSortMode.Deferred, null, SamplerState.LinearWrap, null, null, null, transform);
  142. // Draw player.
  143. player.Draw(spriteBatch);
  144. // Draw foreground tiles.
  145. world.Draw(spriteBatch);
  146. // Aaaaand we're done.
  147. spriteBatch.End();
  148. // Draw lighting to lightingTarget.
  149. GraphicsDevice.SetRenderTarget(lightingTarget);
  150. GraphicsDevice.Clear(new Color(0, 0, 0, 0f));
  151. lightingEffect.Projection = camera.Projection;
  152. linesOfSight.Draw(player, world.CollisionTargets, GraphicsDevice, lightingEffect);
  153. // Draw debug rects & lines on top.
  154. Debug.Draw(GraphicsDevice, lightingEffect);
  155. // Draw sceneTarget to screen.
  156. GraphicsDevice.SetRenderTarget(null);
  157. GraphicsDevice.Clear(Color.CornflowerBlue);
  158. if (framesToSuppress == 0) {
  159. spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend,
  160. SamplerState.PointClamp, DepthStencilState.Default,
  161. RasterizerState.CullNone);
  162. Rectangle drawRect = new Rectangle(
  163. 0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);
  164. spriteBatch.Draw(sceneTarget, drawRect, Color.White);
  165. spriteBatch.Draw(lightingTarget, drawRect, Color.White);
  166. // Draw debug toasts.
  167. Debug.DrawToasts(spriteBatch, font);
  168. spriteBatch.End();
  169. }
  170. base.Draw(gameTime);
  171. drawTimer.Stop();
  172. }
  173. }
  174. }