LevelSelectController.cs 2.32 KB
Newer Older
Markus Schlösser's avatar
Markus Schlösser committed
1
using UnityEngine;
2
using UnityEngine.UI;
3

Markus Schlösser's avatar
Markus Schlösser committed
4
5
public class LevelSelectController : MonoBehaviour {
	private void Start() {
6
		ActivateAllPages();
7
		UpdateLevelSelect();
8
		DisablePagesAfterFirst();
9
10
	}

Markus Schlösser's avatar
Markus Schlösser committed
11
12
13
14
15
16
17
18
19
20
	/// <summary>
	/// Locks all currently not unlocked levels
	/// </summary>
	public void UpdateLevelSelect() {
		int highestLevel = GameManager.Instance.HighestLevel;
		int lastLevel = GameManager.Instance.LastLevel;

		for (int i = 1; i <= 18; i++) {
			string buttonName = "ButtonLevel" + i;
			if (GameObject.Find(buttonName) != null) {
21
22
				GameObject button = GameObject.Find(buttonName);
				Image image = button.transform.Find("Image").GetComponent<Image>();
23
				// Level unlocked
Markus Schlösser's avatar
Markus Schlösser committed
24
25
				if (i <= highestLevel) {
					string loadStr = i < 10 ? "LevelSelect/Level0" + i : "LevelSelect/Level" + i;
26
27
28
					image.sprite = Resources.Load(loadStr, typeof(Sprite)) as Sprite;
					button.GetComponent<Button>().interactable = true;
				}
29
				// Level locked
Markus Schlösser's avatar
Markus Schlösser committed
30
				else if (i <= lastLevel) {
31
32
33
					image.sprite = Resources.Load("LevelSelect/lockedTEMP", typeof(Sprite)) as Sprite;
					button.GetComponent<Button>().interactable = false;
				}
34
				// Level doesn't exist
Markus Schlösser's avatar
Markus Schlösser committed
35
				else {
36
					button.SetActive(false);
Markus Schlösser's avatar
Markus Schlösser committed
37

38
					// Deactivates NextPageButton because there isnt' a next page
39
40
41
42
43
44
45
					if (button.transform.parent.Find("ButtonNextPage") != null)
					{

						GameObject nextPage = button.transform.parent.Find("ButtonNextPage").gameObject;
						
						nextPage.SetActive(false);
					}
Markus Schlösser's avatar
Markus Schlösser committed
46

47
					// Next page can't be opened so buttons on it don't have to be changed
48
49
50
					if (i % 6 == 0)
						return;
				}
51
52
53
54
			}
		}
	}

Markus Schlösser's avatar
Markus Schlösser committed
55
56
57
58
	/// <summary>
	/// Disables all pages except the first one
	/// </summary>
	private void DisablePagesAfterFirst() {
59
		bool firstPage = true;
Markus Schlösser's avatar
Markus Schlösser committed
60
61
		foreach (GameObject page in GameObject.FindGameObjectsWithTag("LevelSelectPage")) {
			if (firstPage) {
62
				firstPage = false;
Markus Schlösser's avatar
Markus Schlösser committed
63
			} else {
64
				page.SetActive(false);
Markus Schlösser's avatar
Markus Schlösser committed
65
			}
66
67
68
		}
	}

Markus Schlösser's avatar
Markus Schlösser committed
69
70
71
72
73
	/// <summary>
	/// Ensures all pages are active before loading "locked" images
	/// </summary>
	private void ActivateAllPages() {
		foreach (GameObject page in GameObject.FindGameObjectsWithTag("LevelSelectPage")) {
74
			page.SetActive(true);
75
76
		}
	}
77

Markus Schlösser's avatar
Markus Schlösser committed
78
79
	public void LoadLevel(int level) {
		GameManager.Instance.LoadLevel(level);
80
	}
81

Markus Schlösser's avatar
Markus Schlösser committed
82
	public void OpenLevelSelect() {
83
84
85
86
		ActivateAllPages();
		UpdateLevelSelect();
		DisablePagesAfterFirst();
	}
87
}