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.1 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 readonly Sprite sprite;
  44. private readonly Vector2 spriteCenter;
  45. private readonly Vector2 eyeOffset = new Vector2(4, -9);
  46. private readonly FSM<NPC> fsm;
  47. private AABB physicsBox;
  48. private readonly Vector2 halfSize = new Vector2(12, 24);
  49. public NPC(Point position, int facing) {
  50. sprite = Sprites.Executioner;
  51. Position = position;
  52. spriteCenter = new Vector2(
  53. sprite.Width / 2, sprite.Height - halfSize.Y - sprite.GroundPadding);
  54. physicsBox = new AABB(position.ToVector2(), halfSize);
  55. Facing = facing;
  56. fsm = new FSM<NPC>(new Dictionary<string, IState<NPC>> {
  57. { "idle", new IdleState() },
  58. { "run", new RunState() }
  59. }, "run");
  60. }
  61. public int Facing;
  62. public Point Position;
  63. public Vector2 EyePosition {
  64. get {
  65. return Vector2.Add(
  66. Position.ToVector2(), new Vector2(eyeOffset.X * Facing, eyeOffset.Y));
  67. }
  68. }
  69. public float VisionRange {
  70. get {
  71. return 150;
  72. }
  73. }
  74. public float FieldOfView {
  75. get {
  76. return FMath.DegToRad(120);
  77. }
  78. }
  79. public Vector2 VisionRay {
  80. get {
  81. return new Vector2(VisionRange * Facing, 0);
  82. }
  83. }
  84. public void Update(float modelTime, World world) {
  85. fsm.Update(this, modelTime, world);
  86. physicsBox = new AABB(Position.ToVector2(), halfSize);
  87. Debug.AddRect(physicsBox, Color.White);
  88. }
  89. public void Draw(SpriteBatch spriteBatch) {
  90. Rectangle textureSource =
  91. sprite.GetTextureSource(fsm.StateName, Clock.ModelTime.TotalSeconds);
  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. }