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.

110 lines
2.9 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, 33));
  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 = 2;
  46. private readonly Vector2 eyeOffset = new Vector2(4, -3);
  47. private readonly FSM<NPC> fsm;
  48. public NPC(Point position, int facing) {
  49. Position = position;
  50. Facing = facing;
  51. fsm = new FSM<NPC>(new Dictionary<string, IState<NPC>> {
  52. { "idle", new IdleState() },
  53. { "run", new RunState() }
  54. }, "run");
  55. }
  56. public int Facing;
  57. public Point Position;
  58. public Vector2 EyePosition {
  59. get {
  60. return Vector2.Add(
  61. Position.ToVector2(), new Vector2(eyeOffset.X * Facing, eyeOffset.Y));
  62. }
  63. }
  64. public float VisionRange {
  65. get {
  66. return 150;
  67. }
  68. }
  69. public float FieldOfView {
  70. get {
  71. return FMath.DegToRad(120);
  72. }
  73. }
  74. public Vector2 VisionRay {
  75. get {
  76. return new Vector2(VisionRange * Facing, 0);
  77. }
  78. }
  79. public void Update(float modelTime, World world) {
  80. fsm.Update(this, modelTime, world);
  81. }
  82. public void Draw(SpriteBatch spriteBatch) {
  83. Rectangle textureSource = Sprites.Executioner.GetTextureSource(
  84. fsm.StateName, Clock.ModelTime.TotalSeconds);
  85. // TODO: move this into Sprite metadata.
  86. Vector2 spriteCenter = new Vector2(spriteWidth / 2, spriteHeight / 2 + spriteCenterYOffset);
  87. SpriteEffects effect = Facing == 1 ?
  88. SpriteEffects.None : SpriteEffects.FlipHorizontally;
  89. Color color = Color.White;
  90. spriteBatch.Draw(Textures.Executioner.Get, Position.ToVector2(), textureSource, color, 0f,
  91. spriteCenter, Vector2.One, effect, 0f);
  92. }
  93. }
  94. }