-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.gd
157 lines (134 loc) · 3.9 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
extends Area2D
signal part_pickup
signal hp_update
signal break_part
signal unnecessary_parts_break
signal take_hit
signal player_dead
var move_start_pos = Vector2(-1, -1)
var move_started = false
var width = 32
var height = 64
var collected_spare_parts = 0
# TODO if adjusted, adjust progress bar max value
var max_hp = 10000
var hp = max_hp
var break_level = 0
var immune = false
var collision_type = "Player"
var upgrade_idx = 0
var repairing = false
onready var sprite = $AnimatedSprite
# Called when the node enters the scene tree for the first time.
func _ready():
set_process_input(true)
func _input(event):
if event is InputEventScreenDrag:
position += event.relative
$BulletSpawner.initial_velocity = event.relative
elif event is InputEventScreenTouch:
if event.is_pressed():
$BulletSpawner.enable()
else:
$BulletSpawner.disable()
if event is InputEventKey and event.is_pressed():
if event.get_scancode() == KEY_R:
_repair_ship()
print("DEBUG: break level ", break_level)
if event.get_scancode() == KEY_B:
_break_ship()
if event.get_scancode() == KEY_U:
upgrade()
print("DEBUG: upgrade ")
if event.get_scancode() == KEY_G:
immune = !immune
print("DEBUG: god mode ", immune)
func _update_hp():
if break_level == 0 && hp < max_hp:
hp += 2
else:
hp -= break_level + 1
emit_signal("hp_update", hp)
if hp <= 0:
emit_signal("player_dead")
func _process(delta):
_update_hp()
if position.x - width / 2 < 0:
position.x = width / 2
elif position.x + width / 2 > get_viewport_rect().size.x:
position.x = get_viewport_rect().size.x - width / 2
if position.y < 0:
position.y = height / 2
elif position.y > get_viewport_rect().size.y:
position.y = get_viewport_rect().size.y - height / 2
func _on_Player_area_entered(area):
if area.collision_type == "PartPickup":
emit_signal("part_pickup", area.type)
if area.type <= 2:
if break_level == 0:
collected_spare_parts += 1
if collected_spare_parts > 2:
collected_spare_parts = 0
# TODO display notification at hud
emit_signal("unnecessary_parts_break")
_break_ship()
else:
collected_spare_parts = 0
else:
_break_ship()
func add_hp(h):
hp = min(max_hp, hp + h)
func upgrade():
add_hp(300)
var update_speed = (upgrade_idx % 2) == 1
if update_speed:
$BulletSpawner.set_bullet_delay(max(200, $BulletSpawner.fire_delay_ms - 50))
else:
$BulletSpawner.bullet_count = min(15, $BulletSpawner.bullet_count + 1)
upgrade_idx += 1
func _break_ship():
if immune:
return
$ImmunityTimer.start()
$FlickerTimer.start()
immune = true
emit_signal("take_hit")
if break_level == 3:
hp -= 200
return
if repairing:
repairing = false
break_level -= 1
var tmp = break_level
break_level = min(3, break_level + 1)
emit_signal("break_part", break_level)
func _repair_ship():
repairing = false
add_hp(300)
var tmp = break_level
break_level = max(0, break_level -1)
func _on_Main_ship_upgraded():
upgrade()
func _on_Main_ship_repaired():
_repair_ship()
func _on_ImmunityTimer_timeout():
immune = false
func _on_FlickerTimer_timeout():
if is_visible():
hide()
else:
show()
if immune:
$FlickerTimer.start()
else:
show()
func update_anim(hp):
if hp < 4000:
sprite.animation = 'Broke2'
elif hp < 7500:
sprite.animation = 'Broke1'
else:
sprite.animation = 'idle'
func _on_Main_repair_started():
if break_level > 0:
repairing = true