-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.cs
253 lines (216 loc) · 6.97 KB
/
Main.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
using Godot;
using System;
using System.Collections;
using System.Collections.Generic;
public partial class Main : Control
{
const int MaxTurnsCompleted = 13;
private Godot.RandomNumberGenerator godotRandom = new Godot.RandomNumberGenerator();
private bool goRight = true;
private Vector2 prevPos = new Vector2();
private bool isRolling = false;
private AnimatedSprite2D male1;
private AnimatedSprite2D wizard;
private AnimatedSprite2D sheep;
private PathFollow2D wizardLoc;
private PathFollow2D male1Loc;
private Button rollButton;
private LeftPanel leftPanel;
private MiddlePanel middlePanel;
private RightPanel rightPanel;
private TextureButton soundButton;
private DicePad dicePad;
private CanvasModulate moonlightModulate;
private CanvasLayer fireworksLayer;
private int _totalScore = 0;
private int _bonusScore = 0;
public event EventHandler MustRoll;
public event EventHandler RollsCompleted;
public event EventHandler FirstRoll;
public int RollsRemaining { get; set; } = 3;
public int TurnsCompleted { get; set; } = 0;
public bool UtopzeeScored { get; set; } = false;
public (int[], bool) GetInstanceCounts() => DicePad.GetInstanceCounts();
public IList<Dice> DiceList => DicePad.DiceList;
public bool CanLockDice => RollsRemaining > 0 && RollsRemaining != 3;
public DicePad DicePad => dicePad;
public int BonusScore
{
get { return _bonusScore; }
set
{
_bonusScore = value;
GetNode<Label>("RightPanel/BonusLabel/Label").Text = _bonusScore.ToString();
}
}
public int TotalScore
{
get { return _totalScore; }
set
{
_totalScore = value;
GetNode<Label>("ScoreBanner/ScoreLabel").Text = $"Score: {value}";
}
}
public void ResetForNextRoll(int additionalScore, bool addBonusUtopzee = false)
{
TotalScore += additionalScore;
RollsRemaining = Constants.DefaultRollsRemaining;
if (UtopzeeScored && addBonusUtopzee)
{
TotalScore += 100;
BonusScore += 100;
}
TurnsCompleted++;
ForceRollNext();
UnlockDice();
if (TurnsCompleted == MaxTurnsCompleted)
{
GameOver();
}
}
public void GameOver()
{
rollButton.Disabled = true;
fireworksLayer.Visible = true;
moonlightModulate.Visible = true;
GetNode<Label>("FireworksLayer/FinalScoreBanner/ScoreLabel").Text = $"Score: {TotalScore}";
}
private void ForceRollNext()
{
MustRoll?.Invoke(this, new EventArgs());
}
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
male1 = GetNode<AnimatedSprite2D>("Male1");
wizard = GetNode<AnimatedSprite2D>("Wizard");
sheep = GetNode<AnimatedSprite2D>("Sheep");
rollButton = GetNode<Button>("RollButton");
soundButton = GetNode<TextureButton>("SoundButton");
leftPanel = GetNode<LeftPanel>("LeftPanel");
leftPanel.MainScene = this;
middlePanel = GetNode<MiddlePanel>("MiddlePanel");
middlePanel.MainScene = this;
rightPanel = GetNode<RightPanel>("RightPanel");
rightPanel.MainScene = this;
dicePad = GetNode<DicePad>("DicePad");
moonlightModulate = GetNode<CanvasModulate>("MoonlightModulate");
fireworksLayer = GetNode<CanvasLayer>("FireworksLayer");
male1Loc = GetNode<PathFollow2D>("Male1Path/PathFollow2D");
wizardLoc = GetNode<PathFollow2D>("WizardPath/PathFollow2D");
ForceRollNext();
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta)
{
rollButton.Text = $"{RollsRemaining} Rolls";
rollButton.Disabled = (RollsRemaining <= 0);
if (CanLockDice)
{
for (int i = 1; i <= 5; i++)
{
if(Input.IsActionJustPressed("Lock" + i))
{
GetNode<Sprite2D>("DicePad/Lock" + i).Visible = !GetNode<Sprite2D>("DicePad/Lock" + i).Visible;
}
}
}
if (isRolling)
{
male1.Animation = "cheer";
wizard.Animation = "cheer";
return;
}
delta = delta / 4.0;
wizardLoc.ProgressRatio += (float) delta / 4.0f;
if (goRight)
{
male1.Animation = "right";
float newLoc = male1Loc.ProgressRatio + (float) delta;
if (newLoc >= 1.0f)
{
goRight = false;
male1Loc.ProgressRatio = 1.0f;
}
else
{
male1Loc.ProgressRatio += (float) delta;
}
}
else
{
male1.Animation = "left";
float newLoc = male1Loc.ProgressRatio - (float) delta;
if (newLoc <= 0.0f)
{
goRight = true;
male1Loc.ProgressRatio = 0.0f;
}
else
{
male1Loc.ProgressRatio -= (float) delta;
}
}
if (prevPos[0] < wizardLoc.Position[0])
{
wizard.Animation = "right";
}
else
{
wizard.Animation = "left";
}
prevPos = wizardLoc.Position;
male1.Position = male1Loc.Position;
wizard.Position = wizardLoc.Position;
}
public void OnTimer()
{
for (int i = 1; i < 6; i++)
{
if (!GetNode<Sprite2D>("DicePad/Lock" + i).Visible)
{
GetNode<AnimatedSprite2D>("DicePad/Die" + i).Frame = godotRandom.RandiRange(0, 5);
//GetNode<AnimatedSprite2D>("DicePad/Die" + i).Frame = GetRandomNumber(random, 0, 6);
}
}
}
public void OnRollClicked()
{
GetNode<Timer>("DiceTimer").Start();
GetNode<Timer>("RollTimer").Start();
sheep.Animation = "cheer";
isRolling = true;
rollButton.Disabled = true;
RollsRemaining--;
}
public void OnRollTimer()
{
sheep.Animation = "idle";
GetNode<Timer>("DiceTimer").Stop();
isRolling = false;
if (RollsRemaining == 0)
{
RollsCompleted?.Invoke(this, new EventArgs());
}
if (RollsRemaining == 2)
{
FirstRoll?.Invoke(this, new EventArgs());
}
if (!CanLockDice)
{
UnlockDice();
}
}
private void UnlockDice()
{
for (int i = 1; i <= 5; i++)
{
GetNode<Sprite2D>("DicePad/Lock" + i).Visible = false;
}
}
public void OnSoundClicked()
{
GetNode<AudioStreamPlayer>("BackgroundMusic").Playing = !GetNode<AudioStreamPlayer>("BackgroundMusic").Playing;
}
}