Compare commits
No commits in common. "0f8d9c2814ed9094b30226f930ec7addb1101c25" and "08091106eaac0be60a3834b3e33eb61cd051e47e" have entirely different histories.
0f8d9c2814
...
08091106ea
@ -2,17 +2,17 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace SemiColinGames {
|
namespace SemiColinGames {
|
||||||
public interface IState<T> {
|
public interface IState {
|
||||||
public void Enter();
|
public void Enter();
|
||||||
public string? Update(T obj, float modelTime, World world);
|
public string? Update(NPC npc, float modelTime, AABB[] collisionTargets);
|
||||||
}
|
}
|
||||||
|
|
||||||
public class FSM<T> {
|
public class FSM {
|
||||||
float timeInState = 0f;
|
float timeInState = 0f;
|
||||||
Dictionary<string, IState<T>> states;
|
Dictionary<string, IState> states;
|
||||||
IState<T> state;
|
IState state;
|
||||||
|
|
||||||
public FSM(Dictionary<string, IState<T>> states, string initial) {
|
public FSM(Dictionary<string, IState> states, string initial) {
|
||||||
this.states = states;
|
this.states = states;
|
||||||
StateName = initial;
|
StateName = initial;
|
||||||
Transition(StateName);
|
Transition(StateName);
|
||||||
@ -20,9 +20,9 @@ namespace SemiColinGames {
|
|||||||
|
|
||||||
public string StateName { get; private set; }
|
public string StateName { get; private set; }
|
||||||
|
|
||||||
public void Update(T obj, float modelTime, World world) {
|
public void Update(NPC npc, float modelTime, AABB[] collisionTargets) {
|
||||||
timeInState += modelTime;
|
timeInState += modelTime;
|
||||||
string? newState = state.Update(obj, modelTime, world);
|
string? newState = state.Update(npc, modelTime, collisionTargets);
|
||||||
if (newState != null) {
|
if (newState != null) {
|
||||||
Transition(newState);
|
Transition(newState);
|
||||||
}
|
}
|
||||||
@ -32,7 +32,7 @@ namespace SemiColinGames {
|
|||||||
Debug.WriteLine("{0} -> {1} @ {2}", StateName, state, timeInState);
|
Debug.WriteLine("{0} -> {1} @ {2}", StateName, state, timeInState);
|
||||||
timeInState = 0f;
|
timeInState = 0f;
|
||||||
StateName = state;
|
StateName = state;
|
||||||
IState<T> newState = states[state];
|
IState newState = states[state];
|
||||||
this.state = newState;
|
this.state = newState;
|
||||||
this.state.Enter();
|
this.state.Enter();
|
||||||
}
|
}
|
||||||
|
@ -14,7 +14,7 @@ namespace SemiColinGames {
|
|||||||
// Console.WriteLine("{0} {1} {2}", h[0], h[1], h[2]); // 7 5 3
|
// Console.WriteLine("{0} {1} {2}", h[0], h[1], h[2]); // 7 5 3
|
||||||
// h.Add(11); h.Add(13);
|
// h.Add(11); h.Add(13);
|
||||||
// Console.WriteLine("{0} {1} {2}", h[0], h[1], h[2]); // 13 11 7
|
// Console.WriteLine("{0} {1} {2}", h[0], h[1], h[2]); // 13 11 7
|
||||||
public class History<T> {
|
class History<T> {
|
||||||
|
|
||||||
// Backing store for the History's items.
|
// Backing store for the History's items.
|
||||||
private readonly T[] items;
|
private readonly T[] items;
|
||||||
|
@ -2,7 +2,7 @@ using Microsoft.Xna.Framework;
|
|||||||
using Microsoft.Xna.Framework.Input;
|
using Microsoft.Xna.Framework.Input;
|
||||||
|
|
||||||
namespace SemiColinGames {
|
namespace SemiColinGames {
|
||||||
public readonly struct Input {
|
readonly struct Input {
|
||||||
public readonly Vector2 Motion;
|
public readonly Vector2 Motion;
|
||||||
public readonly bool Jump;
|
public readonly bool Jump;
|
||||||
public readonly bool Attack;
|
public readonly bool Attack;
|
||||||
|
@ -3,14 +3,14 @@ using Microsoft.Xna.Framework.Graphics;
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace SemiColinGames {
|
namespace SemiColinGames {
|
||||||
class IdleState : IState<NPC> {
|
class IdleState : IState {
|
||||||
float timeInState = 0;
|
float timeInState = 0;
|
||||||
|
|
||||||
public void Enter() {
|
public void Enter() {
|
||||||
timeInState = 0;
|
timeInState = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public string? Update(NPC npc, float modelTime, World world) {
|
public string? Update(NPC npc, float modelTime, AABB[] collisionTargets) {
|
||||||
timeInState += modelTime;
|
timeInState += modelTime;
|
||||||
if (timeInState > 1.0f) {
|
if (timeInState > 1.0f) {
|
||||||
npc.Facing *= -1;
|
npc.Facing *= -1;
|
||||||
@ -20,17 +20,17 @@ namespace SemiColinGames {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class RunState : IState<NPC> {
|
class RunState : IState {
|
||||||
public void Enter() {}
|
public void Enter() {}
|
||||||
|
|
||||||
public string? Update(NPC npc, float modelTime, World world) {
|
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 = npc.Position.X + (int) (moveSpeed * npc.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, npc.Position.Y), new Vector2(1, 33));
|
||||||
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 collisionTargets) {
|
||||||
if (box.Intersect(npcBox) != null) {
|
if (box.Intersect(npcBox) != null) {
|
||||||
foundBox = true;
|
foundBox = true;
|
||||||
break;
|
break;
|
||||||
@ -50,11 +50,11 @@ namespace SemiColinGames {
|
|||||||
private const int spriteHeight = 81;
|
private const int spriteHeight = 81;
|
||||||
private const int spriteCenterYOffset = 2;
|
private const int spriteCenterYOffset = 2;
|
||||||
|
|
||||||
private FSM<NPC> fsm;
|
private FSM fsm;
|
||||||
|
|
||||||
public NPC(Point position) {
|
public NPC(Point position) {
|
||||||
Position = position;
|
Position = position;
|
||||||
fsm = new FSM<NPC>(new Dictionary<string, IState<NPC>> {
|
fsm = new FSM(new Dictionary<string, IState> {
|
||||||
{ "idle", new IdleState() },
|
{ "idle", new IdleState() },
|
||||||
{ "run", new RunState() }
|
{ "run", new RunState() }
|
||||||
}, "run");
|
}, "run");
|
||||||
@ -63,8 +63,8 @@ namespace SemiColinGames {
|
|||||||
public int Facing = 1;
|
public int Facing = 1;
|
||||||
public Point Position;
|
public Point Position;
|
||||||
|
|
||||||
public void Update(float modelTime, World world) {
|
public void Update(float modelTime, AABB[] collisionTargets) {
|
||||||
fsm.Update(this, modelTime, world);
|
fsm.Update(this, modelTime, collisionTargets);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Draw(SpriteBatch spriteBatch) {
|
public void Draw(SpriteBatch spriteBatch) {
|
||||||
|
@ -4,7 +4,7 @@ using System;
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace SemiColinGames {
|
namespace SemiColinGames {
|
||||||
public class Player {
|
class Player {
|
||||||
private enum Pose { Walking, Standing, Crouching, Stretching, SwordSwing, Jumping };
|
private enum Pose { Walking, Standing, Crouching, Stretching, SwordSwing, Jumping };
|
||||||
|
|
||||||
private const int moveSpeed = 180;
|
private const int moveSpeed = 180;
|
||||||
@ -48,7 +48,7 @@ namespace SemiColinGames {
|
|||||||
|
|
||||||
public Point Position { get { return position; } }
|
public Point Position { get { return position; } }
|
||||||
|
|
||||||
public void Update(float modelTime, World world, History<Input> input) {
|
public void Update(float modelTime, AABB[] collisionTargets, History<Input> input) {
|
||||||
AABB BoxOffset(Point position, int yOffset) {
|
AABB BoxOffset(Point position, int yOffset) {
|
||||||
return new AABB(new Vector2(position.X, position.Y + yOffset), halfSize);
|
return new AABB(new Vector2(position.X, position.Y + yOffset), halfSize);
|
||||||
}
|
}
|
||||||
@ -72,7 +72,7 @@ namespace SemiColinGames {
|
|||||||
AABB largeBox = new AABB(
|
AABB largeBox = new AABB(
|
||||||
new Vector2(position.X + movement.X / 2, position.Y + movement.Y / 2),
|
new Vector2(position.X + movement.X / 2, position.Y + movement.Y / 2),
|
||||||
new Vector2(halfSize.X + Math.Abs(movement.X) + 1, halfSize.Y + Math.Abs(movement.Y) + 1));
|
new Vector2(halfSize.X + Math.Abs(movement.X) + 1, halfSize.Y + Math.Abs(movement.Y) + 1));
|
||||||
foreach (var box in world.CollisionTargets) {
|
foreach (var box in collisionTargets) {
|
||||||
if (box.Intersect(largeBox) != null) {
|
if (box.Intersect(largeBox) != null) {
|
||||||
// Debug.AddRect(box, Color.Green);
|
// Debug.AddRect(box, Color.Green);
|
||||||
candidates.Add(box);
|
candidates.Add(box);
|
||||||
|
@ -114,7 +114,7 @@ namespace SemiColinGames {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class World {
|
class World {
|
||||||
|
|
||||||
public const int TileSize = 16;
|
public const int TileSize = 16;
|
||||||
readonly Tile[] tiles;
|
readonly Tile[] tiles;
|
||||||
@ -191,9 +191,9 @@ namespace SemiColinGames {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void Update(float modelTime, History<Input> input) {
|
public void Update(float modelTime, History<Input> input) {
|
||||||
Player.Update(modelTime, this, input);
|
Player.Update(modelTime, CollisionTargets, input);
|
||||||
foreach (NPC npc in npcs) {
|
foreach (NPC npc in npcs) {
|
||||||
npc.Update(modelTime, this);
|
npc.Update(modelTime, CollisionTargets);
|
||||||
}
|
}
|
||||||
if (Player.Health <= 0) {
|
if (Player.Health <= 0) {
|
||||||
Reset();
|
Reset();
|
||||||
|
Loading…
Reference in New Issue
Block a user