private float GENERATE_TIME_INTERVAL = 2;
public List<GameObject> enemies;
public List<GameObject> generators;
public GameObject player;
private float curTime;
// Use this for initialization
void Start ()
{
curTime = 0;
}
// Update is called once per frame
void Update ()
{
curTime += Time.deltaTime;
if (curTime > GENERATE_TIME_INTERVAL && enemyIsAvailable())
{
//Debug.Log("Update Manager AI");
curTime = 0;
generateEnemyCloseToPlayer();
}
}
private bool enemyIsAvailable()
{
foreach (GameObject enemy in enemies)
{
if (enemy.GetComponent<EnemyAI>().isEnemyIdle())
{
return true;
}
}
return false;
}
private void generateEnemyCloseToPlayer()
{
Vector3 genPos = getGeneratorCloserToPlayer().GetComponent<Transform>().position;
genPos.y += 0.3f;
GameObject enemy = null;
foreach (GameObject curEnemy in enemies)
{
//Debug.Log("generateENemy");
if (curEnemy.GetComponent<EnemyAI>().isEnemyIdle())
{
curEnemy.GetComponent<Transform>().position = genPos;
curEnemy.GetComponent<EnemyAI>().startHunting();
break;
}
}
}
private GameObject getGeneratorCloserToPlayer()
{
GameObject closerGenerator = null;
foreach(GameObject generator in generators)
{
//Debug.Log("getGeneratorCloser");
if (closerGenerator == null)
{
closerGenerator = generator;
}
else
{
Vector3 posOne = closerGenerator.GetComponent<Transform>().position;
Vector3 posTwo = generator.GetComponent<Transform>().position;
Vector3 playerPos = player.GetComponent<Transform>().position;
float distOne = Vector3.Magnitude(playerPos - posOne);
float distTwo = Vector3.Magnitude(playerPos - posTwo);
if (distTwo < distOne)
{
closerGenerator = generator;
}
}
}
return closerGenerator;
}
No comments:
Post a Comment