-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMinigameController.cs
228 lines (191 loc) · 7.03 KB
/
MinigameController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
using System;
using System.Collections.Generic;
using System.Text;
using Sirenix.OdinInspector;
using UnityEngine;
namespace RLO.Science.BasicsOfProgramming
{
public class MinigameController : MonoBehaviour
{
public Action OnLevelComplete = delegate { };
public Action<ERROR> OnLevelFailed = delegate { };
[SerializeField] private InstructionContainer _container;
[SerializeField] private GameObject _monobot;
[SerializeField] private MonobotController _botController;
[SerializeField] private ProgrammingLevel[] _levels;
private ProgrammingLevel _currentLevel;
private int _currentLevelId;
private bool _bananaBoost;
private bool _stopExecution;
private GameState _gameState;
private void Start()
{
_botController.OnItemPickup += HandleItemPickup;
_monobot.transform.localScale = Vector3.zero;
}
public void BeginGame(Action onSpawnComplete)
{
SwitchLevel(0, onSpawnComplete, null);
}
public void NextLevel(Action onComplete, Action onFailure)
{
SwitchLevel(_currentLevelId + 1 ,onComplete, onFailure);
}
public void SwitchLevel(int level, Action onComplete, Action onFailure)
{
if (level > _levels.Length - 1)
{
onFailure?.Invoke();
return;
}
_container.Clear();
if (_currentLevel == null)
{
_currentLevel = _levels[level];
_currentLevel.gameObject.SetActive(true);
var startTile = _currentLevel.StartTile;
_currentLevel.SpawnLevel(() =>
{
onComplete?.Invoke();
_botController.SpawnBot(null, startTile);
});
}
else
{
_currentLevel.DespawnLevel(() =>
{
_currentLevel.gameObject.SetActive(false);
_currentLevel = _levels[level];
_currentLevel.gameObject.SetActive(true);
var startTile = _currentLevel.StartTile;
_currentLevel.SpawnLevel(() =>
{
onComplete?.Invoke();
_botController.SpawnBot(null, startTile);
});
});
}
_currentLevelId = level;
_gameState = new GameState {DatesLeft = _currentLevel.DatesToCollect};
}
public void RestartLevel(Action onComplete)
{
_gameState = new GameState {DatesLeft = _currentLevel.DatesToCollect};
_botController.DespawnBot(() =>
{
_currentLevel.RestartLevel(() =>
{
_botController.SpawnBot(null, _currentLevel.StartTile);
onComplete?.Invoke();
});
});
}
public void BeginProgram()
{
var instructions = _container.GetInstructions();
if (instructions.Count == 0)
{
Debug.Log("Instruction queue empty!");
return;
}
_stopExecution = false;
Action<ERROR> onFail = x => { Debug.Log($"Dun' goofed: {x}"); };
Action onSuccess = HandleProgramCompletion;
ExecuteInstruction(instructions, onFail, onSuccess); // this is recursive
}
private void ExecuteInstruction(Queue<INSTRUCTION> q, Action<ERROR> onFailure, Action onComplete)
{
if (_stopExecution)
{
_stopExecution = false;
return;
}
if (_bananaBoost)
{
// The boost adds 3 forward movements to program.
var x = new List<INSTRUCTION>(q);
x.Insert(0, INSTRUCTION.Forward);
x.Insert(0, INSTRUCTION.Forward);
x.Insert(0, INSTRUCTION.Forward);
q = new Queue<INSTRUCTION>(x);
_bananaBoost = false;
}
if (q.Count == 0)
{
// Program complete!
onComplete?.Invoke();
return;
}
// TODO: Debug helper, remove on deployment.
StringBuilder sb = new StringBuilder();
sb.Append("Instruction queue: ");
foreach (var i in q)
{
sb.Append(i + ", ");
}
Debug.Log(sb.ToString());
var instruction = q.Dequeue();
Debug.Log($"Executing {instruction}");
// END OF DEBUG MSG
Action success = () => ExecuteInstruction(q, onFailure, onComplete);
Action fail = () => onFailure.Invoke(ERROR.OOB);
if (instruction == INSTRUCTION.Forward)
{
_botController.MoveForward(success, fail);
}
else
{
_botController.Turn(instruction == INSTRUCTION.Right, success);
}
}
private void HandleItemPickup(ITEM item)
{
switch (item)
{
// check for basket
case ITEM.Date when _gameState.DatesPickedUp > 0 && !_gameState.BasketAcquired:
Debug.Log("Monobot tried to picked up more than 1 date, but had no basket.");
_stopExecution = true;
return;
case ITEM.Date:
_gameState.DatesLeft--;
_gameState.DatesPickedUp++;
if (_gameState.DatesLeft == 0)
{
_gameState.VictoryAchieved = true;
}
break;
case ITEM.Basket:
_gameState.BasketAcquired = true;
break;
case ITEM.Banana:
_bananaBoost = true;
break;
}
}
private void HandleProgramCompletion()
{
if (!_gameState.VictoryAchieved)
OnLevelFailed.Invoke(ERROR.NoVictory);
else
OnLevelComplete.Invoke();
}
// TODO: Remove on deployment.
[Button(ButtonSizes.Small)]
public void DEV_SpawnNextLevel()
{
_botController.DespawnBot(() =>
{
SwitchLevel(_currentLevelId + 1 ,null, null);
});
}
private struct GameState
{
public int DatesLeft;
public int DatesPickedUp;
public bool BasketAcquired;
public bool VictoryAchieved;
}
}
public enum ERROR { OOB, NoBasket, NoVictory}
}