Compare commits

..

2 Commits

6 changed files with 26 additions and 26 deletions

View File

@ -2,17 +2,17 @@
using System.Collections.Generic; using System.Collections.Generic;
namespace SemiColinGames { namespace SemiColinGames {
public interface IState { public interface IState<T> {
public void Enter(); public void Enter();
public string? Update(NPC npc, float modelTime, AABB[] collisionTargets); public string? Update(T obj, float modelTime, World world);
} }
public class FSM { public class FSM<T> {
float timeInState = 0f; float timeInState = 0f;
Dictionary<string, IState> states; Dictionary<string, IState<T>> states;
IState state; IState<T> state;
public FSM(Dictionary<string, IState> states, string initial) { public FSM(Dictionary<string, IState<T>> 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(NPC npc, float modelTime, AABB[] collisionTargets) { public void Update(T obj, float modelTime, World world) {
timeInState += modelTime; timeInState += modelTime;
string? newState = state.Update(npc, modelTime, collisionTargets); string? newState = state.Update(obj, modelTime, world);
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 newState = states[state]; IState<T> newState = states[state];
this.state = newState; this.state = newState;
this.state.Enter(); this.state.Enter();
} }

View File

@ -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
class History<T> { public 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;

View File

@ -2,7 +2,7 @@ using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Input;
namespace SemiColinGames { namespace SemiColinGames {
readonly struct Input { public 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;

View File

@ -3,14 +3,14 @@ using Microsoft.Xna.Framework.Graphics;
using System.Collections.Generic; using System.Collections.Generic;
namespace SemiColinGames { namespace SemiColinGames {
class IdleState : IState { class IdleState : IState<NPC> {
float timeInState = 0; float timeInState = 0;
public void Enter() { public void Enter() {
timeInState = 0; timeInState = 0;
} }
public string? Update(NPC npc, float modelTime, AABB[] collisionTargets) { public string? Update(NPC npc, float modelTime, World world) {
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 { class RunState : IState<NPC> {
public void Enter() {} public void Enter() {}
public string? Update(NPC npc, float modelTime, AABB[] collisionTargets) { public string? Update(NPC npc, float modelTime, World world) {
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 collisionTargets) { foreach (AABB box in world.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 fsm; private FSM<NPC> fsm;
public NPC(Point position) { public NPC(Point position) {
Position = position; Position = position;
fsm = new FSM(new Dictionary<string, IState> { fsm = new FSM<NPC>(new Dictionary<string, IState<NPC>> {
{ "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, AABB[] collisionTargets) { public void Update(float modelTime, World world) {
fsm.Update(this, modelTime, collisionTargets); fsm.Update(this, modelTime, world);
} }
public void Draw(SpriteBatch spriteBatch) { public void Draw(SpriteBatch spriteBatch) {

View File

@ -4,7 +4,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
namespace SemiColinGames { namespace SemiColinGames {
class Player { public 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, AABB[] collisionTargets, History<Input> input) { public void Update(float modelTime, World world, 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 collisionTargets) { foreach (var box in world.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);

View File

@ -114,7 +114,7 @@ namespace SemiColinGames {
} }
} }
class World { public 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, CollisionTargets, input); Player.Update(modelTime, this, input);
foreach (NPC npc in npcs) { foreach (NPC npc in npcs) {
npc.Update(modelTime, CollisionTargets); npc.Update(modelTime, this);
} }
if (Player.Health <= 0) { if (Player.Health <= 0) {
Reset(); Reset();