private IEnumerator _StartTraining()
    {
        ItemsCollection.ItemData itemData = Items.GetItem(_currentTrainingTroop);
        float buildTime = itemData.configuration.buildTime;

        trainingStartTime = Time.realtimeSinceStartup;
        yield return(new WaitForSeconds(buildTime));

        Vector3        randomFrontCell = _baseItem.GetRandomFrontCellPosition();
        BaseItemScript newUnit         = SceneManager.instance.AddItem(_currentTrainingTroop, -1, (int)randomFrontCell.x, (int)randomFrontCell.y, true, true);
        BaseItemScript nearestArmyCamp = SceneManager.instance.GetNearestArmyCamp(newUnit.GetPosition());

        if (nearestArmyCamp != null)
        {
            newUnit.WalkRandom(nearestArmyCamp);
        }

        _currentTrainingTroop = -1;
        trainingQueue.RemoveAt(0);

        if (trainingQueue.Count > 0)
        {
            _Train(trainingQueue[0]);
        }

        if (TrainTroopsWindowScript.instance != null)
        {
            TrainTroopsWindowScript.instance.RenderTrainingQueue();
        }
    }
    public void UpdateBaseItemNodes(BaseItemScript item, Action action)
    {
        if (item != null)
        {
            Vector3 pos   = item.GetPosition();
            int     x     = (int)(pos.x);
            int     z     = (int)(pos.z);
            int     sizeX = (int)item.GetSize().x;
            int     sizeZ = (int)item.GetSize().z;

            for (int indexX = x; indexX < x + sizeX; indexX++)
            {
                for (int indexZ = z; indexZ < z + sizeZ; indexZ++)
                {
                    bool isCellWalkable = false;
                    if ((sizeX > 2 && indexX == x) || (sizeX > 2 && indexX == x + sizeX - 1) ||
                        (sizeZ > 2 && indexZ == z) || (sizeZ > 2 && indexZ == z + sizeZ - 1))
                    {
                        //use this for make outer edge walkable for items have size morethan 2x2
                        isCellWalkable = true;
                    }

                    if (item.itemData.name == "ArmyCamp")
                    {
                        //make every cell walkable in army camp
                        isCellWalkable = true;
                    }

                    if (action == Action.ADD)
                    {
                        //adding scene item to nodes, so walkable is false
                        this.instanceNodes[indexX, indexZ] = item.instanceId;

                        if (item.itemData.name == "Wall")
                        {
                            this.pathNodesWithoutWall[indexX, indexZ] = true;
                            this.pathNodesWithWall[indexX, indexZ]    = false;
                        }
                        else
                        {
                            this.pathNodesWithoutWall[indexX, indexZ] = isCellWalkable;
                        }
                    }
                    else if (action == Action.REMOVE)
                    {
                        if (this.instanceNodes[indexX, indexZ] == item.instanceId)
                        {
                            this.instanceNodes[indexX, indexZ]        = -1;
                            this.pathNodesWithoutWall[indexX, indexZ] = true;
                            this.pathNodesWithWall[indexX, indexZ]    = true;
                        }
                    }
                }
            }
        }
    }
    public void Defend()
    {
        BaseItemScript target = _GetNearestTargetItem();

        if (target != null)
        {
            this._currentTarget      = target;
            this._currentTargetPoint = target.GetPosition();

            this.DefendLoop();
        }
        else
        {
            this.SearchForAttacker();
        }
    }