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
|
extends KinematicBody2D
class_name Player
export(float) var WALK_ANIM_MULTIPLIER: float = 0.02
export(float) var WALK_SPEED: float = 300
export(float) var WALK_SPEED_LERP_W: float = 0.2
export(float) var JUMP_FORCE: float = 100
export(float) var COYOTE_TIME: float = 250
export(float) var JUMP_BUFFER: float = 220
var velocity = Vector2.ZERO
var coyote_timer: float = INF
var jump_buffer: float = INF
# Called when the node enters the scene tree for the first time.
func _ready():
pass
#$AnimationTree.set("parameters/walk_speed/scale", 0)
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(_delta):
pass
#print($AnimationPlayer.current_animation)
func _physics_process(delta):
velocity.y += delta * GlobalData.GRAVITY
jump_buffer += delta * 1000
if Input.is_action_pressed("move_left"):
velocity.x = lerp(velocity.x, -WALK_SPEED, WALK_SPEED_LERP_W)
$Sprite.set_flip_h(false)
$AnimationTree.set("parameters/is_moving/current", true)
elif Input.is_action_pressed("move_right"):
velocity.x = lerp(velocity.x, WALK_SPEED, WALK_SPEED_LERP_W)
$Sprite.set_flip_h(true)
$AnimationTree.set("parameters/is_moving/current", true)
else:
velocity.x = 0
$AnimationTree.set("parameters/is_moving/current", false)
if $FloorRaycastL.is_colliding() || $FloorRaycastR.is_colliding():
$AnimationTree.set("parameters/is_grounded/current", true)
else:
$AnimationTree.set("parameters/is_grounded/current", false)
if is_on_floor():
coyote_timer = 0
velocity.y = 0
else:
coyote_timer += delta * 1000
if Input.is_action_just_pressed("jump"):
jump_buffer = 0
if coyote_timer < COYOTE_TIME && jump_buffer < JUMP_BUFFER:
velocity.y = -JUMP_FORCE
coyote_timer = INF
jump_buffer = INF
if is_on_ceiling():
velocity.y = 1 # Just a bit downwards to not get stuck
velocity.y = clamp(velocity.y, -INF, GlobalData.MAX_FALL_SPEED)
# TODO fix walking down ramps with snap
# snap needs to be ZERO when jumping
# print($AnimationPlayer.current_animation, ", ", $AnimationTree.get("parameters/walk_speed/scale"))
move_and_slide(velocity, Vector2.UP)
|