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.

129 lines
3.3 KiB

  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using System;
  4. using System.Collections.Generic;
  5. namespace SemiColinGames {
  6. class IdleState : IState<Object> {
  7. private readonly NPC npc;
  8. private float timeInState = 0;
  9. public IdleState(NPC npc) {
  10. this.npc = npc;
  11. }
  12. public void Enter() {
  13. timeInState = 0;
  14. }
  15. public string Update(float modelTime, SneakWorld world, Object _) {
  16. timeInState += modelTime;
  17. if (timeInState > 1.0f) {
  18. npc.Facing *= -1;
  19. return "run";
  20. }
  21. return null;
  22. }
  23. }
  24. class RunState : IState<Object> {
  25. private readonly NPC npc;
  26. public RunState(NPC npc) {
  27. this.npc = npc;
  28. }
  29. public void Enter() {}
  30. public string Update(float modelTime, SneakWorld world, Object _) {
  31. float moveSpeed = 120;
  32. float desiredX = npc.Position.X + moveSpeed * npc.Facing * modelTime;
  33. float testPoint = desiredX + npc.Box.HalfSize.X * npc.Facing;
  34. AABB npcBox = new AABB(
  35. new Vector2(testPoint, npc.Position.Y + 1), new Vector2(1, npc.Box.HalfSize.Y));
  36. Debug.AddRect(npcBox, Color.Cyan);
  37. bool foundBox = false;
  38. foreach (AABB box in world.CollisionTargets) {
  39. if (box.Intersect(npcBox) != null) {
  40. foundBox = true;
  41. break;
  42. }
  43. }
  44. if (!foundBox) {
  45. return "idle";
  46. }
  47. npc.Position.X = desiredX;
  48. return null;
  49. }
  50. }
  51. public class NPC {
  52. private readonly Sprite sprite;
  53. private readonly Vector2 spriteCenter;
  54. private readonly Vector2 eyeOffset = new Vector2(4, -9);
  55. private readonly FSM<Object> fsm;
  56. private readonly Vector2 halfSize = new Vector2(11, 24);
  57. public NPC(Vector2 position, int facing) {
  58. sprite = Sprites.Executioner;
  59. spriteCenter = new Vector2(
  60. sprite.Width / 2, sprite.Height - halfSize.Y - sprite.GroundPadding);
  61. Position = position;
  62. Box = new AABB(Position, halfSize);
  63. Facing = facing;
  64. fsm = new FSM<Object>("run", new Dictionary<string, IState<Object>> {
  65. { "idle", new IdleState(this) },
  66. { "run", new RunState(this) }
  67. });
  68. }
  69. public int Facing;
  70. public Vector2 Position;
  71. public AABB Box { get; private set; }
  72. public Vector2 EyePosition {
  73. get {
  74. return Vector2.Add(Position, new Vector2(eyeOffset.X * Facing, eyeOffset.Y));
  75. }
  76. }
  77. public float VisionRange {
  78. get {
  79. return 150;
  80. }
  81. }
  82. public float FieldOfView {
  83. get {
  84. return FMath.DegToRad(120);
  85. }
  86. }
  87. public Vector2 VisionRay {
  88. get {
  89. return new Vector2(VisionRange * Facing, 0);
  90. }
  91. }
  92. public void Update(float modelTime, SneakWorld world) {
  93. fsm.Update(modelTime, world, null);
  94. Box = new AABB(Position, halfSize);
  95. Debug.AddRect(Box, Color.White);
  96. }
  97. public void Draw(SpriteBatch spriteBatch) {
  98. Rectangle textureSource =
  99. sprite.GetTextureSource(fsm.StateName, Clock.ModelTime.TotalSeconds);
  100. SpriteEffects effect = Facing == 1 ?
  101. SpriteEffects.None : SpriteEffects.FlipHorizontally;
  102. Color color = Color.White;
  103. spriteBatch.Draw(Textures.Executioner.Get, Vectors.Round(Position), textureSource, color, 0f,
  104. spriteCenter, Vector2.One, effect, 0f);
  105. }
  106. }
  107. }