|
1 |
| -using System; |
2 |
| -using System.Collections; |
3 |
| -using System.Collections.Generic; |
4 |
| -using System.Linq; |
5 |
| -using UnityEngine; |
6 |
| - |
7 |
| -public class GameController : MonoBehaviour |
8 |
| -{ |
9 |
| - private GameObject[] triangles; |
10 |
| - private Rotation[] level = new Rotation[] { Rotation.Up, Rotation.Left, Rotation.Down }; |
11 |
| - private bool win = false; |
12 |
| - private bool loose = false; |
13 |
| - |
14 |
| - public Sprite TriangleSuccess; |
15 |
| - public Sprite TriangleError; |
16 |
| - |
17 |
| - // Start is called before the first frame update |
18 |
| - void Start() |
19 |
| - { |
20 |
| - GameEvents.instance.OnCountEnd += DoOnLoose; |
21 |
| - |
22 |
| - triangles = GameObject.FindGameObjectsWithTag("Triangle"); |
23 |
| - Array.Sort(triangles, CompareNames); |
24 |
| - |
25 |
| - StartCoroutine(WatchForWin()); |
26 |
| - } |
27 |
| - |
28 |
| - // Update is called once per frame |
29 |
| - void Update() |
30 |
| - { |
31 |
| - if (win || loose) |
32 |
| - { |
33 |
| - return; |
34 |
| - } |
35 |
| - |
36 |
| - var rotations = GetCurrentRotations(); |
37 |
| - win = TestForWin(rotations); |
38 |
| - |
39 |
| - Debug.Log(String.Join(", ", rotations)); |
40 |
| - } |
41 |
| - |
42 |
| - private Rotation[] GetCurrentRotations() |
43 |
| - { |
44 |
| - return triangles |
45 |
| - .Select(MapObjectToRotation) |
46 |
| - .Select(MapAngleToRotation) |
47 |
| - .ToArray(); |
48 |
| - } |
49 |
| - |
50 |
| - private bool TestForWin(Rotation[] rotations) |
51 |
| - { |
52 |
| - return rotations.SequenceEqual(level); |
53 |
| - } |
54 |
| - |
55 |
| - private IEnumerator WatchForWin() |
56 |
| - { |
57 |
| - while (true) |
58 |
| - { |
59 |
| - if (loose) |
60 |
| - { |
61 |
| - yield break; |
62 |
| - } |
63 |
| - else if (win) |
64 |
| - { |
65 |
| - DoOnWin(); |
66 |
| - yield break; |
67 |
| - } |
68 |
| - yield return null; |
69 |
| - } |
70 |
| - } |
71 |
| - |
72 |
| - private void DoOnWin() |
73 |
| - { |
74 |
| - GameEvents.instance.TriggerWin(); |
75 |
| - foreach (GameObject triangle in triangles) |
76 |
| - { |
77 |
| - SetSuccessSprite(triangle); |
78 |
| - } |
79 |
| - } |
80 |
| - |
81 |
| - private void DoOnLoose() |
82 |
| - { |
83 |
| - SetLoose(); |
84 |
| - |
85 |
| - int index = 0; |
86 |
| - foreach (GameObject triangle in triangles) |
87 |
| - { |
88 |
| - var angle = MapObjectToRotation(triangle); |
89 |
| - var rotation = MapAngleToRotation(angle); |
90 |
| - if (level[index] == rotation) |
91 |
| - { |
92 |
| - SetSuccessSprite(triangle); |
93 |
| - } |
94 |
| - else |
95 |
| - { |
96 |
| - SetErrorSprite(triangle); |
97 |
| - } |
98 |
| - index++; |
99 |
| - } |
100 |
| - } |
101 |
| - |
102 |
| - private int CompareNames(GameObject a, GameObject b) |
103 |
| - { |
104 |
| - return a.name.CompareTo(b.name); |
105 |
| - } |
106 |
| - |
107 |
| - private int MapObjectToRotation(GameObject obj) |
108 |
| - { |
109 |
| - return Convert.ToInt32(obj.transform.rotation.eulerAngles.z); |
110 |
| - } |
111 |
| - |
112 |
| - private Rotation MapAngleToRotation(int angle) |
113 |
| - { |
114 |
| - return (Rotation)Enum.ToObject(typeof(Rotation), angle); |
115 |
| - } |
116 |
| - |
117 |
| - private void SetSuccessSprite(GameObject obj) |
118 |
| - { |
119 |
| - SetSprite(obj, TriangleSuccess); |
120 |
| - } |
121 |
| - |
122 |
| - private void SetErrorSprite(GameObject obj) |
123 |
| - { |
124 |
| - SetSprite(obj, TriangleError); |
125 |
| - } |
126 |
| - |
127 |
| - private void SetSprite(GameObject obj, Sprite sprite) |
128 |
| - { |
129 |
| - var renderer = obj.GetComponent<SpriteRenderer>(); |
130 |
| - renderer.sprite = sprite; |
131 |
| - } |
132 |
| - |
133 |
| - private void SetLoose() |
134 |
| - { |
135 |
| - loose = true; |
136 |
| - } |
137 |
| - |
138 |
| - private void ResetLoose() |
139 |
| - { |
140 |
| - loose = false; |
141 |
| - } |
142 |
| -} |
| 1 | +using System; |
| 2 | +using System.Collections; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Linq; |
| 5 | +using UnityEngine; |
| 6 | + |
| 7 | +public class GameController : MonoBehaviour |
| 8 | +{ |
| 9 | + private static readonly int MIN_WEIGHT = 2; |
| 10 | + |
| 11 | + private int weight = MIN_WEIGHT; |
| 12 | + private bool win = false; |
| 13 | + private bool loose = false; |
| 14 | + |
| 15 | + private GameObject[] triangles; |
| 16 | + private Rotation[] level; |
| 17 | + |
| 18 | + public GameObject GridLayout; |
| 19 | + public GameObject TriangleTemplate; |
| 20 | + |
| 21 | + public Sprite TriangleSuccess; |
| 22 | + public Sprite TriangleError; |
| 23 | + |
| 24 | + // Start is called before the first frame update |
| 25 | + void Start() |
| 26 | + { |
| 27 | + BindGameEvents(); |
| 28 | + Restart(); |
| 29 | + } |
| 30 | + |
| 31 | + // Update is called once per frame |
| 32 | + void Update() |
| 33 | + { |
| 34 | + if (win || loose) |
| 35 | + { |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + var rotations = GetCurrentRotations(); |
| 40 | + win = TestForWin(rotations); |
| 41 | + |
| 42 | + Debug.Log("Current: " + String.Join(", ", rotations)); |
| 43 | + } |
| 44 | + |
| 45 | + private void BindGameEvents() |
| 46 | + { |
| 47 | + GameEvents.instance.OnCountEnd += DoOnLoose; |
| 48 | + } |
| 49 | + |
| 50 | + private void Restart() |
| 51 | + { |
| 52 | + var generator = new LevelGenerator(); |
| 53 | + level = generator.Create(weight); |
| 54 | + Debug.Log("Level: " + String.Join(", ", level)); |
| 55 | + |
| 56 | + // TODO: should check whether rotations not the same as level |
| 57 | + var rotations = generator.Create(weight); |
| 58 | + triangles = GenerateTriangles(rotations); |
| 59 | + |
| 60 | + StartCoroutine(WatchForWin()); |
| 61 | + } |
| 62 | + |
| 63 | + private Rotation[] GetCurrentRotations() |
| 64 | + { |
| 65 | + return triangles |
| 66 | + .Select(MapObjectToRotation) |
| 67 | + .Select(MapAngleToRotation) |
| 68 | + .ToArray(); |
| 69 | + } |
| 70 | + |
| 71 | + private bool TestForWin(Rotation[] rotations) |
| 72 | + { |
| 73 | + return rotations.SequenceEqual(level); |
| 74 | + } |
| 75 | + |
| 76 | + private IEnumerator WatchForWin() |
| 77 | + { |
| 78 | + while (true) |
| 79 | + { |
| 80 | + if (loose) |
| 81 | + { |
| 82 | + yield break; |
| 83 | + } |
| 84 | + else if (win) |
| 85 | + { |
| 86 | + DoOnWin(); |
| 87 | + yield break; |
| 88 | + } |
| 89 | + yield return null; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + private void DoOnWin() |
| 94 | + { |
| 95 | + GameEvents.instance.TriggerWin(); |
| 96 | + foreach (GameObject triangle in triangles) |
| 97 | + { |
| 98 | + SetSuccessSprite(triangle); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + private void DoOnLoose() |
| 103 | + { |
| 104 | + SetLoose(); |
| 105 | + |
| 106 | + int index = 0; |
| 107 | + foreach (GameObject triangle in triangles) |
| 108 | + { |
| 109 | + var angle = MapObjectToRotation(triangle); |
| 110 | + var rotation = MapAngleToRotation(angle); |
| 111 | + if (level[index] == rotation) |
| 112 | + { |
| 113 | + SetSuccessSprite(triangle); |
| 114 | + } |
| 115 | + else |
| 116 | + { |
| 117 | + SetErrorSprite(triangle); |
| 118 | + } |
| 119 | + index++; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + private int MapObjectToRotation(GameObject obj) |
| 124 | + { |
| 125 | + return Convert.ToInt32(obj.transform.rotation.eulerAngles.z); |
| 126 | + } |
| 127 | + |
| 128 | + private Rotation MapAngleToRotation(int angle) |
| 129 | + { |
| 130 | + return (Rotation)Enum.ToObject(typeof(Rotation), angle); |
| 131 | + } |
| 132 | + |
| 133 | + private void SetSuccessSprite(GameObject obj) |
| 134 | + { |
| 135 | + SetSprite(obj, TriangleSuccess); |
| 136 | + } |
| 137 | + |
| 138 | + private void SetErrorSprite(GameObject obj) |
| 139 | + { |
| 140 | + SetSprite(obj, TriangleError); |
| 141 | + } |
| 142 | + |
| 143 | + private void SetSprite(GameObject obj, Sprite sprite) |
| 144 | + { |
| 145 | + var renderer = obj.GetComponent<SpriteRenderer>(); |
| 146 | + renderer.sprite = sprite; |
| 147 | + } |
| 148 | + |
| 149 | + private void SetLoose() |
| 150 | + { |
| 151 | + loose = true; |
| 152 | + } |
| 153 | + |
| 154 | + private void ResetLoose() |
| 155 | + { |
| 156 | + loose = false; |
| 157 | + } |
| 158 | + |
| 159 | + private GameObject[] GenerateTriangles(Rotation[] rotations) |
| 160 | + { |
| 161 | + var generator = new TriangleGenerator(GridLayout.transform, TriangleTemplate); |
| 162 | + return generator.Create(rotations); |
| 163 | + } |
| 164 | + |
| 165 | + private void IncWeight() |
| 166 | + { |
| 167 | + weight++; |
| 168 | + } |
| 169 | +} |
0 commit comments