public void Complete()
{
if (factionEntity.FactionID == GameManager.PlayerFactionID && completeAudio != null) //if this is the local player faction ID and there's task completed audio
gameMgr.AudioMgr.PlaySFX(completeAudio.Fetch(), false); //Play the audio clip
completeEvent.Invoke(); //invoke the complete unity event.
switch (type) //type of the task that has been completed.
{
case TaskTypes.createUnit:
//Randomly pick a prefab to produce from the list
Unit unitPrefab = unitCreationAttributes.prefabs[Random.Range(0, unitCreationAttributes.prefabs.Count)];
Vector3 spawnPosition = factionEntity.transform.position;
Building createdBy = null;
//get the unit spawn position:
if (factionEntity.Type == EntityTypes.building) //if this is a building, then see if it has a dedicated spawn position
{
createdBy = (Building)factionEntity;
spawnPosition = createdBy.GetSpawnPosition(unitPrefab.GetComponent<NavMeshAgent>().areaMask);
}
gameMgr.UnitMgr.CreateUnit(unitPrefab, spawnPosition, unitPrefab.transform.rotation, spawnPosition, factionEntity.FactionID, createdBy, false, false); //finally create the unit
break;
public Unit CreateUnitLocal (Unit unitPrefab, Vector3 spawnPosition, Quaternion spawnRotation, Vector3 gotoPosition, int factionID, Building createdBy, bool free = false, bool updatePopulation = true)
{
//only if the prefab is valid:
if (unitPrefab == null)
return null;
// create the new unit:
unitPrefab.gameObject.GetComponent<NavMeshAgent>().enabled = false; //disable this component before spawning the unit as it might place the unit in an unwanted position when spawned
Unit newUnit = Instantiate(unitPrefab.gameObject, spawnPosition, spawnRotation).GetComponent<Unit>();
newUnit.Init(gameMgr, factionID, free, createdBy, gotoPosition);
if(!free) //if this is not a free unit
{
if (updatePopulation) //update faction population
{
gameMgr.GetFaction(factionID).UpdateCurrentPopulation(newUnit.GetPopulationSlots()); //update population slots
newUnit.FactionMgr.UpdateLimitsList(newUnit.GetCode(), newUnit.GetCategory(), true);
}
//TO BE CHANGED
/*else //do not update faction population
newUnit.SetPopulationSlots(0); //reset population slots so it won't be removed when the unit is destroyed.*/
}
return newUnit;
}