-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.gd
99 lines (84 loc) · 3.41 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
extends CharacterBody2D
@export var movement_data: PlayerMovementData
var air_jump := false
var just_wall_jumped := false
var gravity: int = ProjectSettings.get_setting("physics/2d/default_gravity")
var was_wall_normal := Vector2.ZERO
@onready var animated_sprite_2d: AnimatedSprite2D = $AnimatedSprite2D
@onready var coyote_jump_timer: Timer = $CoyoteJumpTimer
@onready var starting_position = global_position
@onready var wall_jump_timer: Timer = $WallJumpTimer
func _physics_process(delta: float) -> void:
apply_gravity(delta)
handle_wall_jump()
handle_jump()
var input_axis := Input.get_axis("move_left", "move_right")
handle_acceleration(input_axis, delta)
handle_air_acceleration(input_axis, delta)
apply_friction(input_axis, delta)
apply_air_resistance(input_axis, delta)
var was_on_floor := is_on_floor()
var was_on_wall := is_on_wall_only()
if was_on_wall:
was_wall_normal = get_wall_normal()
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()
just_wall_jumped = false
var just_left_wall := was_on_wall and not is_on_wall()
if just_left_wall:
wall_jump_timer.start()
update_animations(input_axis)
func apply_gravity(delta: float) -> void:
if not is_on_floor():
velocity.y += gravity * movement_data.gravity_scale * delta
func handle_wall_jump() -> void:
if not is_on_wall_only() and wall_jump_timer.time_left <= 0:
return
var wall_normal := get_wall_normal()
if wall_jump_timer.time_left > 0.0:
wall_normal = was_wall_normal
if Input.is_action_just_pressed("jump"):
animated_sprite_2d.flip_h = wall_normal.x < 0
velocity.x = wall_normal.x * movement_data.speed
velocity.y = movement_data.jump_velocity
just_wall_jumped = true
func handle_jump() -> void:
if is_on_floor():
air_jump = true
if is_on_floor() or coyote_jump_timer.time_left > 0.0:
if Input.is_action_just_pressed("jump"):
velocity.y = movement_data.jump_velocity
elif not is_on_floor():
if Input.is_action_just_released("jump") and velocity.y < movement_data.jump_velocity / 2:
velocity.y = movement_data.jump_velocity / 2
if Input.is_action_just_pressed("jump") and air_jump and not just_wall_jumped:
velocity.y = movement_data.jump_velocity * 0.8
air_jump = false
func handle_acceleration(input_axis: float, delta: float) -> void:
if not is_on_floor():
return
if input_axis != 0:
velocity.x = move_toward(velocity.x, movement_data.speed * input_axis, movement_data.accelaration * delta)
func handle_air_acceleration(input_axis: float, delta: float) -> void:
if is_on_floor():
return
if input_axis != 0:
velocity.x = move_toward(velocity.x, movement_data.speed * input_axis, movement_data.air_acceleration * delta)
func apply_friction(input_axis: float, delta: float) -> void:
if input_axis == 0 and is_on_floor():
velocity.x = move_toward(velocity.x, 0, movement_data.friction * delta)
func apply_air_resistance(input_axis: float, delta: float) -> void:
if input_axis == 0 and not is_on_floor():
velocity.x = move_toward(velocity.x, 0, movement_data.air_resistance * delta)
func update_animations(input_axis: float) -> void:
if input_axis != 0:
animated_sprite_2d.flip_h = input_axis < 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: Area2D) -> void:
global_position = starting_position