-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgressSystem.cs
More file actions
612 lines (506 loc) · 21.4 KB
/
Copy pathProgressSystem.cs
File metadata and controls
612 lines (506 loc) · 21.4 KB
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
using System;
using System.Collections.Generic;
using Godot;
using Range = Godot.Range;
public partial class ProgressSystem : CanvasLayer
{
private List<Achievement> playerAchievements = new List<Achievement>(); //we store all the achievements of the player
private Dictionary<Employee, List<Achievement>> allAchievements = new Dictionary<Employee, List<Achievement>>(); //all achievements of the game
private List<Employee> allEmployees = new List<Employee>();
public int TotalStars { get; set; }
private TextureProgressBar _starsProgressBar; //the circular progress bar
private ProgressBar _totalProgressBar; //the global progress bar
private AchievementDisplay _achievementDisplay; //shown when the player get an achievements
//audio stram for the progress bar and the stars
private AudioStreamPlayer _starsPlayer;
private AudioStreamPlayer _totalProgressPlayer;
private Label _starsLevel;
private List<string> _unlockableEmployeeOrder = new List<string>();
[Export]
public int MaxStarsValue { get; set; } = 6; //we can put back 18 if we need
[Export]
private int MaxProgressValue { get; set; } = 100; //not used anymore
[Export]
private float WaitTimeBeforeLevelUp { get; set; } = 1f;
private float WaitTimeBeforeNextAchievement; //by default this value will be the time to display one achievement + fade in time + fade out time
private int _currentLevel = 0; //the level that will be shown at the top left corner. The level will increase when the star will be full
private Queue<Achievement> achievementQueue = new Queue<Achievement>();
private bool isProcessingAchievements;
private int _currentAmountsOfStars;
//these two variables are used to avoid concurrent access
private bool isTweening;
private Queue<(int stars, Range node)> animationQueue = new Queue<(int stars, Range node)>();
private GlobalSignals _globalSignals;
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
_totalProgressBar = GetNode<ProgressBar>("TotalProgress");
_starsProgressBar = GetNode<TextureProgressBar>("StarsProgress");
_achievementDisplay = GetNode<AchievementDisplay>("AchievementDisplay");
_starsPlayer = GetNode<AudioStreamPlayer>("StarsPlayer");
_totalProgressPlayer = GetNode<AudioStreamPlayer>("TotalProgressPlayer");
_starsLevel = _starsProgressBar.GetNode<Label>("StarsLevel");
_globalSignals = GetNode<GlobalSignals>("../../GlobalSignals");
var employees = GetNode<Node2D>("../../Employees").GetChildren();
_starsLevel.Text = _currentLevel.ToString(); //we start at the level 1
_starsProgressBar.MaxValue = MaxStarsValue;
_totalProgressBar.MaxValue = MaxProgressValue;
_achievementDisplay.Visible = true;
WaitTimeBeforeNextAchievement = _achievementDisplay.AchievementDisplayTime + _achievementDisplay.AchievementFadeInTime + _achievementDisplay.AchievementFadeOutTime; ;
_globalSignals.NewWorkDone += IncreaseStar;
//this loop is used for the employess that are already in the game when we start it, not the one added by the rooms
foreach (var emp in employees)
{
//we store all employees and when the state of the employee change we will call CheckNewAchievements
if (emp is Employee empConv)
{
allEmployees.Add((Employee)emp);
empConv.EmployeeStateChanged += CheckNewAchievements;
empConv.CheckAchievement += CheckNewAchievements;
allAchievements[(Employee)emp] = new List<Achievement>();
AddAchievementsForOneEmployee(empConv);
}
}
}
private void IncreaseStar()
{
AnimateProgress(1, _starsProgressBar);
_currentLevel++;
TotalStars++;
_starsLevel.Text = _currentLevel.ToString();
GetTree().CreateTimer(5).Timeout += () =>
{
if (_currentLevel >= MaxStarsValue)
{
IncreaseLevel();
}
};
}
private void IncreaseLevel()
{
AnimateProgress(-(int)_starsProgressBar.Value, _starsProgressBar);
_starsLevel.Text = "?";
_globalSignals.EmitUnlockGlasses();
}
public void AddAchievementsForOneEmployee(Employee employee)
{
/**
* When an employee has been created by a room, we need to add the achievements associated
*/
allEmployees.Add(employee);
allAchievements[employee] = new List<Achievement>();
employee.EmployeeStateChanged += CheckNewAchievements;
employee.CheckAchievement += CheckNewAchievements;
if (employee.NameOfEmployee == "Musicien")
{
var musicien = (Musicien)employee;
var achievementMusicien = new Achievement(
"After the last breath, only the silence remains",
"The music has stopped for the first time",
40,
() => musicien.CurrentState == Employee.EmployeeState.NotWorking, // Condition to unlock the achievement
3
);
var achievementMusicien2 = new Achievement(
"Music is the language of emotions",
"The Musician has worked for the first time",
70,
() => musicien.getNumberOfTimeWorked() == 1,
1
);
var achievementMusicien3 = new Achievement(
"The Symphony has started",
"The Musician has worked for the second time",
70,
() => musicien.getNumberOfTimeWorked() == 2,
2
);
var achievementMusicien4 = new Achievement(
"You can feel it",
"The Musician has worked for the third time",
70,
() => musicien.getNumberOfTimeWorked() == 3,
3
);
allAchievements[musicien].Add(achievementMusicien);
allAchievements[musicien].Add(achievementMusicien2);
allAchievements[musicien].Add(achievementMusicien3);
allAchievements[musicien].Add(achievementMusicien4);
}
else if(employee.NameOfEmployee == "Painter")
{
var painter = (Painter)employee;
var achievementPainter = new Achievement(
"Roses are red, but this red is all yours!",
"The red color is back for the first time",
40,
() => painter.firstTimeGettingRed,
3
);
var achievementPainter2 = new Achievement(
"Why is the sky blue?",
"The blue color is back for the first time",
40,
() => painter.firstTimeGettingBlue,
3
);
var achievementPainter3 = new Achievement(
"Nature approves your choice",
"The green color is back for the first time",
40,
() => painter.firstTimeGettingGreen,
3
);
var achievementPainter4 = new Achievement(
"You can see in RGB!",
"You gaved back all the colors to the Painter at least one time",
40,
() => painter.firstTimeGettingRed && painter.firstTimeGettingBlue && painter.firstTimeGettingGreen,
3
);
var achievementPainter5 = new Achievement(
"When you say blue, let others mean red",
"You worked 1 times with the Painter, you unlocked a new color",
40,
() => painter.getNumberOfTimeWorked() == 1,
1
);
var achievementPainter6 = new Achievement(
"It was a little bit too hard alone, so we both held hands",
"You worked 2 times with the Painter, you unlocked a new color",
40,
() => painter.getNumberOfTimeWorked() == 2,
2
);
var achievementPainter7 = new Achievement(
"Only the two of us was a little bit sad, so we made a circle of three",
"You worked 3 times with the Painter, you unlocked a new color",
40,
() => painter.getNumberOfTimeWorked() == 3,
3
);
allAchievements[painter].Add(achievementPainter);
allAchievements[painter].Add(achievementPainter2);
allAchievements[painter].Add(achievementPainter3);
allAchievements[painter].Add(achievementPainter4);
allAchievements[painter].Add(achievementPainter5);
allAchievements[painter].Add(achievementPainter6);
allAchievements[painter].Add(achievementPainter7);
}
else if(employee.NameOfEmployee == "Security")
{
var security = (Security)employee;
var achievementSecurity1 = new Achievement(
"We have the best security ever",
"You worked with the Security for the first time",
40,
() => security.getNumberOfTimeWorked() == 1,
1
);
var achievementSecurity2 = new Achievement(
"We are in war!",
"The Security has stopped working for the first time",
40,
() => security.CurrentState == Employee.EmployeeState.NotWorking,
3
);
var achievementSecurity3 = new Achievement(
"She is starting to get very afraid",
"You haven't chased the frog for a long time",
40,
() => security.GetStopTimeWorking() >= 60,
3
);
var achievementSecurity4 = new Achievement(
"Playing it safe is the riskiest choice we can make",
"You have worked with the Security two time",
40,
() => security.getNumberOfTimeWorked() == 2,
2
);
var achievementSecurity5 = new Achievement(
"I am not afraid of frogs",
"You have worked with the Security three time",
40,
() => security.getNumberOfTimeWorked() == 3,
3
);
allAchievements[security].Add(achievementSecurity1);
allAchievements[security].Add(achievementSecurity2);
allAchievements[security].Add(achievementSecurity3);
allAchievements[security].Add(achievementSecurity4);
allAchievements[security].Add(achievementSecurity5);
}
else if(employee.NameOfEmployee == "Technician")
{
var technicien = (Technician)employee;
var achievementTechnicien1 = new Achievement(
"I am a vampire",
"You worked with the Technician for the first time",
40,
() => technicien.getNumberOfTimeWorked() == 1,
1
);
var achievementTechnicien2 = new Achievement(
"Where are you?",
"The Technician stopped working for the first time",
40,
() => technicien.CurrentState == Employee.EmployeeState.NotWorking,
3
);
var achievementTechnicien3 = new Achievement(
"Moonlight drowns out all but the brightest stars",
"You worked with the Technician for the second time",
40,
() => technicien.getNumberOfTimeWorked() == 2,
2
);
var achievementTechnicien4 = new Achievement(
"Give light, and the darkness will disappear of itself",
"You worked with the Technician for the third time",
40,
() => technicien.getNumberOfTimeWorked() == 3,
3
);
allAchievements[technicien].Add(achievementTechnicien1);
allAchievements[technicien].Add(achievementTechnicien2);
allAchievements[technicien].Add(achievementTechnicien3);
allAchievements[technicien].Add(achievementTechnicien4);
}
else if(employee.NameOfEmployee == "Accountant")
{
var marketing = (Accountant)employee;
var achievementMarketing1 = new Achievement(
" 1 + 1 = 3",
"The Accountant has stopped working for the first time",
40,
() => marketing.CurrentState== Employee.EmployeeState.NotWorking,
3
);
var achievementMarketing2 = new Achievement(
"You are better than me",
"You completed the accountant minigame one time",
40,
() => marketing.getNumberOfTimeWorked() == 1,
1
);
var achievementMarketing3 = new Achievement(
"You are WAY better than me",
"You completed the accountant minigame two time",
40,
() => marketing.getNumberOfTimeWorked() == 2,
2
);
var achievementMarketing4 = new Achievement(
"Could you replace me?",
"You completed the accountant minigame three time",
40,
() => marketing.getNumberOfTimeWorked() == 3,
3
);
allAchievements[marketing].Add(achievementMarketing1);
allAchievements[marketing].Add(achievementMarketing2);
allAchievements[marketing].Add(achievementMarketing3);
allAchievements[marketing].Add(achievementMarketing4);
}
else if(employee.NameOfEmployee == "Cook")
{
var cook = (Cook)employee;
var achievementCook1 = new Achievement(
"Let him cook",
"You completed the cook minigame for the first time",
40,
() => cook.getNumberOfTimeWorked() == 1,
1
);
var achievementCook2 = new Achievement(
"Where are my ingredients?",
"The Cook has stopped working for the first time",
40,
() => cook.CurrentState == Employee.EmployeeState.NotWorking,
3
);
var achievementCook3 = new Achievement(
"The fire is burning",
"You completed the cook minigame for the second time",
40,
() => cook.getNumberOfTimeWorked() == 2,
2
);
var achievementCook4 = new Achievement(
"No one is born a great cook",
"You completed the cook minigame for the third time",
40,
() => cook.getNumberOfTimeWorked() == 3,
3
);
allAchievements[cook].Add(achievementCook1);
allAchievements[cook].Add(achievementCook2);
allAchievements[cook].Add(achievementCook3);
allAchievements[cook].Add(achievementCook4);
}
}
private void AddNewEmployee()
{
if(_unlockableEmployeeOrder.Count <= 0)
{
return;
}
_unlockableEmployeeOrder.RemoveAt(0);
}
private void CheckNewAchievements(int newState, string employeeName)
{
/**
* Check if an achievements has been unlocked for the employee "employeeName"
*/
//we get the employee
var employee = allEmployees.Find(emp => emp.NameOfEmployee == employeeName) ?? throw new ArgumentException("The state of " + employeeName + " changed but it seems that the name is wrong, we can't check their achievements !");
var achivements = allAchievements[employee];
foreach (Achievement achievement in achivements)
{
//if the achievement is completed, we will add it to the queue
if (!playerAchievements.Contains(achievement) && achievement.IsCompleted())
{
achievementQueue.Enqueue(achievement);
}
}
//we show all the achievements that the player unlocked
ShowAllAchievements();
}
private void ShowAllAchievements()
{
/**
* Recursive method based on a queue. It will show all the achievements of the queue
*/
//when the queue is empty,we check if we should increase the level
if (achievementQueue.Count == 0)
{
return;
}
var achievement = achievementQueue.Dequeue();
if (!playerAchievements.Contains(achievement))
{
//the player got the achievement
achievement.Unlocked = true;
playerAchievements.Add(achievement); //we add it
//AnimateProgress(achievement.NumberOfStars, _starsProgressBar); //circular animation
_achievementDisplay.ShowAchievement(achievement);
_starsPlayer.Play(); //we play the sound
_currentAmountsOfStars += achievement.NumberOfStars;
}
//we show the next achievement after a short delay
GetTree().CreateTimer(WaitTimeBeforeNextAchievement).Timeout += () =>
{
if (achievementQueue.Count >= 0)
{
ShowAllAchievements();
}
};
//then we will check if the player can level up
}
private void AnimateProgress(int stars, Range node)
{
if (isTweening)
{
// if an animation is going, we add it in the queue
animationQueue.Enqueue((stars, node));
return;
}
StartTween(stars, node);
}
private void StartTween(int stars, Range node)
{
/**
* Start the tween animation for the number of stars
*/
isTweening = true;
var targetValue = node.Value + stars;
Tween tween = CreateTween();
tween.SetEase(Tween.EaseType.Out);
tween.SetTrans(Tween.TransitionType.Sine);
tween.TweenProperty(node, "value", targetValue, 1.0f);
tween.Finished += () =>
{
isTweening = false;
// we animate the next animation
if (animationQueue.Count > 0)
{
var nextAnimation = animationQueue.Dequeue();
StartTween(nextAnimation.stars, nextAnimation.node);
}
};
tween.Play();
}
public List<Achievement> GetUnlockedAchievements()
{
var unlockedNames = new List<Achievement>();
foreach (var achievement in playerAchievements)
{
achievement.Unlocked = true;
unlockedNames.Add(achievement);
}
return unlockedNames;
}
public List<Achievement> GetAllAchievements()
{
var list = new List<Achievement>
{
new Achievement("After the last breath, only the silence remains", 3),
new Achievement("Music is the language of emotions", 1),
new Achievement("The Symphony has started", 2),
new Achievement("You can feel it", 3),
new Achievement("Roses are red, but this red is all yours!", 1),
new Achievement("Why is the sky blue?", 1),
new Achievement("Nature approves your choice", 1),
new Achievement("You can see in RGB!", 3),
new Achievement("It was a little bit too hard alone, so we both held hands", 2),
new Achievement("Only the two of us was a little bit sad, so we made a circle of three", 3),
new Achievement("We have the best security ever", 1),
new Achievement("We are in war!", 1),
new Achievement("She is starting to get very afraid", 3),
new Achievement("I am not afraid of frogs", 3),
new Achievement("I am a vampire", 1),
new Achievement("Where are you?", 1),
new Achievement("1 + 1 = 3", 1),
new Achievement("You are better than me", 1),
new Achievement("You are WAY better than me", 2),
new Achievement("Could you replace me?", 3),
new Achievement("Let him cook", 1),
new Achievement("Where are my ingredients?", 1),
new Achievement("The fire is burning", 2),
new Achievement("No one is born a great cook", 3),
new Achievement("Moonlight drowns out all but the brightest stars", 2),
new Achievement("Give light, and the darkness will disappear of itself", 3),
new Achievement("Playing it safe is the riskiest choice we can make", 3)
};
foreach(var a in list)
{
if(playerAchievements.Contains(a))
{
a.Unlocked = true;
}
}
return list;
}
public List<Achievement> GetLockedAchievements()
{
/**
* Return the list of achievement that he player hasn't unlocked yet
*/
var list = new List<Achievement>();
var allAchievementsList = GetAllAchievements();
foreach(var achievement in allAchievementsList)
{
if(!playerAchievements.Contains(achievement))
{
list.Add(achievement);
}
}
return list;
}
public void PlayAchievement(Achievement achievement)
{
achievementQueue.Enqueue(achievement);
ShowAllAchievements();
}
}