Aimbot-ParallelEnv/Assets/Script/InGame/EnemyContainer.cs
Koha9 cfccd12820 V3.1.1 优化代码
优化可读性与规范化命名方式
2023-06-30 18:30:12 +09:00

64 lines
2.2 KiB
C#

using UnityEngine;
public class EnemyContainer : MonoBehaviour
{
public GameObject enemyPrefab;
public GameObject environmentObj;
public GameObject targetControllerObj;
private TargetController targetCon;
private void Start()
{
targetCon = targetControllerObj.GetComponent<TargetController>();
}
// initialize enemy by random
public void RandomInitEnemys(int EnemyNum)
{
for (int i = 0; i < EnemyNum; i++)
{
float randX = UnityEngine.Random.Range(targetCon.minEnemyAreaX, targetCon.maxEnemyAreaX);
float randZ = UnityEngine.Random.Range(targetCon.minEnemyAreaZ, targetCon.maxEnemyAreaZ);
int enemyY = 1;
InitEnemyAtHere(new Vector3(randX, enemyY, randZ));
}
}
// initialize enemy by random but not in block area
public void RandomInitEnemysExcept(int enemyNum, Vector3 blockPosition, float sceneSize)
{
float randX = 0f;
float randZ = 0f;
for (int i = 0; i < enemyNum; i++)
{
randX = UnityEngine.Random.Range(targetCon.minEnemyAreaX, targetCon.maxEnemyAreaX);
randZ = UnityEngine.Random.Range(targetCon.minEnemyAreaZ, targetCon.maxEnemyAreaZ);
while (Vector3.Distance(blockPosition, new Vector3(randX, 0f, randZ)) < sceneSize / 2)
{
// while in scene area then respawn
Debug.Log("spawn enemy in area, re:roll");
randX = UnityEngine.Random.Range(targetCon.minEnemyAreaX, targetCon.maxEnemyAreaX);
randZ = UnityEngine.Random.Range(targetCon.minEnemyAreaZ, targetCon.maxEnemyAreaZ);
}
int enemyY = 1;
InitEnemyAtHere(new Vector3(randX, enemyY, randZ));
}
}
// initialize enemy to thisPosition
public void InitEnemyAtHere(Vector3 thisPosition)
{
Instantiate(enemyPrefab, thisPosition + environmentObj.transform.position, Quaternion.identity, this.transform);
}
// destroyEnemy delete enemyContainer's all enemy
public void DestroyAllEnemys()
{
foreach (Transform childObj in this.transform)
{
childObj.GetComponent<States>().DestroyMe();
}
}
}