-
Notifications
You must be signed in to change notification settings - Fork 25
/
player.gd
400 lines (300 loc) · 9.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
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
class_name Player
extends CharacterBody2D
enum Direction {
LEFT = -1,
RIGHT = +1,
}
enum State {
IDLE,
RUNNING,
JUMP,
FALL,
LANDING,
WALL_SLIDING,
WALL_JUMP,
ATTACK_1,
ATTACK_2,
ATTACK_3,
HURT,
DYING,
SLIDING_START,
SLIDING_LOOP,
SLIDING_END,
}
const GROUND_STATES := [
State.IDLE, State.RUNNING, State.LANDING,
State.ATTACK_1, State.ATTACK_2, State.ATTACK_3,
]
const RUN_SPEED := 160.0
const FLOOR_ACCELERATION := RUN_SPEED / 0.2
const AIR_ACCELERATION := RUN_SPEED / 0.1
const JUMP_VELOCITY := -320.0
const WALL_JUMP_VELOCITY := Vector2(380, -280)
const KNOCKBACK_AMOUNT := 512.0
const SLIDING_DURATION := 0.3
const SLIDING_SPEED := 256.0
const SLIDING_ENERGY := 4.0
const LANDING_HEIGHT := 100.0
@export var can_combo := false
@export var direction := Direction.RIGHT:
set(v):
direction = v
if not is_node_ready():
await ready
graphics.scale.x = direction
var default_gravity := ProjectSettings.get("physics/2d/default_gravity") as float
var is_first_tick := false
var is_combo_requested := false
var pending_damage: Damage
var fall_from_y: float
var interacting_with: Array[Interactable]
@onready var graphics: Node2D = $Graphics
@onready var animation_player: AnimationPlayer = $AnimationPlayer
@onready var coyote_timer: Timer = $CoyoteTimer
@onready var jump_request_timer: Timer = $JumpRequestTimer
@onready var hand_checker: RayCast2D = $Graphics/HandChecker
@onready var foot_checker: RayCast2D = $Graphics/FootChecker
@onready var state_machine: Node = $StateMachine
@onready var stats: Node = Game.player_stats
@onready var invincible_timer: Timer = $InvincibleTimer
@onready var slide_request_timer: Timer = $SlideRequestTimer
@onready var interaction_icon: AnimatedSprite2D = $InteractionIcon
@onready var game_over_screen: Control = $CanvasLayer/GameOverScreen
@onready var pause_screen: Control = $CanvasLayer/PauseScreen
func _ready() -> void:
stand(default_gravity, 0.01)
func _unhandled_input(event: InputEvent) -> void:
if event.is_action_pressed("jump"):
jump_request_timer.start()
if event.is_action_released("jump"):
jump_request_timer.stop()
if velocity.y < JUMP_VELOCITY / 2:
velocity.y = JUMP_VELOCITY / 2
if event.is_action_pressed("attack") and can_combo:
is_combo_requested = true
if event.is_action_pressed("slide"):
slide_request_timer.start()
if event.is_action_pressed("interact") and interacting_with:
interacting_with.back().interact()
if event.is_action_pressed("pause"):
pause_screen.show_pause()
func tick_physics(state: State, delta: float) -> void:
interaction_icon.visible = not interacting_with.is_empty()
if invincible_timer.time_left > 0:
graphics.modulate.a = sin(Time.get_ticks_msec() / 20) * 0.5 + 0.5
else:
graphics.modulate.a = 1
match state:
State.IDLE:
move(default_gravity, delta)
State.RUNNING:
move(default_gravity, delta)
State.JUMP:
move(0.0 if is_first_tick else default_gravity, delta)
State.FALL:
move(default_gravity, delta)
State.LANDING:
stand(default_gravity, delta)
State.WALL_SLIDING:
move(default_gravity / 3, delta)
direction = Direction.LEFT if get_wall_normal().x < 0 else Direction.RIGHT
State.WALL_JUMP:
if state_machine.state_time < 0.1:
stand(0.0 if is_first_tick else default_gravity, delta)
direction = Direction.LEFT if get_wall_normal().x < 0 else Direction.RIGHT
else:
move(default_gravity, delta)
State.ATTACK_1, State.ATTACK_2, State.ATTACK_3:
stand(default_gravity, delta)
State.HURT, State.DYING:
stand(default_gravity, delta)
State.SLIDING_END:
stand(default_gravity, delta)
State.SLIDING_START, State.SLIDING_LOOP:
slide(delta)
is_first_tick = false
func move(gravity: float, delta: float) -> void:
var movement := Input.get_axis("move_left", "move_right")
var acceleration := FLOOR_ACCELERATION if is_on_floor() else AIR_ACCELERATION
velocity.x = move_toward(velocity.x, movement * RUN_SPEED, acceleration * delta)
velocity.y += gravity * delta
if not is_zero_approx(movement):
direction = Direction.LEFT if movement < 0 else Direction.RIGHT
move_and_slide()
func stand(gravity: float, delta: float) -> void:
var acceleration := FLOOR_ACCELERATION if is_on_floor() else AIR_ACCELERATION
velocity.x = move_toward(velocity.x, 0.0, acceleration * delta)
velocity.y += gravity * delta
move_and_slide()
func slide(delta: float) -> void:
velocity.x = graphics.scale.x * SLIDING_SPEED
velocity.y += default_gravity * delta
move_and_slide()
func die() -> void:
game_over_screen.show_game_over()
func register_interactable(v: Interactable) -> void:
if state_machine.current_state == State.DYING:
return
if v in interacting_with:
return
interacting_with.append(v)
func unregister_interactable(v: Interactable) -> void:
interacting_with.erase(v)
func can_wall_slide() -> bool:
return is_on_wall() and hand_checker.is_colliding() and foot_checker.is_colliding()
func should_slide() -> bool:
if slide_request_timer.is_stopped():
return false
if stats.energy < SLIDING_ENERGY:
return false
return not foot_checker.is_colliding()
func get_next_state(state: State) -> int:
if stats.health == 0:
return StateMachine.KEEP_CURRENT if state == State.DYING else State.DYING
if pending_damage:
return State.HURT
var can_jump := is_on_floor() or coyote_timer.time_left > 0
var should_jump := can_jump and jump_request_timer.time_left > 0
if should_jump:
return State.JUMP
if state in GROUND_STATES and not is_on_floor():
return State.FALL
var movement := Input.get_axis("move_left", "move_right")
var is_still := is_zero_approx(movement) and is_zero_approx(velocity.x)
match state:
State.IDLE:
if Input.is_action_just_pressed("attack"):
return State.ATTACK_1
if should_slide():
return State.SLIDING_START
if not is_still:
return State.RUNNING
State.RUNNING:
if Input.is_action_just_pressed("attack"):
return State.ATTACK_1
if should_slide():
return State.SLIDING_START
if is_still:
return State.IDLE
State.JUMP:
if velocity.y >= 0:
return State.FALL
State.FALL:
if is_on_floor():
var height := global_position.y - fall_from_y
return State.LANDING if height >= LANDING_HEIGHT else State.RUNNING
if can_wall_slide():
return State.WALL_SLIDING
State.LANDING:
if not animation_player.is_playing():
return State.IDLE
State.WALL_SLIDING:
if jump_request_timer.time_left > 0:
return State.WALL_JUMP
if is_on_floor():
return State.IDLE
if not is_on_wall():
return State.FALL
State.WALL_JUMP:
if can_wall_slide() and not is_first_tick:
return State.WALL_SLIDING
if velocity.y >= 0:
return State.FALL
State.ATTACK_1:
if not animation_player.is_playing():
return State.ATTACK_2 if is_combo_requested else State.IDLE
State.ATTACK_2:
if not animation_player.is_playing():
return State.ATTACK_3 if is_combo_requested else State.IDLE
State.ATTACK_3:
if not animation_player.is_playing():
return State.IDLE
State.HURT:
if not animation_player.is_playing():
return State.IDLE
State.SLIDING_START:
if not animation_player.is_playing():
return State.SLIDING_LOOP
State.SLIDING_END:
if not animation_player.is_playing():
return State.IDLE
State.SLIDING_LOOP:
if state_machine.state_time > SLIDING_DURATION or is_on_wall():
return State.SLIDING_END
return StateMachine.KEEP_CURRENT
func transition_state(from: State, to: State) -> void:
print("[%s] %s => %s" % [
Engine.get_physics_frames(),
State.keys()[from] if from != -1 else "<START>",
State.keys()[to],
])
if from not in GROUND_STATES and to in GROUND_STATES:
coyote_timer.stop()
match to:
State.IDLE:
animation_player.play("idle")
State.RUNNING:
animation_player.play("running")
State.JUMP:
animation_player.play("jump")
velocity.y = JUMP_VELOCITY
coyote_timer.stop()
jump_request_timer.stop()
SoundManager.play_sfx("Jump")
State.FALL:
animation_player.play("fall")
if from in GROUND_STATES:
coyote_timer.start()
fall_from_y = global_position.y
State.LANDING:
animation_player.play("landing")
State.WALL_SLIDING:
animation_player.play("wall_sliding")
State.WALL_JUMP:
animation_player.play("jump")
velocity = WALL_JUMP_VELOCITY
velocity.x *= get_wall_normal().x
jump_request_timer.stop()
State.ATTACK_1:
animation_player.play("attack_1")
is_combo_requested = false
SoundManager.play_sfx("Attack")
State.ATTACK_2:
animation_player.play("attack_2")
is_combo_requested = false
State.ATTACK_3:
animation_player.play("attack_3")
is_combo_requested = false
State.HURT:
animation_player.play("hurt")
#Input.start_joy_vibration(0, 0, 0.8, 0.8)
Game.shake_camera(4)
stats.health -= pending_damage.amount
var dir := pending_damage.source.global_position.direction_to(global_position)
velocity = dir * KNOCKBACK_AMOUNT
pending_damage = null
invincible_timer.start()
State.DYING:
animation_player.play("die")
invincible_timer.stop()
interacting_with.clear()
State.SLIDING_START:
animation_player.play("sliding_start")
slide_request_timer.stop()
stats.energy -= SLIDING_ENERGY
State.SLIDING_LOOP:
animation_player.play("sliding_loop")
State.SLIDING_END:
animation_player.play("sliding_end")
is_first_tick = true
func _on_hurtbox_hurt(hitbox: Hitbox) -> void:
if invincible_timer.time_left > 0:
return
pending_damage = Damage.new()
pending_damage.amount = 1
pending_damage.source = hitbox.owner
func _on_hitbox_hit(hurtbox: Variant) -> void:
Game.shake_camera(2)
Engine.time_scale = 0.01
await get_tree().create_timer(0.05, true, false, true).timeout
Engine.time_scale = 1