-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer.gd
More file actions
298 lines (267 loc) · 7.99 KB
/
player.gd
File metadata and controls
298 lines (267 loc) · 7.99 KB
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
extends CharacterBody2D
const SPEED = 375.0
const JUMP_VELOCITY = -430.0
var wall_x_force = 1000.0
var wall_y_force = -645.0
var can_coyote = false
var jump_buffered = false
var sliding = false
var slide_timeout = false
var wall_jumping = false
var can_wall_jump = true
var last_on_wall = INF
var was_wall_jumping = false
var cur_dir = -1
var was_running = 0
var attacking = false
var damaged = []
var is_hit = false
var hit_by
var dead = false
var health = 100
@onready var coyote_timer = $CoyoteTimer
@onready var jump_buffer = $JumpBuffer
@onready var wall_slide_timer = $WallSlideTimer
@onready var wall_jump_timer = $WallJumpTimer
@onready var left_raycast = $Raycasters/LeftRaycast
@onready var right_raycast = $Raycasters/RightRaycast
@onready var sprite = $Sprite2D
@onready var collision = $CollisionShape2D
@onready var collision_rect = load("res://player_collision_rectangle.tres")
@onready var attack_area = $AttackArea
@onready var attack_polygon_behind = $AttackArea/CollisionPolygon2D2
@onready var bottom_raycast = $Raycasters/BottomRaycast
@onready var knockback = $KnockbackTimer
@onready var healthbar = $ProgressBar
@onready var respawn_timer = $RespawnTimer
@onready var safe_timer = $SafeTimer
@onready var boss = $"../Boss"
func _process(delta: float) -> void:
healthbar.value = health
if sprite.flip_h:
healthbar.position = Vector2(-26,-10)
match sprite.animation:
"idle", "run", "jump":
collision_rect.size = Vector2(20.75, 38.0)
collision.position = Vector2(11.25, 42.0)
"wall_hang", "wall_slide":
collision_rect.size = Vector2(20.0, 38.0)
collision.position = Vector2(2, 42.0)
"attack_1", "attack_2":
collision_rect.size = Vector2(23.25, 38.0)
collision.position = Vector2(8.75, 42.0)
else:
healthbar.position = Vector2(-45,-10)
match sprite.animation:
"idle", "run", "jump":
collision_rect.size = Vector2(20.75, 38.0)
collision.position = Vector2(-10.75, 42.0)
"wall_hang", "wall_slide":
collision_rect.size = Vector2(22.0, 34.0)
collision.position = Vector2(-2, 42.0)
"attack_1", "attack_2":
collision_rect.size = Vector2(23.25, 38.0)
collision.position = Vector2(-8.25, 42.0)
func _physics_process(delta: float) -> void:
if not dead:
# Gravity
if not is_on_floor():
if safe_timer.is_stopped():
if velocity.y >= 0:
velocity += get_gravity() * delta * 1.8
else:
velocity += get_gravity() * delta * 1.2
else:
velocity += get_gravity() * delta * 0.75
# Jump
if Input.is_action_just_pressed("jump") or jump_buffered:
if sprite.animation == "turn":
cur_dir = -cur_dir
sprite.flip_h = true if cur_dir == -1 else false
if is_on_floor():
if not attacking:
sprite.play("jump")
jump_buffered = false
velocity.y = JUMP_VELOCITY
elif can_coyote:
sprite.play("jump")
attacking = false
can_coyote = false
velocity.y = JUMP_VELOCITY
elif velocity.y >= 0 and not jump_buffered:
jump_buffer.start()
jump_buffered = true
# Allow variable jump height
if Input.is_action_just_released("jump") and velocity.y <= 0 and not wall_jumping:
velocity.y *= 0.5
if not wall_jumping and (not attacking or not is_on_floor()):
# Movement
var direction := Input.get_axis("left", "right")
if direction and not is_hit:
if was_wall_jumping:
velocity.x = direction * SPEED * 1.3
cur_dir = direction
else:
if cur_dir == direction or velocity.y != 0:
cur_dir = direction
if velocity.y == 0 and not is_hit and not attacking:
sprite.play("run")
velocity.x = direction * SPEED
else:
if was_running < 0:
sprite.flip_h = true if cur_dir == 1 else false
cur_dir = direction
velocity.x = direction * SPEED
else:
if sprite.animation != "turn" and not attacking:
sprite.play("turn")
velocity.x = -direction * SPEED / 2
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
else:
velocity.x = move_toward(velocity.x, 0, SPEED/1.2)
if velocity.y <= 0:
wall_jumping = false
was_wall_jumping = true
last_on_wall += delta
if is_on_wall_only() and not bottom_raycast.is_colliding():
sprite.play("wall_slide")
if left_raycast.is_colliding():
sprite.flip_h = false
elif right_raycast.is_colliding():
sprite.flip_h = true
last_on_wall = 0
if not slide_timeout:
if not sliding:
wall_slide_timer.start()
sliding = true
velocity.y = 100
else:
sliding = false
slide_timeout = false
if last_on_wall < 0.2 and Input.is_action_just_pressed("jump") and can_wall_jump:
wall_jump_timer.start()
can_wall_jump = false
wall_jumping = true
velocity.y = wall_y_force
if left_raycast.is_colliding():
sprite.flip_h = true
velocity.x = wall_x_force
elif right_raycast.is_colliding():
sprite.flip_h = false
velocity.x = -wall_x_force
else:
wall_jumping = false
# Check if landed
if is_on_floor():
wall_jumping = false
was_wall_jumping = false
# Coyote Time
var was_on_floor := is_on_floor()
if velocity.x == 0:
if not attacking and not is_hit and not was_wall_jumping and not wall_jumping:
sprite.play("idle")
was_running -= delta
else:
was_running = 0.1
attack_polygon_behind.disabled = true if boss.active else false
if Input.is_action_just_pressed("attack") and not attacking:
attack_area.scale.x = -1 if sprite.flip_h else 1
if boss.active:
sprite.play("attack_1")
else:
sprite.play("attack_2")
attacking = true
damaged = []
if attacking:
if (sprite.animation == "attack_1" and sprite.frame != 0) or (sprite.animation == "attack_2" and not sprite.frame in [0,1]):
for attacked_body in attack_area.get_overlapping_bodies():
if not attacked_body in damaged:
damaged.append(attacked_body)
attacked_body.hit(self)
if is_hit:
velocity.x = position.direction_to(hit_by.position).x * -600
if not sprite.animation == "turn" and not sprite.animation == "wall_slide":
if velocity.x < 0:
sprite.flip_h = true
elif velocity.x > 0:
sprite.flip_h = false
# Move character
move_and_slide()
# Coyote Time
if was_on_floor and not is_on_floor() and velocity.y >= 0:
can_coyote = true
coyote_timer.start()
else:
if not is_on_floor():
velocity = Vector2(0,0)
if velocity.y >= 0:
velocity += get_gravity() * delta * 1.8
else:
velocity += get_gravity() * delta * 1.2
move_and_slide()
func _on_coyote_timeout() -> void:
can_coyote = false
func _on_jump_buffer_timeout() -> void:
jump_buffered = false
func _on_wall_slide_timer_timeout() -> void:
slide_timeout = true
func _on_wall_jump_timer_timeout() -> void:
can_wall_jump = true
func _on_sprite_2d_animation_finished() -> void:
match sprite.animation:
"turn":
sprite.play("run")
cur_dir = -cur_dir
sprite.flip_h = true if cur_dir == -1 else false
"attack_1", "attack_2": attacking = false
func hit(attacker):
if not dead:
if "Fire" in attacker.name:
if not dead and safe_timer.is_stopped():
health = 0
sprite.play("death")
dead = true
healthbar.hide()
respawn_timer.start()
safe_timer.start()
elif "Skeleton" in attacker.name:
if not dead and safe_timer.is_stopped():
health -= 33.34
if health <= 0:
healthbar.hide()
sprite.play("death")
dead = true
respawn_timer.start()
safe_timer.start()
else:
sprite.play("hit")
is_hit = true
knockback.start()
hit_by = attacker
elif "Boss" in attacker.name:
if not dead:
health -= 12.5
if health <= 0:
healthbar.hide()
sprite.play("death")
dead = true
respawn_timer.start(3)
safe_timer.start(3.5)
else:
is_hit = true
sprite.play("hit")
knockback.start()
hit_by = attacker
func _on_knockback_timer_timeout() -> void:
is_hit = false
attacking = false
sprite.play("idle")
func _on_boss_battle_begin() -> void:
health = 100
func _on_respawn_timer_timeout() -> void:
position = Vector2(137,150)
health = 100
healthbar.show()
sprite.play("idle")
dead = false