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.

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