EndTurn() public method

Method makes turn transitions. It is called by player at the end of his turn.
public EndTurn ( ) : void
return void
Esempio n. 1
0
 void Update()
 {
     if (Input.GetKeyDown(KeyCode.N))
     {
         CellGrid.EndTurn();//User ends his turn by pressing "n" on keyboard.
     }
 }
Esempio n. 2
0
 void Update()
 {
     if (Input.GetKeyDown(KeyCode.N) && !(CellGrid.CellGridState is CellGridStateBlockInput))
     {
         CellGrid.EndTurn();//User ends his turn by pressing "n" on keyboard.
     }
 }
Esempio n. 3
0
 void Update()
 {
     if (Input.GetKeyDown(KeyCode.N))
     {
         CellGrid.EndTurn();
     }
 }
Esempio n. 4
0
 public void turnend()
 {
     if (!(CellGrid.CellGridState is CellGridStateAiTurn))
     {
         CellGrid.EndTurn();//User ends his turn by pressing "n" on keyboard.
     }
 }
Esempio n. 5
0
 void OnGUI()
 {
     if (GUI.Button(new Rect(20, 10, 100, 50), "End Turn"))
     {
         CellGrid.EndTurn();
     }
 }
Esempio n. 6
0
 void Update()
 {
     if (isFinished == true)
     {
         EndTrn.EndTurn();
         isFinished = false;
     }
 }
Esempio n. 7
0
 void Update()
 {
     if (Input.GetKeyDown(KeyCode.N) && !(CellGrid.CellGridState is CellGridStateAiTurn))
     {
         CellGrid.EndTurn();//User ends his turn by pressing "n" on keyboard.
         //InfoText.text = "Player " + ((sender as CellGrid).CurrentPlayerNumber + 1);
     }
 }
Esempio n. 8
0
 // Update is called once per frame
 void Update()
 {
     if ((DarkRift.DarkRiftAPI.isConnected && DarkRift.DarkRiftAPI.id - 1 == GameObject.Find("CellGrid").GetComponent <CellGrid>().CurrentPlayerNumber || Unit.debugoverride) &&
         Input.GetKeyDown(KeyCode.N))
     {
         UnitUpdate update = new UnitUpdate();
         update.command = UnitUpdateCommand.END_TURN;
         NetManager.SendData(TagIndex.Controller, TagIndex.PlayerUpdate, update);
         cellGrid.EndTurn();//User ends hit turn by pressing the N key
     }
     //else if(Input.GetKeyDown(KeyCode.U))
     //{
     //    var o = Instantiate(redUnit, cellGrid.Cells[0].transform.position, Quaternion.identity);
     //    ((SampleUnit2)(o)).transform.parent = GameObject.Find("Units Parent").transform;
     //    //cellGrid.SpawnMoreUnits();
     //    List<Cell> cells = new List<Cell>();
     //    cells.Add(cellGrid.Cells[0]);
     //    cellGrid.GetComponent<CustomUnitGenerator>().SpawnUnit((Unit)o, cellGrid.Cells[0]);
     //    cellGrid.GetComponent<CustomUnitGenerator>().SnapToGrid() ;
     //}
 }
Esempio n. 9
0
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.N))
        {
            CellGrid.EndTurn();//User ends his turn by pressing "n" on keyboard.
            CurrentUnit = CellGrid.InitTracker[0];
        }

        if (Input.GetKeyDown(KeyCode.Escape) && CurrentUnit.State.Equals(Unit.CurrentState.Normal))
        {
            CellGrid.CellGridState = new CellGridStateWaitingForInput(CellGrid);
        }
    }
Esempio n. 10
0
    void Start()
    {
        //UnitImage.color = Color.gray;

        CellGrid.GameStarted += OnGameStarted;
        CellGrid.TurnEnded   += OnTurnEnded;
        CellGrid.GameEnded   += OnGameEnded;
        CellGrid.UnitCreated += OnUnitCreated;

        CreateFootmanButton.onClick.AddListener(delegate { OnCreateFootMan(this, new EventArgs()); });
        CreateCavalryButton.onClick.AddListener(delegate { OnCreateCavalry(this, new EventArgs()); });
        CreateArcherButton.onClick.AddListener(delegate { OnCreateArcher(this, new EventArgs()); });
        CreateCatapultButton.onClick.AddListener(delegate { OnCreateCatapult(this, new EventArgs()); });

        NextTurnButton.onClick.AddListener(delegate { CellGrid.EndTurn(); });
    }
Esempio n. 11
0
    private IEnumerator Play()
    {
        var myUnits    = _cellGrid.Units.FindAll(u => u.PlayerNumber.Equals(PlayerNumber)).ToList();
        var enemyUnits = _cellGrid.Units.Except(myUnits).ToList();

        foreach (var unit in myUnits.OrderByDescending(u => u.Cell.GetNeighbours(_cellGrid.Cells).FindAll(u.IsCellTraversable).Count))
        {
            var unitsInRange = new List <Unit>();
            foreach (var enemyUnit in enemyUnits)
            {
                if (unit.IsUnitAttackable(enemyUnit, unit.Cell))
                {
                    unitsInRange.Add(enemyUnit);
                }
            }//Looking for enemies that are in attack range.
            if (unitsInRange.Count != 0)
            {
                var index = _rnd.Next(0, unitsInRange.Count);
                unit.DealDamage(unitsInRange[index]);
                yield return(new WaitForSeconds(0.5f));

                continue;
            }//If there is an enemy in range, attack it.

            List <Cell> potentialDestinations = new List <Cell>();

            foreach (var enemyUnit in enemyUnits)
            {
                potentialDestinations.AddRange(_cellGrid.Cells.FindAll(c => unit.IsCellMovableTo(c) && unit.IsUnitAttackable(enemyUnit, c)));
            }//Making a list of cells that the unit can attack from.

            var notInRange = potentialDestinations.FindAll(c => c.GetDistance(unit.Cell) > unit.MovementPoints);
            potentialDestinations = potentialDestinations.Except(notInRange).ToList();

            if (potentialDestinations.Count == 0 && notInRange.Count != 0)
            {
                potentialDestinations.Add(notInRange.ElementAt(_rnd.Next(0, notInRange.Count - 1)));
            }

            potentialDestinations = potentialDestinations.OrderBy(h => _rnd.Next()).ToList();
            List <Cell> shortestPath = null;
            foreach (var potentialDestination in potentialDestinations)
            {
                var path = unit.FindPath(_cellGrid.Cells, potentialDestination);
                if ((shortestPath == null && path.Sum(h => h.MovementCost) > 0) || shortestPath != null && (path.Sum(h => h.MovementCost) < shortestPath.Sum(h => h.MovementCost) && path.Sum(h => h.MovementCost) > 0))
                {
                    shortestPath = path;
                }

                var pathCost = path.Sum(h => h.MovementCost);
                if (pathCost > 0 && pathCost <= unit.MovementPoints)
                {
                    unit.Move(potentialDestination, path);
                    while (unit.isMoving)
                    {
                        yield return(0);
                    }
                    shortestPath = null;
                    break;
                }
                yield return(0);
            }//If there is a path to any cell that the unit can attack from, move there.

            if (shortestPath != null)
            {
                foreach (var potentialDestination in shortestPath.Intersect(unit.GetAvailableDestinations(_cellGrid.Cells)).OrderByDescending(h => h.GetDistance(unit.Cell)))
                {
                    var path     = unit.FindPath(_cellGrid.Cells, potentialDestination);
                    var pathCost = path.Sum(h => h.MovementCost);
                    if (pathCost > 0 && pathCost <= unit.MovementPoints)
                    {
                        unit.Move(potentialDestination, path);
                        while (unit.isMoving)
                        {
                            yield return(0);
                        }
                        break;
                    }
                    yield return(0);
                }
            }//If the path cost is greater than unit movement points, move as far as possible.

            foreach (var enemyUnit in enemyUnits)
            {
                var enemyCell = enemyUnit.Cell;
                if (unit.IsUnitAttackable(enemyUnit, unit.Cell))
                {
                    unit.DealDamage(enemyUnit);
                    yield return(new WaitForSeconds(0.5f));

                    break;
                }
            }//Look for enemies in range and attack.
        }

        //Check triggers and see if any is activated and apply the effect
        foreach (Trigger trigger in _cellGrid.Triggers)
        {
            for (int i = 0; i < _cellGrid.Units.Count; i++)
            {
                Unit target = _cellGrid.Units [i];

                if (target.PlayerNumber == 0)
                {
                    continue;                     //skip over the human player
                }
                if (target.IsUnitAffectedByTrigger(trigger))
                {
                    bool isTargetDead = target.ApplyTriggerEffectAndCheckDead(trigger);

                    //TODO: enemy units cannot all die at the moment, or Unity will crash :(
                    if (isTargetDead)
                    {
                        _cellGrid.Units.RemoveAt(i);
                        i--;
                    }
                }
            }
        }

        _cellGrid.EndTurn();
    }
Esempio n. 12
0
 public void OnEndTurnButton()
 {
     CellGrid.EndTurn();
 }
Esempio n. 13
0
 public void finalizarTurno()
 {
     cellGrid.EndTurn();
 }
Esempio n. 14
0
    private IEnumerator Play()
    {
        var myUnits = _cellGrid.Units.FindAll(u => u.PlayerNumber.Equals(PlayerNumber)).ToList();

        foreach (var unit in myUnits.OrderByDescending(u => u.Cell.GetNeighbours(_cellGrid.Cells).FindAll(u.IsCellTraversable).Count))
        {
            var enemyUnits   = _cellGrid.Units.Except(myUnits).ToList();
            var unitsInRange = new List <Unit>();
            foreach (var enemyUnit in enemyUnits)
            {
                if (unit.IsUnitAttackable(enemyUnit, unit.Cell))
                {
                    unitsInRange.Add(enemyUnit);
                }
            }//Looking for enemies that are in attack range.
            if (unitsInRange.Count != 0)
            {
                var index = _rnd.Next(0, unitsInRange.Count);
                unit.DealDamage(unitsInRange[index]);
                yield return(new WaitForSeconds(0.5f));

                continue;
            }//If there is an enemy in range, attack it.

            List <Cell> potentialDestinations = new List <Cell>();

            foreach (var enemyUnit in enemyUnits)
            {
                potentialDestinations.AddRange(_cellGrid.Cells.FindAll(c => unit.IsCellMovableTo(c) && unit.IsUnitAttackable(enemyUnit, c)));
            }//Making a list of cells that the unit can attack from.

            var notInRange = potentialDestinations.FindAll(c => c.GetDistance(unit.Cell) > unit.MovementPoints);
            potentialDestinations = potentialDestinations.Except(notInRange).ToList();

            if (potentialDestinations.Count == 0 && notInRange.Count != 0)
            {
                potentialDestinations.Add(notInRange.ElementAt(_rnd.Next(0, notInRange.Count - 1)));
            }

            potentialDestinations = potentialDestinations.OrderBy(h => _rnd.Next()).ToList();
            List <Cell> shortestPath = null;
            foreach (var potentialDestination in potentialDestinations)
            {
                var path = unit.FindPath(_cellGrid.Cells, potentialDestination);
                if ((shortestPath == null && path.Sum(h => h.MovementCost) > 0) || shortestPath != null && (path.Sum(h => h.MovementCost) < shortestPath.Sum(h => h.MovementCost) && path.Sum(h => h.MovementCost) > 0))
                {
                    shortestPath = path;
                }

                var pathCost = path.Sum(h => h.MovementCost);
                if (pathCost > 0 && pathCost <= unit.MovementPoints)
                {
                    unit.Move(potentialDestination, path, _cellGrid.trapmanager);
                    while (unit.isMoving)
                    {
                        yield return(0);
                    }
                    shortestPath = null;
                    break;
                }
                yield return(0);
            }//If there is a path to any cell that the unit can attack from, move there.

            if (shortestPath != null)
            {
                foreach (var potentialDestination in shortestPath.Intersect(unit.GetAvailableDestinations(_cellGrid.Cells)).OrderByDescending(h => h.GetDistance(unit.Cell)))
                {
                    var path     = unit.FindPath(_cellGrid.Cells, potentialDestination);
                    var pathCost = path.Sum(h => h.MovementCost);
                    if (pathCost > 0 && pathCost <= unit.MovementPoints)
                    {
                        unit.Move(potentialDestination, path, _cellGrid.trapmanager);
                        while (unit.isMoving)
                        {
                            yield return(0);
                        }
                        break;
                    }
                    yield return(0);
                }
            }//If the path cost is greater than unit movement points, move as far as possible.

            foreach (var enemyUnit in enemyUnits)
            {
                var enemyCell = enemyUnit.Cell;
                if (unit.IsUnitAttackable(enemyUnit, unit.Cell))
                {
                    unit.DealDamage(enemyUnit);
                    yield return(new WaitForSeconds(0.5f));

                    break;
                }
            }//Look for enemies in range and attack.
        }
        _cellGrid.EndTurn();
    }
Esempio n. 15
0
    /*
     * private IEnumerator Play()
     * {
     *  var myUnits = _cellGrid.Units.FindAll(u => u.PlayerNumber.Equals(PlayerNumber)).ToList();
     *  foreach (var unit in myUnits.OrderByDescending(u => u.Cell.GetNeighbours(_cellGrid.Cells).FindAll(u.IsCellTraversable).Count))
     *  {
     *      var enemyUnits = _cellGrid.Units.Except(myUnits).ToList();
     *      var unitsInRange = new List<Unit>();
     *      foreach (var enemyUnit in enemyUnits)
     *      {
     *          if (unit.IsUnitAttackable(enemyUnit,unit.Cell))
     *          {
     *              unitsInRange.Add(enemyUnit);
     *          }
     *      }//Looking for enemies that are in attack range.
     *      if (unitsInRange.Count != 0)
     *      {
     *          var index = _rnd.Next(0, unitsInRange.Count);
     *          unit.DealDamage(unitsInRange[index], true, false);
     *          yield return new WaitForSeconds(0.5f);
     *          continue;
     *      }//If there is an enemy in range, attack it.
     *
     *      List<Cell> potentialDestinations = new List<Cell>();
     *
     *      foreach (var enemyUnit in enemyUnits)
     *      {
     *          potentialDestinations.AddRange(_cellGrid.Cells.FindAll(c=> unit.IsCellMovableTo(c) && unit.IsUnitAttackable(enemyUnit, c)));
     *      }//Making a list of cells that the unit can attack from.
     *
     *      var notInRange = potentialDestinations.FindAll(c => c.GetDistance(unit.Cell) > unit.MovementPoints);
     *      potentialDestinations = potentialDestinations.Except(notInRange).ToList();
     *
     *      if (potentialDestinations.Count == 0 && notInRange.Count !=0)
     *      {
     *          potentialDestinations.Add(notInRange.ElementAt(_rnd.Next(0,notInRange.Count-1)));
     *      }
     *
     *      potentialDestinations = potentialDestinations.OrderBy(h => _rnd.Next()).ToList();
     *      List<Cell> shortestPath = null;
     *      foreach (var potentialDestination in potentialDestinations)
     *      {
     *          var path = unit.FindPath(_cellGrid.Cells, potentialDestination);
     *          if ((shortestPath == null && path.Sum(h => h.MovementCost) > 0) || shortestPath != null && (path.Sum(h => h.MovementCost) < shortestPath.Sum(h => h.MovementCost) && path.Sum(h => h.MovementCost) > 0))
     *              shortestPath = path;
     *
     *          var pathCost = path.Sum(h => h.MovementCost);
     *          if (pathCost > 0 && pathCost <= unit.MovementPoints)
     *          {
     *              unit.Move(potentialDestination, path);
     *              while (unit.isMoving)
     *                  yield return 0;
     *              shortestPath = null;
     *              break;
     *          }
     *          yield return 0;
     *      }//If there is a path to any cell that the unit can attack from, move there.
     *
     *      if (shortestPath != null)
     *      {
     *          foreach (var potentialDestination in shortestPath.Intersect(unit.GetAvailableDestinations(_cellGrid.Cells)).OrderByDescending(h => h.GetDistance(unit.Cell)))
     *          {
     *              var path = unit.FindPath(_cellGrid.Cells, potentialDestination);
     *              var pathCost = path.Sum(h => h.MovementCost);
     *              if (pathCost > 0 && pathCost <= unit.MovementPoints)
     *              {
     *                  unit.Move(potentialDestination, path);
     *                  while (unit.isMoving)
     *                      yield return 0;
     *                  break;
     *              }
     *              yield return 0;
     *          }
     *      }//If the path cost is greater than unit movement points, move as far as possible.
     *
     *      foreach (var enemyUnit in enemyUnits)
     *      {
     *          var enemyCell = enemyUnit.Cell;
     *          if (unit.IsUnitAttackable(enemyUnit,unit.Cell))
     *          {
     *              unit.DealDamage(enemyUnit, true, false);
     *              yield return new WaitForSeconds(0.5f);
     *              break;
     *          }
     *      }//Look for enemies in range and attack.
     *  }
     *  _cellGrid.EndTurn();
     * }*/

    private IEnumerator SinglePlay(Unit unit) //runs a single turn of an AI Unit
    {
        var myUnits = _cellGrid.Units.FindAll(u => u.PlayerNumber.Equals(PlayerNumber)).ToList();

        var enemyUnits = _cellGrid.Units.Except(myUnits).ToList();

        var unitsInRange = new List <Unit>();

        foreach (var enemyUnit in enemyUnits)
        {
            if (unit.IsUnitAttackable(enemyUnit, unit.Cell))
            {
                unitsInRange.Add(enemyUnit);
            }
        }                                                     //Looking for enemies that are in attack range.
        if (unitsInRange.Count != 0 && unit.ActionPoints > 0) //If there is an enemy in range, attack it.
        {
            var index = _rnd.Next(0, unitsInRange.Count);
            if (unitsInRange[index].Armor > unit.AttackFactor / 2)
            {
                unit.DealDamage(unitsInRange[index], false, false);
                unit.ActionPoints -= 1;
            }
            else
            {
                unit.DealDamage(unitsInRange[index], true, false);
                unit.ActionPoints -= 1;
            }

            yield return(new WaitForSeconds(0.5f));
            //continue;
        }
        else //if no immediate enemy, look for who it can attack
        {
            List <Cell> potentialDestinations = new List <Cell>();

            foreach (var enemyUnit in enemyUnits)
            {
                potentialDestinations.AddRange(_cellGrid.Cells.FindAll(c => unit.IsCellMovableTo(c) && unit.IsUnitAttackable(enemyUnit, c)));
            }//Making a list of cells that the unit can attack from.

            var notInRange = potentialDestinations.FindAll(c => c.GetDistance(unit.Cell) > unit.MovementPoints);
            potentialDestinations = potentialDestinations.Except(notInRange).ToList();

            if (potentialDestinations.Count == 0 && notInRange.Count != 0)
            {
                potentialDestinations.Add(notInRange.ElementAt(_rnd.Next(0, notInRange.Count - 1)));
            }

            potentialDestinations = potentialDestinations.OrderBy(h => _rnd.Next()).ToList();
            List <Cell> shortestPath = null;
            foreach (var potentialDestination in potentialDestinations)
            {
                var path = unit.FindPath(_cellGrid.Cells, potentialDestination);
                if ((shortestPath == null && path.Sum(h => h.MovementCost) > 0) || shortestPath != null && (path.Sum(h => h.MovementCost) < shortestPath.Sum(h => h.MovementCost) && path.Sum(h => h.MovementCost) > 0))
                {
                    shortestPath = path;
                }

                var pathCost = path.Sum(h => h.MovementCost);
                if (pathCost > 0 && pathCost <= unit.MovementPoints)
                {
                    unit.Move(potentialDestination, path);
                    while (unit.isMoving)
                    {
                        yield return(0);
                    }
                    shortestPath = null;
                    break;
                }
                yield return(0);
            }//If there is a path to any cell that the unit can attack from, move there.

            if (shortestPath != null)
            {
                foreach (var potentialDestination in shortestPath.Intersect(unit.GetAvailableDestinations(_cellGrid.Cells)).OrderByDescending(h => h.GetDistance(unit.Cell)))
                {
                    var path     = unit.FindPath(_cellGrid.Cells, potentialDestination);
                    var pathCost = path.Sum(h => h.MovementCost);
                    if (pathCost > 0 && pathCost <= unit.MovementPoints)
                    {
                        unit.Move(potentialDestination, path);
                        while (unit.isMoving)
                        {
                            yield return(0);
                        }
                        break;
                    }
                    yield return(0);
                }
            }//If the path cost is greater than unit movement points, move as far as possible.

            foreach (var enemyUnit in enemyUnits)
            {
                var enemyCell = enemyUnit.Cell;
                if (unit.IsUnitAttackable(enemyUnit, unit.Cell) && unit.ActionPoints > 0)
                {
                    var index = _rnd.Next(0, unitsInRange.Count);
                    if (enemyUnit.Armor > unit.AttackFactor / 2)
                    {
                        unit.DealDamage(enemyUnit, false, false);
                        unit.ActionPoints -= 1;
                        yield return(new WaitForSeconds(0.5f));

                        break;
                    }
                    else
                    {
                        unit.DealDamage(enemyUnit, true, false);
                        unit.ActionPoints -= 1;
                        yield return(new WaitForSeconds(0.5f));

                        break;
                    }
                }
            }//Look for enemies in range and attack.
        }
        _cellGrid.EndTurn(); //event that triggers next unit
    }
Esempio n. 16
0
    void ReceiveData(ushort senderID, byte tag, ushort subject, object data)
    {
        bool ignoreMessage = false;

        if (ignoreMessage)
        {
            return;
        }

        if (tag == TagIndex.Controller)
        {
            if (subject == TagIndex.ControllerSubjects.JoinMessage)
            {
                networkConnected = true;
                DarkRiftAPI.SendMessageToID(senderID, TagIndex.Controller, TagIndex.ControllerSubjects.SpawnPlayer, null);
                SendData(TagIndex.Controller, TagIndex.PlayerUpdate, factions[DarkRiftAPI.id - 1]);
                if (senderID == 1)
                {
                    playerNumber = 2;
                }
                if (senderID > playerCount)
                {
                    playerCount = senderID;
                }
                //GameObject.Find("Name1").GetComponent<UnityEngine.UI.InputField>().readOnly = true;
                //GameObject.Find("Name2").GetComponent<UnityEngine.UI.InputField>().readOnly = false;

                //Handle Connect message
            }

            if (subject == TagIndex.ControllerSubjects.SpawnPlayer)
            {
                networkConnected = true;
                //Handle spawning the player into our game
            }

            if (subject == TagIndex.PlayerUpdate)
            {
                if (data is string && (data as string) == "ERROR")
                {
                    possibleDesyncOccurred = true;
                    RetrySendData();
                }
                if (data is string[])
                {
                    string[] name = data as string[];
                    if (name[0] == "name")
                    {
                        GameObject go = GameObject.Find("Name" + (senderID).ToString());
                        go.GetComponent <UnityEngine.UI.InputField>().text = name[1];
                    }
                    else if (name[0] == "StartGame")
                    {
                        DontDestroyOnLoad(transform.gameObject);
                        DontDestroyOnLoad(GameObject.Find("GameInitiator"));
                        //if (NetManager.playerCount >= 2)
                        //{
                        //    GameInit.mapName = GameInit.mapNames[NetManager.playerCount - 2];
                        //}
                        //string mapDropDownText = GameObject.Find("mapDropDown").GetComponent<Dropdown>().itemText.text;
                        //GameInit.mapName = mapDropDownText.Split(' ')[1];
                        GameInit.mapName = name[1];
                        SceneManager.LoadScene(GameInit.mapName);
                        //SceneManager.LoadScene("Main");
                    }
                    else if (name[0] == "charUpdate")
                    {
                        GameObject.Find(name[1]).GetComponent <UnityEngine.UI.Dropdown>().value = int.Parse(name[2]);
                    }
                }
                if (data is UnitUpdate /*&& messageIndex + 1 == (data as UnitUpdate).index*/)
                {
                    UnitUpdate update      = data as UnitUpdate;
                    GameObject unitsParent = GameObject.Find("Units Parent");
                    Unit[]     units       = unitsParent.GetComponentsInChildren <Unit>();
                    CellGrid   grid        = GameObject.Find("CellGrid").GetComponent <CellGrid>();
                    messageIndex = update.index;
                    if (IsEndTurnCommand(update))
                    {
                        grid.EndTurn();
                        //end turn
                    }
                    else if (IsMoveUnitCommand(update))
                    {
                        foreach (Unit u in units)
                        {
                            if (u.unitIndex == update.moving)
                            {
                                //CellGrid grid = GameObject.Find("CellGrid").GetComponent<CellGrid>();
                                foreach (Cell c in grid.Cells)
                                {
                                    if (c.transform.position.x == update.newLocationX &&
                                        c.transform.position.y == update.newLocationY)
                                    {
                                        u.AdjustCellMovementCosts();
                                        var path = u.FindPath(grid.Cells, c);
                                        u.ResetMovementPoints();
                                        u.Move(c, path);
                                        u.ResetMovementPoints();
                                        break;
                                    }
                                }
                                break;
                            }
                        }
                    }
                    else if (IsAttackCommand(update))
                    {
                        Unit attacker = null;
                        Unit defender = null;
                        foreach (Unit u in units)
                        {
                            if (u.unitIndex == update.moving)
                            {
                                attacker = u;
                                //CellGrid grid = GameObject.Find("CellGrid").GetComponent<CellGrid>();
                            }
                            if (u.unitIndex == update.target)
                            {
                                defender = u;
                            }
                            if (attacker != null && defender != null)
                            {
                                attacker.DealDamage(defender, true);
                                //attack

                                break;
                            }
                        }
                    }
                    else if (IsSpawnUnitCommand(update))
                    {
                        foreach (Unit c in grid.Units)
                        {
                            if (c.transform.position.x == update.newLocationX && c.transform.position.y == update.newLocationY)
                            {
                                if (c is BarracksUnit)
                                {
                                    (c as BarracksUnit).InstantiateUnit(update.type);
                                }
                                else if (c is BuildSite)
                                {
                                    if (update.type >= UnitType.INFANTRY && update.type <= UnitType.ANTIAIR)
                                    {
                                        //TODO 1
                                        //guiCamRef.GetComponent<NewBarracks>().SpawnAStarFromNetwork((int)update.type, (int)update.moving);
                                        guiCamRef.GetComponent <NewBarracks>().SpawnUnitFromNetwork((int)update.type, (int)update.moving, 1);
                                        //GameObject.Find("GUICamera").GetComponent<NewBarracks>().SpawnAStarFromNetwork((int)update.type, (int)update.index);
                                    }
                                    else if (update.type >= UnitType.ANDROID && update.type <= UnitType.TURRET)
                                    {
                                        guiCamRef.GetComponent <NewBarracks>().SpawniFactionFromNetwork((int)update.type, (int)update.moving);
                                    }
                                    else if (update.type >= UnitType.ADVERSARY && update.type <= UnitType.STASISGUN)
                                    {
                                        guiCamRef.GetComponent <NewBarracks>().SpawnusbFromNetwork((int)update.type, (int)update.moving);
                                    }
                                    //(c as BuildSite).InstantiateUnit(update.type);
                                }
                                break;
                            }
                        }
                        foreach (Unit c in grid.Units)
                        {
                            if (c.transform.position.x == update.newLocationX && c.transform.position.y == update.newLocationY)
                            {
                                if (c is Airport)
                                {
                                    (c as Airport).InstantiateUnit(update.type);
                                }
                                else if (c is BuildSite)
                                {
                                    if (update.type >= UnitType.FIGHTER && update.type <= UnitType.COPTER)
                                    {
                                        guiCamRef.GetComponent <NewAirport>().SpawnAStarFromNetwork((int)update.type, (int)update.moving);
                                    }
                                    else if (update.type >= UnitType.CAPCOPTER && update.type <= UnitType.BOMB)
                                    {
                                        guiCamRef.GetComponent <NewAirport>().SpawniFactionFromNetwork((int)update.type, (int)update.moving);
                                    }
                                    else if (update.type >= UnitType.ORBITALSTRIKER && update.type <= UnitType.NULLSTAR)
                                    {
                                        guiCamRef.GetComponent <NewAirport>().SpawniFactionFromNetwork((int)update.type, (int)update.moving);
                                    }
                                    //(c as BuildSite).InstantiateUnit(update.type);
                                }
                                break;
                            }
                        }
                        //CellGrid grid = GameObject.Find("CellGrid").GetComponent<CellGrid>()
                    }
                    else if (IsCaptureCommand(update))
                    {
                    }
                    else if (IsIncreaseMoneGenCommand(update))
                    {
                        foreach (Unit c in grid.Units)
                        {
                            if (c.unitIndex == update.moving)
                            {
                                if (c is City)
                                {
                                    (c as City).SelfIncreaseMoneyGenIfAble();
                                }
                                else if (c is BuildSite)
                                {
                                    (c as BuildSite).SelfIncreaseMoneyGenIfAble();
                                }
                            }
                        }
                    }
                    else if (IsUpgradeBuildSiteCommand(update))
                    {
                        BuildSite site = null;

                        foreach (Unit b in GameObject.Find("CellGrid").GetComponent <CellGrid>().Units)
                        {
                            if (b is BuildSite && b.unitIndex == update.moving)
                            {
                                site = b as BuildSite;
                                break;
                            }
                        }

                        if (site == null)
                        {
                            return;
                        }

                        //handle cost
                        Debug.Log("Site == null: " + (site == null).ToString());
                        Debug.Log("update == null: " + (update == null).ToString());
                        Debug.Log(NetManager.buildCosts[(int)update.buildingType - 1]);

                        if (!site.CanAffordToBuild(NetManager.buildCosts[(int)update.buildingType - 1]))
                        {
                            return;
                        }

                        site.grid.Players[site.PlayerNumber].Money -= NetManager.buildCosts[(int)update.buildingType - 1];
                        PlayerController.currTurnMoney              = site.grid.Players[site.PlayerNumber].Money;
                        GameObject.Find("Canvas").GetComponentsInChildren <Text>()[site.PlayerNumber].text = PlayerController.currTurnMoney.ToString() + "G" + "(+" + site.grid.Players[site.PlayerNumber].income.ToString() + "G)";

                        site.buildingType = update.buildingType;
                        site.GetComponent <ChangeMaterial>().SetNewMaterial(update.buildingType);
                    }

                    else if (data is UnitUpdate && messageIndex + 1 != (data as UnitUpdate).index)
                    {
                        possibleDesyncOccurred = true;
                        SendData(TagIndex.Controller, TagIndex.PlayerUpdate, "ERROR");
                        //send error message
                    }
                }
                else if (data is Unit.Faction)
                {
                    factions[senderID - 1] = (Unit.Faction)data;
                }
            }
        }
    }
Esempio n. 17
0
    private IEnumerator Play()
    {
        var myUnits = _cellGrid.Units.FindAll(u => u.PlayerNumber.Equals(PlayerNumber)).ToList();

        foreach (var unit in myUnits.OrderByDescending(u => u.Cell.GetNeighbours(_cellGrid.Cells).FindAll(u.IsCellTraversable).Count))
        {
            var enemyUnits   = _cellGrid.Units.Except(myUnits).ToList();
            var unitCell     = unit.Cell;
            var unitsInRange = new List <Unit>();
            foreach (var enemyUnit in enemyUnits)
            {
                var enemyCell = enemyUnit.Cell;
                if (enemyCell.GetDistance(unitCell) <= unit.AttackRange)
                {
                    unitsInRange.Add(enemyUnit);
                }
            }//Looking for enemies that are in attack range.
            if (unitsInRange.Count != 0)
            {
                Debug.Log(unit.gameObject.name + " has " + unit.ActionPoints + " points");

                //if(unit.ActionPoints >
                var index = _rnd.Next(0, unitsInRange.Count);
                unit.DealDamage(unitsInRange[index]);

                yield return(new WaitForSeconds(0.5f));

                continue;
            }//If there is an enemy in range, attack it.

            List <Cell> potentialDestinations = new List <Cell>();

            foreach (var enemyUnit in enemyUnits)
            {
                potentialDestinations.AddRange(enemyUnit.Cell.GetNeighbours(_cellGrid.Cells).FindAll(unit.IsCellMovableTo));
            }//Making a list of cells adjacent to enemies.
            potentialDestinations = potentialDestinations.OrderBy(h => _rnd.Next()).ToList();

            List <Cell> shortestPath = null;
            foreach (var potentialDestination in potentialDestinations)
            {
                var path = unit.FindPath(_cellGrid.Cells, potentialDestination);
                if ((shortestPath == null && path.Sum(h => h.MovementCost) > 0) || shortestPath != null && (path.Sum(h => h.MovementCost) < shortestPath.Sum(h => h.MovementCost) && path.Sum(h => h.MovementCost) > 0))
                {
                    shortestPath = path;
                }

                var pathCost = path.Sum(h => h.MovementCost);
                if (pathCost > 0 && pathCost <= unit.MovementPoints)
                {
                    unit.Move(potentialDestination, path);
                    while (unit.isMoving)
                    {
                        yield return(0);
                    }
                    shortestPath = null;
                    unit.changeAnimation("idle");
                    break;
                }
                yield return(0);
            }//If there is a path to any of the cells adjacent to enemy, move there.

            if (shortestPath != null)
            {
                foreach (var potentialDestination in shortestPath.Intersect(unit.GetAvailableDestinations(_cellGrid.Cells)).OrderByDescending(h => h.GetDistance(unit.Cell)))
                {
                    var path     = unit.FindPath(_cellGrid.Cells, potentialDestination);
                    var pathCost = path.Sum(h => h.MovementCost);
                    if (pathCost > 0 && pathCost <= unit.MovementPoints)
                    {
                        unit.Move(potentialDestination, path);
                        while (unit.isMoving)
                        {
                            yield return(0);
                        }
                        unit.changeAnimation("idle");
                        break;
                    }
                    yield return(0);
                }
            }//If the path cost is greater than unit movement points, move as far as possible.

            unitCell = unit.Cell;
            foreach (var enemyUnit in enemyUnits)
            {
                var enemyCell = enemyUnit.Cell;
                if (enemyCell.GetDistance(unitCell) <= unit.AttackRange)
                {
                    unit.DealDamage(enemyUnit);
                    yield return(new WaitForSeconds(2f));

                    break;
                }
            }//Look for enemies in range and attac
        }
        _cellGrid.EndTurn();
    }