Esempio n. 1
0
        public Unit CreateUnit(string name, Vector3 position, OwnerType owner = OwnerType.NONE)
        {
            UnitData unitData;

            if (!_unitsData.TryGetValue(name, out unitData))
            {
                throw new Exception("Requested unit of type '" + name + "' not available." +
                                    "Create a corresponding UnitData first.");
            }
            if (unitData.prefab.GetComponent <NavMeshAgent>())
            {
                NavMeshHit hit;
                if (NavMesh.SamplePosition(position, out hit, 1, NavMesh.AllAreas))
                {
                    position = hit.position;
                }
                else
                {
                    Debug.LogWarning("Couldn't sample a position for new unit " + name + " on the navmesh!");
                }
            }

            Unit unit = Object.Instantiate(unitData.prefab, position, Quaternion.identity);

            _createdUnits.Add(unit.GetInstanceID(), unit);
            _visibleUnits.AddLast(unit);
            unit.Initialize(unitData, owner);
            UnitCreated?.Invoke(unit);
            return(unit);
        }
Esempio n. 2
0
    private void OnUnitCreated(object sender, UnitCreateEventArgs e)
    {
        if (e.Cell.IsTaken)
        {
            Debug.Log("Fail to create a unit in a taken cell");
            return;
        }
        if (CurrentPlayer.Money < gameObject.GetComponent <OnGameUnitGenerator> ().GetUnitCost(e.UnitType))
        {
            Debug.Log("Money not enough!");
            return;
        }
        Transform unitTransform;

        unitTransform = gameObject.GetComponent <OnGameUnitGenerator>().SpawnUnit(e.Cell, e.UnitType, CurrentPlayerNumber);
        Unit unit = unitTransform.gameObject.GetComponent <Unit>();

        if (unit != null)
        {
            Units.Add(unit);
            unit.Cell         = e.Cell;
            unit.Cell.IsTaken = true;
            unit.Initialize();
            unit.OnTurnStart();

            unit.UnitClicked   += OnUnitClicked;
            unit.UnitDestroyed += OnUnitDestroyed;

            CurrentPlayer.Money -= unit.UnitCost;

            if (UnitCreated != null)
            {
                UnitCreated.Invoke(this, new EventArgs());
            }

            Debug.Log("Unit Number: " + Units.Count);
        }
        else
        {
            Debug.LogError("No Unit script in unit");
        }
    }
Esempio n. 3
0
 public void AddUnit(Unit unit)
 {
     _createdUnits.Add(unit.GetInstanceID(), unit);
     _visibleUnits.AddLast(unit);
     UnitCreated?.Invoke(unit);
 }