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.

50 lines
1.6 KiB

  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. namespace SemiColinGames {
  4. class NPC {
  5. private Point position;
  6. private const int spriteWidth = 96;
  7. private const int spriteHeight = 81;
  8. private const int spriteCenterYOffset = 3;
  9. public NPC(Point position) {
  10. this.position = position;
  11. }
  12. public int Facing { get; private set; } = 1;
  13. public void Update(float modelTime, AABB[] collisionTargets) {
  14. int moveSpeed = 120;
  15. int desiredX = position.X + (int) (moveSpeed * Facing * modelTime);
  16. // TODO: define the box modularly & correctly.
  17. AABB npcBox = new AABB(new Vector2(desiredX, position.Y), new Vector2(11, 33));
  18. Debug.AddRect(npcBox, Color.Cyan);
  19. bool foundBox = false;
  20. foreach (AABB box in collisionTargets) {
  21. if (box.Intersect(npcBox) != null) {
  22. foundBox = true;
  23. break;
  24. }
  25. }
  26. if (!foundBox) {
  27. Facing *= -1;
  28. }
  29. position.X = desiredX;
  30. }
  31. public void Draw(SpriteBatch spriteBatch) {
  32. Rectangle textureSource = Sprites.Executioner.GetTextureSource(
  33. "run", (int) Clock.ModelTime.TotalMilliseconds);
  34. // TODO: move this into Sprite metadata.
  35. Vector2 spriteCenter = new Vector2(spriteWidth / 2, spriteHeight / 2 + spriteCenterYOffset);
  36. SpriteEffects effect = Facing == 1 ?
  37. SpriteEffects.None : SpriteEffects.FlipHorizontally;
  38. Color color = Color.White;
  39. spriteBatch.Draw(Textures.Executioner.Get, position.ToVector2(), textureSource, color, 0f,
  40. spriteCenter, Vector2.One, effect, 0f);
  41. }
  42. }
  43. }