|
|
@ -3,14 +3,19 @@ using Microsoft.Xna.Framework.Graphics; |
|
|
|
using System.Collections.Generic; |
|
|
|
|
|
|
|
namespace SemiColinGames { |
|
|
|
class IdleState : IState<NPC> { |
|
|
|
float timeInState = 0; |
|
|
|
class IdleState : IState { |
|
|
|
private NPC npc; |
|
|
|
private float timeInState = 0; |
|
|
|
|
|
|
|
public IdleState(NPC npc) { |
|
|
|
this.npc = npc; |
|
|
|
} |
|
|
|
|
|
|
|
public void Enter() { |
|
|
|
timeInState = 0; |
|
|
|
} |
|
|
|
|
|
|
|
public string Update(NPC npc, float modelTime, World world) { |
|
|
|
public string Update(float modelTime, World world) { |
|
|
|
timeInState += modelTime; |
|
|
|
if (timeInState > 1.0f) { |
|
|
|
npc.Facing *= -1; |
|
|
@ -20,10 +25,16 @@ namespace SemiColinGames { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
class RunState : IState<NPC> { |
|
|
|
class RunState : IState { |
|
|
|
private NPC npc; |
|
|
|
|
|
|
|
public RunState(NPC npc) { |
|
|
|
this.npc = npc; |
|
|
|
} |
|
|
|
|
|
|
|
public void Enter() {} |
|
|
|
|
|
|
|
public string Update(NPC npc, float modelTime, World world) { |
|
|
|
public string Update(float modelTime, World world) { |
|
|
|
float moveSpeed = 120; |
|
|
|
float desiredX = npc.Position.X + moveSpeed * npc.Facing * modelTime; |
|
|
|
float testPoint = desiredX + npc.Box.HalfSize.X * npc.Facing; |
|
|
@ -52,7 +63,7 @@ namespace SemiColinGames { |
|
|
|
private readonly Vector2 spriteCenter; |
|
|
|
private readonly Vector2 eyeOffset = new Vector2(4, -9); |
|
|
|
|
|
|
|
private readonly FSM<NPC> fsm; |
|
|
|
private readonly FSM fsm; |
|
|
|
private readonly Vector2 halfSize = new Vector2(11, 24); |
|
|
|
|
|
|
|
public NPC(Vector2 position, int facing) { |
|
|
@ -64,10 +75,10 @@ namespace SemiColinGames { |
|
|
|
Box = new AABB(Position, halfSize); |
|
|
|
Facing = facing; |
|
|
|
|
|
|
|
fsm = new FSM<NPC>(new Dictionary<string, IState<NPC>> { |
|
|
|
{ "idle", new IdleState() }, |
|
|
|
{ "run", new RunState() } |
|
|
|
}, "run"); |
|
|
|
fsm = new FSM("run", new Dictionary<string, IState> { |
|
|
|
{ "idle", new IdleState(this) }, |
|
|
|
{ "run", new RunState(this) } |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
public int Facing; |
|
|
@ -99,7 +110,7 @@ namespace SemiColinGames { |
|
|
|
} |
|
|
|
|
|
|
|
public void Update(float modelTime, World world) { |
|
|
|
fsm.Update(this, modelTime, world); |
|
|
|
fsm.Update(modelTime, world); |
|
|
|
Box = new AABB(Position, halfSize); |
|
|
|
Debug.AddRect(Box, Color.White); |
|
|
|
} |
|
|
|