// Generating the level based on AppModel._instance.levels[LevelToLoad] IEnumerator LevelSpawner(int LevelID) { Time.timeScale = 1; // load new game by the level index LevelsModel incomingLevel = AppModel._instance.levels[LevelID]; isGameRunning = false; // clear previous objects ObjectPoolList._instance.DespawnAll(); // spawn player/s AppModel._instance.game.playersAlive = AppModel._instance.game.totalPlayers; //Debug.Log("new game with "+ AppModel._instance.game.totalPlayers+ " player"); if (AppModel._instance.game.totalPlayers == 2) { // player 1 is active player1.transform.position = incomingLevel.Player1SpawnPoint.position; player1.gameObject.SetActive(true); AppModel._instance.player[0].b_canMove = true; AppModel._instance.player[0].b_canShoot = true; AppModel._instance.player[0].lives = 1; // player 2 is active player2.transform.position = incomingLevel.Player2SpawnPoint.position; player2.gameObject.SetActive(true); AppModel._instance.player[1].b_canMove = true; AppModel._instance.player[1].b_canShoot = true; AppModel._instance.player[1].lives = 1; } else if (AppModel._instance.game.totalPlayers == 1) { // player 1 is active player1.gameObject.SetActive(true); player1.transform.position = incomingLevel.Player1SpawnPoint.position; AppModel._instance.player[0].b_canMove = true; AppModel._instance.player[0].b_canShoot = true; AppModel._instance.player[0].lives = 1; // player 2 is not active player2.gameObject.SetActive(false); AppModel._instance.player[1].lives = 0; } AppModel._instance.game.CountdownLeft = incomingLevel.TimerCountdown; // generate level int totalBallsCount = 0; List <BallMovement> NewBallsList = new List <BallMovement>(); for (int i = 0; i < incomingLevel.Balls.Length; i++) { // spawn balls GameObject NewBall = ObjectPoolList._instance.SpawnObject(incomingLevel.Balls[i].BallSize.ToString(), incomingLevel.Balls[i].BallSpawnPoint.position); // change ball properties BallMovement NewBallScript = NewBall.GetComponent <BallMovement>(); if (NewBallScript != null) { NewBallScript.SetBallSpeed(); // this sets the internall vector2 'velo' that moves the ball to the current level speed NewBallScript.SetBallxDirection(incomingLevel.Balls[i].BallInitialDirection); NewBallScript.m_Rigidbody.simulated = false; NewBallsList.Add(NewBallScript); } // add ball as collective sum of inner balls to calculate total balls count in the level totalBallsCount += GetTotalBallsCountBySize(incomingLevel.Balls[i].BallSize); } // saves the level's total balls count to check against win condition AppModel._instance.game.ballsLeft = totalBallsCount; float DelayBeforeLevelStarts = 3f; StartIncomingLevelAnimation(true, DelayBeforeLevelStarts); yield return(new WaitForSecondsRealtime(DelayBeforeLevelStarts)); StartIncomingLevelAnimation(false, 0f); isGameRunning = true; // give all balls an initial jump foreach (BallMovement bm in NewBallsList.ToArray()) { bm.m_Rigidbody.simulated = true; bm.InitialJump(); } //Debug.Log("LevelSpawner(" + incomingLevel.LevelID + ") Ended "); }