Exemplo n.º 1
0
 // Update is called once per frame
 void Update()
 {
     if (playerTurn)
     {
         if (Input.GetMouseButtonDown(0) && !moving)
         {
             //IF NO UNIT SEE IF CLICKING ON UNIT
             if (!selected)
             {
                 Vector3Int clickPos = playGrid.getCell();
                 clickPos.z = 0;
                 foreach (Unit unit in FriendlyUnits)
                 {
                     if (unit.GetCellPos() == clickPos && unit.HasActionLeft())
                     {
                         SelectUnit(unit);
                     }
                 }
             }
             else //Unit Selected
             {
                 playGrid.LightDown();
                 Vector3Int     clickPos = playGrid.getCell();
                 GridCell.State state    = playGrid.getCellState(clickPos);
                 clickPos.z = 0;
                 if (state == GridCell.State.Enemy)
                 {
                     for (int i = EnemyUnits.Count - 1; i >= 0; i--)
                     {
                         Unit target = EnemyUnits[i];
                         if (target.GetCellPos() == clickPos && playGrid.GetAttackable(clickPos))
                         {
                             Attack(selected, target);
                         }
                     }
                 }
                 else if (state == GridCell.State.Friendly)
                 {
                     foreach (Unit unit in FriendlyUnits)
                     {
                         if (unit.GetCellPos() == clickPos && unit.HasActionLeft())
                         {
                             SelectUnit(unit);
                         }
                     }
                 }
                 else if (state == GridCell.State.Empty)
                 {
                     if (playGrid.GetReachable(clickPos) && selected.HasMoveLeft())
                     {
                         Move(selected.GetCellPos(), clickPos, selected);
                         GetAttackable(selected);
                     }
                     else
                     {
                         selected = null;
                     }
                 }
             }
         }
     }
     else //Enemy Turn
     {
         if (!moving)
         {
             bool enemyLeft = false;
             foreach (Unit enemy in EnemyUnits)
             {
                 if (enemy.HasActionLeft())
                 {
                     enemyLeft = true;
                 }
                 else
                 {
                     continue;
                 }
                 playGrid.Djikstra(enemy, FriendlyUnits);
                 bool  canAttack  = false;
                 float bestValue  = 0;
                 Unit  bestTarget = null;
                 //Check if target in range
                 for (int i = FriendlyUnits.Count - 1; i >= 0; i--)
                 {
                     Unit target = FriendlyUnits[i];
                     if (playGrid.GetAttackable(target.GetCellPos()))
                     {
                         canAttack = true;
                         if (target.getImportance() > bestValue)
                         {
                             bestTarget = target;
                             bestValue  = target.getImportance();
                         }
                     }
                 }
                 //IF target found attack
                 if (canAttack)
                 {
                     Attack(enemy, bestTarget);
                     break;
                 }
                 else
                 //Find the best square to move to
                 {
                     Vector3Int bestMove = playGrid.GetBestInfluenceMove(EnemyUnits, FriendlyUnits);
                     Move(enemy.GetCellPos(), bestMove, enemy);
                     enemy.DoAction();
                     break;
                 }
             }
             if (!enemyLeft)
             {
                 EndTurn();
             }
         }
     }
 }