-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.gd
111 lines (78 loc) · 2.64 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
extends CharacterBody2D
@export var movement_data: PlayerMovementData
var air_jump = false
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
@onready var animated_sprite_2d = $AnimatedSprite2D
@onready var coyote_jump_timer = $CoyoteJumpTimer
@onready var starting_position = global_position
func _physics_process(delta):
add_gravity(delta)
handle_jump()
handle_wall_jump()
var direction = Input.get_axis("move_left", "move_right")
handle_movement(direction, delta)
var was_on_floor = is_on_floor()
move_and_slide()
var just_left_ledge = was_on_floor and not is_on_floor() and velocity.y >= 0
if just_left_ledge:
coyote_jump_timer.start()
update_animations(direction)
func add_gravity(delta):
if not is_on_floor():
velocity.y += gravity * movement_data.gravity_scale * delta
func handle_jump():
if is_on_floor():
air_jump = true
if is_on_floor() or coyote_jump_timer.time_left > 0:
if Input.is_action_pressed("jump"):
velocity.y = movement_data.jump_velocity
coyote_jump_timer.stop()
elif not is_on_floor():
var short_jump = movement_data.jump_velocity / 2
if Input.is_action_just_released("jump") and velocity.y < short_jump:
velocity.y = short_jump
if Input.is_action_just_pressed("jump") and air_jump:
velocity.y = movement_data.jump_velocity
air_jump = false
func handle_wall_jump():
if not is_on_wall_only():
return
var wall_normal = get_wall_normal()
if Input.is_action_just_pressed("jump"):
velocity.x = wall_normal.x * movement_data.speed
velocity.y = movement_data.jump_velocity
air_jump = true
func handle_movement(direction, delta):
if direction:
apply_acceleration(direction, delta)
else:
apply_friction(delta)
apply_air_resistance(delta)
func apply_acceleration(direction, delta):
if direction == 0:
return
if is_on_floor():
velocity.x = move_toward(
velocity.x, movement_data.speed * direction, movement_data.acceleration * delta
)
else:
velocity.x = move_toward(
velocity.x, movement_data.speed * direction, movement_data.air_acceleration * delta
)
func apply_friction(delta):
if is_on_floor():
velocity.x = move_toward(velocity.x, 0, movement_data.friction * delta)
func apply_air_resistance(delta):
if not is_on_floor():
velocity.x = move_toward(velocity.x, 0, movement_data.air_resistance * delta)
func update_animations(direction):
if direction:
animated_sprite_2d.flip_h = direction < 0
animated_sprite_2d.play("run")
else:
animated_sprite_2d.play("idle")
if not is_on_floor():
animated_sprite_2d.play("jump")
func _on_hazard_detector_area_entered(_area):
global_position = starting_position
animated_sprite_2d.flip_h = false