Compare commits
No commits in common. "53083818c026e270eb8b935c56cb39da765870b1" and "53c6d8483f773121f750f963909f72d51619650c" have entirely different histories.
53083818c0
...
53c6d8483f
@ -1,40 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
|
|
||||||
namespace SemiColinGames {
|
|
||||||
public interface IState {
|
|
||||||
public void Enter();
|
|
||||||
public string? Update(NPC npc, float modelTime, AABB[] collisionTargets);
|
|
||||||
}
|
|
||||||
|
|
||||||
public class FSM {
|
|
||||||
float timeInState = 0f;
|
|
||||||
Dictionary<string, IState> states;
|
|
||||||
IState state;
|
|
||||||
|
|
||||||
public FSM(Dictionary<string, IState> states, string initial) {
|
|
||||||
this.states = states;
|
|
||||||
StateName = initial;
|
|
||||||
Transition(StateName);
|
|
||||||
}
|
|
||||||
|
|
||||||
public string StateName { get; private set; }
|
|
||||||
|
|
||||||
public void Update(NPC npc, float modelTime, AABB[] collisionTargets) {
|
|
||||||
timeInState += modelTime;
|
|
||||||
string? newState = state.Update(npc, modelTime, collisionTargets);
|
|
||||||
if (newState != null) {
|
|
||||||
Transition(newState);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void Transition(string state) {
|
|
||||||
Debug.WriteLine("{0} -> {1} @ {2}", StateName, state, timeInState);
|
|
||||||
timeInState = 0f;
|
|
||||||
StateName = state;
|
|
||||||
IState newState = states[state];
|
|
||||||
this.state = newState;
|
|
||||||
this.state.Enter();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,33 +1,25 @@
|
|||||||
using Microsoft.Xna.Framework;
|
using Microsoft.Xna.Framework;
|
||||||
using Microsoft.Xna.Framework.Graphics;
|
using Microsoft.Xna.Framework.Graphics;
|
||||||
using System.Collections.Generic;
|
|
||||||
|
|
||||||
namespace SemiColinGames {
|
namespace SemiColinGames {
|
||||||
class IdleState : IState {
|
class NPC {
|
||||||
float timeInState = 0;
|
private Point position;
|
||||||
|
|
||||||
public void Enter() {
|
private const int spriteWidth = 96;
|
||||||
timeInState = 0;
|
private const int spriteHeight = 81;
|
||||||
|
private const int spriteCenterYOffset = 3;
|
||||||
|
|
||||||
|
public NPC(Point position) {
|
||||||
|
this.position = position;
|
||||||
}
|
}
|
||||||
|
|
||||||
public string? Update(NPC npc, float modelTime, AABB[] collisionTargets) {
|
public int Facing { get; private set; } = 1;
|
||||||
timeInState += modelTime;
|
|
||||||
if (timeInState > 1.0f) {
|
|
||||||
npc.Facing *= -1;
|
|
||||||
return "run";
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class RunState : IState {
|
public void Update(float modelTime, AABB[] collisionTargets) {
|
||||||
public void Enter() {}
|
|
||||||
|
|
||||||
public string? Update(NPC npc, float modelTime, AABB[] collisionTargets) {
|
|
||||||
int moveSpeed = 120;
|
int moveSpeed = 120;
|
||||||
int desiredX = npc.Position.X + (int) (moveSpeed * npc.Facing * modelTime);
|
int desiredX = position.X + (int) (moveSpeed * Facing * modelTime);
|
||||||
// TODO: define the box modularly & correctly.
|
// TODO: define the box modularly & correctly.
|
||||||
AABB npcBox = new AABB(new Vector2(desiredX, npc.Position.Y), new Vector2(1, 33));
|
AABB npcBox = new AABB(new Vector2(desiredX, position.Y), new Vector2(11, 33));
|
||||||
Debug.AddRect(npcBox, Color.Cyan);
|
Debug.AddRect(npcBox, Color.Cyan);
|
||||||
bool foundBox = false;
|
bool foundBox = false;
|
||||||
foreach (AABB box in collisionTargets) {
|
foreach (AABB box in collisionTargets) {
|
||||||
@ -38,44 +30,20 @@ namespace SemiColinGames {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!foundBox) {
|
if (!foundBox) {
|
||||||
return "idle";
|
Facing *= -1;
|
||||||
}
|
}
|
||||||
npc.Position.X = desiredX;
|
position.X = desiredX;
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public class NPC {
|
|
||||||
private const int spriteWidth = 96;
|
|
||||||
private const int spriteHeight = 81;
|
|
||||||
private const int spriteCenterYOffset = 2;
|
|
||||||
|
|
||||||
private FSM fsm;
|
|
||||||
|
|
||||||
public NPC(Point position) {
|
|
||||||
Position = position;
|
|
||||||
fsm = new FSM(new Dictionary<string, IState> {
|
|
||||||
{ "idle", new IdleState() },
|
|
||||||
{ "run", new RunState() }
|
|
||||||
}, "idle");
|
|
||||||
}
|
|
||||||
|
|
||||||
public int Facing = 1;
|
|
||||||
public Point Position;
|
|
||||||
|
|
||||||
public void Update(float modelTime, AABB[] collisionTargets) {
|
|
||||||
fsm.Update(this, modelTime, collisionTargets);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Draw(SpriteBatch spriteBatch) {
|
public void Draw(SpriteBatch spriteBatch) {
|
||||||
Rectangle textureSource = Sprites.Executioner.GetTextureSource(
|
Rectangle textureSource = Sprites.Executioner.GetTextureSource(
|
||||||
fsm.StateName, (int) Clock.ModelTime.TotalMilliseconds);
|
"run", (int) Clock.ModelTime.TotalMilliseconds);
|
||||||
// TODO: move this into Sprite metadata.
|
// TODO: move this into Sprite metadata.
|
||||||
Vector2 spriteCenter = new Vector2(spriteWidth / 2, spriteHeight / 2 + spriteCenterYOffset);
|
Vector2 spriteCenter = new Vector2(spriteWidth / 2, spriteHeight / 2 + spriteCenterYOffset);
|
||||||
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, position.ToVector2(), textureSource, color, 0f,
|
||||||
spriteCenter, Vector2.One, effect, 0f);
|
spriteCenter, Vector2.One, effect, 0f);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,7 +11,6 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="$(MSBuildThisFileDirectory)Camera.cs" />
|
<Compile Include="$(MSBuildThisFileDirectory)Camera.cs" />
|
||||||
<Compile Include="$(MSBuildThisFileDirectory)ExtensionMethods.cs" />
|
<Compile Include="$(MSBuildThisFileDirectory)ExtensionMethods.cs" />
|
||||||
<Compile Include="$(MSBuildThisFileDirectory)FSM.cs" />
|
|
||||||
<Compile Include="$(MSBuildThisFileDirectory)NPC.cs" />
|
<Compile Include="$(MSBuildThisFileDirectory)NPC.cs" />
|
||||||
<Compile Include="$(MSBuildThisFileDirectory)SoundEffects.cs" />
|
<Compile Include="$(MSBuildThisFileDirectory)SoundEffects.cs" />
|
||||||
<Compile Include="$(MSBuildThisFileDirectory)Sprites.cs" />
|
<Compile Include="$(MSBuildThisFileDirectory)Sprites.cs" />
|
||||||
|
@ -64,7 +64,6 @@ namespace SemiColinGames {
|
|||||||
child.SelectToken("frame.y").Value<int>(),
|
child.SelectToken("frame.y").Value<int>(),
|
||||||
child.SelectToken("frame.w").Value<int>(),
|
child.SelectToken("frame.w").Value<int>(),
|
||||||
child.SelectToken("frame.h").Value<int>());
|
child.SelectToken("frame.h").Value<int>());
|
||||||
// TODO: convert all durations to floats.
|
|
||||||
int durationMs = child.SelectToken("duration").Value<int>();
|
int durationMs = child.SelectToken("duration").Value<int>();
|
||||||
frames.Add(new Frame(source, durationMs));
|
frames.Add(new Frame(source, durationMs));
|
||||||
}
|
}
|
||||||
@ -76,7 +75,7 @@ namespace SemiColinGames {
|
|||||||
int end = child.SelectToken("to").Value<int>();
|
int end = child.SelectToken("to").Value<int>();
|
||||||
string directionString = child.SelectToken("direction").Value<string>();
|
string directionString = child.SelectToken("direction").Value<string>();
|
||||||
AnimationDirection direction = directionString == "pingpong" ?
|
AnimationDirection direction = directionString == "pingpong" ?
|
||||||
AnimationDirection.PingPong : AnimationDirection.Forward;
|
AnimationDirection.PingPong: AnimationDirection.Forward;
|
||||||
int durationMs = 0;
|
int durationMs = 0;
|
||||||
for (int i = start; i <= end; i++) {
|
for (int i = start; i <= end; i++) {
|
||||||
durationMs += frames[i].DurationMs;
|
durationMs += frames[i].DurationMs;
|
||||||
|
Loading…
Reference in New Issue
Block a user