| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using cfg;
- using System.Threading.Tasks;
- using Base.Common;
- using System.Linq;
- using System;
- [Serializable]
- public struct CreaterControl
- {
- public bool isCreated;
- public GameMapTile tile;
- public GameMapLayer mapLayer;
- public SpawnTime spawnTime;
- public int waveStartTime;
- public List<Vector3> pos;
- }
- public class EnemyCreater : MonoBehaviour
- {
- public static EnemyCreater instance;
- public List<CreaterControl> createCharacter = new();
- public Dictionary<int, List<Enemy>> enemyDic;
- public Dictionary<int, List<GameObject>> buildingDic;
- private LevelSelect.Level curLevel;
- public GameMapsAsset mapsAsset;
- //import
- public LevelSelect levelSelect;
- private void Awake()
- {
- if (!instance)
- {
- instance = this;
- }
- else
- {
- DestroyImmediate(gameObject);
- return;
- }
- enemyDic = new Dictionary<int, List<Enemy>>();
- buildingDic = new Dictionary<int, List<GameObject>>();
- }
- private void Start()
- {
- levelSelect = GetComponent<LevelSelect>();
- curLevel = GetComponent<LevelSelect>().levelConfig[levelSelect.curLevelID - 1];
- LoadMapAsset(0);
- //把所有怪物prefab生成一遍防止后面初次生成卡顿
- //foreach (var item in createCharacter)
- //{
- // for(int i = 0; i < item.tile.spawnTime[item.spawnTimeId].num; i++)
- // {
- // CreateEnemy(item.tile, Vector3.zero, 1, 1,false);
- // }
- //}
- }
- public void LoadMapAsset(int mapIdx)
- {
- GameMap map = mapsAsset.maps[mapIdx];
- int startTime = 0;
- for (int i = 0; i < map.layers.Count(); i++)
- {
- GameMapLayer layer = map.layers[i];
- List<SpawnTime> spawnTimes = layer.spawnTimes;
- for(int j = 0; j < spawnTimes.Count; j++)
- {
- SpawnTime spawnTime = layer.spawnTimes[j];
- CreaterControl createrControl = new();
- createrControl.isCreated = false;
- GameMapTile t = mapsAsset.tileAsset.GetTile(spawnTime.id);
- createrControl.tile = t;
- createrControl.spawnTime = spawnTime;
- createrControl.pos = new List<Vector3>();
- for (int k = 0; k < spawnTime.pos.Count(); k++)
- {
- int posInt = spawnTime.pos[k];
- float posX = posInt % map.width;
- posX = posX + UnityEngine.Random.Range(-t.radius.x / 2, t.radius.x / 2);
- float posY = posInt / map.width;
- posY = posY + UnityEngine.Random.Range(-t.radius.y / 2, t.radius.y / 2);
- posY = posY < 0 ? 0 : posY;
- createrControl.pos.Add(new Vector3(posX, posY, 0));
- }
- createrControl.waveStartTime = startTime;
- createrControl.mapLayer = layer;
- createCharacter.Add(createrControl);
- }
- startTime += map.layers[i].duration;
- }
- }
- public void OnGameTimeChange(float gameTime)
- {
- for (int i = 0; i < createCharacter.Count; i++)
- {
- if (!createCharacter[i].isCreated && createCharacter[i].spawnTime.startTime + createCharacter[i].waveStartTime <= gameTime)
- {
- CreaterControl createrControl = createCharacter[i];
- createrControl.isCreated = true;
- createCharacter[i] = createrControl;
- Debug.Log(createCharacter[i].spawnTime.id);
- StartCreateEnemy(createCharacter[i]);
- }
- }
- }
- public async void StartCreateEnemy(CreaterControl createrControl)
- {
- SpawnTime spawnTime = createrControl.spawnTime;
- for (int i = 0; i < spawnTime.num; i++)
- {
- if (!instance)
- {
- return;
- }
- GameMapLayer gameMapLayer = createrControl.mapLayer;
- for(int j= 0; j < createrControl.pos.Count; j++)
- {
- CreateEnemy(createrControl.tile, createrControl.pos[j], gameMapLayer.Hp, gameMapLayer.moveSpeed, gameMapLayer.attack);
- }
-
- if(spawnTime.num == 1)
- {
- return;
- }
- float TimeInterval = (float)(spawnTime.endTime - spawnTime.startTime) / (spawnTime.num - 1);
- await Task.Delay((int)(TimeInterval * 1000));
- }
- }
- public GameObject CreateEnemy(GameMapTile gameMapTile, Vector3 pos, float hpRatio, float moveSpeedRatio, float attackRatio, bool active = true)
- {
- string enemyStr = $"Prefab/{gameMapTile.type}/{gameMapTile.ch}";
- GameObject enemyObj = Util.Instantiate(enemyStr, pos, active: active);
- Parameter parameter = gameMapTile.parameter;
- AttackInfo attackInfo;
- switch (gameMapTile.type)
- {
- case GameMapTile.Type.Tower:
- EnemyTower enemyTower = enemyObj.GetComponent<EnemyTower>();
- Tower tower = enemyObj.GetComponent<Tower>();
- if (enemyTower != null)
- {
- enemyTower.id = gameMapTile.id;
- enemyTower.totalHp = (int)(parameter.HP * hpRatio);
- enemyTower.hp = enemyTower.totalHp;
- attackInfo = enemyTower.Attack_summonInfos[0];
- attackInfo.damage = (int)(parameter.Attack_summon * attackRatio);
- enemyTower.Attack_summonInfos[0] = attackInfo;
- enemyTower.Init();
- }
- if (tower != null)
- {
- tower.id = gameMapTile.id;
- tower.totalHp = (int)(parameter.HP * hpRatio);
- tower.hp = tower.totalHp;
- attackInfo = tower.Attack_summonInfos[0];
- attackInfo.damage = (int)(parameter.Attack_summon * attackRatio);
- tower.Attack_summonInfos[0] = attackInfo;
- tower.Init();
- }
- if (!buildingDic.ContainsKey(gameMapTile.id))
- {
- buildingDic.Add(gameMapTile.id, new List<GameObject>());
- }
- buildingDic[gameMapTile.id].Add(enemyObj);
- break;
- case GameMapTile.Type.Enemy:
- Enemy enemy = enemyObj.GetComponent<Enemy>();
- enemy.id = gameMapTile.id;
- if (!enemyDic.ContainsKey(gameMapTile.id))
- {
- enemyDic.Add(gameMapTile.id, new List<Enemy>());
- }
- enemyDic[gameMapTile.id].Add(enemy);
- enemy.totalHp = (int)(parameter.HP * hpRatio);
- enemy.hp = enemy.totalHp;
- enemy.minMoveSpeed = parameter.MinMoveSpeed * moveSpeedRatio;
- enemy.maxMoveSpeed = parameter.MaxMoveSpeed * moveSpeedRatio;
- attackInfo = enemy.Attack_summonInfos[0];
- attackInfo.damage = (int)(parameter.Attack_summon * attackRatio);
- enemy.Attack_summonInfos[0] = attackInfo;
- attackInfo = enemy.Attack_marchInfos[0];
- attackInfo.damage = (int)(parameter.Attack_march * attackRatio);
- enemy.Attack_marchInfos[0] = attackInfo;
- if (enemy.canFly)
- {
- enemy.flyHeight = enemy.transform.position.y;
- }
- enemy.Init();
- enemy.SetSortingOrder(enemy.baseSortingOrder + enemyDic[gameMapTile.id].Count);
- break;
- }
- enemyObj.transform.position = pos;
- return enemyObj;
- }
- public void OnEnemyRecycle(Enemy enemy)
- {
- if (!enemyDic.ContainsKey(enemy.id))
- {
- return;
- }
- enemyDic[enemy.id].Remove(enemy);
- for (int i = 0; i < enemyDic[enemy.id].Count; i++)
- {
- enemyDic[enemy.id][i].SetSortingOrder(enemy.baseSortingOrder + 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;
- }
- }
|