-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCatching.gd
342 lines (306 loc) · 10.9 KB
/
Catching.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
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
extends Spatial
# for 3D
onready var cameraL:Camera = $ViewportContainerL/ViewportL/CameraL
onready var cameraR:Camera = $ViewportContainerR/ViewportR/CameraR
onready var CHIP:Spatial = $CHIP
onready var CHIP_Animation:AnimationPlayer = $CHIP/chip/AnimationPlayer
onready var DALE:Spatial = $DALE
onready var DALE_Animation:AnimationPlayer = $DALE/dale/AnimationPlayer
onready var DONALD:Spatial = $DONALD
onready var DONALD_Animation:AnimationPlayer = $DONALD/DonaldDuck/AnimationPlayer
onready var FALLINGS:Spatial = $FALLINGS
onready var templates:Spatial = $Templates
onready var alicorn:Spatial = $Templates/alicorn
onready var popcorn:Spatial = $Templates/popcorn
onready var heart:Spatial = $Templates/heart
onready var star:Spatial = $Templates/star
onready var countCHIP:Button = $countCHIP
onready var countDALE:Button = $countDALE
onready var countDONALD:Button = $countDONALD
onready var messageText:Button = $messageText
onready var audioAlicorn:AudioStreamPlayer = $audioAlicorn
onready var audioPopcorn:AudioStreamPlayer = $audioPopcorn
onready var audioStar:AudioStreamPlayer = $audioStar
onready var audioHeart:AudioStreamPlayer = $audioHeart
const vx:Vector3 = Vector3(1,0,0)
const vz:Vector3 = Vector3(0,0,1)
var positionCHIP:Vector3 = Vector3(-2,0,0)
var directionCHIP:Vector3 = Vector3.ZERO
var speedCHIP:float = 3
var positionDALE:Vector3 = Vector3(2,0,0)
var directionDALE:Vector3 = Vector3.ZERO
var speedDALE:float = 3
var positionDONALD:Vector3 = Vector3(-5,0,-5)
var directionDONALD:Vector3 = Vector3(1,0,1)
var speedDONALD:float = 2
const arenaSize:float = 5.5
var fallSpeed:Vector3
var fallingObjects:Array = []
var popRate:float
var currentRate:float = 0.0
const catchCountThreshold:int = 20
var winTimeout:float = -1
var catchCountCHIP:int = 0
var catchCountDALE:int = 0
var catchCountDONALD:int = 0
# Called when the node enters the scene tree for the first time.
func _ready():
var _err = get_tree().get_root().connect("size_changed", self, "resize")
randomize()
templates.visible = false
CHIP.visible = true
DALE.visible = true
DONALD.visible = true
CHIP_Animation.play("idle")
DALE_Animation.play("idle")
DONALD_Animation.play("idle")
set_process_input(true)
resize()
reset()
func reset():
winTimeout = -1
popRate = 1
fallSpeed = Vector3(0,2,0)
positionCHIP = Vector3(-2,0,0)
positionDALE = Vector3(2,0,0)
positionDONALD = Vector3(-5,0,-5)
directionCHIP = Vector3.ZERO
directionDALE = Vector3.ZERO
directionDONALD = Vector3.ZERO
catchCountCHIP = 0
catchCountDALE = 0
catchCountDONALD = 0
fallingObjects.clear()
delete_children(FALLINGS)
updateText()
setMessageText("START !!!", 3)
func updateText():
countCHIP.text = "CHIP: " + str(catchCountCHIP)
countDALE.text = "DALE: " + str(catchCountDALE)
countDONALD.text = "DONALD: " + str(catchCountDONALD)
var messageTextDissapear:float = -1
func setMessageText(s:String, seconds:float):
messageText.text = s
messageTextDissapear = seconds
func _input(_ev):
#reset
if Input.is_key_pressed(KEY_P): reset()
# no winner yet
if winTimeout < 0:
#dale movements
directionDALE = Vector3.ZERO
if Input.is_action_pressed("dale_left"): directionDALE -= vx
if Input.is_action_pressed("dale_right"): directionDALE += vx
if Input.is_action_pressed("dale_up"): directionDALE -= vz
if Input.is_action_pressed("dale_down"): directionDALE += vz
if directionDALE.length() > 0.2:
directionDALE = directionDALE.normalized()
#chip movements
directionCHIP = Vector3.ZERO
if Input.is_action_pressed("chip_left"): directionCHIP -= vx
if Input.is_action_pressed("chip_right"): directionCHIP += vx
if Input.is_action_pressed("chip_up"): directionCHIP -= vz
if Input.is_action_pressed("chip_down"): directionCHIP += vz
if directionCHIP.length() > 0.2:
directionCHIP = directionCHIP.normalized()
#donald movement to the first non-taken element
if fallingObjects.size() > 0:
var lastObject:Spatial = fallingObjects[fallingObjects.size()-1]
var distVector = (lastObject.translation - positionDONALD)
distVector.y = 0
directionDONALD = distVector.normalized()
if Input.is_action_just_pressed("3d_toggle"):
OS.window_fullscreen = not OS.window_fullscreen
if OS.window_fullscreen:
cameraL.translate(Vector3(-0.16,0,0))
cameraR.translate(Vector3( 0.16,0,0))
else:
cameraL.translate(Vector3(0.16,0,0))
cameraR.translate(Vector3(-0.16,0,0))
resize()
# drop speed
if Input.is_key_pressed(KEY_EQUAL):
fallSpeed += Vector3(0,0.1, 0)
if Input.is_key_pressed(KEY_MINUS):
if fallSpeed.y > 2:
fallSpeed -= Vector3(0, 0.1, 0)
# drop ratio
if Input.is_key_pressed(KEY_K):
popRate -= 0.1
if Input.is_key_pressed(KEY_L):
if popRate < 3: popRate += 0.1
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
hideMessageText(delta)
# we have a winner
if winTimeout > 0:
winTimeout -= delta
if winTimeout < 0: reset()
if catchCountCHIP + catchCountDALE >= catchCountThreshold:
CHIP_Animation.play("win")
DALE_Animation.play("win")
DONALD_Animation.play("lose")
else:
CHIP_Animation.play("lose")
DALE_Animation.play("lose")
DONALD_Animation.play("win")
else:
# normal game cycle
movement(delta)
generateObjects(delta)
scrollObjects(delta)
checkColisions()
func movement(delta):
#DALE
var newPositionDALE = positionDALE + delta * speedDALE * directionDALE
if abs(newPositionDALE.x) < arenaSize and abs(newPositionDALE.z) < arenaSize:
positionDALE = newPositionDALE
DALE.transform = Transform.IDENTITY
DALE.translate(positionDALE)
DALE.rotate_y(atan2(directionDALE.x, directionDALE.z))
if directionDALE.length() > 0.2:
DALE_Animation.play("run")
else:
DALE_Animation.play("idle")
#CHIP
var newPositionCHIP = positionCHIP + delta * speedCHIP * directionCHIP
if abs(newPositionCHIP.x) < arenaSize and abs(newPositionCHIP.z) < arenaSize:
positionCHIP = newPositionCHIP
CHIP.transform = Transform.IDENTITY
CHIP.translate(positionCHIP)
CHIP.rotate_y(atan2(directionCHIP.x, directionCHIP.z))
if directionCHIP.length() > 0.2:
CHIP_Animation.play("run")
else:
CHIP_Animation.play("idle")
#DONALD
directionDONALD = directionDONALD.normalized()
var newPositionDONALD = positionDONALD + delta * speedDONALD * directionDONALD
if abs(newPositionDONALD.x) < arenaSize and abs(newPositionDONALD.z) < arenaSize:
positionDONALD = newPositionDONALD
DONALD.transform = Transform.IDENTITY
DONALD.translate(positionDONALD)
DONALD.rotate_y(atan2(directionDONALD.x, directionDONALD.z))
if directionDONALD.length() > 0.2:
DONALD_Animation.play("run")
else:
DONALD_Animation.play("idle")
func hideMessageText(delta):
if messageTextDissapear > 0:
messageTextDissapear -= delta
var rem = fmod(3 * messageTextDissapear,1)
var color = Color(1, rem, rem)
messageText.add_color_override("font_color", color)
if messageTextDissapear < 0:
messageText.text = ""
func sort_nan(a, b):
return (a < b or b != b)
func checkColisions():
# check bone
for ix in range(fallingObjects.size()-1, -1, -1):
var chipWin = hasColision(positionCHIP, fallingObjects[ix].translation)
var daleWin = hasColision(positionDALE, fallingObjects[ix].translation)
var donaldWin = hasColision(positionDONALD, fallingObjects[ix].translation)
if chipWin == chipWin or daleWin == daleWin or donaldWin == donaldWin:
FALLINGS.remove_child(fallingObjects[ix])
fallingObjects.remove(ix)
# play coin
audioAlicorn.play()
# sort dostances
var distances: Array = [chipWin, daleWin, donaldWin]
distances.sort_custom(self, "sort_nan")
var win:float = distances[0]
if win == win:
# using that NaN is false for all operations, except NaN != NaN
if chipWin == win:
catchCountCHIP += 1
elif daleWin == win:
catchCountDALE += 1
elif donaldWin == win:
catchCountDONALD += 1
updateText()
# we have winner
if catchCountCHIP + catchCountDALE >= catchCountThreshold:
winTimeout = 6
setMessageText("CHIP & DALE are WINNERS !!!", winTimeout)
elif catchCountDONALD >= catchCountThreshold:
winTimeout = 6
setMessageText("DONALD is a WINNER !!!", winTimeout)
# face the camera on winning/loosing
if winTimeout > 0:
CHIP.transform = Transform.IDENTITY
CHIP.translate(positionCHIP)
CHIP.rotate_y(0)
DALE.transform = Transform.IDENTITY
DALE.translate(positionDALE)
DALE.rotate_y(0)
DONALD.transform = Transform.IDENTITY
DONALD.translate(positionDONALD)
DONALD.rotate_y(0)
func generateObjects(delta):
var obj = null
currentRate += delta
if currentRate > popRate:
currentRate = 0
var chance:float = 4 * randf()
if chance > 3: obj = alicorn.duplicate()
elif chance > 2: obj = popcorn.duplicate()
elif chance > 1: obj = star.duplicate()
else: obj = heart.duplicate()
obj.rotate_y(randf()*PI)
fallingObjects.push_front(obj)
if obj != null:
var angle:float = 2*PI*randf()
var length:float = arenaSize * randf()
obj.translation = Vector3(length*cos(angle),10, length*sin(angle))
obj.visible = true
FALLINGS.add_child(obj)
func scrollObjects(delta):
# scroll objects
var scroll:Vector3 = - delta * fallSpeed
for obj in fallingObjects:
obj.translation += scroll
if obj.translation.y <= 1: obj.translation.y = 1
func hasColision(position:Vector3, obj: Vector3):
if (obj.y - 1 < 1 ):
var absDist:float = position.distance_squared_to(obj)
if absDist < 1.2: return absDist
return NAN
func resize():
var viewport = get_tree().get_root()
var height = viewport.get_visible_rect().size.y
var width = viewport.get_visible_rect().size.x
var mid = int(width/2)
var D3D:bool = OS.window_fullscreen
if D3D:
$ViewportContainerL.margin_left = 0
$ViewportContainerL.margin_top = 0
$ViewportContainerL.margin_bottom = int(height/2)
$ViewportContainerL.margin_right = int(width/2)
$ViewportContainerL.rect_scale = Vector2(1, 2)
$ViewportContainerL/ViewportL.set_size_override(true, Vector2(width, height))
$ViewportContainerR.margin_left = mid
$ViewportContainerR.margin_top = 0
$ViewportContainerR.margin_bottom = int(height/2)
$ViewportContainerR.margin_right = width
$ViewportContainerR.rect_scale = Vector2(1, 2)
$ViewportContainerR/ViewportR.set_size_override(true, Vector2(width, height))
else:
$ViewportContainerL.margin_left = 0
$ViewportContainerL.margin_top = 0
$ViewportContainerL.margin_bottom = height
$ViewportContainerL.margin_right = width
$ViewportContainerL.rect_scale = Vector2(1.0, 1.0)
$ViewportContainerL/ViewportL.set_size_override(true, Vector2(width, height) )
$ViewportContainerR.margin_left = 0
$ViewportContainerR.margin_top = 0
$ViewportContainerR.margin_bottom = 0
$ViewportContainerR.margin_right = 0
$ViewportContainerR/ViewportR.set_size_override(true, Vector2(width, height) )
func apply_material_color(mesh_instance_node:MeshInstance, color:int):
var material = SpatialMaterial.new()
material.albedo_color = Color(color)
mesh_instance_node.set_material_override(material)
func delete_children(node):
for n in node.get_children():
node.remove_child(n)