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.

117 lines
3.0 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. float moveSpeed = 120;
  23. float desiredX = npc.Position.X + moveSpeed * npc.Facing * modelTime;
  24. float testPoint = desiredX + npc.Box.HalfSize.X * npc.Facing;
  25. AABB npcBox = new AABB(
  26. new Vector2(testPoint, npc.Position.Y + 1), new Vector2(1, npc.Box.HalfSize.Y));
  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 readonly Vector2 halfSize = new Vector2(11, 24);
  48. public NPC(Vector2 position, int facing) {
  49. sprite = Sprites.Executioner;
  50. spriteCenter = new Vector2(
  51. sprite.Width / 2, sprite.Height - halfSize.Y - sprite.GroundPadding);
  52. Position = position;
  53. Box = new AABB(Position, halfSize);
  54. Facing = facing;
  55. fsm = new FSM<NPC>(new Dictionary<string, IState<NPC>> {
  56. { "idle", new IdleState() },
  57. { "run", new RunState() }
  58. }, "run");
  59. }
  60. public int Facing;
  61. public Vector2 Position;
  62. public AABB Box { get; private set; }
  63. public Vector2 EyePosition {
  64. get {
  65. return Vector2.Add(Position, new Vector2(eyeOffset.X * Facing, eyeOffset.Y));
  66. }
  67. }
  68. public float VisionRange {
  69. get {
  70. return 150;
  71. }
  72. }
  73. public float FieldOfView {
  74. get {
  75. return FMath.DegToRad(120);
  76. }
  77. }
  78. public Vector2 VisionRay {
  79. get {
  80. return new Vector2(VisionRange * Facing, 0);
  81. }
  82. }
  83. public void Update(float modelTime, World world) {
  84. fsm.Update(this, modelTime, world);
  85. Box = new AABB(Position, halfSize);
  86. Debug.AddRect(Box, Color.White);
  87. }
  88. public void Draw(SpriteBatch spriteBatch) {
  89. Rectangle textureSource =
  90. sprite.GetTextureSource(fsm.StateName, Clock.ModelTime.TotalSeconds);
  91. SpriteEffects effect = Facing == 1 ?
  92. SpriteEffects.None : SpriteEffects.FlipHorizontally;
  93. Color color = Color.White;
  94. spriteBatch.Draw(Textures.Executioner.Get, Vector2.Round(Position), textureSource, color, 0f,
  95. spriteCenter, Vector2.One, effect, 0f);
  96. }
  97. }
  98. }