NPC: Position is now a Vector2, not a Point

This commit is contained in:
Colin McMillen 2020-03-11 15:07:00 -04:00
parent e97ff479d0
commit dfb80c9fde
2 changed files with 18 additions and 16 deletions

View File

@ -24,11 +24,11 @@ namespace SemiColinGames {
public void Enter() {} public void Enter() {}
public string Update(NPC npc, float modelTime, World world) { public string Update(NPC npc, float modelTime, World world) {
int moveSpeed = 120; float moveSpeed = 120;
int desiredX = npc.Position.X + (int) (moveSpeed * npc.Facing * modelTime); float desiredX = npc.Position.X + moveSpeed * npc.Facing * modelTime;
int testPoint = desiredX + 12 * npc.Facing; float testPoint = desiredX + npc.Box.HalfSize.X * npc.Facing;
// TODO: define the box modularly & correctly. AABB npcBox = new AABB(
AABB npcBox = new AABB(new Vector2(testPoint, npc.Position.Y), new Vector2(1, 25)); new Vector2(testPoint, npc.Position.Y + 1), new Vector2(1, npc.Box.HalfSize.Y));
Debug.AddRect(npcBox, Color.Cyan); Debug.AddRect(npcBox, Color.Cyan);
bool foundBox = false; bool foundBox = false;
foreach (AABB box in world.CollisionTargets) { foreach (AABB box in world.CollisionTargets) {
@ -47,21 +47,23 @@ namespace SemiColinGames {
} }
public class NPC { public class NPC {
private readonly Sprite sprite; private readonly Sprite sprite;
private readonly Vector2 spriteCenter; private readonly Vector2 spriteCenter;
private readonly Vector2 eyeOffset = new Vector2(4, -9); private readonly Vector2 eyeOffset = new Vector2(4, -9);
private readonly FSM<NPC> fsm; private readonly FSM<NPC> fsm;
private AABB physicsBox;
private readonly Vector2 halfSize = new Vector2(12, 24); private readonly Vector2 halfSize = new Vector2(12, 24);
public NPC(Point position, int facing) { public NPC(Vector2 position, int facing) {
sprite = Sprites.Executioner; sprite = Sprites.Executioner;
Position = position;
spriteCenter = new Vector2( spriteCenter = new Vector2(
sprite.Width / 2, sprite.Height - halfSize.Y - sprite.GroundPadding); sprite.Width / 2, sprite.Height - halfSize.Y - sprite.GroundPadding);
physicsBox = new AABB(position.ToVector2(), halfSize);
Position = position;
Box = new AABB(Position, halfSize);
Facing = facing; Facing = facing;
fsm = new FSM<NPC>(new Dictionary<string, IState<NPC>> { fsm = new FSM<NPC>(new Dictionary<string, IState<NPC>> {
{ "idle", new IdleState() }, { "idle", new IdleState() },
{ "run", new RunState() } { "run", new RunState() }
@ -69,12 +71,12 @@ namespace SemiColinGames {
} }
public int Facing; public int Facing;
public Point Position; public Vector2 Position;
public AABB Box { get; private set; }
public Vector2 EyePosition { public Vector2 EyePosition {
get { get {
return Vector2.Add( return Vector2.Add(Position, new Vector2(eyeOffset.X * Facing, eyeOffset.Y));
Position.ToVector2(), new Vector2(eyeOffset.X * Facing, eyeOffset.Y));
} }
} }
@ -98,8 +100,8 @@ namespace SemiColinGames {
public void Update(float modelTime, World world) { public void Update(float modelTime, World world) {
fsm.Update(this, modelTime, world); fsm.Update(this, modelTime, world);
physicsBox = new AABB(Position.ToVector2(), halfSize); Box = new AABB(Position, halfSize);
Debug.AddRect(physicsBox, Color.White); Debug.AddRect(Box, Color.White);
} }
public void Draw(SpriteBatch spriteBatch) { public void Draw(SpriteBatch spriteBatch) {
@ -108,7 +110,7 @@ namespace SemiColinGames {
SpriteEffects effect = Facing == 1 ? SpriteEffects effect = Facing == 1 ?
SpriteEffects.None : SpriteEffects.FlipHorizontally; SpriteEffects.None : SpriteEffects.FlipHorizontally;
Color color = Color.White; 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); spriteCenter, Vector2.One, effect, 0f);
} }
} }

View File

@ -143,7 +143,7 @@ namespace SemiColinGames {
if (name == "player") { if (name == "player") {
player = new Player(new Point(x, y), facing); player = new Player(new Point(x, y), facing);
} else if (name == "executioner") { } 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()); return (player, npcs.ToArray());