-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.gd
90 lines (69 loc) · 3 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
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_timer = $CoyoteTimer
@onready var starting_position = global_position
func _physics_process(delta: float):
apply_gravity(delta)
handle_jump()
handle_wall_jump()
var input_axis = Input.get_axis("ui_left", "ui_right")
apply_friction(input_axis, delta)
apply_air_resistance(input_axis, delta)
handle_acceleration(input_axis, delta)
handle_air_acceleration(input_axis, delta)
handle_animation(input_axis)
var was_on_floor = is_on_floor()
move_and_slide()
if was_on_floor and not is_on_floor() and velocity.y >= 0:
coyote_timer.start()
if Input.is_action_just_pressed("ui_accept"):
movement_data = load("res://FasterMovementData.tres")
func apply_gravity(delta: float):
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 not coyote_timer.is_stopped():
if Input.is_action_just_pressed("ui_up"):
velocity.y = movement_data.jump_velocity
elif not is_on_floor():
if Input.is_action_just_released("ui_up") and velocity.y < movement_data.jump_velocity / 3:
velocity.y = movement_data.jump_velocity / 3
if Input.is_action_just_pressed("ui_up") and not is_on_wall() and air_jump:
velocity.y = movement_data.jump_velocity / 2
air_jump = false
func handle_wall_jump():
if not is_on_wall() or is_on_floor():
return
if Input.is_action_just_pressed("ui_up"):
var normal = get_wall_normal()
velocity.x = movement_data.wall_jump_x * sign(normal.x)
velocity.y = movement_data.jump_velocity
func handle_acceleration(input_axis: float, delta: float):
if not is_on_floor():
return
if input_axis:
velocity.x = move_toward(velocity.x, input_axis * movement_data.speed, movement_data.acceleration * delta)
func handle_air_acceleration(input_axis: float, delta: float):
if is_on_floor():
return
if input_axis:
velocity.x = move_toward(velocity.x, input_axis * movement_data.speed, movement_data.air_acceleration * delta)
func apply_friction(input_axis: float, delta: float):
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):
if input_axis == 0 and not is_on_floor():
velocity.x = move_toward(velocity.x, 0, movement_data.air_resistance * delta)
func handle_animation(inpus_axis: float):
if velocity.x != 0:
animated_sprite_2d.play("run")
animated_sprite_2d.flip_h = inpus_axis < 0
else:
animated_sprite_2d.play("idle")
func _on_hazard_detector_area_entered(area):
global_position = starting_position