LevelSelectController.cs 3.18 KB
Newer Older
1
2
3
using System;
using TMPro;
using UnityEngine;
4
using UnityEngine.UI;
5

Markus Schlösser's avatar
Markus Schlösser committed
6
public class LevelSelectController : MonoBehaviour {
7
8
9
	[SerializeField] private GameObject pagePrefab;
	[SerializeField] private int nonPageObjects = 3;
	
Markus Schlösser's avatar
Markus Schlösser committed
10
	private void Start() {
11
		InitalizeLevelSelect();
12
13
	}

Markus Schlösser's avatar
Markus Schlösser committed
14
	/// <summary>
15
	/// Initializes LevelSelect
Markus Schlösser's avatar
Markus Schlösser committed
16
	/// </summary>
17
18
	public void InitalizeLevelSelect()
	{
19
20
		int highestLevel = GameManager.Instance.HighestLevel;
		int lastLevel = GameManager.Instance.LastLevel;
21
		int pagesNeeded = lastLevel % 6 == 0? lastLevel/6 : lastLevel/6 + 1;
22
23
		int buttonsOnLastPage = lastLevel % 6;
		GameObject currentPage;
Markus Schlösser's avatar
Markus Schlösser committed
24

25
26
27
28
29
30
31
32
33
34
		for (int i = 1; i <= pagesNeeded; i++)
		{
			currentPage = GameObject.Instantiate(pagePrefab, transform.GetChild(0));
			currentPage.GetComponent<LevelSelectPageController>().PageNr = i;
			
			//Load images for buttons
			for (int j = 1; j <= 6; j++)
			{
				String buttonName = "ButtonLevel" + j;
				int levelNr = j + (currentPage.GetComponent<LevelSelectPageController>().PageNr-1) * 6;
Markus Schlösser's avatar
Markus Schlösser committed
35

36
37
38
39
				GameObject button = currentPage.transform.Find(buttonName).gameObject;
				Image image = button.transform.Find("Image").GetComponent<Image>();
				// Level unlocked
				if (levelNr <= highestLevel)
40
				{
41
42
43
44
45
46
47
48
49
50
					string loadStr = levelNr < 10 ? "LevelSelect/Level0" + levelNr : "LevelSelect/Level" + levelNr;
					image.sprite = Resources.Load(loadStr, typeof(Sprite)) as Sprite;
					button.transform.Find("Text").gameObject.GetComponent<TextMeshProUGUI>().text = levelNr + "";
					button.GetComponent<Button>().interactable = true;
				}
				// Level locked
				else if (levelNr <= lastLevel)
				{
					image.sprite = Resources.Load("LevelSelect/lockedTEMP", typeof(Sprite)) as Sprite;
					button.GetComponent<Button>().interactable = false;
51
52
53
54
55
56
57
58
59
60
61
62
				}
			}
			
			// Deactivate not needed NextPage- & PreviousPage-Buttons
			if (i == 1)
			{
				currentPage.transform.Find("ButtonPrevPage").gameObject.SetActive(false);
			}
			if (i == pagesNeeded)
			{
				currentPage.transform.Find("ButtonNextPage").gameObject.SetActive(false);
				
63
				// Deactivate not needed level buttons
64
65
66
67
68
69
				if (buttonsOnLastPage != 0)
				{
					for (int j = 6; j > buttonsOnLastPage; j--)
					{
						String buttonName = "ButtonLevel" + j;
						currentPage.transform.Find(buttonName).gameObject.SetActive(false);
70
					}
71
				}
72
73
			}
		}
74
		DisablePagesAfterFirst();
75
76
	}

Markus Schlösser's avatar
Markus Schlösser committed
77
78
79
80
	/// <summary>
	/// Disables all pages except the first one
	/// </summary>
	private void DisablePagesAfterFirst() {
81
		bool firstPage = true;
Markus Schlösser's avatar
Markus Schlösser committed
82
		foreach (GameObject page in GameObject.FindGameObjectsWithTag("LevelSelectPage")) {
83
84
			if (firstPage) 
			{
85
				firstPage = false;
86
87
88
			} 
			else 
			{
89
				page.SetActive(false);
Markus Schlösser's avatar
Markus Schlösser committed
90
			}
91
92
93
		}
	}

94
95
96
97
98
99
100
101
102
103
104
105
	public void ShowNextPage(int pageNr)
	{
		GameObject canvas = transform.Find("Canvas").gameObject;
		canvas.transform.GetChild(pageNr + nonPageObjects - 1).gameObject.SetActive(false);
		canvas.transform.GetChild(pageNr + nonPageObjects).gameObject.SetActive(true);
	}
	
	public void ShowPrevPage(int pageNr)
	{
		GameObject canvas = transform.Find("Canvas").gameObject;
		canvas.transform.GetChild(pageNr + nonPageObjects - 1).gameObject.SetActive(false);
		canvas.transform.GetChild(pageNr + nonPageObjects - 2).gameObject.SetActive(true);
106
	}
107
}