-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.gd
188 lines (148 loc) · 3.68 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
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 KinematicBody2D
# components
onready var sprite = $Small
onready var collider = $SmallCollision
onready var damage_timer = $DamageTimer
# signals
signal health_change(health)
signal damage_taken(damage)
# Declare member variables here. Examples:
# var a = 2
# var b = "text"
var health : int = 20
var stage : int = 0
var score : int = 0
var speed : int = 200
var jumpForce: int = 220
var gravity : int = 800
var vel : Vector2 = Vector2()
var grounded : bool = false
var invulnerable : bool = false
var dead : bool = false
const STARTING_POS : Vector2 = Vector2(50, 75)
const SPEED_TINY : int = 200
const SPEED_MEDIUM : int = 140
const SPEED_BIG : int = 80
const DAMAGE_LASER : int = 10
const DAMAGE_ANTIBODY : int = 10
const DAMAGE_CHEMO : int = 20
const DAMAGE_COOLDOWN : int = 5
# Called when the node enters the scene tree for the first time.
func _ready():
sprite.visible = true
collider.disabled = false
sprite.get_node("PlayerEffect").play("Idle")
func reset():
position = STARTING_POS
health = 20
dead = false
collider.disabled = false
change_stage(0)
invulnerable = true
damage_timer.start()
sprite.get_node("PlayerEffect").play("Idle")
get_parent().get_tree().reload_current_scene()
func die():
dead = true
collider.disabled = true
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
pass
func _physics_process(delta):
if dead:
return
vel.x = 0
if Input.is_action_pressed("move_left"):
vel.x -= speed
if Input.is_action_pressed("move_right"):
vel.x += speed
vel = move_and_slide(vel, Vector2.UP)
vel.y += gravity * delta
if Input.is_action_pressed("jump") and is_on_floor():
vel.y -= jumpForce
if vel.x < 0:
sprite.flip_h = true
elif vel.x > 0:
sprite.flip_h = false
if position.y > 250:
reset()
func change_stage(stage):
sprite.visible = false
collider.disabled = true
if stage == 0:
sprite = $Small
collider = $SmallCollision
speed = SPEED_TINY
elif stage == 1:
sprite = $Medium
collider = $MediumCollision
speed = SPEED_MEDIUM
else:
sprite = $Large
collider = $LargeCollision
speed = SPEED_BIG
sprite.visible = true
collider.disabled = false
func take_damage(damage):
if invulnerable or dead:
return
change_health(-damage)
invulnerable = true
emit_signal("damage_taken", damage)
sprite.get_node("PlayerEffect").play("Damage")
if dead:
sprite.get_node("PlayerEffect").queue("Death")
# sprite.get_node("PlayerEffect").queue("Invulnerable")
else:
damage_timer.start()
sprite.get_node("PlayerEffect").queue("Invulnerable")
func take_antibody_damage():
if dead or invulnerable:
return
$AntibodySound.play()
take_damage(DAMAGE_ANTIBODY)
func take_laser_damage():
if dead or invulnerable:
return
$LaserSound.play()
take_damage(DAMAGE_LASER)
func take_chemo_damage():
if dead or invulnerable:
return
var taken_damage : bool = not invulnerable
$BombSound.play()
take_damage(DAMAGE_CHEMO)
return taken_damage
func change_health(delta):
health += delta
if health < 0:
health = 0
if health > 100:
health = 100
var new_stage = -1
if health < 33:
new_stage = 0
elif health < 66:
new_stage = 1
else:
new_stage = 2
if new_stage != stage:
stage = new_stage
change_stage(new_stage)
if delta:
emit_signal("health_change", health)
if health == 0:
die()
func _on_HealthTimer_timeout():
change_health(1)
pass # Replace with function body.
func _on_Laser_area_entered(area):
take_laser_damage()
print("entered")
func _on_Laser_body_entered(body):
pass # Replace with function body.
func _on_DamageTimer_timeout():
invulnerable = false
sprite.visible = true
sprite.get_node("PlayerEffect").stop()
sprite.get_node("PlayerEffect").play("Idle")