IEnumerator ChangeTarget()
    {
        while (true)
        {
            List <AsteroidController> notEmpty = new List <AsteroidController>();
            foreach (AsteroidController asteroid in asteroids)
            {
                if (!asteroid.Empty())
                {
                    notEmpty.Add(asteroid);
                }
            }
            asteroids = notEmpty;

            if (asteroids.Count > 0)
            {
                if (energyConsumer.energy > 0)
                {
                    AsteroidController oldTarget = target;
                    for (int attempt = 0; attempt < 99; attempt++)
                    {
                        target = asteroids[Random.Range(0, asteroids.Count)];
                        if (target != oldTarget)
                        {
                            break;
                        }
                    }
                    float   distance  = (target.gameObject.transform.position - gameObject.transform.position).magnitude;
                    Vector2 direction = (target.gameObject.transform.position - gameObject.transform.position).normalized;
                    float   angle     = -Mathf.Atan2(direction.x, direction.y) * 180 / Mathf.PI;

                    yield return(new WaitForSeconds(powerUpDelay));

                    if (ray == null)
                    {
                        ray = Instantiate(rayPrefab, gameObject.transform.position, Quaternion.identity);
                    }
                    ray.transform.parent     = gameObject.transform;
                    ray.transform.position   = (target.gameObject.transform.position + gameObject.transform.position) / 2;
                    ray.transform.rotation   = Quaternion.AngleAxis(angle, Vector3.forward);
                    ray.transform.localScale = new Vector3(1, distance * 0.4f, 1);
                    yield return(new WaitForSeconds(interval - powerUpDelay));

                    Destroy(ray);
                    target.Harvest(power);
                    gameController.AddMinerals(power);
                    energyConsumer.Spend();
                }
                else
                {
                    yield return(new WaitForSeconds(powerUpDelay));
                }
            }
            else
            {
                target = null;
                Destroy(ray);
                if (Time.timeSinceLevelLoad > creationTime + minLifeTime)
                {
                    Instantiate(beepPrefab, gameObject.transform.position, Quaternion.identity);
                    Destroy(gameObject);
                }
                yield return(new WaitForSeconds(powerUpDelay));
            }
        }
    }
Beispiel #2
0
    void Update()
    {
        List <GameObject> targets = new List <GameObject>();

        targets.AddRange(GameObject.FindGameObjectsWithTag("Enemy"));

        if (targets.Count > 0)
        {
            GameObject nearest         = targets[0];
            float      nearestDistance = float.MaxValue;
            foreach (GameObject target in targets)
            {
                float distance = (target.transform.position - gameObject.transform.position).magnitude;
                if (distance < nearestDistance)
                {
                    nearestDistance = distance;
                    nearest         = target;
                }
            }

            if (nearestDistance <= distance)
            {
                if (energyConsumer.energy > 0)
                {
                    fireTime += Time.deltaTime;
                    if (fireTime >= 1.0f / energyConsumptionPerSecond)
                    {
                        fireTime -= 1.0f / energyConsumptionPerSecond;
                        energyConsumer.Spend();
                    }

                    Vector2 direction = (nearest.transform.position - gameObject.transform.position).normalized;
                    float   angle     = -Mathf.Atan2(direction.x, direction.y) * 180 / Mathf.PI;

                    nearest.GetComponent <EnemyFighterController>().hull -= dps * Time.deltaTime;

                    if (flare == null)
                    {
                        flare = Instantiate(flarePrefab, gameObject.transform.position, Quaternion.identity);
                        flare.transform.parent = gameObject.transform;
                    }
                    if (ray == null)
                    {
                        ray = Instantiate(rayPrefab, gameObject.transform.position, Quaternion.identity);
                        ray.transform.parent = gameObject.transform;
                    }

                    if (lastTarget != nearest || boom == null)
                    {
                        Destroy(boom);
                        boom = Instantiate(boomPrefab, nearest.transform.position, Quaternion.identity);
                        boom.transform.parent = nearest.transform;
                    }

                    ray.transform.position   = (nearest.transform.position + gameObject.transform.position) / 2;
                    ray.transform.rotation   = Quaternion.AngleAxis(angle, Vector3.forward);
                    ray.transform.localScale = new Vector3(1, nearestDistance * 0.4f, 1);

                    lastTarget = nearest;
                }
                else
                {
                    Destroy(flare);
                    Destroy(ray);
                    Destroy(boom);
                    lastTarget = null;
                }
            }
            else
            {
                Destroy(flare);
                Destroy(ray);
                Destroy(boom);
                lastTarget = null;
            }
        }
        else
        {
            Destroy(flare);
            Destroy(ray);
            Destroy(boom);
            lastTarget = null;
        }
    }