-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.gd
143 lines (112 loc) · 3.46 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
extends KinematicBody2D
class_name Player
enum { MOVE, CLIMB }
export (Resource) var moveData
var velocity = Vector2.ZERO
var state = MOVE
var double_jump = 1
var buffered_jump = false
var coyote_jump = false
onready var animatedSprite: = $AnimatedSprite
onready var ladderCheck: = $LadderCheck
onready var jumpBufferTimer: = $JumpBufferTimer
onready var coyoteJumpTimer: = $CoyoteJumpTimer
onready var remoteTransform2D: = $RemoteTransform2D
func _physics_process(delta):
var input = Vector2.ZERO
input.x = Input.get_axis("ui_left", "ui_right")
input.y = Input.get_axis("ui_up", "ui_down")
match state:
MOVE: move_state(input)
CLIMB: climb_state(input)
func move_state(input):
if isOnLadder() and Input.is_action_pressed("ui_up"):
state = CLIMB
apply_gravity()
if not horizontal_move(input):
apply_friction()
animatedSprite.animation = "idle"
else:
apply_acceleration(input.x)
animatedSprite.animation = "run"
animatedSprite.flip_h = input.x > 0
if is_on_floor():
reset_double_jump()
else:
animatedSprite.animation = "jump"
if can_jump():
input_jump()
else:
input_jump_release()
input_double_jump()
buffer_jump()
fast_fall()
var was_in_air = not is_on_floor()
var was_on_floor = is_on_floor()
velocity = move_and_slide(velocity, Vector2.UP)
var just_landed = is_on_floor() and was_in_air
if just_landed:
animatedSprite.animation = "run"
animatedSprite.frame = 1
var just_left_ground = not is_on_floor() and was_on_floor
if just_left_ground and velocity.y >= 0:
coyote_jump = true
coyoteJumpTimer.start()
func horizontal_move(input):
return input.x != 0
func can_jump():
return is_on_floor() or coyote_jump
func input_jump():
if Input.is_action_just_pressed("ui_up") or buffered_jump:
SoundPlayer.play_sound(SoundPlayer.JUMP)
velocity.y = moveData.JUMP_FORCE
buffered_jump = false
func input_jump_release():
if Input.is_action_just_released("ui_up") and velocity.y < moveData.JUMP_RELEASE_FORCE:
velocity.y = moveData.JUMP_RELEASE_FORCE
func input_double_jump():
if Input.is_action_just_pressed("ui_up") and double_jump > 0:
SoundPlayer.play_sound(SoundPlayer.JUMP)
velocity.y = moveData.JUMP_FORCE
double_jump -= 1
func fast_fall():
if velocity.y > 0:
velocity.y += moveData.FALL_GRAVITY
func buffer_jump():
if Input.is_action_just_pressed("ui_up"):
buffered_jump = true
jumpBufferTimer.start()
func reset_double_jump():
double_jump = moveData.DOUBLE_JUMP_COUNT
func climb_state(input):
if not isOnLadder():
state = MOVE
if input.length() != 0:
animatedSprite.animation = "run"
else:
animatedSprite.animation = "idle"
velocity = input * moveData.CLIMB_SPEED
velocity = move_and_slide(velocity, Vector2.UP)
func player_die():
SoundPlayer.play_sound(SoundPlayer.HURT)
queue_free()
Events.emit_signal("player_died")
func connect_camera(camera):
var camera_path = camera.get_path()
remoteTransform2D.remote_path = camera_path
func isOnLadder():
if not ladderCheck.is_colliding(): return false
var collider = ladderCheck.get_collider()
if not collider is Ladder: return false
return true
func apply_gravity():
velocity.y += moveData.GRAVITY
velocity.y = min(velocity.y, 400)
func apply_friction():
velocity.x = move_toward(velocity.x, 0, moveData.FRICTION)
func apply_acceleration(amount):
velocity.x = move_toward(velocity.x, moveData.MAX_SPEED * amount, moveData.ACCELERATION)
func _on_JumpBufferTimer_timeout():
buffered_jump = false
func _on_CoyoteJumpTimer_timeout():
coyote_jump = false