diff options
Diffstat (limited to 'Player.gd')
| -rw-r--r-- | Player.gd | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/Player.gd b/Player.gd new file mode 100644 index 0000000..8fbeb35 --- /dev/null +++ b/Player.gd @@ -0,0 +1,73 @@ +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 GRAVITY: float = 200 +export(float) var JUMP_FORCE: float = 100 +export(float) var MAX_FALL_SPEED: float = 1000 +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 * 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, 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")) + # warning-ignore:return_value_discarded + move_and_slide(velocity, Vector2.UP) |
