FSM / Player / NPCs now get World as an argument to Update()
This commit is contained in:
parent
1b124f84df
commit
0f8d9c2814
@ -4,7 +4,7 @@ using System.Collections.Generic;
|
|||||||
namespace SemiColinGames {
|
namespace SemiColinGames {
|
||||||
public interface IState<T> {
|
public interface IState<T> {
|
||||||
public void Enter();
|
public void Enter();
|
||||||
public string? Update(T obj, float modelTime, AABB[] collisionTargets);
|
public string? Update(T obj, float modelTime, World world);
|
||||||
}
|
}
|
||||||
|
|
||||||
public class FSM<T> {
|
public class FSM<T> {
|
||||||
@ -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, AABB[] collisionTargets) {
|
public void Update(T obj, float modelTime, World world) {
|
||||||
timeInState += modelTime;
|
timeInState += modelTime;
|
||||||
string? newState = state.Update(obj, modelTime, collisionTargets);
|
string? newState = state.Update(obj, modelTime, world);
|
||||||
if (newState != null) {
|
if (newState != null) {
|
||||||
Transition(newState);
|
Transition(newState);
|
||||||
}
|
}
|
||||||
|
@ -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;
|
||||||
|
@ -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;
|
||||||
|
@ -10,7 +10,7 @@ namespace SemiColinGames {
|
|||||||
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;
|
||||||
@ -23,14 +23,14 @@ namespace SemiColinGames {
|
|||||||
class RunState : IState<NPC> {
|
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;
|
||||||
@ -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) {
|
||||||
|
@ -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);
|
||||||
|
@ -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();
|
||||||
|
Loading…
Reference in New Issue
Block a user