rts engine中生成小兵逻辑

        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;
        }

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值