diff --git a/DepthsBelow/DepthsBelow/Core.cs b/DepthsBelow/DepthsBelow/Core.cs index 1ae2f5d..9cd3eee 100755 --- a/DepthsBelow/DepthsBelow/Core.cs +++ b/DepthsBelow/DepthsBelow/Core.cs @@ -24,11 +24,11 @@ namespace DepthsBelow public Lua Lua; - public EntityManager EntityManager; + public EntityManager EntityManager; + public TurnManager TurnManager; public Camera Camera; - public bool PlayerTurn = true; public static MouseInput MouseInput; public static KeyboardInput KeyboardInput; public List Squad; @@ -64,18 +64,24 @@ namespace DepthsBelow /// and initialize them as well. /// protected override void Initialize() - { + { + Lua = new Lua(); + // TODO: Add your initialization logic here EntityManager = new EntityManager(); + + TurnManager = new TurnManager(new string[] { "Player", "Computer" }); + Console.WriteLine("Current turn: " + TurnManager.CurrentTurn.Name); + TurnManager.EndTurn(); + Console.WriteLine("Current turn: " + TurnManager.CurrentTurn.Name); + TurnManager.EndTurn(); + Console.WriteLine("Current turn: " + TurnManager.CurrentTurn.Name); + Camera = new Camera(this); Squad = new List(); Swarm = new List(); Volley = new List(); - // Run scripts after everything is initialized - Lua = new Lua(); - Lua.LoadScripts(); - base.Initialize(); } @@ -104,7 +110,10 @@ namespace DepthsBelow TestMonster = new SmallEnemy(EntityManager, ref Swarm); TestMonster.X = 12; TestMonster.Y = 4; - Swarm.Add(TestMonster); + Swarm.Add(TestMonster); + + // Load Lua scripts after everything is created + Lua.LoadScripts(); } /// @@ -159,8 +168,6 @@ namespace DepthsBelow // Draw mouse input visuals MouseInput.Draw(spriteBatch); - TestMonster.GetComponent().Draw(spriteBatch); - spriteBatch.End(); // Start drawing GUI components diff --git a/DepthsBelow/DepthsBelow/DepthsBelow.csproj b/DepthsBelow/DepthsBelow/DepthsBelow.csproj index f67f7a9..be246ec 100644 --- a/DepthsBelow/DepthsBelow/DepthsBelow.csproj +++ b/DepthsBelow/DepthsBelow/DepthsBelow.csproj @@ -121,6 +121,7 @@ + diff --git a/DepthsBelow/DepthsBelow/KeyboardInput.cs b/DepthsBelow/DepthsBelow/KeyboardInput.cs index d40133d..01882d9 100644 --- a/DepthsBelow/DepthsBelow/KeyboardInput.cs +++ b/DepthsBelow/DepthsBelow/KeyboardInput.cs @@ -12,41 +12,36 @@ namespace DepthsBelow { public class KeyboardInput { - Core core; - bool Enter = false; - bool turn; + Core core; + private KeyboardState lastKeyboardState; + int checkerSpeed = 2; public KeyboardInput(Core core) { this.core = core; - } - + } + public void Update(GameTime gameTime) { KeyboardState ks = Keyboard.GetState(); - if (ks.IsKeyUp(Keys.Enter) && Enter == true) - { - Enter = false; - } - - if (ks.IsKeyDown(Keys.Enter) && Enter == false) - { - Enter = true; - if (core.PlayerTurn == false) + if (ks.IsKeyDown(Keys.Enter) && lastKeyboardState.IsKeyUp(Keys.Enter)) + { + if (core.TurnManager.CurrentTurn == core.TurnManager["Player"]) { - core.PlayerTurn = true; foreach (var unit in core.Squad) { unit.step = 0; unit.Fired = false; - } + } + + core.TurnManager.EndTurn(); } - else + + if (core.TurnManager.CurrentTurn == core.TurnManager["Computer"]) { core.TestMonster.step = 0; - core.PlayerTurn = false; Point target = Point.Zero; float distance = 7000; foreach (var unit in core.Squad) @@ -63,7 +58,9 @@ namespace DepthsBelow } } core.TestMonster.GetComponent().Goal = target; - distance = 7000; + distance = 7000; + + core.TurnManager.EndTurn(); } } if (ks.IsKeyDown(Keys.A)) @@ -161,7 +158,9 @@ namespace DepthsBelow hit = false; } } - } + } + + lastKeyboardState = ks; } } } \ No newline at end of file diff --git a/DepthsBelow/DepthsBelow/MouseInput.cs b/DepthsBelow/DepthsBelow/MouseInput.cs index 877dca4..7e01cf3 100755 --- a/DepthsBelow/DepthsBelow/MouseInput.cs +++ b/DepthsBelow/DepthsBelow/MouseInput.cs @@ -71,7 +71,7 @@ namespace DepthsBelow } } - if (core.PlayerTurn == true) + if (core.TurnManager.CurrentTurn == core.TurnManager["Player"]) { // Selection rectangle if (ms.LeftButton == ButtonState.Pressed) diff --git a/DepthsBelow/DepthsBelow/TurnManager.cs b/DepthsBelow/DepthsBelow/TurnManager.cs new file mode 100755 index 0000000..15215c8 --- /dev/null +++ b/DepthsBelow/DepthsBelow/TurnManager.cs @@ -0,0 +1,116 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Text; + +namespace DepthsBelow +{ + /// + /// Keeps track of whose turn it is. + /// + public class TurnManager + { + /// + /// A player turn token. + /// + public class Token + { + /// + /// Unique identifying name of the token. + /// + public string Name { get; private set; } + private TurnManager turnManager; + + + /// + /// Initializes a new instance of the class. + /// Preferably use instead. + /// + /// The turn manager the token belongs to. + /// Unique identifying name of the token. + public Token(TurnManager turnManager, string name) + { + this.turnManager = turnManager; + this.Name = name; + turnManager.AddToken(this); + } + } + + /// + /// Dictionary of turn tokens. + /// + public Dictionary Tokens; + private int currentTokenIndex; + + /// + /// The turn token belonging to a player. + /// + public Token CurrentTurn + { + get { return Tokens.ElementAt(currentTokenIndex).Value; } + } + + /// + /// Get the turn token by the specified name. + /// + /// The identifying name of the player. + /// Returns a player turn token. + public Token this[string name] + { + get { return this.Tokens[name]; } + } + + /// + /// Create a turn manager. + /// + public TurnManager() + { + Tokens = new Dictionary(); + } + + /// + /// Create a turn manager. + /// + /// Tokens of players. + public TurnManager(string[] tokenNames) + : this() + { + foreach (var tokenName in tokenNames) + CreateToken(tokenName); + } + + /// + /// Create a turn token. + /// + /// Name of the token. + /// Returns a player turn token. + public Token CreateToken(string name) + { + return Tokens.ContainsKey(name) ? Tokens[name] : new Token(this, name); + } + + /// + /// Add a turn token to + /// + /// + public void AddToken(Token token) + { + if (!Tokens.ContainsKey(token.Name)) + Tokens.Add(token.Name, token); + } + + /// + /// End the current turn, skipping to the next token in the dictionary. + /// + public void EndTurn() + { + var prevTurn = CurrentTurn; + + if (++currentTokenIndex > Tokens.Count - 1) + currentTokenIndex = 0; + + Debug.WriteLine(prevTurn.Name + " turn ended. Current turn: " + CurrentTurn.Name); + } + } +}