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.

152 lines
4.5 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. bool fullScreen = false;
  12. bool paused = false;
  13. IDisplay display;
  14. readonly History<Input> input = new History<Input>(2);
  15. readonly FpsCounter fpsCounter = new FpsCounter();
  16. readonly Timer updateTimer = new Timer(TARGET_FRAME_TIME / 2.0, "UpdateTimer");
  17. readonly Timer drawTimer = new Timer(TARGET_FRAME_TIME / 2.0, "DrawTimer");
  18. // Draw() needs to be called without IsRunningSlowly this many times before we actually
  19. // attempt to draw the scene. This is a workaround for the fact that otherwise the first few
  20. // frames can be really slow to draw.
  21. int framesToSuppress;
  22. Scene scene;
  23. Player player;
  24. World world;
  25. LinesOfSight linesOfSight;
  26. Camera camera = new Camera();
  27. public SneakGame() {
  28. graphics = new GraphicsDeviceManager(this) {
  29. SynchronizeWithVerticalRetrace = true,
  30. GraphicsProfile = GraphicsProfile.HiDef
  31. };
  32. IsFixedTimeStep = true;
  33. TargetElapsedTime = TimeSpan.FromSeconds(TARGET_FRAME_TIME);
  34. IsMouseVisible = true;
  35. Content.RootDirectory = "Content";
  36. }
  37. // Performs initialization that's needed before starting to run.
  38. protected override void Initialize() {
  39. display = (IDisplay) Services.GetService(typeof(IDisplay));
  40. display.Initialize(Window, graphics);
  41. display.SetFullScreen(fullScreen);
  42. Debug.Initialize(GraphicsDevice);
  43. RasterizerState rasterizerState = new RasterizerState() {
  44. CullMode = CullMode.None
  45. };
  46. GraphicsDevice.RasterizerState = rasterizerState;
  47. base.Initialize();
  48. }
  49. // Called once per game. Loads all game content.
  50. protected override void LoadContent() {
  51. base.LoadContent();
  52. linesOfSight = new LinesOfSight(GraphicsDevice);
  53. LoadLevel();
  54. }
  55. private void LoadLevel() {
  56. framesToSuppress = 2;
  57. camera = new Camera();
  58. player = new Player(Content.Load<Texture2D>("Ninja_Female"));
  59. world = new World(Content.Load<Texture2D>("grassland"), Levels.ONE_ONE);
  60. scene = new Scene(GraphicsDevice, camera, Content);
  61. }
  62. // Called once per game. Unloads all game content.
  63. protected override void UnloadContent() {
  64. base.UnloadContent();
  65. updateTimer.DumpStats();
  66. drawTimer.DumpStats();
  67. }
  68. // Updates the game world.
  69. protected override void Update(GameTime gameTime) {
  70. updateTimer.Start();
  71. input.Add(new Input(GamePad.GetState(PlayerIndex.One), Keyboard.GetState()));
  72. if (input[0].Exit) {
  73. Exit();
  74. }
  75. if (input[0].Pause && !input[1].Pause) {
  76. paused = !paused;
  77. }
  78. if (input[0].FullScreen && !input[1].FullScreen) {
  79. fullScreen = !fullScreen;
  80. display.SetFullScreen(fullScreen);
  81. }
  82. if (input[0].Restart && !input[1].Restart) {
  83. LoadLevel();
  84. }
  85. Debug.Clear(paused);
  86. if (input[0].Debug && !input[1].Debug) {
  87. Debug.Enabled = !Debug.Enabled;
  88. }
  89. if (!paused) {
  90. float modelTime = (float) gameTime.ElapsedGameTime.TotalSeconds;
  91. Clock.AddModelTime(modelTime);
  92. player.Update(modelTime, input, world.CollisionTargets);
  93. linesOfSight.Update(player, world.CollisionTargets);
  94. camera.Update(player.Position, world.Width);
  95. }
  96. base.Update(gameTime);
  97. updateTimer.Stop();
  98. }
  99. // Called when the game should draw itself.
  100. protected override void Draw(GameTime gameTime) {
  101. drawTimer.Start();
  102. // Enable the scene after we've gotten enough non-slow frames.
  103. if (framesToSuppress > 0 && !gameTime.IsRunningSlowly) {
  104. framesToSuppress--;
  105. if (framesToSuppress == 0) {
  106. scene.Enabled = true;
  107. }
  108. }
  109. // We need to update the FPS counter in Draw() since Update() might get called more
  110. // frequently, especially when gameTime.IsRunningSlowly.
  111. fpsCounter.Update();
  112. string fpsText = $"{GraphicsDevice.Viewport.Width}x{GraphicsDevice.Viewport.Height}, " +
  113. $"{fpsCounter.Fps} FPS";
  114. if (paused) {
  115. fpsText += " (paused)";
  116. }
  117. Debug.SetFpsText(fpsText);
  118. scene.Draw(world, player, linesOfSight);
  119. base.Draw(gameTime);
  120. drawTimer.Stop();
  121. }
  122. }
  123. }