-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTank.gd
279 lines (239 loc) · 5.94 KB
/
Tank.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
extends RigidBody2D
export (PackedScene) var Shell
export (PackedScene) var Box
export (int) var player
signal dead
signal zero
signal robot
const SMALL_ROTATE_RATIO = 0.2
const ROTATE_IMPULSE = 6000
const THROTTLE_IMPULSE = 600
const FIRE_IMPULSE = 60
const FIRE_SKEW = 40
const LOAD_TIME = 0.260
const SHOTS_PER_SEQUENCE = 3
const DAMAGE_RATIO = 1.4
const DESTROY_REWARD = 15
const SHOW_OFF_IMPULSE = 100
const FULL_LIFE = Color(0.1, 0.7, 0.1)
const HIGH_LIFE = Color(0.6, 0.7, 0.1)
const MEDIUM_LIFE = Color(0.9, 0.7, 0.1)
const LOW_LIFE = Color(0.9, 0.1, 0.1)
const OFF = Color(0.1, 0.1, 0.1, 0)
const MAX_LIFE = 32
var life = MAX_LIFE
var buttonDuration = 0
var loading = false
var shot = 0
var points = 30
var lastDelta = 0
var focused = false
var ready = false
var virgin = true
var springed = false
var bigShell = 0
var smallShells = 0
var robot = false
var others = []
var color
enum Type { HUMAN, ROBOT, REMOVE }
var type = Type.HUMAN
func _ready():
if player == 1:
color = Color(0.8, 0.6, 0)
if player == 2:
color = Color(0, 0.6, 0.9)
if player == 3:
color = Color(0.7, 0.4, 0.7)
$Sprite.modulate = color
$InitTimer.start()
$Remove.hide()
$Robot/Sprite.hide()
$Label.hide()
$Life.hide()
func _physics_process(delta):
if focused:
apply_torque_impulse(SHOW_OFF_IMPULSE)
if life < 0 or focused or not ready:
return
var buttonPressed = Input.is_action_pressed("p"+str(player)+"_button")
var rightPressed = Input.is_action_pressed("p"+str(player)+"_right")
var leftPressed = Input.is_action_pressed("p"+str(player)+"_left")
var amplitude = SMALL_ROTATE_RATIO if Input.is_action_just_pressed("p"+str(player)+"_right") or \
Input.is_action_just_pressed("p"+str(player)+"_left") else 1
var n = 0
n += 1 if buttonPressed else 0
n += 1 if leftPressed else 0
n += 1 if rightPressed else 0
if n > 1:
return
if n == 1:
virgin = false
if rightPressed:
rotation_impulse(delta, true, amplitude)
return
if leftPressed:
rotation_impulse(delta, false, amplitude)
return
if buttonPressed:
buttonDuration += delta
if buttonDuration > 0.15:
throttle(delta)
else:
if buttonDuration > 0 && buttonDuration < 0.15:
fire()
buttonDuration = 0
func throttle(delta):
apply_impulse(Vector2(0,0), transform.basis_xform(Vector2(0,-THROTTLE_IMPULSE*delta)))
func rotation_impulse(delta, inverse, amplitude=1):
if inverse:
apply_torque_impulse(ROTATE_IMPULSE*delta*amplitude)
else:
apply_torque_impulse(-ROTATE_IMPULSE*delta*amplitude)
func fire(angle=0):
if not loading and points > 0 or bigShell > 0 or smallShells > 0:
var s = Shell.instance()
if bigShell > 0:
s.setBig()
bigShell -= 1
elif smallShells > 0:
s.setSmall()
smallShells -= 1
if angle == 0:
fire(-PI/28)
fire(PI/28)
else:
shot += 1
addPoints(-1)
s.transform = transform * $Muzzle.transform
s.rotate(angle)
get_parent().add_child(s)
$Muzzle/MuzzleFire/Timer.start_default()
s.setSource(self)
if shot == SHOTS_PER_SEQUENCE:
showPoints()
$LoadTimer.start(2 * LOAD_TIME)
shot = 0
else:
$LoadTimer.start(LOAD_TIME)
# recoil
if not springed and angle == 0 or bigShell:
apply_impulse(Vector2(FIRE_SKEW*randf()-FIRE_SKEW/2,FIRE_SKEW*randf()-FIRE_SKEW/2),
transform.basis_xform(Vector2(0, FIRE_IMPULSE*randf())))
loading = true
func hit(damage, source):
virgin = false
if life < 0:
return
life -= damage * DAMAGE_RATIO
showLife()
if life < 0:
source.addPoints(DESTROY_REWARD)
die()
func showLife():
var color
color = FULL_LIFE if life == MAX_LIFE else OFF
$Life/Full.modulate = color
if color == OFF:
color = HIGH_LIFE if life > MAX_LIFE*0.75 else OFF
$Life/High.modulate = color
if color == OFF:
color = MEDIUM_LIFE if life > MAX_LIFE*0.5 else OFF
$Life/Medium.modulate = color
if color == OFF:
color = LOW_LIFE
$Life/Low.modulate = color
$Life.show()
$Life/Fade.start()
func addLife(ratio):
life = min(MAX_LIFE, life + ratio*MAX_LIFE)
showLife()
func addPoints(delta):
var newPoints = max(0, points+delta)
lastDelta = points - newPoints
points = newPoints
if abs(lastDelta) > 1:
showPoints()
if points == 0:
emit_signal("zero")
func addBigShell():
bigShell += 2
func addSmallShells():
smallShells += 9
func showPoints(hide=true):
if(lastDelta == 0):
$Label.modulate = Color(1, 1, 1)
if(lastDelta < 0):
$Label.modulate = Color(0.7, 1, 0.7)
if(lastDelta > 0):
$Label.modulate = Color(1, 0.7, 0.7)
$Label.text = str(points)
$Label.show()
if hide:
$Label/Hide.start()
func focus():
focused = true
showPoints(false)
$Button.action = "reset"
$Camera2D/FocusTimer.start()
func _on_LoadTimer_timeout():
loading = false
func _on_Fade_timeout():
$Life.hide()
func addSpring():
springed = true
func setOthers(tanks):
others = tanks
func die(emit=true):
emit_signal("dead")
$Life.hide()
$Sprite.modulate = Color(0.2,0.2,0.2)
$Explosion.restart()
if points > 0 and emit:
var box = Box.instance()
box.shells = points / 3
box.global_position = global_position
get_parent().add_child(box)
box.collision_layer = 128
box.collision_mask = 128
box.apply_central_impulse(-transform.y * 300)
if virgin:
queue_free()
func _on_Hide_timeout():
if not focused:
$Label.hide()
func _on_FocusTimer_timeout():
$Camera2D.make_current()
func _on_InitTimer_timeout():
showLife()
showPoints()
ready = true
func _on_TankButton_pressed():
switchType()
func _on_MouseButton_pressed():
switchType()
func setType(newType):
robot = false
if newType == 1:
type = Type.ROBOT
robot = true
$Robot/Sprite.show()
$Sprite.modulate = Color(0.2, 0.7, 0.3)
elif newType == 2:
$Sprite.modulate = color
type = Type.REMOVE
$Robot/Sprite.hide()
$Remove.show()
elif newType == 0:
type = Type.HUMAN
$Remove.hide()
func switchType():
if virgin:
if type == Type.HUMAN:
setType(Type.ROBOT)
elif type == Type.ROBOT:
setType(Type.REMOVE)
elif type == Type.REMOVE:
setType(Type.HUMAN)
func updatePaths(paths):
$Robot.updatePaths(paths)