From 0f8d9c2814ed9094b30226f930ec7addb1101c25 Mon Sep 17 00:00:00 2001 From: Colin McMillen Date: Fri, 6 Mar 2020 12:28:58 -0500 Subject: [PATCH] FSM / Player / NPCs now get World as an argument to Update() --- Shared/FSM.cs | 6 +++--- Shared/History.cs | 2 +- Shared/Input.cs | 2 +- Shared/NPC.cs | 10 +++++----- Shared/Player.cs | 6 +++--- Shared/World.cs | 6 +++--- 6 files changed, 16 insertions(+), 16 deletions(-) diff --git a/Shared/FSM.cs b/Shared/FSM.cs index 413f96c..d313787 100644 --- a/Shared/FSM.cs +++ b/Shared/FSM.cs @@ -4,7 +4,7 @@ using System.Collections.Generic; namespace SemiColinGames { public interface IState { public void Enter(); - public string? Update(T obj, float modelTime, AABB[] collisionTargets); + public string? Update(T obj, float modelTime, World world); } public class FSM { @@ -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); } diff --git a/Shared/History.cs b/Shared/History.cs index 56abd77..1835f98 100644 --- a/Shared/History.cs +++ b/Shared/History.cs @@ -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 { + public class History { // Backing store for the History's items. private readonly T[] items; diff --git a/Shared/Input.cs b/Shared/Input.cs index 4afeb6a..aa86b6e 100644 --- a/Shared/Input.cs +++ b/Shared/Input.cs @@ -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; diff --git a/Shared/NPC.cs b/Shared/NPC.cs index 545c3a2..d912ea9 100644 --- a/Shared/NPC.cs +++ b/Shared/NPC.cs @@ -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 { 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) { diff --git a/Shared/Player.cs b/Shared/Player.cs index b3c8429..d5b1818 100644 --- a/Shared/Player.cs +++ b/Shared/Player.cs @@ -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) { + public void Update(float modelTime, World world, History 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); diff --git a/Shared/World.cs b/Shared/World.cs index df14a22..1adb05a 100644 --- a/Shared/World.cs +++ b/Shared/World.cs @@ -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) { - 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();