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.

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