-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.gd
160 lines (131 loc) · 3.13 KB
/
player.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
extends KinematicBody2D
const SCORES_FOR_STAR = 100
const GRAV_ACCEL = 6
const ACCEL_Y = 10
const ACCEL_X = 3
const DEACCEL_X = 1
const MAX_ACC_X = 400
var accel_y = 0
var accel_x = 0
var view_width = 0
var width = 0
var global
# shields
var shield
var shield_anim
var shield_count = 3
var hit_delay = 0
var max_hit_delay = 1 # delay between hits
# fuel
const MAX_FUEL = 100
var fuel_count = MAX_FUEL
# some force
var force_impulse = vec2(0,0)
func game_over():
var parent = get_parent()
set_fixed_process(false)
global.world.game_over()
var ex = preload("res://explosion.scn").instance()
global.world.add_child(ex)
ex.explode(get_global_pos())
parent.set_process(false)
get_node("/root/global").is_gameover = true
queue_free()
# kills player
func kill():
global.hud.set_shields(0)
game_over()
# adds external force
func add_impulse(impulse):
force_impulse += impulse
hit()
# hits player
func hit():
shield_anim.play("down")
shield_count -= 1
global.hud.set_shields(shield_count)
if shield_count < 1:
kill()
else:
global.world.play_sound("hit")
func add_shield():
if (shield_count < 3):
shield_count += 1
global.hud.set_shields(shield_count)
# adds fuel
func add_fuel():
fuel_count = MAX_FUEL
# picks goods
func pick_goods(goods):
if goods.is_star():
global.world.scores += SCORES_FOR_STAR
elif goods.is_shield():
add_shield()
elif goods.is_fuel():
add_fuel()
func _fixed_process(delta):
if is_colliding():
var collider = get_collider()
if collider != null && collider extends TileMap:
kill()
return
if hit_delay > max_hit_delay:
hit_delay = 0
hit()
hit_delay += delta
var force = vec2(0, 0)
var pos = get_pos()
var fly = Input.is_action_pressed("ui_up")
var right = Input.is_action_pressed("ui_right")
var left = Input.is_action_pressed("ui_left")
if fly && fuel_count > 0:
accel_y -= ACCEL_Y
get_node("ThrustBottom").set_emitting(true)
fuel_count -= delta
else:
get_node("ThrustBottom").set_emitting(false)
accel_y += GRAV_ACCEL
if right && fuel_count > 0:
if pos.x < view_width - width / 2:
get_node("ThrustLeft").set_emitting(true)
accel_x += ACCEL_X
fuel_count -= delta
else:
get_node("ThrustLeft").set_emitting(false)
if left && fuel_count > 0:
if pos.x > width / 2:
get_node("ThrustRight").set_emitting(true)
accel_x -= ACCEL_X
fuel_count -= delta
else:
accel_x = 0
else:
get_node("ThrustRight").set_emitting(false)
var s = sign(accel_x)
accel_x = (abs(accel_x) - DEACCEL_X) * s
if accel_x > MAX_ACC_X:
accel_x = MAX_ACC_X
if pos.x < width / 2:
accel_x = 0
pos.x = width / 2
set_pos(pos)
elif pos.x > view_width - width / 2:
accel_x = 0
pos.x = view_width - width / 2
set_pos(pos)
else:
force.y = accel_y * delta
force.x = accel_x * delta
# applies force
force += force_impulse
move(force)
force_impulse = force_impulse / 1.1
# decreas fuel
global.hud.set_fuel(fuel_count)
func _ready():
global = get_node("/root/global")
shield = get_node("Shield")
shield_anim = get_node("ShieldAnim")
width = get_node("Sprite").get_texture().get_size().width
view_width = get_viewport_rect().size.width
set_fixed_process(true)