FSM / Player / NPCs now get World as an argument to Update()

This commit is contained in:
Colin McMillen 2020-03-06 12:28:58 -05:00
parent 1b124f84df
commit 0f8d9c2814
6 changed files with 16 additions and 16 deletions

View File

@ -4,7 +4,7 @@ using System.Collections.Generic;
namespace SemiColinGames {
public interface IState<T> {
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> {
@ -20,9 +20,9 @@ namespace SemiColinGames {
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;
string? newState = state.Update(obj, modelTime, collisionTargets);
string? newState = state.Update(obj, modelTime, world);
if (newState != null) {
Transition(newState);
}

View File

@ -14,7 +14,7 @@ namespace SemiColinGames {
// Console.WriteLine("{0} {1} {2}", h[0], h[1], h[2]); // 7 5 3
// h.Add(11); h.Add(13);
// 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.
private readonly T[] items;

View File

@ -2,7 +2,7 @@ using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
namespace SemiColinGames {
readonly struct Input {
public readonly struct Input {
public readonly Vector2 Motion;
public readonly bool Jump;
public readonly bool Attack;

View File

@ -10,7 +10,7 @@ namespace SemiColinGames {
timeInState = 0;
}
public string? Update(NPC npc, float modelTime, AABB[] collisionTargets) {
public string? Update(NPC npc, float modelTime, World world) {
timeInState += modelTime;
if (timeInState > 1.0f) {
npc.Facing *= -1;
@ -23,14 +23,14 @@ namespace SemiColinGames {
class RunState : IState<NPC> {
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 desiredX = npc.Position.X + (int) (moveSpeed * npc.Facing * modelTime);
// TODO: define the box modularly & correctly.
AABB npcBox = new AABB(new Vector2(desiredX, npc.Position.Y), new Vector2(1, 33));
Debug.AddRect(npcBox, Color.Cyan);
bool foundBox = false;
foreach (AABB box in collisionTargets) {
foreach (AABB box in world.CollisionTargets) {
if (box.Intersect(npcBox) != null) {
foundBox = true;
break;
@ -63,8 +63,8 @@ namespace SemiColinGames {
public int Facing = 1;
public Point Position;
public void Update(float modelTime, AABB[] collisionTargets) {
fsm.Update(this, modelTime, collisionTargets);
public void Update(float modelTime, World world) {
fsm.Update(this, modelTime, world);
}
public void Draw(SpriteBatch spriteBatch) {

View File

@ -4,7 +4,7 @@ using System;
using System.Collections.Generic;
namespace SemiColinGames {
class Player {
public class Player {
private enum Pose { Walking, Standing, Crouching, Stretching, SwordSwing, Jumping };
private const int moveSpeed = 180;
@ -48,7 +48,7 @@ namespace SemiColinGames {
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) {
return new AABB(new Vector2(position.X, position.Y + yOffset), halfSize);
}
@ -72,7 +72,7 @@ namespace SemiColinGames {
AABB largeBox = new AABB(
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));
foreach (var box in collisionTargets) {
foreach (var box in world.CollisionTargets) {
if (box.Intersect(largeBox) != null) {
// Debug.AddRect(box, Color.Green);
candidates.Add(box);

View File

@ -114,7 +114,7 @@ namespace SemiColinGames {
}
}
class World {
public class World {
public const int TileSize = 16;
readonly Tile[] tiles;
@ -191,9 +191,9 @@ namespace SemiColinGames {
}
public void Update(float modelTime, History<Input> input) {
Player.Update(modelTime, CollisionTargets, input);
Player.Update(modelTime, this, input);
foreach (NPC npc in npcs) {
npc.Update(modelTime, CollisionTargets);
npc.Update(modelTime, this);
}
if (Player.Health <= 0) {
Reset();