예제 #1
0
    void playerDie()
    {
        GameManager.isPlayerDead = true;
        particles.Emit(20);
        gameObject.GetComponent <MeshRenderer>().enabled   = false;
        gameObject.GetComponent <Collider>().enabled       = false;
        gameObject.GetComponent <PlayerMovement>().enabled = false;
        Destroy(gameObject, 1);
        double enemiesKilled = ScoreScript.getScore();
        int    waveCount     = EnemySpawn.getWave();

        Debug.Log("Enemies Killed: " + enemiesKilled + " Wave Reached: " + waveCount + " Final Score: " + waveCount * enemiesKilled);
        // Set gamestate to over
    }
예제 #2
0
    private void updateEnemies()
    {
        // Check for dead enemies
        for (int i = enemies.Count - 1; i >= 0; --i)
        {
            if (enemies[i] == null)
            {
                enemies.RemoveAt(i);
                player.GetComponent <Player>().receiveExperience(1);
                enemiesRemaining--;
            }
        }
        // Spawn new enemies
        if (enemiesRemaining == 0 && planetHealth > 0)
        {
            waveNumber++;
            EnemySpawn[] enemySpawns = EnemySpawn.getWave(waveNumber);
            enemiesRemaining = enemySpawns.Length;
            var randomAngle = Random.Range(0, 360);
            foreach (EnemySpawn enemySpawn in enemySpawns)
            {
                if (enemySpawn.delay == 0f)
                {
                    spawnEnemy(enemySpawn.distance, randomAngle + enemySpawn.angleOffset);
                }
                else
                {
                    delayedSpawns.Add(new DelayedSpawn(enemySpawn.distance, randomAngle + enemySpawn.angleOffset, enemySpawn.delay));
                }
            }
        }
        // Spawn delayed enemies
        for (int i = delayedSpawns.Count - 1; i >= 0; i--)
        {
            var delayedSpawn = delayedSpawns[i];
            delayedSpawn.delay -= Time.deltaTime;
            if (delayedSpawn.delay <= 0f)
            {
                spawnEnemy(delayedSpawn.distance, delayedSpawn.angle);
                delayedSpawns.RemoveAt(i);
            }
        }
        // Update off-screen enemy indicators
        Vector2 screenCenter = new Vector2(Screen.width / 2, Screen.height / 2);

        for (int i = 0; i < indicators.Count; i++)
        {
            var indicator = indicators[i];
            if (i >= enemies.Count)
            {
                indicator.SetActive(false);
                continue;
            }
            var     enemy        = enemies[i];
            var     borderOffset = 30;
            Vector2 screenPos    = Camera.main.WorldToScreenPoint(enemy.transform.position);
            if (screenPos.x < 0 || screenPos.x > Screen.width ||
                screenPos.y < 0 || screenPos.y > Screen.height)
            {
                indicator.SetActive(true);
                screenPos -= screenCenter;
                var absScreenPos = new Vector2(Mathf.Abs(screenPos.x), Mathf.Abs(screenPos.y));
                // compare y:x ratio of the screenPos vector to the y:x ratio of the screen
                // If it is larger, top of screen. otherwise, side of screen.
                if ((absScreenPos.y / absScreenPos.x) > (screenCenter.y / screenCenter.x))
                {
                    screenPos = ((screenCenter.y - borderOffset) / absScreenPos.y) * screenPos;
                }
                else
                {
                    screenPos = ((screenCenter.x - borderOffset) / absScreenPos.x) * screenPos;
                }
                // The indicators are children of the canvas, so they use coordinates relative to the screen center
                indicator.transform.localPosition = screenPos;
                float angle = Mathf.Rad2Deg * Mathf.Atan2(screenPos.y, screenPos.x);
                indicator.transform.localEulerAngles = new Vector3(0, 0, angle);
            }
            else
            {
                indicator.SetActive(false);
            }
        }
    }