-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameManager.gd
159 lines (127 loc) · 3.89 KB
/
GameManager.gd
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
extends Node2D
@export var actualGameScene = preload("res://Gameplay.tscn")
@export var gradients: Array[Texture]
@export var loseScene= preload("res://UI/dead_screen.tscn")
@onready var animations = $AnimationPlayer
var currentIteration = 0
var currentDifficultyLevel = 0
var waitForClick = false
@onready
var currentGradient = gradients[0]
@onready
var nextGradient = gradients[0]
var loseSceneInstance=null
var activeGameInstance : GamePlay=null
func _ready():
restartLevel()
signal mouseClicked
signal treeHitFrame
func _input(event):
if event.is_action_pressed("mouse_down") and waitForClick:
mouseClicked.emit()
func restartLevel():
currentGradient = nextGradient
nextGradient = gradients.pick_random()
currentIteration += 1
currentDifficultyLevel += 1
removeGameplay()
# Create new game instance
activeGameInstance = actualGameScene.instantiate() as GamePlay
activeGameInstance.name = "GAMEPLAY"
activeGameInstance.difficultyLevel = currentDifficultyLevel
activeGameInstance.win.connect(_on_game_ground_reached)
activeGameInstance.dead.connect(_on_game_lost)
activeGameInstance.setCurrentGradient(currentGradient)
activeGameInstance.setNextGradient(nextGradient)
treeHitFrame.connect(activeGameInstance.hitTree)
# Start game
add_child(activeGameInstance)
animations.animation_finished.connect(
func(_a):
# pass,
waitAndStart(activeGameInstance),
CONNECT_ONE_SHOT
)
animations.play("fade_out")
func waitAndStart(game, time=2):
animations.animation_finished.connect(
tree_falling,
CONNECT_ONE_SHOT
)
get_tree().create_timer(time).timeout.connect(
func(): animations.play("hak"),
CONNECT_ONE_SHOT
)
func tree_falling(_a):
GameState.change(GameState.STATE.PLAYING)
activeGameInstance.startGame()
get_tree().create_timer(3).timeout.connect(
func():
$AnimationPlayer/chop2.play()
$AnimationPlayer/treefalls.play(),
CONNECT_ONE_SHOT
)
func removeGameplay():
var gamePlay = get_node_or_null("GAMEPLAY")
if gamePlay != null:
gamePlay.name = "WILL_BE_REMOVED" # Ensure no clash with new game instance name
gamePlay.stopGame()
gamePlay.queue_free()
func _on_game_ground_reached():
$GAMEPLAY.stopGame()
print("Stukske winnaar")
# Start de victory animaties, nen welgemeende proficiat
waitForClick = true
$ClickToContinue.visible = true
mouseClicked.connect(
func():
restartGracefully()
waitForClick = false
$ClickToContinue.visible = false,
CONNECT_ONE_SHOT
)
# Als animaties klaar zijn dan moogt ge herstarten
func restartGracefully():
animations.animation_finished.connect(
func(_anim):
get_tree().create_timer(1).timeout.connect(
func():
restartLevel()
),
CONNECT_ONE_SHOT
)
animations.play_backwards("fade_out")
func restartAfterLose():
#Maybe reset the entire game in this function isntead of soft reseting like in other wins?
currentIteration = 0
currentDifficultyLevel = 0
remove_child(loseSceneInstance)
loseSceneInstance=null
restartLevel()
func _on_game_lost(reason: Death.REASON = Death.REASON.UNKNOWN):
GameState.change(GameState.STATE.STOPPED)
# Start de death animaties + ga terug naar start, je krijgt geen 2 miljoen
#Wait 3 sec after game loss to play lose animations
activeGameInstance.hidePlayer()
get_tree().create_timer(3).timeout.connect(
func():
#Zie hier, de befaamde lambda in nen lambda
animations.animation_finished.connect(
func(_a):
# animations.play("fade_out")
loseSceneInstance=loseScene.instantiate()
loseSceneInstance.setScore(currentIteration)
add_child(loseSceneInstance)
loseSceneInstance.connect("restart",restartAfterLose)
animations.play("fade_out")
get_tree().create_timer(.05).timeout.connect(removeGameplay)
print("Stukske loser"),
CONNECT_ONE_SHOT
)
animations.play_backwards("fade_out")
,
CONNECT_ONE_SHOT
)
# Als de animaties klaar zijn, dan moogt ge terug naar main menu
func hitTree():
treeHitFrame.emit()