Hello,
As mentioned in the title, the LevelManager.cs seems to take the first brick being destroyed as the last one… So after any brick is destroyed in a level, the next one loads immediately. It’s a nice clear code to have, but i’d like the player to play the game first…
Here is the Code for the Brick.cs
public class Brick : MonoBehaviour {
public Sprite[] hitSprites;
public static int breakableCount;
private int TimesHit;
private LevelManager levelManager;
bool isBreakable;
// Use this for initialization
void Start ()
{
breakableCount = 0;
isBreakable = (this.tag == "Breakable");
//Here we will keep track of the number of bricks in the game
if (isBreakable) {
breakableCount ++;
}
TimesHit = 0;
levelManager = GameObject.FindObjectOfType <LevelManager>();
}
void OnCollisionEnter2D (Collision2D Collision)
{
if (isBreakable) {
HandelHits ();
}
}
void HandelHits ()
{
int MaxHits = hitSprites.Length + 1;
TimesHit ++;
if (TimesHit >= MaxHits) {
breakableCount --;
levelManager.BricksDestroyed ();
Destroy (gameObject);
} else {
LoadSprites();
}
}
void LoadSprites ()
{
int spriteIndex = TimesHit - 1;
if (hitSprites [spriteIndex]){
this.GetComponent<SpriteRenderer> ().sprite = hitSprites [spriteIndex];
}
}
//TODO Reomve this method once doen
void SimulateWin ()
{
levelManager.LoadNextLevel ();
}
void Update () {
}
}
And here is the Code in LevelManager.cs
public class LevelManager : MonoBehaviour {
public void LoadLevel (string name)
{
Debug.Log ("Level Load Requested for " + name);
Application.LoadLevel (name);
}
public void QuitGame ()
{
Debug.Log ("I'm leaving...");
Application.Quit ();
}
public void LoadNextLevel () {
Application.LoadLevel (Application.loadedLevel + 1);
}
public void BricksDestroyed () {
if (Brick.breakableCount <= 0) {
LoadNextLevel ();
}
}
}
Thank you
