diff --git a/Shared/NPC.cs b/Shared/NPC.cs index f8c3109..1e1d6ef 100644 --- a/Shared/NPC.cs +++ b/Shared/NPC.cs @@ -24,11 +24,11 @@ namespace SemiColinGames { public void Enter() {} public string Update(NPC npc, float modelTime, World world) { - int moveSpeed = 120; - int desiredX = npc.Position.X + (int) (moveSpeed * npc.Facing * modelTime); - int testPoint = desiredX + 12 * npc.Facing; - // TODO: define the box modularly & correctly. - AABB npcBox = new AABB(new Vector2(testPoint, npc.Position.Y), new Vector2(1, 25)); + float moveSpeed = 120; + float desiredX = npc.Position.X + moveSpeed * npc.Facing * modelTime; + float testPoint = desiredX + npc.Box.HalfSize.X * npc.Facing; + AABB npcBox = new AABB( + new Vector2(testPoint, npc.Position.Y + 1), new Vector2(1, npc.Box.HalfSize.Y)); Debug.AddRect(npcBox, Color.Cyan); bool foundBox = false; foreach (AABB box in world.CollisionTargets) { @@ -47,21 +47,23 @@ namespace SemiColinGames { } public class NPC { + private readonly Sprite sprite; private readonly Vector2 spriteCenter; private readonly Vector2 eyeOffset = new Vector2(4, -9); private readonly FSM fsm; - private AABB physicsBox; private readonly Vector2 halfSize = new Vector2(12, 24); - public NPC(Point position, int facing) { + public NPC(Vector2 position, int facing) { sprite = Sprites.Executioner; - Position = position; spriteCenter = new Vector2( sprite.Width / 2, sprite.Height - halfSize.Y - sprite.GroundPadding); - physicsBox = new AABB(position.ToVector2(), halfSize); + + Position = position; + Box = new AABB(Position, halfSize); Facing = facing; + fsm = new FSM(new Dictionary> { { "idle", new IdleState() }, { "run", new RunState() } @@ -69,12 +71,12 @@ namespace SemiColinGames { } public int Facing; - public Point Position; + public Vector2 Position; + public AABB Box { get; private set; } public Vector2 EyePosition { get { - return Vector2.Add( - Position.ToVector2(), new Vector2(eyeOffset.X * Facing, eyeOffset.Y)); + return Vector2.Add(Position, new Vector2(eyeOffset.X * Facing, eyeOffset.Y)); } } @@ -98,8 +100,8 @@ namespace SemiColinGames { public void Update(float modelTime, World world) { fsm.Update(this, modelTime, world); - physicsBox = new AABB(Position.ToVector2(), halfSize); - Debug.AddRect(physicsBox, Color.White); + Box = new AABB(Position, halfSize); + Debug.AddRect(Box, Color.White); } public void Draw(SpriteBatch spriteBatch) { @@ -108,7 +110,7 @@ namespace SemiColinGames { SpriteEffects effect = Facing == 1 ? SpriteEffects.None : SpriteEffects.FlipHorizontally; Color color = Color.White; - spriteBatch.Draw(Textures.Executioner.Get, Position.ToVector2(), textureSource, color, 0f, + spriteBatch.Draw(Textures.Executioner.Get, Vector2.Round(Position), textureSource, color, 0f, spriteCenter, Vector2.One, effect, 0f); } } diff --git a/Shared/World.cs b/Shared/World.cs index fcbaf13..d980a39 100644 --- a/Shared/World.cs +++ b/Shared/World.cs @@ -143,7 +143,7 @@ namespace SemiColinGames { if (name == "player") { player = new Player(new Point(x, y), facing); } else if (name == "executioner") { - npcs.Add(new NPC(new Point(x, y), facing)); + npcs.Add(new NPC(new Vector2(x, y), facing)); } } return (player, npcs.ToArray());