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.

218 lines
7.4 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. Texture2D whiteTexture;
  30. Player player;
  31. World world;
  32. LinesOfSight linesOfSight;
  33. readonly Camera camera = new Camera();
  34. public SneakGame() {
  35. graphics = new GraphicsDeviceManager(this) {
  36. SynchronizeWithVerticalRetrace = true,
  37. GraphicsProfile = GraphicsProfile.HiDef
  38. };
  39. IsFixedTimeStep = true;
  40. TargetElapsedTime = TimeSpan.FromSeconds(TARGET_FRAME_TIME);
  41. IsMouseVisible = true;
  42. Content.RootDirectory = "Content";
  43. }
  44. // Performs initialization that's needed before starting to run.
  45. protected override void Initialize() {
  46. display = (IDisplay) Services.GetService(typeof(IDisplay));
  47. display.Initialize(Window, graphics);
  48. display.SetFullScreen(fullScreen);
  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. spriteBatch = new SpriteBatch(GraphicsDevice);
  68. font = Content.Load<SpriteFont>("font");
  69. player = new Player(Content.Load<Texture2D>("Ninja_Female"));
  70. world = new World(Content.Load<Texture2D>("grassland"), Levels.ONE_ONE);
  71. linesOfSight = new LinesOfSight(GraphicsDevice);
  72. grasslandBg1 = Content.Load<Texture2D>("grassland_bg1");
  73. grasslandBg2 = Content.Load<Texture2D>("grassland_bg2");
  74. whiteTexture = new Texture2D(GraphicsDevice, 1, 1);
  75. whiteTexture.SetData(new Color[] { Color.White });
  76. Debug.Initialize(whiteTexture);
  77. }
  78. // Called once per game. Unloads all game content.
  79. protected override void UnloadContent() {
  80. whiteTexture.Dispose();
  81. updateTimer.DumpStats();
  82. drawTimer.DumpStats();
  83. }
  84. // Updates the game world.
  85. protected override void Update(GameTime gameTime) {
  86. updateTimer.Start();
  87. input.Add(new Input(GamePad.GetState(PlayerIndex.One), Keyboard.GetState()));
  88. if (input[0].Exit) {
  89. Exit();
  90. }
  91. if (input[0].Pause && !input[1].Pause) {
  92. paused = !paused;
  93. }
  94. if (input[0].FullScreen && !input[1].FullScreen) {
  95. fullScreen = !fullScreen;
  96. display.SetFullScreen(fullScreen);
  97. }
  98. Debug.Clear(paused);
  99. if (input[0].Debug && !input[1].Debug) {
  100. Debug.Enabled = !Debug.Enabled;
  101. }
  102. if (!paused) {
  103. float modelTime = (float) gameTime.ElapsedGameTime.TotalSeconds;
  104. Clock.AddModelTime(modelTime);
  105. player.Update(modelTime, input, world.CollisionTargets);
  106. linesOfSight.Update(player, world.CollisionTargets);
  107. camera.Update(player.Position, world.Width);
  108. }
  109. base.Update(gameTime);
  110. updateTimer.Stop();
  111. }
  112. // Called when the game should draw itself.
  113. protected override void Draw(GameTime gameTime) {
  114. drawTimer.Start();
  115. if (framesToSuppress > 0 && !gameTime.IsRunningSlowly) {
  116. framesToSuppress--;
  117. }
  118. // We need to update the FPS counter in Draw() since Update() might get called more
  119. // frequently, especially when gameTime.IsRunningSlowly.
  120. fpsCounter.Update();
  121. string fpsText = $"{GraphicsDevice.Viewport.Width}x{GraphicsDevice.Viewport.Height}, " +
  122. $"{fpsCounter.Fps} FPS";
  123. if (paused) {
  124. fpsText += " (paused)";
  125. }
  126. Debug.SetFpsText(fpsText);
  127. // Draw scene to sceneTarget.
  128. GraphicsDevice.SetRenderTarget(sceneTarget);
  129. GraphicsDevice.Clear(Color.CornflowerBlue);
  130. spriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.LinearWrap, null, null);
  131. // Draw background.
  132. Rectangle bgSource = new Rectangle(
  133. (int) (camera.Left * 0.25), 0, camera.Width, camera.Height);
  134. Rectangle bgTarget = new Rectangle(0, 0, camera.Width, camera.Height);
  135. spriteBatch.Draw(grasslandBg2, bgTarget, bgSource, Color.White);
  136. bgSource = new Rectangle(
  137. (int) (camera.Left * 0.5), 0, camera.Width, camera.Height);
  138. spriteBatch.Draw(grasslandBg1, bgTarget, bgSource, Color.White);
  139. spriteBatch.End();
  140. // Set up transformation matrix for drawing world objects.
  141. Matrix transform = Matrix.CreateTranslation(-camera.Left, -camera.Top, 0);
  142. spriteBatch.Begin(
  143. SpriteSortMode.Deferred, null, SamplerState.LinearWrap, null, null, null, transform);
  144. // Draw player.
  145. player.Draw(spriteBatch);
  146. // Draw foreground tiles.
  147. world.Draw(spriteBatch);
  148. // Draw debug rects & lines.
  149. Debug.Draw(spriteBatch);
  150. // Aaaaand we're done.
  151. spriteBatch.End();
  152. // Draw lighting to lightingTarget.
  153. GraphicsDevice.SetRenderTarget(lightingTarget);
  154. GraphicsDevice.Clear(new Color(0, 0, 0, 0f));
  155. lightingEffect.Projection = camera.Projection;
  156. linesOfSight.Draw(player, world.CollisionTargets, GraphicsDevice, lightingEffect);
  157. // Draw sceneTarget to screen.
  158. GraphicsDevice.SetRenderTarget(null);
  159. GraphicsDevice.Clear(Color.CornflowerBlue);
  160. if (framesToSuppress == 0) {
  161. spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend,
  162. SamplerState.PointClamp, DepthStencilState.Default,
  163. RasterizerState.CullNone);
  164. Rectangle drawRect = new Rectangle(
  165. 0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);
  166. spriteBatch.Draw(sceneTarget, drawRect, Color.White);
  167. spriteBatch.Draw(lightingTarget, drawRect, Color.White);
  168. // Draw debug toasts.
  169. Debug.DrawToasts(spriteBatch, font);
  170. spriteBatch.End();
  171. }
  172. base.Draw(gameTime);
  173. drawTimer.Stop();
  174. }
  175. }
  176. }