Exemplo n.º 1
0
    public void CreateUnit(int index)
    {
        if (building.maxUnits > housedAmount)
        {
            Unit selectedUnit = building.unitsToProduce[index];
            // This line is usless -->>   line

            if (myPlayerResources.HasResources(selectedUnit.cost))
            {
                myPlayerResources.RemoveResources(selectedUnit.cost);
                uIController.OnResourcesChanged();
                Debug.Log(myPlayerResources.wood);
                GameObject unit      = new GameObject(selectedUnit.name);
                HealthUI   healthtxt = unit.AddComponent <HealthUI>();
                healthtxt.CreateText(building.healthUI);
                UnitController controller;
                if (unit.name == "Villager")
                {
                    controller = unit.AddComponent <ViligerController>();
                }
                else
                {
                    controller = unit.AddComponent <UnitController>();
                }
                controller.SetUnit(selectedUnit, this);
                controller.myTeam            = myTeam;
                currentTile.tile.currentUnit = selectedUnit.type;
                Material mat = GetComponent <MeshRenderer>().material;
                unit.AddComponent <MeshRenderer>().sharedMaterial = mat;
                MeshFilter filter = unit.AddComponent <MeshFilter>();
                filter.sharedMesh = selectedUnit.mesh;


                unit.transform.parent      = currentTile.transform;
                unit.transform.position    = currentTile.transform.position + new Vector3(0, 1, 0);
                unit.transform.localScale /= 2;
                housedAmount++;
                controller.SetStats(new CharacterStats(selectedUnit.damage, selectedUnit.defence, selectedUnit.health, controller));
                playersManager.AddUnit(controller);

                DisFocus();
            }
        }
    }