Inheritance: StaticTileObject
 private void OnTowerExit(ref Tower tower)
 {
     if (TowerExit != null)
         TowerExit(ref tower);
 }
 public void UnregisterTowerListenerForUnit(ref Tower tower)
 {
     UnitEnter -= tower.RegisterAttackUnit;
     UnitExit -= tower.UnregisterAttackUnit;
 }
 private void OnTowerEnter(ref Tower tower)
 {
     if (TowerEnter != null)
         TowerEnter(ref tower);
 }
 //public override string ToString()
 //{
 //    return this.ID.ToString() + " " + TileType.ToString();
 //}
 public void SetBlockableObject(Tower gameObject)
 {
     Tower = gameObject;
     SetTileType(TileType.Blocked);
     OnTowerEnter(ref gameObject);
 }
 public void UnregisterTowerListenerForTower(ref Tower tower)
 {
     TowerEnter -= tower.RegisterAttackTower;
     TowerExit -= tower.UnregisterAttackTower;
 }
        public static void SetTowerForLevelMap(Tower tower, Tile tile)
        {
            tile.SetBlockableObject(tower);
            //tile2.SetBlockableObject(tower);

            //UpdateTilePaths();

            //List<Tile> walkableNeighbors = GetWalkableNeighbors(tile);

            for (int i = 0; i < tile.tileNeighbors.Count; i++)
            {
                tile.tileNeighbors[i].RegisterTowerListenerForUnit(ref tower);
            }

            for (int i = 0; i < tile.tileNeighbors.Count; i++)
            {
                tile.tileNeighbors[i].RegisterTowerListenerForTower(ref tower);
            }
            //OnTowerCreated();
        }
 public void RemoveBlockableObject()
 {
     this.tower = null;
     SetTileType(TileType.Walkable);
 }
 public void SetTower(Tower tower)
 {
     this.tower = tower;
     SetTileType(TileType.Blocked);
 }
 public void UnregisterTowerListener(Tower tower)
 {
     UnitEnter -= tower.RegisterAttackUnit;
 }
        public static bool SetTower(Tower tower, Tile tile)
        {
            tile.SetTower(tower);
            if (IsTilePathsValid())
            {
                UpdateTilePaths();

                List<Tile> walkableNeighbors = GetWalkableNeighbors(tile);

                for (int i = 0; i < walkableNeighbors.Count; i++)
                {
                   walkableNeighbors[i].RegisterTowerListener(tower);
                }
                OnTowerCreated();
                return true;
            }

            RemoveTower(tile);
            return false;
        }
 public void RemoveTower()
 {
     this.tower = null;
     SetTileType(TileType.Walkable);
 }
 public void UnregisterAttackTower(ref Tower tower)
 {
     towersToAttack.Remove(tower);
 }
        public void RegisterAttackTower(ref Tower tower)
        {
            if (tower.TeamNum == this.TeamNum)
                return;

            if (towersToAttack.Count > 0 && towersToAttack[0].Health < tower.Health)
            {
                towersToAttack.Insert(0, tower);
            }
            else if (!towersToAttack.Contains(tower))
            {
                towersToAttack.Add(tower);
            }
        }
 public void SetBlockableObject(Tower gameObject)
 {
     this.tower = gameObject;
     SetTileType(TileType.Blocked);
 }
 public static void SetObject(Tower gameObject, Tile Tile2)
 {
     SetTower(ref gameObject, ref Tile2);
 }
        public void RegisterTowerListenerForTower(ref Tower tower)
        {
            TowerEnter += tower.RegisterAttackTower;
            TowerExit += tower.UnregisterAttackTower;

            if(this.Tower != null)
                OnTowerEnter(ref this.Tower);
        }
        public static bool SetTower(ref Tower tower, ref Tile Tile2)
        {
            //Tile2.SetBlockableObject(tower);
            //AStar2.UpdateWalkableNeighborsForTileID(Tile2.ID);
            if (IsTilePathsValidFor(Tile2.ID))
            {
                Tile2.SetBlockableObject(tower);
                //AStar2.UpdateWalkableNeighborsForTileID(Tile2.ID);
                AStar2.UpdateNeeded = true;
                ////UpdateTilePaths();

                //walkableNeighbors = GetWalkableNeighbors(Tile2);

                //IDEA: WHAT ABOUT OFFSETTING THIS TO THE THREAD?
                //Maintain a list of towers to update on the next update pass after walkableNeighbors is relevant?
                for (int i = 0; i < Tile2.tileNeighbors.Count; i++)
                {
                    Tile2.tileNeighbors[i].RegisterTowerListenerForUnit(ref tower);
                }
                for (int i = 0; i < Tile2.tileNeighbors.Count; i++)
                {
                    Tile2.tileNeighbors[i].RegisterTowerListenerForTower(ref tower);
                }
                tower.tile = Tile2;
                OnTowerCreated();

                return true;
            }

               // RemoveTower(ref Tile2);

            //Tile2.RemoveBlockableObject();
            //for (int i = 0; i < Tile2.tileNeighbors.Count; i++)
            //{
            //    Tile2.tileNeighbors[i].UnregisterTowerListenerForTower(ref tower);
            //    Tile2.tileNeighbors[i].UnregisterTowerListenerForUnit(ref tower);
            //}
            //AStar2.UpdateWalkableNeighborsForTileID(Tile2.ID);
            //for (int j = 0; j < bases.Count; j++)
            //{
            //    for (int i = 0; i < bases.Count; i++)
            //    {
            //        if (i != j)
            //        {
            //            bases[i].Tile.UpdatePathTo(bases[j].Tile);
            //        }
            //    }
            //}
            return false;
        }
        public void RemoveBlockableObject()
        {
            if (TileType == TileType.Base)
                return;

            OnTowerExit(ref Tower);
            Tower = null;
            SetTileType(TileType.Walkable);
        }
        public static void Destroy(ref Tower tower)
        {
            tower.Status = TowerStatus.Inactive;
            tower.tile.RemoveBlockableObject();

            //for (int i = 0; i < tower.tile.tileNeighbors.Count; i++)
            //{
            //    tower.tile.tileNeighbors[i].UnregisterTowerListenerForTower(ref tower);
            //    tower.tile.tileNeighbors[i].UnregisterTowerListenerForUnit(ref tower);
            //}
        }
        public void HandleInput()
        {
            //Human Player
            if (Type == PlayerType.Human)
            {
                avatarMoved = avatar.HandleInput(playerIndexes[PlayerNum]);
                if (avatarMoved)
                {
                    if (PlayScreen.GameType == PlayerScreenType.Scenario)
                        ScenarioManager.RegisterAction(ScenarioItemType.AvatarMove);
                }
                if (avatar.Position.X < TileMap.Left)
                {
                    avatar.Position = new Vector3(TileMap.Left, avatar.Position.Y, avatar.Position.Z);
                    avatarMoved = true;
                }

                if (avatar.Position.Z < TileMap.Top)
                {
                    avatar.Position = new Vector3(avatar.Position.X, avatar.Position.Y, TileMap.Top);
                    avatarMoved = true;
                }

                if (avatar.Position.X > TileMap.Right)
                {
                    avatar.Position = new Vector3(TileMap.Right, avatar.Position.Y, avatar.Position.Z);
                    avatarMoved = true;
                }

                if (avatar.Position.Z > TileMap.Bottom)
                {
                    avatar.Position = new Vector3(avatar.Position.X, avatar.Position.Y, TileMap.Bottom);
                    avatarMoved = true;
                }

                if (ScreenManager.InputManager.CheckNewAction(InputAction.TowerBuild, playerIndexes[PlayerNum]))
                {
                    if (!this.Rezone)
                    {
                        Tower tower = TowerCollection.Add(PlayerNum, TeamNum, Money, lastBuiltTower, this.avatarFollowingTile.Position);
                        if (tower != null)
                            Money -= tower.Cost;

                        if (PlayScreen.GameType == PlayerScreenType.Scenario)
                            switch (lastBuiltTower)
                            {
                                case TowerType.Cannon:
                                case TowerType.Electric:
                                case TowerType.Plasma:
                                    ScenarioManager.RegisterAction(ScenarioItemType.BuildDefenseTower);
                                    break;
                                case TowerType.SmallUnit:
                                case TowerType.LargeUnit:
                                    ScenarioManager.RegisterAction(ScenarioItemType.BuildUnitTower);
                                    break;
                            }

                        ///MoneyString = Money.ToString();
                    }
                }

                if (ScreenManager.InputManager.CheckNewAction(InputAction.TowerDestroy, playerIndexes[PlayerNum]))
                {
                    if (!this.Rezone)
                    {
                        Money += TowerCollection.Remove(PlayerNum, ref this.avatarFollowingTile.Position);

                        if(PlayScreen.GameType == PlayerScreenType.Scenario)
                            ScenarioManager.RegisterAction(ScenarioItemType.DestroyTower);
                        //MoneyString = Money.ToString();
                    }
                }

                if (ScreenManager.InputManager.CheckNewAction(InputAction.TowerRepair, playerIndexes[PlayerNum]))
                {
                    if (!this.Rezone)
                    {
                        Money -= TowerCollection.Repair(PlayerNum, Money, ref this.avatarFollowingTile.Position);
                        if (PlayScreen.GameType == PlayerScreenType.Scenario)
                            ScenarioManager.RegisterAction(ScenarioItemType.RepairTower);
                        //MoneyString = Money.ToString();
                    }
                }

                if (ScreenManager.InputManager.CheckNewAction(InputAction.TowerUpgrade, playerIndexes[PlayerNum]))
                {
                    if (!this.Rezone)
                    {
                        Money -= TowerCollection.Upgrade(TeamNum, Money, ref this.avatarFollowingTile.Position);
                        if (PlayScreen.GameType == PlayerScreenType.Scenario)
                            ScenarioManager.RegisterAction(ScenarioItemType.UpgradeTower);
                        //MoneyString = Money.ToString();
                    }

                }

                if (ScreenManager.InputManager.CheckNewAction(InputAction.PlayerMenuLeft, playerIndexes[PlayerNum]))
                {
                    if (((int)currentlySelectedPlayerStatus - 1) >= 0)
                        currentlySelectedPlayerStatus--;
                    unitScreenActivated = false;

                    if (towerInfoScreenActivated)
                    {
                        if(hoverTower != null)
                            hoverTower.InputLeft();
                    }
                    else if (currentlySelectedPlayerStatus == PlayerMenuTabs.DefenseTower)
                    {
                        //if (((int)defenseTowerSelected - 1) >= 0)
                        //    defenseTowerSelected--;
                        //else
                        //    defenseTowerSelected = NUM_DEFENSE_TOWERS - 1;
                        lastBuiltTower = defenseTowerInfo[defenseTowerSelected].type;
                    }
                    else if (currentlySelectedPlayerStatus == PlayerMenuTabs.UnitTower)
                    {
                        //if (((int)defenseTowerSelected - 1) >= 0)
                        //    defenseTowerSelected--;
                        //else
                        //    defenseTowerSelected = NUM_DEFENSE_TOWERS - 1;
                        lastBuiltTower = offenseTowerInfo[offenseTowerSelected].type;
                    }

                }

                if (ScreenManager.InputManager.CheckNewAction(InputAction.PlayerMenuRight, playerIndexes[PlayerNum]))
                {
                    if (((int)currentlySelectedPlayerStatus + 1) < playerMenuTabsEnumType.Length)
                        currentlySelectedPlayerStatus++;
                    unitScreenActivated = false;

                    if (towerInfoScreenActivated)
                    {
                        if (hoverTower != null)
                            hoverTower.InputRight();
                    }
                    else if (currentlySelectedPlayerStatus == PlayerMenuTabs.DefenseTower)
                    {
                        //if (((int)defenseTowerSelected - 1) >= 0)
                        //    defenseTowerSelected--;
                        //else
                        //    defenseTowerSelected = NUM_DEFENSE_TOWERS - 1;
                        lastBuiltTower = defenseTowerInfo[defenseTowerSelected].type;
                    }
                    else if (currentlySelectedPlayerStatus == PlayerMenuTabs.UnitTower)
                    {
                        //if (((int)defenseTowerSelected - 1) >= 0)
                        //    defenseTowerSelected--;
                        //else
                        //    defenseTowerSelected = NUM_DEFENSE_TOWERS - 1;
                        lastBuiltTower = offenseTowerInfo[offenseTowerSelected].type;
                    }

                }

                if (ScreenManager.InputManager.CheckNewAction(InputAction.PlayerMenuUp, playerIndexes[PlayerNum]))
                {
                    if (towerInfoScreenActivated)
                    {
                        if (hoverTower != null)
                            hoverTower.InputUp();
                    }
                    else if (currentlySelectedPlayerStatus == PlayerMenuTabs.DefenseTower)
                    {
                        if (((int)defenseTowerSelected - 1) >= 0)
                            defenseTowerSelected--;
                        else
                            defenseTowerSelected = NUM_DEFENSE_TOWERS - 1;
                        lastBuiltTower = defenseTowerInfo[defenseTowerSelected].type;
                    }
                    else if (currentlySelectedPlayerStatus == PlayerMenuTabs.UnitTower)
                    {
                        if (((int)offenseTowerSelected - 1) >= 0)
                            offenseTowerSelected--;
                        else
                            offenseTowerSelected = NUM_OFFENSE_TOWERS - 1;
                        lastBuiltTower = offenseTowerInfo[offenseTowerSelected].type;
                    }
                    else if (currentlySelectedPlayerStatus == PlayerMenuTabs.Powers)
                    {
                        if (((int)powerSelected - 1) >= 0)
                            powerSelected--;
                        else
                            powerSelected = NUM_POWERS - 1;
                    }

                    unitScreenActivated = false;
                }

                if (ScreenManager.InputManager.CheckNewAction(InputAction.PlayerMenuDown, playerIndexes[PlayerNum]))
                {
                    if (towerInfoScreenActivated)
                    {
                        if (hoverTower != null)
                            hoverTower.InputDown();
                    }
                    else if (currentlySelectedPlayerStatus == PlayerMenuTabs.DefenseTower)
                    {
                        if (((int)defenseTowerSelected + 1) < NUM_DEFENSE_TOWERS)
                            defenseTowerSelected++;
                        else
                            defenseTowerSelected = 0;
                        lastBuiltTower = defenseTowerInfo[defenseTowerSelected].type;
                    }
                    else if (currentlySelectedPlayerStatus == PlayerMenuTabs.UnitTower)
                    {
                        if (((int)offenseTowerSelected + 1) < NUM_OFFENSE_TOWERS)
                            offenseTowerSelected++;
                        else
                            offenseTowerSelected =0;
                        lastBuiltTower = offenseTowerInfo[offenseTowerSelected].type;
                    }
                    else if (currentlySelectedPlayerStatus == PlayerMenuTabs.Powers)
                    {
                        if (((int)powerSelected + 1) < NUM_POWERS)
                            powerSelected++;
                        else
                            powerSelected = 0;
                    }
                    unitScreenActivated = false;
                }

                if (ScreenManager.InputManager.CheckAction(InputAction.UnitUp, playerIndexes[PlayerNum]))
                {
                    if (unitSelectionPosition.Y < 1)
                        unitSelectionPosition.Y += 1;

                    unitScreenActivated = true;
                }

                if (ScreenManager.InputManager.CheckAction(InputAction.UnitLeft, playerIndexes[PlayerNum]))
                {
                    if (unitSelectionPosition.X > -1)
                        unitSelectionPosition.X -= 1;
                    unitScreenActivated = true;

                }

                if (ScreenManager.InputManager.CheckAction(InputAction.UnitDown, playerIndexes[PlayerNum]))
                {
                    if (unitSelectionPosition.Y > -1)
                        unitSelectionPosition.Y -= 1;
                    unitScreenActivated = true;
                }

                if (ScreenManager.InputManager.CheckAction(InputAction.UnitRight, playerIndexes[PlayerNum]))
                {
                    if (unitSelectionPosition.X < 1)
                        unitSelectionPosition.X += 1;
                    unitScreenActivated = true;
                }

                // 0 | 1 | 2
                // 3 | - | 4
                // 5 | 6 | 7
                //case 1
                if (unitSelectionPosition.X == 0 && unitSelectionPosition.Y == 1)
                {
                    selectedUnit = 1;
                }
                else if (unitSelectionPosition.X == 0 && unitSelectionPosition.Y == -1)
                {
                    selectedUnit = 6;
                }

                if (ScreenManager.InputManager.CheckAction(InputAction.UnitBuild, playerIndexes[PlayerNum]))
                {

                    if (selectedUnit != queuedUnitType)
                    {
                        queuedUnits = 0;
                        queuedDeclineMode = false;
                        unitsDeployed = 0;
                        queuedUnitsToDeploy = 0;
                        percentOfUnitsQueued = 0;
                    }

                    if (queuedDeclineMode && ScreenManager.InputManager.CheckNewAction(InputAction.UnitBuild, playerIndexes[PlayerNum]))
                    {
                        queuedUnits = 0;
                        queuedDeclineMode = false;
                        unitsDeployed = 0;
                        queuedUnitsToDeploy = 0;
                        percentOfUnitsQueued = 0;
                    }

                    if (!queuedDeclineMode || queuedUnitsToDeploy == 0)
                    {
                        int amountLeft =  UnitCollection.MaxUnitsToDeployFor(PlayerNum, unitInformation[selectedUnit].type);
                        if (queuedUnits + queuedUnitAmount <= amountLeft)
                        {
                            queuedUnits += 100;// queuedUnitAmount;

                            if (queuedUnitsToDeploy != 0 && queuedDeclineMode)
                            {
                                percentOfUnitsQueued = (float)queuedUnits / (float)queuedUnitsToDeploy;
                            }
                            queuedDeclineMode = false;
                            if (PlayScreen.GameType == PlayerScreenType.Scenario)
                                ScenarioManager.RegisterAction(ScenarioItemType.Deploy);
                        }

                        if (!queuedDeclineMode)
                        {
                            percentOfUnitsQueued += 1.4f;
                        }
                        queuedUnitType = selectedUnit;
                    }
                }

                if (ScreenManager.InputManager.CheckNewReleaseAction(InputAction.UnitBuild, playerIndexes[PlayerNum]) || (percentOfUnitsQueued >= 100.0f))
                {
                    if (!queuedDeclineMode)
                    {
                        queuedDeclineMode = true;
                        int totalUnits = UnitCollection.MaxUnitsToDeployFor(PlayerNum, unitInformation[selectedUnit].type) + unitsDeployed;
                        queuedUnits = (int)(totalUnits * (percentOfUnitsQueued / 100.0f)) - unitsDeployed;
                        queuedUnitsToDeploy = totalUnits - unitsDeployed;
                        percentOfUnitsQueued = 0;
                    }
                }

                if (ScreenManager.InputManager.CheckNewAction(InputAction.HUD, playerIndexes[PlayerNum]))
                {
                    isHUDDisplayed = true;
                    if (PlayScreen.GameType == PlayerScreenType.Scenario)
                        ScenarioManager.RegisterAction(ScenarioItemType.ShowHUD);
                }

                if(ScreenManager.InputManager.CheckNewReleaseAction(InputAction.HUD, playerIndexes[PlayerNum]))
                {
                    isHUDDisplayed = false;
                }

                if (ScreenManager.InputManager.CheckNewAction(InputAction.PickEnemyTarget, playerIndexes[PlayerNum]))
                {
                    if (pickEnemyTargetPressed)
                    {
                        this.TargetPlayerNum = PlayerCollection.GetNextTargetFor(this.PlayerNum);
                        PlayerCollection.SetTargetFor(PlayerNum, TargetPlayerNum);
                    }

                    //get opacity to display draw
                    enemyTargetOpacity.A = 255;
                    pickEnemyTargetPressed = true;
                }

                if (ScreenManager.InputManager.CheckAction(InputAction.TowerInformation, playerIndexes[PlayerNum]))
                {
                    towerInfoScreenActivated = true;
                    if (true)//avatarMoved || !towerInfoScreenActivated)
                    {
                        if ((hoverTower = TileMap.GetTowerInformationAtPosition(avatar.Position)) != null)
                        {
                            towerInfoScreenActivated = true;
                            unitScreenActivated = false;
                        }
                        else
                        {
                            towerInfoScreenActivated = false;
                        }
                    }
                }

                if (ScreenManager.InputManager.CheckNewReleaseAction(InputAction.TowerInformation, playerIndexes[PlayerNum]))
                {
                    towerInfoScreenActivated = false;
                }

                if (ScreenManager.InputManager.CheckNewAction(InputAction.Pause, playerIndexes[PlayerNum] ))
                {
                    screenManager.ShowScreen(new PauseScreen());
                }

                if (ScreenManager.InputManager.CheckNewAction(InputAction.PowerActivate, playerIndexes[PlayerNum]))
                {
                    PowerManager.AddPower((PowerType)powerSelected, PlayerNum);
                    if (PlayScreen.GameType == PlayerScreenType.Scenario)
                        ScenarioManager.RegisterAction(ScenarioItemType.Power);
                }
            }
        }