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.

115 lines
3.2 KiB

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