From 6e206fe5b24a1b2474d4389f5b0d1517cf54b6d9 Mon Sep 17 00:00:00 2001
From: Susan Yuen <susan_loves_cheese@hotmail.com>
Date: Wed, 2 Nov 2016 16:06:17 -0400
Subject: [PATCH] Added second player. Set game to turn based.

---
 src/Blaze-Brigade/Blaze_Brigade/Game.cs       | 56 +++++++++++--------
 .../Blaze_Brigade/GameFunction.cs             | 29 +++++++++-
 src/Blaze-Brigade/Blaze_Brigade/GameState.cs  | 16 +++++-
 .../Blaze_Brigade/MouseHandler.cs             |  9 ++-
 src/Blaze-Brigade/Blaze_Brigade/Player.cs     | 18 ------
 src/Blaze-Brigade/Blaze_Brigade/Unit.cs       |  4 +-
 src/Blaze-Brigade/Blaze_Brigade/Warrior.cs    | 45 ++++++++-------
 7 files changed, 107 insertions(+), 70 deletions(-)

diff --git a/src/Blaze-Brigade/Blaze_Brigade/Game.cs b/src/Blaze-Brigade/Blaze_Brigade/Game.cs
index b69eb86..44ffb21 100644
--- a/src/Blaze-Brigade/Blaze_Brigade/Game.cs
+++ b/src/Blaze-Brigade/Blaze_Brigade/Game.cs
@@ -101,16 +101,11 @@ namespace Controller
             player2 = new Player();
 
             // load character sprite and set position
-            Vector2 unit1Position = new Vector2(32.0f, 32.0f);
-            MenuButton attackButton = new MenuButton(MenuButtonType.Attack, unit1Position, Content.Load<Texture2D>("attack"));
-            MenuButton moveButton = new MenuButton(MenuButtonType.Move, unit1Position, Content.Load<Texture2D>("move"));
-            MenuButton itemButton = new MenuButton(MenuButtonType.Items, unit1Position, Content.Load<Texture2D>("items")); ;
-            MenuButton waitButton = new MenuButton(MenuButtonType.Wait, unit1Position, Content.Load<Texture2D>("wait")); ;
+            player1.addUnit(getNewUnit(UnitType.Warrior, new Vector2(32f, 32f)));
+            player2.addUnit(getNewUnit(UnitType.Warrior, new Vector2(15*32f, 32f)));
 
-            player1.addUnit(new Warrior(Content.Load<Texture2D>("charSprite"), attackButton, moveButton, 
-                itemButton, waitButton, Content.Load<Texture2D>("warrior_Info"), unit1Position));
-
-            GameState.setPlayerCurrentlyMoving(player1); // set game state
+            GameState.setCurrentPlayer(player1);
+            GameState.setEnemyPlayer(player2);
         }
 
         // Updates game in real time - 60fps
@@ -171,6 +166,13 @@ namespace Controller
 
                 case GameMenuState.Playing: // if true.. load new image...
                     backGround = Content.Load<Texture2D>("Game_Map"); // load background
+                    if (GameFunction.isTurnOver())
+                    {
+                        Player tempPlayer = GameState.getCurrentPlayer();
+                        GameState.setCurrentPlayer(GameState.getEnemyPlayer());
+                        GameState.setEnemyPlayer(tempPlayer);
+                        GameFunction.startTurn(GameState.getCurrentPlayer());
+                    }
                     break;
             }
             #endregion
@@ -192,8 +194,9 @@ namespace Controller
                     // draws units for player 1
                     for (int i = 0; i < player1.getNumOfUnits(); i++)
                     {
-                        Unit unit = player1.getUnits().ElementAt(i); //gets unit at i
-                        spriteBatch.Draw(unit.getSpriteImage(), unit.getPixelCoordinates(), null, Color.White, 0, Vector2.Zero, 1f, SpriteEffects.None, 0.8f); // redraws char sprite
+                        Unit unit = player1.getUnits().ElementAt(i); // gets unit at i
+                        spriteBatch.Draw(unit.getSpriteImage(), unit.getPixelCoordinates(),
+                            null, Color.White, 0, Vector2.Zero, 1f, SpriteEffects.None, 0.8f); // redraws char sprite
 
                     }
                     #endregion
@@ -205,8 +208,6 @@ namespace Controller
                             Unit unit = GameState.getSelectedUnit();
                         if (!GameState.getIsAnimating())
                         {
-
-
                             if (GameState.getBeforeMove()) // if unit has yet to move, display the overall move and attack range of unit
                             {
                             #region Highlight nodes
@@ -228,7 +229,7 @@ namespace Controller
                             }
                                 #endregion
                             }
-                            else // elseif unit has already moved, only display the attack range
+                            else // else if unit has already moved, only display the attack range
                             {
                                 LinkedList<Node> attackableNodes = GameFunction.getAttackRangeAfterMoving(graph, unit);
                                 foreach (Node attack in attackableNodes)
@@ -240,6 +241,7 @@ namespace Controller
                             #region Drop Down menu
                             if (GameState.getMenuOpen()) // if dropDowMenu should be opened, draw dropDownMenu
                             {
+                                unit.setMenuButtonCoordinates(unit.getPixelCoordinates());
                                 foreach (MenuButton button in unit.getMenuButtons())
                                 {
                                     if (button.getActive())
@@ -274,24 +276,34 @@ namespace Controller
                     // draws units for player 2
                     for (int i = 0; i < player2.getNumOfUnits(); i++)
                     {
-                        Unit unit = player1.getUnits().ElementAt(i);
+                        Unit unit = player2.getUnits().ElementAt(i);
                         spriteBatch.Draw(unit.getSpriteImage(), unit.getPixelCoordinates(), Color.White);
                         
                     }
                     #endregion
-
-                    //spriteBatch.Draw(backGround, Vector2.Zero, Color.White, );    // draws background
-
-
+                    
                     spriteBatch.Draw(backGround, Vector2.Zero, null, Color.White, 0, Vector2.Zero, 1f, SpriteEffects.None, 1);
                     
                     break;
-
-                    
             }
+
             spriteBatch.End();  // end spriteBatch
             base.Draw(gameTime);    // repeatedly calls draw
         }
-        
+
+        private Unit getNewUnit(UnitType unitType, Vector2 unitPosition)
+        {
+            MenuButton attackButton = new MenuButton(MenuButtonType.Attack, unitPosition, Content.Load<Texture2D>("attack"));
+            MenuButton moveButton = new MenuButton(MenuButtonType.Move, unitPosition, Content.Load<Texture2D>("move"));
+            MenuButton itemButton = new MenuButton(MenuButtonType.Items, unitPosition, Content.Load<Texture2D>("items")); ;
+            MenuButton waitButton = new MenuButton(MenuButtonType.Wait, unitPosition, Content.Load<Texture2D>("wait")); ;
+
+            if (unitType == UnitType.Warrior)
+            {
+                return new Warrior(Content.Load<Texture2D>("charSprite"), attackButton, moveButton,
+                    itemButton, waitButton, Content.Load<Texture2D>("warrior_Info"), unitPosition);
+            }
+            return null;
+        }
     }
 }
diff --git a/src/Blaze-Brigade/Blaze_Brigade/GameFunction.cs b/src/Blaze-Brigade/Blaze_Brigade/GameFunction.cs
index 2f243df..9d293ff 100644
--- a/src/Blaze-Brigade/Blaze_Brigade/GameFunction.cs
+++ b/src/Blaze-Brigade/Blaze_Brigade/GameFunction.cs
@@ -4,6 +4,7 @@ using System.Linq;
 using System.Text;
 
 using Model;
+using View;
 
 namespace Controller
 {
@@ -20,9 +21,13 @@ namespace Controller
         // sets all units to "not moved" upon start of a turn
         public static void startTurn(Player player)
         {
-            for (int i = 0; i < player.getUnits().Count; i++)
+            foreach (Unit unit in player.getUnits())
             {
-                player.getUnits().ElementAt(i).setMoved(false);
+                MenuButton[] menuButtons = unit.getMenuButtons();
+                for (int i=0; i<menuButtons.Count(); i++)
+                {
+                    menuButtons[i].setActive(true);
+                }
             }
         }
 
@@ -37,10 +42,28 @@ namespace Controller
         // checks if specified unit can perform actions
         public static bool hasUnitFinishedActions(Unit unit)
         {
-            // TODO
+            if (!unit.isButtonActive(MenuButtonType.Attack)) {
+                return true;
+            }
+            if (!unit.isButtonActive(MenuButtonType.Wait))
+            {
+                return true;
+            }
             return false;
         }
 
+        public static bool isTurnOver()
+        {
+            foreach (Unit unit in GameState.getCurrentPlayer().getUnits())
+            {
+                if (!hasUnitFinishedActions(unit))
+                {
+                    return false;
+                }
+            }
+            return true;
+        }
+
         // checks lose/win conditions to determine if game is over
         public static bool isGameOver()
         {
diff --git a/src/Blaze-Brigade/Blaze_Brigade/GameState.cs b/src/Blaze-Brigade/Blaze_Brigade/GameState.cs
index 358b808..8f66cd5 100644
--- a/src/Blaze-Brigade/Blaze_Brigade/GameState.cs
+++ b/src/Blaze-Brigade/Blaze_Brigade/GameState.cs
@@ -14,6 +14,7 @@ namespace Model
         private static bool playableUnitSelected;      // indicates whether a unit is currently selected
         private static Unit selectedUnit;              // the currently selected unit
         private static Player currentPlayer;           // player of the current turn
+        private static Player enemyPlayer;             // enemy player of current turn
         private static bool isAnimating = false;       // indicates whether an animation sequence is on screen
         private static bool dropDownMenuOpen;          // indicates whether drop down menu should be open
         private static bool beforeMove;                // true before unit moves, false after it moves. Used to determine what tiles are highlighted
@@ -41,16 +42,27 @@ namespace Model
         }
 
         // returns the player that is currently moving
-        public static Player playerCurrentlyMoving()
+        public static Player getCurrentPlayer()
         {
             return currentPlayer;
         }
 
-        public static void setPlayerCurrentlyMoving(Player p)
+        public static void setCurrentPlayer(Player p)
         {
             currentPlayer = p;
         }
 
+        // returns the enemy player
+        public static Player getEnemyPlayer()
+        {
+            return enemyPlayer;
+        }
+
+        public static void setEnemyPlayer(Player p)
+        {
+            enemyPlayer = p;
+        }
+
         // return whether an animation is currently displaying on screen
         public static bool getIsAnimating()
         {
diff --git a/src/Blaze-Brigade/Blaze_Brigade/MouseHandler.cs b/src/Blaze-Brigade/Blaze_Brigade/MouseHandler.cs
index c6cf22f..2724442 100644
--- a/src/Blaze-Brigade/Blaze_Brigade/MouseHandler.cs
+++ b/src/Blaze-Brigade/Blaze_Brigade/MouseHandler.cs
@@ -84,7 +84,7 @@ namespace Controller
                 else
                 {
                     // if there is a playable, player-owned unit on position clicked, set selected unit status
-                    Unit unit = getPlayableUnitOnNodeClicked(graph.getNode(mouseClickCoordinates), mouseClickCoordinates, GameState.playerCurrentlyMoving());
+                    Unit unit = getPlayableUnitOnNodeClicked(graph.getNode(mouseClickCoordinates), mouseClickCoordinates, GameState.getCurrentPlayer());
                     if (unit != null)
                     {
                         setSelectedUnit(unit, true);
@@ -210,7 +210,7 @@ namespace Controller
             {
                 case MenuButtonType.Attack:         // if attack clicked
                     turnState = TurnState.Attack;
-                    GameState.setMenuOpen(true);    // close the dropdownmenu when selecting who to attack
+                    GameState.setMenuOpen(false);    // close the dropdownmenu when selecting who to attack
                     button.setActive(false);
                     break;
                 case MenuButtonType.Move:           // if moved is clicked
@@ -220,10 +220,13 @@ namespace Controller
                     break;
                 case MenuButtonType.Items:          // if item is clicked
                     turnState = TurnState.Items;
-                    button.setActive(false);
+                    GameState.setMenuOpen(false);
                     break;
                 case MenuButtonType.Wait:           // if wait is clicked
+                    turnState = TurnState.Wait;
+                    GameState.setMenuOpen(false);
                     GameState.setPlayableUnitSelected(false);
+                    button.setActive(false);
                     break;
 
                 default:
diff --git a/src/Blaze-Brigade/Blaze_Brigade/Player.cs b/src/Blaze-Brigade/Blaze_Brigade/Player.cs
index 53c8b42..63c3b8d 100644
--- a/src/Blaze-Brigade/Blaze_Brigade/Player.cs
+++ b/src/Blaze-Brigade/Blaze_Brigade/Player.cs
@@ -38,24 +38,6 @@ namespace Model
         {
             ownedUnits.AddLast(unit);
         }
-        // checks if current turn is over yet (no more possible moves)
-        // use this as a guard for while loop?
-        // checks if current turn is over yet (no more possible moves)
-        // use this as a guard for while loop?
-        public bool isTurnOver()
-        {
-            int count = 0;
-            for (int i = 0; i < this.getUnits().Count; i++)
-            {
-                if (this.getUnits().ElementAt(i).getMoved() == true)
-                {
-                    count++;
-                }
-            }
-            // if the number of moved units equals the number of total units left
-
-            return (count == this.getUnits().Count);
-        }
     }
 }
 
diff --git a/src/Blaze-Brigade/Blaze_Brigade/Unit.cs b/src/Blaze-Brigade/Blaze_Brigade/Unit.cs
index 1309cdc..1921bb2 100644
--- a/src/Blaze-Brigade/Blaze_Brigade/Unit.cs
+++ b/src/Blaze-Brigade/Blaze_Brigade/Unit.cs
@@ -11,7 +11,6 @@ namespace Model
 {
     interface Unit
     {
-        bool getMoved();                        // getter function for bool value that says this unit has moved.
         bool isAlive();                         // whether unit is dead or alive
         int getHp();                            // returns unit's HP
         int getStr();                           // returns unit's strength
@@ -29,14 +28,15 @@ namespace Model
         UnitType getClass();                    // returns unit's class (warrior, mage, archer)
         Texture2D getSpriteImage();             // returns the sprite image of the unit
         Texture2D getButtonImage(MenuButtonType buttonType);        // returns the button texture at index i
+        bool isButtonActive(MenuButtonType buttonType);     // indicates whether a button has already been previously selected or not
         Texture2D getCharInfo();                // returns the char info screen texture
         Tuple<int, int> getPosition();          // returns the current position (by node) of the unit
         Vector2 getPixelCoordinates();          // returns the pixel coordinates of the sprite
         void setPixelCoordinates(Vector2 p);    // sets the pixel coordinates of the sprite
         void setPosition(int x, int y);         // sets the current position (by node) of the unit
         void setEquippedWeapon(Weapon w);       // sets the unit's currently equipped weapon
-        void setMoved(bool a);                  // on start of players turn, set all units to unmoved (F)
         MenuButton[] getMenuButtons();          // returns the dropdown menu buttons of the unit
+        void setMenuButtonCoordinates(Vector2 pixelCoordinates);    // sets the coordinates of menu buttons
     }
 
     enum UnitType { Warrior, Archer, Mage };    //defines the possible classes of a unit
diff --git a/src/Blaze-Brigade/Blaze_Brigade/Warrior.cs b/src/Blaze-Brigade/Blaze_Brigade/Warrior.cs
index bb33a50..f5a4219 100644
--- a/src/Blaze-Brigade/Blaze_Brigade/Warrior.cs
+++ b/src/Blaze-Brigade/Blaze_Brigade/Warrior.cs
@@ -11,7 +11,6 @@ namespace Model
 {
     class Warrior : Unit
     {
-        private bool moved;
         private bool alive;
         private int hp;
         private int str;
@@ -147,17 +146,36 @@ namespace Model
                 case MenuButtonType.Attack: // if attack clicked
                     return menuButtons[0].getImage();
                 case MenuButtonType.Items: // if moved is clicked
-                    return menuButtons[3].getImage();
+                    return menuButtons[2].getImage();
                 case MenuButtonType.Move: // if item is clicked
                     return menuButtons[1].getImage();
                 case MenuButtonType.Wait: // if wait is clicked
-                    return menuButtons[2].getImage();
+                    return menuButtons[3].getImage();
 
                 default:
                     return null;
             }
         }
 
+        // indicates whether a button has already been previously selected or not
+        public bool isButtonActive(MenuButtonType buttonType)
+        {
+            switch (buttonType)
+            {
+                case MenuButtonType.Attack:
+                    return menuButtons[0].getActive();
+                case MenuButtonType.Items:
+                    return menuButtons[2].getActive();
+                case MenuButtonType.Move:
+                    return menuButtons[1].getActive();
+                case MenuButtonType.Wait:
+                    return menuButtons[3].getActive();
+
+                default:
+                    return false;
+            }
+        }
+
         public Texture2D getCharInfo()
         {
             return charInfo;
@@ -177,13 +195,11 @@ namespace Model
         {
             position = new Tuple<int, int>(x, y);
             pixelCoordinates = new Vector2(x * 32, y * 32);
-            setMenuButtonCoordinates(pixelCoordinates);
         }
 
         public void setPixelCoordinates(Vector2 p)
         {
             pixelCoordinates = p;
-            setMenuButtonCoordinates(pixelCoordinates);
             int positionX = (int)Math.Round(pixelCoordinates.X / 32);
             int positionY = (int)Math.Round(pixelCoordinates.Y / 32);
             position = new Tuple<int, int>(positionX, positionY);
@@ -193,16 +209,6 @@ namespace Model
         {
             equippedWeapon = w;
         }
-        public bool getMoved()
-        {
-           
-            return moved;
-        }
-
-        public void setMoved(bool a)
-        {
-            this.moved = a;
-        }
 
         public MenuButton[] getMenuButtons()
         {
@@ -210,15 +216,14 @@ namespace Model
         }
         
         // updates menu button positions
-        private void setMenuButtonCoordinates(Vector2 pixelCoordinates)
+        public void setMenuButtonCoordinates(Vector2 pixelCoordinates)
         {
             int increment = 0;
-
-            for (int i=0; i<menuButtons.Length; i++)
+            foreach (MenuButton button in menuButtons)
             {
-                if (menuButtons[i].getActive())
+                if (button.getActive())
                 {
-                    menuButtons[i].setPixelCoordinates((int) (pixelCoordinates.X+32), (int) (pixelCoordinates.Y + (increment*32)));
+                    button.setPixelCoordinates((int)(pixelCoordinates.X + 32), (int)(pixelCoordinates.Y + (increment * 32)));
                     increment++;
                 }
             }
-- 
GitLab