2020-02-28 22:08:34 +00:00
|
|
|
|
using Microsoft.Xna.Framework;
|
|
|
|
|
using Microsoft.Xna.Framework.Graphics;
|
2020-03-05 22:39:17 +00:00
|
|
|
|
using System.Collections.Generic;
|
2020-02-28 22:08:34 +00:00
|
|
|
|
|
|
|
|
|
namespace SemiColinGames {
|
2020-03-06 17:16:33 +00:00
|
|
|
|
class IdleState : IState<NPC> {
|
2020-03-05 22:39:17 +00:00
|
|
|
|
float timeInState = 0;
|
2020-02-28 22:08:34 +00:00
|
|
|
|
|
2020-03-05 22:39:17 +00:00
|
|
|
|
public void Enter() {
|
|
|
|
|
timeInState = 0;
|
|
|
|
|
}
|
2020-02-28 22:08:34 +00:00
|
|
|
|
|
2020-03-06 19:20:17 +00:00
|
|
|
|
public string Update(NPC npc, float modelTime, World world) {
|
2020-03-05 22:39:17 +00:00
|
|
|
|
timeInState += modelTime;
|
|
|
|
|
if (timeInState > 1.0f) {
|
|
|
|
|
npc.Facing *= -1;
|
|
|
|
|
return "run";
|
|
|
|
|
}
|
|
|
|
|
return null;
|
2020-02-28 22:08:34 +00:00
|
|
|
|
}
|
2020-03-05 22:39:17 +00:00
|
|
|
|
}
|
2020-02-28 22:08:34 +00:00
|
|
|
|
|
2020-03-06 17:16:33 +00:00
|
|
|
|
class RunState : IState<NPC> {
|
2020-03-05 22:39:17 +00:00
|
|
|
|
public void Enter() {}
|
2020-02-28 22:08:34 +00:00
|
|
|
|
|
2020-03-06 19:20:17 +00:00
|
|
|
|
public string Update(NPC npc, float modelTime, World world) {
|
2020-03-05 21:17:57 +00:00
|
|
|
|
int moveSpeed = 120;
|
2020-03-05 22:39:17 +00:00
|
|
|
|
int desiredX = npc.Position.X + (int) (moveSpeed * npc.Facing * modelTime);
|
2020-03-10 14:09:55 +00:00
|
|
|
|
int testPoint = desiredX + 12 * npc.Facing;
|
2020-03-05 21:17:57 +00:00
|
|
|
|
// TODO: define the box modularly & correctly.
|
2020-03-11 17:56:33 +00:00
|
|
|
|
AABB npcBox = new AABB(new Vector2(testPoint, npc.Position.Y), new Vector2(1, 25));
|
2020-03-05 21:17:57 +00:00
|
|
|
|
Debug.AddRect(npcBox, Color.Cyan);
|
|
|
|
|
bool foundBox = false;
|
2020-03-06 17:28:58 +00:00
|
|
|
|
foreach (AABB box in world.CollisionTargets) {
|
2020-03-05 21:17:57 +00:00
|
|
|
|
if (box.Intersect(npcBox) != null) {
|
|
|
|
|
foundBox = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2020-02-28 22:08:34 +00:00
|
|
|
|
}
|
2020-03-05 21:17:57 +00:00
|
|
|
|
|
|
|
|
|
if (!foundBox) {
|
2020-03-05 22:39:17 +00:00
|
|
|
|
return "idle";
|
2020-02-28 22:08:34 +00:00
|
|
|
|
}
|
2020-03-05 22:39:17 +00:00
|
|
|
|
npc.Position.X = desiredX;
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class NPC {
|
|
|
|
|
private const int spriteWidth = 96;
|
|
|
|
|
private const int spriteHeight = 81;
|
2020-03-11 17:56:33 +00:00
|
|
|
|
private const int spriteCenterYOffset = 10;
|
2020-03-09 16:41:07 +00:00
|
|
|
|
private readonly Vector2 eyeOffset = new Vector2(4, -3);
|
2020-03-05 22:39:17 +00:00
|
|
|
|
|
2020-03-10 19:06:25 +00:00
|
|
|
|
private readonly FSM<NPC> fsm;
|
2020-03-11 17:56:33 +00:00
|
|
|
|
private AABB physicsBox;
|
|
|
|
|
private readonly Vector2 halfSize = new Vector2(12, 24);
|
2020-03-05 22:39:17 +00:00
|
|
|
|
|
2020-03-08 21:23:51 +00:00
|
|
|
|
public NPC(Point position, int facing) {
|
2020-03-05 22:39:17 +00:00
|
|
|
|
Position = position;
|
2020-03-11 17:56:33 +00:00
|
|
|
|
physicsBox = new AABB(position.ToVector2(), halfSize);
|
2020-03-08 21:23:51 +00:00
|
|
|
|
Facing = facing;
|
2020-03-06 17:16:33 +00:00
|
|
|
|
fsm = new FSM<NPC>(new Dictionary<string, IState<NPC>> {
|
2020-03-05 22:39:17 +00:00
|
|
|
|
{ "idle", new IdleState() },
|
|
|
|
|
{ "run", new RunState() }
|
2020-03-06 16:44:19 +00:00
|
|
|
|
}, "run");
|
2020-03-05 22:39:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-08 21:23:51 +00:00
|
|
|
|
public int Facing;
|
2020-03-05 22:39:17 +00:00
|
|
|
|
public Point Position;
|
|
|
|
|
|
2020-03-09 16:41:07 +00:00
|
|
|
|
public Vector2 EyePosition {
|
|
|
|
|
get {
|
|
|
|
|
return Vector2.Add(
|
|
|
|
|
Position.ToVector2(), new Vector2(eyeOffset.X * Facing, eyeOffset.Y));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public float VisionRange {
|
|
|
|
|
get {
|
|
|
|
|
return 150;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public float FieldOfView {
|
|
|
|
|
get {
|
|
|
|
|
return FMath.DegToRad(120);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Vector2 VisionRay {
|
|
|
|
|
get {
|
|
|
|
|
return new Vector2(VisionRange * Facing, 0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-06 17:28:58 +00:00
|
|
|
|
public void Update(float modelTime, World world) {
|
|
|
|
|
fsm.Update(this, modelTime, world);
|
2020-03-11 17:56:33 +00:00
|
|
|
|
physicsBox = new AABB(Position.ToVector2(), halfSize);
|
|
|
|
|
Debug.AddRect(physicsBox, Color.White);
|
2020-02-28 22:08:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Draw(SpriteBatch spriteBatch) {
|
2020-03-03 22:14:05 +00:00
|
|
|
|
Rectangle textureSource = Sprites.Executioner.GetTextureSource(
|
2020-03-06 02:12:11 +00:00
|
|
|
|
fsm.StateName, Clock.ModelTime.TotalSeconds);
|
2020-03-03 22:14:05 +00:00
|
|
|
|
// TODO: move this into Sprite metadata.
|
2020-02-28 22:08:34 +00:00
|
|
|
|
Vector2 spriteCenter = new Vector2(spriteWidth / 2, spriteHeight / 2 + spriteCenterYOffset);
|
|
|
|
|
SpriteEffects effect = Facing == 1 ?
|
|
|
|
|
SpriteEffects.None : SpriteEffects.FlipHorizontally;
|
|
|
|
|
Color color = Color.White;
|
2020-03-05 22:39:17 +00:00
|
|
|
|
spriteBatch.Draw(Textures.Executioner.Get, Position.ToVector2(), textureSource, color, 0f,
|
2020-02-28 22:08:34 +00:00
|
|
|
|
spriteCenter, Vector2.One, effect, 0f);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|