-
Notifications
You must be signed in to change notification settings - Fork 0
/
DiceController.cs
217 lines (188 loc) · 6.1 KB
/
DiceController.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DiceController : MonoBehaviour
{
public ScoreController scoreController;
public DieSelector dieSelector;
public SavedDice savedDice;
public Vector3 rollPosition;
public Vector3 rollForce;
private GameObject spawnPoint = null;
private ArrayList thrownDice;
public int noOfThrown;
public int noOfSelected;
public int noOfSaved;
public string diceColor;
public bool dots;
private bool newThrow;
private bool firstThrow;
private bool failedThrow;
// Start is called before the first frame update
void Start()
{
newThrow = false;
firstThrow = true;
thrownDice = null;
failedThrow = false;
}
// Update is called once per frame
void Update()
{
// we want to update thrownDice once per throw, otherwise we will get new references to the dice each frame:
if (Dice.rolling && !newThrow) {
newThrow = true;
}
if (!Dice.rolling && newThrow) {
thrownDice = Dice.GetAllDice();
newThrow = false;
}
scoreController.UpdateSelectedScore(thrownDice);
scoreController.UpdateSelectable(thrownDice);
// Handle input from dice being clicked:
if (Input.GetMouseButtonDown(0) && !Dice.rolling && !Parameters.AIPlaying) {
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, 1000.0f)) {
if (hit.transform && hit.transform.gameObject.tag == "Dice") {
RollingDie die = GetRollingDie(hit.transform.gameObject);
dieSelector.OnClick(die, thrownDice, scoreController.NumberOfDuplicates(die.value, thrownDice));
}
}
}
// update number of selected dice:
noOfSelected = 0;
if (!firstThrow) {
foreach (RollingDie die in thrownDice) {
if (die.selected) {
noOfSelected++;
}
}
}
if (!CanPlay() && !Dice.rolling) {
SetFail();
}
// If we have marked a fail wrongly, redeem it:
else if (failedThrow){
ResetFail();
}
// Sometimes there is a bug that markes a single die throw as fail before it has time to start rolling, this should stop that:
if (Dice.rolling && failedThrow) {
ResetFail();
}
}
public void SaveSelected() {
foreach(RollingDie die in thrownDice) {
if (die.selected) {
die.selected = false;
die.saved = true;
savedDice.SaveDie(die.value);
}
}
}
public void Roll() {
if (firstThrow) {
Dice.Clear();
RollDice(6);
firstThrow = false;
}
else if (noOfSelected > 0) {
noOfSaved += noOfSelected;
scoreController.handScore += scoreController.selectedScore;
SaveSelected();
Dice.SaveSelected();
Dice.Clear();
newThrow = true;
//if all are saved, throw all dice again:
if (noOfSaved == 6) {
noOfSaved = 0;
savedDice.ClearSaved();
}
RollDice(6 - noOfSaved);
}
}
private void RollDice(int noDice) {
Dice.Roll(noDice + "d6", "d6-" + diceColor + (dots ? "-dots" : ""), rollPosition, rollForce);
}
public void TurnReset() {
scoreController.handScore = 0;
scoreController.selectedScore = 0;
noOfSaved = 0;
Dice.Clear();
firstThrow = true;
failedThrow = false;
savedDice.ClearSaved();
}
public int GetHandScore() {
return scoreController.handScore + scoreController.selectedScore;
}
public int GetSelectedScore() {
return scoreController.selectedScore;
}
public void SelectSelectable() {
foreach(RollingDie die in thrownDice) {
if (die.selectable) {
dieSelector.SelectDie(die, scoreController.NumberOfDuplicates(die.value, thrownDice), thrownDice);
}
}
}
private RollingDie GetRollingDie(GameObject gameObject) {
foreach (RollingDie die in thrownDice) {
if (die.gameObject == gameObject) {
return die;
}
}
return null;
}
public bool CanBank(int playerScore) {
if (firstThrow || dieSelector.NumberOfSelected(thrownDice) == thrownDice.Count || failedThrow) {
return false;
}
if (dieSelector.AllSelected(thrownDice)) {
return scoreController.CanBank(playerScore);
}
return false;
}
private bool CanPlay() {
if (firstThrow) {
return true;
}
foreach (RollingDie die in thrownDice) {
// When we encounter a single die that is selectable we can return true:
if (die.selectable) {
return true;
}
}
return false;
}
private void SetFail() {
dieSelector.MarkFail(thrownDice);
failedThrow = true;
}
private void ResetFail() {
dieSelector.UnmarkFail(thrownDice);
failedThrow = false;
}
public bool FailedThrow() {
return failedThrow;
}
public bool CanRoll() {
if (Dice.rolling) {
return false;
}
// If we have not rolled any dice yet, we can roll
if (firstThrow)
{
return true;
}
// We have to save at least one dice each roll:
if (dieSelector.NumberOfSelected(thrownDice) == 0) {
return false;
}
if (failedThrow)
{
return false;
}
return true;
}
}