Exemplo n.º 1
0
        void Update()
        {
            float dt = Time.deltaTime;

            Debug.Assert(WaveData.EnemyCount >= 0);
            Debug.Assert(WaveData.EnemiesOnScreen >= 0);
            if (WaveData.EnemyCount > 0)
            {
                Debug.Assert(WaveData.EnemiesOnScreen <= WaveData.EnemyCount &&
                             WaveData.EnemiesOnScreen >= 0);
                while (WaveData.EnemiesOnScreen < WaveData.MaxSpawnedEnemiesOnScreen &&
                       WaveData.EnemiesOnScreen < WaveData.EnemyCount)
                {
                    PTAEntity enemyEntity = PTAEntity.CreateEnemy(this);
                    if (enemyEntity != null)
                    {
                        enemyEntity.HasSpawned = false;

                        enemyEntity.Move = MoveFunctions.SineMove;
                        enemyEntity.Data.MoveDirection = UnityEngine.Random.insideUnitCircle;
                        enemyEntity.Think = ThinkFunctions.HostileThink;

                        enemyEntity.Transform.position = GenerateEntityPosition();

                        enemyEntity.Collider.BoxCollider.enabled = false;
                    }

                    ++WaveData.EnemiesOnScreen;
                    Debug.Assert(WaveData.EnemiesOnScreen <= WaveData.EnemyCount);
                    Debug.Assert(WaveData.EnemiesOnScreen <= WaveData.MaxSpawnedEnemiesOnScreen);
                    Debug.Assert(WaveData.EnemiesOnScreen == Entities.Count(ent =>
                    {
                        return(ent.EntityTypeID == EntityType.Enemy && ent.IsActive);
                    }));
                }
            }
            else
            {
                Debug.Assert(WaveData.WildPowerupCount >= 0);
                if (WaveData.WildPowerupCount == 0)
                {
                    int nextWave = WaveData.CurrentWave + 1;
                    if (nextWave < WaveData.MaxWave)
                    {
                        int newEnemyCount = nextWave;
                        WaveData.EnemyCount = newEnemyCount;

                        if (nextWave % 5 == 0 &&
                            WaveData.PowerupWaitTime > WaveData.MinPowerupWaitTime)
                        {
                            WaveData.PowerupWaitTime -= 0.5f;
                        }

                        UI.WaveText.text = $"Wave: {nextWave}";

                        WaveData.CurrentWave = nextWave;
                    }
                    else
                    {
                        // TODO(SpectatorQL): End screen.
                        WaveData.EnemyCount = 0;
                        Debug.Log("YOU WIN!!!");
                    }
                }
            }


            if (WaveData.PowerupCount < WaveData.MaxPowerupsOnScreen)
            {
                if (WaveData.RunningPowerupTime <= 0)
                {
                    // NOTE(SpectatorQL): Oh, so this thing is _exclusive_ but the float version is _inclusive_. Gotta love Unity...
                    PowerupType powerupType = (PowerupType)UnityEngine.Random.Range(0, (int)PowerupType.Count);
                    Debug.Assert(powerupType < PowerupType.Count);

                    PTAEntity powerupEntity = null;
                    switch (powerupType)
                    {
                    case PowerupType.Turret:
                    {
                        powerupEntity = PTAEntity.CreateTurretPowerup(this);
                        break;
                    }

                    case PowerupType.Drive:
                    {
                        powerupEntity = PTAEntity.CreateDrivePowerup(this);
                        break;
                    }
                    }

                    if (powerupEntity != null)
                    {
                        powerupEntity.Transform.position = GenerateEntityPosition();
                    }

                    WaveData.RunningPowerupTime = WaveData.PowerupWaitTime;
                    ++WaveData.PowerupCount;
                }
            }
            if (WaveData.RunningPowerupTime > 0)
            {
                WaveData.RunningPowerupTime -= dt;
            }


            if (Cheats.RapidFire)
            {
                PlayerEntity.Data.AttackSpeed = Cheats.RapidFireSpeed;
            }


            for (int i = 0;
                 i < ENTITY_COUNT;
                 ++i)
            {
                PTAEntity entity = Entities[i];
                if (entity.IsActive)
                {
                    entity.Think(this, entity, dt);
                }
            }
        }
Exemplo n.º 2
0
        void Start()
        {
            PTAEntity.PlayerLayer = LayerMask.NameToLayer("Player");
            PTAEntity.ThingsLayer = LayerMask.NameToLayer("Things");
            if (PTAEntity.PlayerLayer == -1 ||
                PTAEntity.ThingsLayer == -1)
            {
                Debug.LogError("Layers are missing! Check the layers settings in Edit/Project Settings/Tags and Layers !");
            }


            Vector2 bottomLeft      = Camera.main.ScreenToWorldPoint(new Vector3(0, 0, 10));
            Vector2 topRight        = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, 10));
            float   offscreenOffset = 1.0f;

            PlayArea.MinX   = bottomLeft.x - offscreenOffset;
            PlayArea.MinY   = bottomLeft.y - offscreenOffset;
            PlayArea.MaxX   = topRight.x + offscreenOffset;
            PlayArea.MaxY   = topRight.y + offscreenOffset;
            PlayArea.Center = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width / 2, Screen.height / 2, 10));

            Vector2 xWallSize = new Vector2(1.0f, PlayArea.MaxY - PlayArea.MinY);
            Vector2 yWallSize = new Vector2(PlayArea.MaxX - PlayArea.MinX, 1.0f);

            const int WALL_COUNT = 2;

            for (WallType i = 0;
                 i < WallType.Count;
                 ++i)
            {
                for (int j = 0;
                     j < WALL_COUNT;
                     ++j)
                {
                    PTAWall wall = Instantiate(WallPrefab).GetComponent <PTAWall>();
                    wall.World       = this;
                    wall.BoxCollider = wall.gameObject.GetComponent <BoxCollider2D>();
                    wall.WallTypeID  = i;
                    if (wall.WallTypeID == WallType.XWall)
                    {
                        wall.BoxCollider.size = xWallSize;
                    }
                    else if (wall.WallTypeID == WallType.YWall)
                    {
                        wall.BoxCollider.size = yWallSize;
                    }

                    if (j == 0)
                    {
                        if (wall.WallTypeID == WallType.XWall)
                        {
                            wall.transform.position = new Vector2(PlayArea.MinX, PlayArea.Center.y);
                        }
                        else if (wall.WallTypeID == WallType.YWall)
                        {
                            wall.transform.position = new Vector2(PlayArea.Center.x, PlayArea.MinY);
                        }
                    }
                    else if (j == 1)
                    {
                        if (wall.WallTypeID == WallType.XWall)
                        {
                            wall.transform.position = new Vector2(PlayArea.MaxX, PlayArea.Center.y);
                        }
                        else if (wall.WallTypeID == WallType.YWall)
                        {
                            wall.transform.position = new Vector2(PlayArea.Center.x, PlayArea.MaxY);
                        }
                    }
                    else
                    {
                        Debug.LogError($"Error! Too many walls of type {i} have been spawned!");
                    }
                }
            }


            Entities = new PTAEntity[ENTITY_COUNT];
            for (int i = 0;
                 i < ENTITY_COUNT;
                 ++i)
            {
                Entities[i] = new PTAEntity();
            }

            PlayerEntity = PTAEntity.CreatePlayer(this);

#if UNITY_EDITOR
            PTAEntity turretL = PTAEntity.CreateTurretPowerup(this);
            turretL.Transform.position = GenerateEntityPosition();
            ++WaveData.PowerupCount;

            PTAEntity turretR = PTAEntity.CreateTurretPowerup(this);
            turretR.Transform.position = GenerateEntityPosition();
            ++WaveData.PowerupCount;

            PTAEntity freeTurret = PTAEntity.CreateTurretPowerup(this);
            freeTurret.Transform.position = GenerateEntityPosition();
            ++WaveData.PowerupCount;

            PTAEntity drive = PTAEntity.CreateDrivePowerup(this);
            drive.Transform.position = GenerateEntityPosition();
            ++WaveData.PowerupCount;
#else
            Cheats.Invincibility = false;
            Cheats.RapidFire     = false;
#endif

            UI = FindObjectOfType <PTAUI>();
        }