-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.gd
188 lines (168 loc) · 4.79 KB
/
Game.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
extends Node2D
# TODO:
# Effects! Swing, Onbeat/Offbeat, Laugh track(?), MORE COWBELLS
var touchOS = ["Android", "iOS", "BlackBerry 10"]
var keyOS = ["Windows", "X11", "OSX", "HTML5"]
var os
var thrown:bool = false
var cowbell:bool = true
var level = 0
var tempo = 120
var step:float = 60*1000/tempo
var interval:float = 60*1000/120/5
var effect = 0
var catchTime
var gotInput = false
var checkedInput = false
var beatTime = 0
var lockInput = false
var holding = false
var lastPos:Vector2
var effectsName = ["","Light Swing","Swing","Heavy Swing","Onbeat only","Offbeat only","Cowbells","*Laughter*"]
var effectsWeight = [10,10,10,30,15,15,30]
var rng = RandomNumberGenerator.new()
signal throw(step)
signal catch(success)
signal catch_fail
signal update_record(record)
# Called when the node enters the scene tree for the first time.
func _ready():
rng.seed = OS.get_unix_time()
var save_game = File.new()
if not save_game.file_exists("user://record.save"):
return # Error! We don't have a save to load.
save_game.open("user://record.save", File.READ)
emit_signal("update_record",save_game.get_8())
os = OS.get_name()
if touchOS.find(os) >= 0:
#$Instructions.rect_position = Vector2(71,98)
$Instructions.text = "Flick it!"
elif keyOS.find(os) >= 0:
#$Instructions.rect_position = Vector2(59,98)
$Instructions.text = "Press Space!"
save_game.close()
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(_delta):
if gotInput and not checkedInput:
checkInput()
if thrown:
if beatTime+interval < OS.get_ticks_msec():
checkedInput = false
emit_signal("catch_fail")
level = 0
cowbell = true
tempo = 120
step = 60*1000/tempo
effect = generateEffect()
thrown = false
gotInput = false
lockInput = true
print("GAME OVER")
func generateEffect():
if $EffectQuestion.pressed:
var totalWeight = 0
for weight in effectsWeight:
totalWeight += weight
var randomEffect = rng.randi_range(0,totalWeight)
var actualWeight = 0
print(randomEffect)
for number in range(0,effectsWeight.size()):
actualWeight += effectsWeight[number]
if actualWeight>=randomEffect:
print(number)
return number+1
else:
return 0
func _input(event):
if not lockInput:
if event is InputEventScreenTouch:
if not holding and thrown:
handleInput("touch")
lastPos = event.position
lastPos = event.position
if holding:
holding = false
else:
holding = true
if event is InputEventScreenDrag:
var firstPos = lastPos
lastPos = event.position
var travelDistance = firstPos.y - lastPos.y
print(str(travelDistance))
if travelDistance > 20:
handleInput("flick")
func _unhandled_input(event):
if Input.is_key_pressed(KEY_SPACE) and not lockInput and not event.is_echo():
handleInput("kb")
func handleInput(type):
if not thrown and type != "touch":
if touchOS.find(os) >= 0 and $Instructions.visible:
#$Instructions.rect_position = Vector2(26,98)
$Instructions.text = "Tap to catch the coin."
elif keyOS.find(os) >= 0 and $Instructions.visible:
#$Instructions.rect_position = Vector2(29,81)
$Instructions.text = "Use Space again\nto catch the coin."
$EffectQuestion.visible = false
$EffectQuestion.disabled = true
$Effect.text = effectsName[effect]
$Effect.visible = true
$Hand.play("throw")
beatTime = OS.get_ticks_msec()+step*6
$Coin.play()
if not cowbell:
$Screen.visible = false
$beat.playBeat(tempo,level, effect)
level+=1
else:
$Screen.visible = true
$beat.playCowbell(tempo)
cowbell = false
emit_signal("throw",step)
thrown = true
yield(get_tree().create_timer(2), "timeout")
$Instructions.visible = false
elif thrown and type != "flick":
if not gotInput:
gotInput = true
if checkedInput:
emit_signal("catch",false)
gotInput = false
elif thrown and type == "flick":
if checkedInput:
checkedInput = false
$Hand.play("throw")
func checkInput():
catchTime = OS.get_ticks_msec()
checkedInput = true
gotInput = false
if beatTime-interval < catchTime or beatTime+interval < catchTime:
checkedInput = false
gotInput = false
thrown = false
emit_signal("catch",true)
if level > 3:
level = 0
cowbell = true
tempo -= tempo/10
step = 60*1000/tempo
effect = generateEffect()
elif beatTime-interval > catchTime:
emit_signal("catch",false)
func _on_Animations_animation_finished(anim_name):
if anim_name == "Search":
$EffectQuestion.visible = true
$EffectQuestion.disabled = false
$Effect.visible = false
lockInput = false
$Screen.visible = true
$Screen.reset()
func _on_EffectQuestion_toggled(button_pressed):
if button_pressed:
effect = generateEffect()
else:
effect = 0
func _on_ScoreBox_new_record(record):
var save_game = File.new()
save_game.open("user://record.save", File.WRITE)
save_game.store_8(record)
save_game.close()