// Spawns a new enemy/bonus if needed. private void SpawnNewEnemy() { // Get the number of enemies to spawn. int enemies = mCurrentDifficulty.SpawnNumber(); if (enemies > 0) { // Get the bonus type. int bonusType = GetSpawnBonusType(); if (bonusType >= 0) { // If should spawn a bonus. Spawn one of the given type at a random position. mActiveObjects.Add(EnemyFactory.DispatchBonus(bonusType, (EnemyFactory.Column)UnityEngine.Random.Range(0, 3))); } else { // If should spawn an enemy. Spawn one at a random position. mActiveObjects.Add(EnemyFactory.Dispatch((EnemyFactory.Column)UnityEngine.Random.Range(0, 3))); } } }
void Update() { GameDeltaTime = Paused ? 0.0f : Time.deltaTime; if (mGameStatus == State.Level) { mDistanceTravelled += ScenerySpeed * GameDeltaTime; if (mDistanceTravelled != 0f) { UpdateText(string.Format("Distance: {0:0.0} m", mDistanceTravelled)); } mLevelTimeLeft -= GameDeltaTime; if (!GameFrozen) { if (mLevelTimeLeft > 0f) { //Get a bit pattern and spawn enemies according to it int enemies = mCurrentDifficulty.SpawnPattern(); for (int ColumnCount = 0; ColumnCount < 3; ColumnCount++) { if ((enemies & (1 << ColumnCount)) != 0) { EnemyFactory.Dispatch((EnemyFactory.Column)ColumnCount); } } PowerupFactory.DetectCollisions(mPlayerCharacter.transform.position); } else if (mActiveEnemies.Count == 0) { switch (SlowDownStage) { case 0: //We are just now starting to slow down mCurrentDifficulty.SlowDown(); break; //For case 1 - slowing down in progress - nothing is to be done case 2: //Slowing down has finished; switch to boss state mGameStatus = State.Boss; boss = new Boss(GameplayCamera, EnemyMaterial); mCurrentDifficulty.TriggerTutorial(11); break; } //At this point, we know there are no enemies to check, so it is OK to skip the loop below return; } } /* Traverse the list descendingly so that removals of already visited elements does not affect the * elements yet to be visited. */ for (int count = mActiveEnemies.Count - 1; count >= 0; count--) { Vector3 ThisPosition = mActiveEnemies[count].transform.position; //Update each enemy's position according to the game speed ThisPosition.y -= GameDeltaTime * EnemySpeed; mActiveEnemies[count].transform.position = ThisPosition; //Check if the enemy has flown off screen. If so, return it to the factory and count it as missed. if (ThisPosition.y < ScreenHeight * -0.5f) { EnemyFactory.Return(mActiveEnemies[count]); mMissedEnemies++; } //Check if the enemy is too close to the player. If so, end the game. else if ((mPlayerCharacter.transform.position - ThisPosition).sqrMagnitude < PlayerKillDistance) { GameOver("You died!"); } //Check if the enemy has been hit by a bullet else { for (int bullet = 0; bullet < mPlayerCharacter.Weapon.ActiveBullets.Count; bullet++) { if (mPlayerCharacter.Weapon.ActiveBullets[bullet].CheckHit(ThisPosition, BulletKillDistance, false)) { EnemyFactory.Return(mActiveEnemies[count]); break; } } } } //Check if the player has been "invaded" (too many enemies were missed if (mMissedEnemies >= MaxMissedEnemies) { GameOver("You have been invaded!"); } } else if (mGameStatus == State.Boss) { if (boss != null) { //Only if the boss is allowed to move if (!GameFrozen) { //Move and rotate boss to point to player's position boss.Update(mPlayerCharacter.transform.position); //Check if boss has eaten the player and if so end the game if (boss.HasEaten(mPlayerCharacter.transform.position)) { GameOver("You have been eaten!"); } } //Check wether boss was hit by a bullet foreach (Bullet bullet in mPlayerCharacter.Weapon.ActiveBullets) { if (bullet.CheckHit(boss.Position(), BossHitDistance, true)) { if (boss.Hit(bullet.DamageValue)) { boss = null; mCurrentDifficulty.TriggerTutorial(13); } break; } } } //No more boss, transitioning to normal else { mDistanceTravelled += ScenerySpeed * GameDeltaTime; UpdateText(string.Format("Distance: {0:0.0} m", mDistanceTravelled)); if (mCurrentDifficulty.LevelUp()) { mGameStatus = State.Level; mMissedEnemies = 0; mLevelTimeLeft = DifficultyCurve.LevelDuration; } } } else if (mGameStatus == State.TapToStart) { mCurrentDifficulty.TriggerTutorial(1); if (mCurrentDifficulty.TutorialStage != 0) { mGameStatus = State.Level; } else { GameText.text = "Tap the screen or press W to start"; } } }
void Update() { if (UserData.LevelMaxPoints != null && UserData.LevelMaxPoints.Count > UserData.CurrentLevel) { GameObject.Find("BestScore").GetComponent <TextMesh>().text = string.Format("Previous\nBest :\n{0}", UserData.LevelMaxPoints[UserData.CurrentLevel]); } GameDeltaTime = Paused ? 0.0f : Time.deltaTime; if (mGameStatus == State.Game) { GameText.transform.Find("Points").GetComponent <TextMesh>().text = string.Format("{0}", UserData.LevelPoints); if (UserData.GetIsTutorialReady()) // Display some tutorials for a few sec depending on state of the game { UserData.EnableTutorialMovementControl(); GameText.GetComponent <TextMesh>().text = string.Format("Control your ship by\ndragging it and try to earn as many\npoints as possible!"); } else if (UserData.GetSideLaser() > 0 && UserData.GetIsTutorialSLReady()) { UserData.EnableTutorialSideLaser(); GameText.GetComponent <TextMesh>().text = string.Format("When your laser is charged,\ntilt your device or\npress Q/E to use it!"); } else if (UserData.CountDownTutorialMovementControl() && UserData.CountDownTutorialSideLaser())//If tutorial has been displayed long enough { GameText.GetComponent <TextMesh>().text = string.Format(""); } string enemies = mCurrentDifficulty.SpawnCount(); int iEnemies; if (int.TryParse(enemies, out iEnemies)) { if (enemies == "-2") // Boss level - boss already spawned { if (mActiveEnemies.Count == 0) //Boss Defeated ! { mCurrentDifficulty.FinishLevel(8.0f); } } else if (DifficultyCurve.Levels[UserData.CurrentLevel].Length == 1) // Boss level { mActiveEnemies.Add(EnemyFactory.Dispatch(int.Parse(enemies), EnemyFactory.Column.Two, true)); } else if (enemies.Length == 1) { mActiveEnemies.Add(EnemyFactory.Dispatch(int.Parse(enemies), (EnemyFactory.Column)Random.Range(0, 3))); } else if (enemies.Length == 2) { int config = Random.Range(0, 3); if (config == 0) { mActiveEnemies.Add(EnemyFactory.Dispatch(int.Parse(enemies[0].ToString()), EnemyFactory.Column.One)); mActiveEnemies.Add(EnemyFactory.Dispatch(int.Parse(enemies[1].ToString()), EnemyFactory.Column.Two)); } else if (config == 1) { mActiveEnemies.Add(EnemyFactory.Dispatch(int.Parse(enemies[0].ToString()), EnemyFactory.Column.One)); mActiveEnemies.Add(EnemyFactory.Dispatch(int.Parse(enemies[1].ToString()), EnemyFactory.Column.Three)); } else { mActiveEnemies.Add(EnemyFactory.Dispatch(int.Parse(enemies[0].ToString()), EnemyFactory.Column.Two)); mActiveEnemies.Add(EnemyFactory.Dispatch(int.Parse(enemies[1].ToString()), EnemyFactory.Column.Three)); } } else if (enemies.Length == 3) { mActiveEnemies.Add(EnemyFactory.Dispatch(int.Parse(enemies.Substring(0, 1)), EnemyFactory.Column.One)); mActiveEnemies.Add(EnemyFactory.Dispatch(int.Parse(enemies.Substring(1, 1)), EnemyFactory.Column.Two)); mActiveEnemies.Add(EnemyFactory.Dispatch(int.Parse(enemies.Substring(2, 1)), EnemyFactory.Column.Three)); } } // Update the position of each active enemy, keep a track of enemies which have gone off screen List <GameObject> oldEnemys = new List <GameObject>(); for (int count = 0; count < mActiveEnemies.Count; count++) { Vector3 position = mActiveEnemies[count].transform.position; if (DifficultyCurve.Levels[UserData.CurrentLevel].Length == 1 && position.y <= ScreenHeight - 25.0f) // If boss, then force him to strafe back and forth after entering the screen { position = mActiveEnemies[count].GetComponent <EnemyBehaviour>().StrafeBackAndForth(); } else//If not boss then just go straight down (Could have some more logic here to implement different movement patterns) { position.y -= GameDeltaTime * GameSpeed; } mActiveEnemies[count].transform.position = position; mActiveEnemies[count].GetComponent <EnemyBehaviour>().DoAction(); if (position.y < ScreenHeight * -0.5f) { EnemyFactory.Return(mActiveEnemies[count]); oldEnemys.Add(mActiveEnemies[count]); mMissedEnemies++; } } mPowerUpFactory.MovePowerups(GameDeltaTime * GameSpeed); /* Mechanic no longer in use - kept here if needed * if( mMissedEnemies >= MaxMissedEnemies ) * { * // Too many missed enemies - Game over * mCurrentDifficulty.Stop(); * mGameOverTime = Time.timeSinceLevelLoad; * mGameStatus = State.GameOver; * GameText.text = string.Format( "You Been Invaded!\nTotal Distance: {0:0.0} m", mDistanceTravelled ); * } */ for (int count = 0; count < oldEnemys.Count; count++) { mActiveEnemies.Remove(oldEnemys[count]); } } }