2020-02-28 22:08:34 +00:00
|
|
|
|
using Microsoft.Xna.Framework;
|
|
|
|
|
using Microsoft.Xna.Framework.Graphics;
|
2020-03-25 21:04:58 +00:00
|
|
|
|
using System;
|
2020-03-05 22:39:17 +00:00
|
|
|
|
using System.Collections.Generic;
|
2020-02-28 22:08:34 +00:00
|
|
|
|
|
|
|
|
|
namespace SemiColinGames {
|
2020-03-25 21:04:58 +00:00
|
|
|
|
class IdleState : IState<Object> {
|
|
|
|
|
private readonly NPC npc;
|
2020-03-25 19:16:31 +00:00
|
|
|
|
private float timeInState = 0;
|
|
|
|
|
|
|
|
|
|
public IdleState(NPC npc) {
|
|
|
|
|
this.npc = npc;
|
|
|
|
|
}
|
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-07-15 19:17:19 +00:00
|
|
|
|
public string Update(float modelTime, SneakWorld world, Object _) {
|
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-25 21:04:58 +00:00
|
|
|
|
class RunState : IState<Object> {
|
|
|
|
|
private readonly NPC npc;
|
2020-03-25 19:16:31 +00:00
|
|
|
|
|
|
|
|
|
public RunState(NPC npc) {
|
|
|
|
|
this.npc = npc;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-05 22:39:17 +00:00
|
|
|
|
public void Enter() {}
|
2020-02-28 22:08:34 +00:00
|
|
|
|
|
2020-07-15 19:17:19 +00:00
|
|
|
|
public string Update(float modelTime, SneakWorld world, Object _) {
|
2020-03-11 19:07:00 +00:00
|
|
|
|
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));
|
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 {
|
2020-03-11 19:07:00 +00:00
|
|
|
|
|
2020-03-11 18:42:10 +00:00
|
|
|
|
private readonly Sprite sprite;
|
2020-03-11 18:19:27 +00:00
|
|
|
|
private readonly Vector2 spriteCenter;
|
|
|
|
|
private readonly Vector2 eyeOffset = new Vector2(4, -9);
|
2020-03-05 22:39:17 +00:00
|
|
|
|
|
2020-03-25 21:04:58 +00:00
|
|
|
|
private readonly FSM<Object> fsm;
|
2020-03-16 18:56:32 +00:00
|
|
|
|
private readonly Vector2 halfSize = new Vector2(11, 24);
|
2020-03-05 22:39:17 +00:00
|
|
|
|
|
2020-03-11 19:07:00 +00:00
|
|
|
|
public NPC(Vector2 position, int facing) {
|
2020-03-11 18:42:10 +00:00
|
|
|
|
sprite = Sprites.Executioner;
|
|
|
|
|
spriteCenter = new Vector2(
|
|
|
|
|
sprite.Width / 2, sprite.Height - halfSize.Y - sprite.GroundPadding);
|
2020-03-11 19:07:00 +00:00
|
|
|
|
|
|
|
|
|
Position = position;
|
|
|
|
|
Box = new AABB(Position, halfSize);
|
2020-03-08 21:23:51 +00:00
|
|
|
|
Facing = facing;
|
2020-03-11 19:07:00 +00:00
|
|
|
|
|
2020-03-25 21:04:58 +00:00
|
|
|
|
fsm = new FSM<Object>("run", new Dictionary<string, IState<Object>> {
|
2020-03-25 19:16:31 +00:00
|
|
|
|
{ "idle", new IdleState(this) },
|
|
|
|
|
{ "run", new RunState(this) }
|
|
|
|
|
});
|
2020-03-05 22:39:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-08 21:23:51 +00:00
|
|
|
|
public int Facing;
|
2020-03-11 19:07:00 +00:00
|
|
|
|
public Vector2 Position;
|
|
|
|
|
public AABB Box { get; private set; }
|
2020-03-05 22:39:17 +00:00
|
|
|
|
|
2020-03-09 16:41:07 +00:00
|
|
|
|
public Vector2 EyePosition {
|
|
|
|
|
get {
|
2020-03-11 19:07:00 +00:00
|
|
|
|
return Vector2.Add(Position, new Vector2(eyeOffset.X * Facing, eyeOffset.Y));
|
2020-03-09 16:41:07 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-07-15 19:17:19 +00:00
|
|
|
|
public void Update(float modelTime, SneakWorld world) {
|
2020-03-25 21:04:58 +00:00
|
|
|
|
fsm.Update(modelTime, world, null);
|
2020-03-11 19:07:00 +00:00
|
|
|
|
Box = new AABB(Position, halfSize);
|
|
|
|
|
Debug.AddRect(Box, Color.White);
|
2020-02-28 22:08:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Draw(SpriteBatch spriteBatch) {
|
2020-03-11 18:42:10 +00:00
|
|
|
|
Rectangle textureSource =
|
|
|
|
|
sprite.GetTextureSource(fsm.StateName, Clock.ModelTime.TotalSeconds);
|
2020-02-28 22:08:34 +00:00
|
|
|
|
SpriteEffects effect = Facing == 1 ?
|
|
|
|
|
SpriteEffects.None : SpriteEffects.FlipHorizontally;
|
|
|
|
|
Color color = Color.White;
|
2020-03-11 19:07:00 +00:00
|
|
|
|
spriteBatch.Draw(Textures.Executioner.Get, Vector2.Round(Position), textureSource, color, 0f,
|
2020-02-28 22:08:34 +00:00
|
|
|
|
spriteCenter, Vector2.One, effect, 0f);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|