| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using cfg;
- using System.Threading.Tasks;
- using Base.Common;
- using System.Linq;
- public class EnemyCreater : MonoBehaviour
- {
- public static EnemyCreater instance;
- public List<SingleCreateEnemyConfig> cfgCreateEnemy;
- public List<SingleCreateBuildingConfig> cfgCreateBuilding;
- public List<bool> createdEnemy;
- public List<bool> createdBuilding;
- public Dictionary<int, List<Enemy>> enemyDic;
- public Dictionary<int, GameObject> buildingDic;
- [Header("传送门")]
- public float portalsCreateTime = 10; //传送门提前出现时间
- public Transform portalUIParent; //传送门出现提示UI的父物体
- private void Awake()
- {
- if (!instance)
- {
- instance = this;
- }
- else
- {
- DestroyImmediate(gameObject);
- return;
- }
- enemyDic = new Dictionary<int, List<Enemy>>();
- buildingDic = new Dictionary<int, GameObject>();
- }
- private void Start()
- {
- cfgCreateEnemy = GameManager.instance.allCfgData.CfgCreateEnemy.DataList;
- cfgCreateBuilding = GameManager.instance.allCfgData.CfgCreateBuilding.DataList;
- createdEnemy = new List<bool>();
- createdBuilding = new List<bool>();
- for (int i = 0; i < cfgCreateEnemy.Count; i++)
- {
- createdEnemy.Add(false);
- }
- for (int i = 0; i < cfgCreateBuilding.Count; i++)
- {
- createdBuilding.Add(false);
- }
- }
- public void OnGameTimeChange(float gameTime)
- {
- for (int i = 0; i < cfgCreateEnemy.Count; i++)
- {
- if(cfgCreateEnemy[i].BuildingID == 0)
- {
- if (cfgCreateEnemy[i].Time <= gameTime && !createdEnemy[i])
- {
- createdEnemy[i] = true;
- StartCreateEnemy(i);
- }
- }
- else
- {
- GameObject result = null;
- buildingDic.TryGetValue(cfgCreateEnemy[i].BuildingID, out result);
- if (result != null)
- {
- EnemyTower enemyTower = result.GetComponent<EnemyTower>();
- if (enemyTower.hp * 100 <= enemyTower.totalHp * cfgCreateEnemy[i].BuildingHP && !createdEnemy[i])
- {
- createdEnemy[i] = true;
- StartCreateEnemy(i);
- }
- }
- }
- }
- for (int i = 0; i < cfgCreateBuilding.Count; i++)
- {
- if (!createdBuilding[i]
- &&(cfgCreateBuilding[i].Type == 0 && cfgCreateBuilding[i].Time - portalsCreateTime <= gameTime
- ||cfgCreateBuilding[i].Type == 1 && cfgCreateBuilding[i].Time <= gameTime))
- {
- createdBuilding[i] = true;
- if(gameTime< portalsCreateTime)
- {
- StartCreateBuilding(i, cfgCreateBuilding[i].Time);
- }
- else
- {
- StartCreateBuilding(i, portalsCreateTime);
- }
-
- }
- }
- }
- public void StartCreateBuilding(int id,float createTime)
- {
- SingleCreateBuildingConfig singleCreateBuilding = cfgCreateBuilding[id];
- if (!instance)
- {
- return;
- }
- switch (singleCreateBuilding.Type)
- {
- case 0:
- List<float> pos = singleCreateBuilding.Position.Concat(singleCreateBuilding.Position1).ToList();
- CreatePortal(singleCreateBuilding.ID, pos, singleCreateBuilding.Scale, singleCreateBuilding.HPRatio, createTime);
- break;
- case 1:
- CreateEnemyTower(singleCreateBuilding.ID, singleCreateBuilding.HPRatio, singleCreateBuilding.AttackRatio);
- break;
- }
- }
- public async void StartCreateEnemy(int id)
- {
- SingleCreateEnemyConfig singleCreateEnemy = cfgCreateEnemy[id];
- for (int i = 0; i < singleCreateEnemy.Count; i++)
- {
- if (!instance)
- {
- return;
- }
- Vector3 pos = Vector3.zero;
- switch (singleCreateEnemy.Type)
- {
- case 0:
- pos = new Vector3(
- singleCreateEnemy.Position[0],
- singleCreateEnemy.Position[1]
- + Random.Range(-singleCreateEnemy.YRandomRange / 2, singleCreateEnemy.YRandomRange / 2),
- singleCreateEnemy.Position[2]
- + Random.Range(-singleCreateEnemy.ZRandomRange / 2, singleCreateEnemy.ZRandomRange / 2));
- break;
- case 1:
- pos = buildingDic[singleCreateEnemy.BuildingID].transform.position;
- pos.y += Random.Range(0, singleCreateEnemy.YRandomRange);
- pos.z += Random.Range(0, singleCreateEnemy.ZRandomRange);
- break;
- }
- CreateEnemy(singleCreateEnemy.EnemyID, pos, singleCreateEnemy.HPRatio, singleCreateEnemy.AttackRatio);
- await Task.Delay((int)(singleCreateEnemy.TimeInterval * 1000));
- }
- }
- public void CreateEnemyTower(int buildingID, float hpRatio, float attackRatio)
- {
- SingleBuildingConfig cfgBuilding = GameManager.instance.allCfgData.CfgBuilding.Get(buildingID);
- GameObject towerObj = Util.Instantiate(cfgBuilding.BuildingPrefab);
- EnemyTower enemyTower = towerObj.GetComponent<EnemyTower>();
- buildingDic.Add(buildingID, towerObj);
- enemyTower.totalHp = (int)(cfgBuilding.HP * hpRatio);
- enemyTower.Init();
- AttackInfo attackInfo = enemyTower.attack1Infos[0];
- attackInfo.damage = (int)(cfgBuilding.Attack * attackRatio);
- enemyTower.attack1Infos[0] = attackInfo;
- }
- public void CreatePortal(int buildingID, List<float> pos, List<float> scale, float hpRatio, float createTime)
- {
- SingleBuildingConfig cfgBuilding = GameManager.instance.allCfgData.CfgBuilding.Get(buildingID);
- GameObject portalObj = Util.Instantiate(cfgBuilding.BuildingPrefab);
- PortalsCreater portalsCreater = portalObj.GetComponent<PortalsCreater>();
- portalsCreater.id = buildingID;
- buildingDic.Add(buildingID, portalObj);
- portalsCreater.totalHp = (int)(cfgBuilding.HP * hpRatio);
- portalsCreater.enemyCreater = this;
- portalsCreater.portalUIParent = portalUIParent;
- portalsCreater.Init(pos,scale,createTime);
- portalsCreater.enemyNumber -= 1;
- portalsCreater.enemyNumberText.text = portalsCreater.enemyNumber.ToString();
- if (portalsCreater.enemyNumber == 0)
- {
- portalsCreater.enemyNumberText.transform.parent.gameObject.SetActive(false);
- if (portalsCreater.onlyEnemy)
- {
- portalsCreater.coreCharacter.CoreBreak();
- }
- }
- }
- public void CreateEnemy(int enemyId, Vector3 pos, float hpRatio, float attackRatio)
- {
- SingleEnemyConfig cfgEnemy = GameManager.instance.allCfgData.CfgEnemy.Get(enemyId);
- GameObject enemyObj = Util.Instantiate(cfgEnemy.EnemyPrefab, pos);
- if (enemyId > 1000)
- {
- Boss boss = enemyObj.GetComponentInChildren<Boss>();
- }
- else
- {
- Enemy enemy = enemyObj.GetComponent<Enemy>();
- enemy.id = enemyId;
- if (!enemyDic.ContainsKey(enemyId))
- {
- enemyDic.Add(enemyId, new List<Enemy>());
- }
- enemyDic[enemyId].Add(enemy);
- enemy.totalHp = (int)(cfgEnemy.HP * hpRatio);
- enemy.moveSpeed = cfgEnemy.MoveSpeed;
- for (int i = 0; i < cfgEnemy.Attack1.Count; i++)
- {
- AttackInfo attackInfo = enemy.attack1Infos[i];
- attackInfo.damage = (int)(cfgEnemy.Attack1[i] * attackRatio);
- enemy.attack1Infos[i] = attackInfo;
- }
- for (int i = 0; i < cfgEnemy.Attack1.Count; i++)
- {
- AttackInfo attackInfo = enemy.attack2Infos[i];
- attackInfo.damage = (int)(cfgEnemy.Attack2[i] * attackRatio);
- enemy.attack2Infos[i] = attackInfo;
- }
- if (enemy.canFly)
- {
- enemy.flyHeight = enemy.transform.position.y;
- }
- enemy.Init();
- GameObject prefab = Resources.Load<GameObject>(cfgEnemy.EnemyPrefab);
- int sortingOrder = prefab.GetComponent<Enemy>().sortingOrder;
- enemy.SetSortingOrder(sortingOrder + enemyDic[enemyId].Count);
- }
- }
- public void OnEnemyRecycle(Enemy enemy)
- {
- if (!enemyDic.ContainsKey(enemy.id))
- {
- return;
- }
- enemyDic[enemy.id].Remove(enemy);
- SingleEnemyConfig cfgEnemy = GameManager.instance.allCfgData.CfgEnemy.Get(enemy.id);
- for (int i = 0; i < enemyDic[enemy.id].Count; i++)
- {
- enemyDic[enemy.id][i].SetSortingOrder(enemy.id * 1000 + i);
- }
- }
- public Enemy GetMinDisOtherEnemy(Enemy self)
- {
- Enemy minDisEnemy = null;
- foreach (var item in enemyDic)
- {
- for (int i = 0; i < item.Value.Count; i++)
- {
- if (item.Value[i] != self && !item.Value[i].isDie && item.Value[i].gameObject.activeInHierarchy)
- {
- if (!minDisEnemy || (minDisEnemy.transform.position - self.transform.position).magnitude
- > (item.Value[i].transform.position - self.transform.position).magnitude)
- {
- minDisEnemy = item.Value[i];
- }
- }
- }
- }
- return minDisEnemy;
- }
- }
|