GMTK-2024/gmtk_2024/scripts/player_movement.gd

54 lines
1.2 KiB
GDScript3
Raw Normal View History

2024-08-16 21:03:06 +02:00
extends CharacterBody2D
2024-08-17 22:26:53 +08:00
@export var speed = 340
@export var gravity = 50
var jump_count = 1
@export var jump_strength = 100
2024-08-17 22:26:53 +08:00
var is_touching_floor : bool = true
var coyote_timer : float = 0.2 # 200 millisecond buffer
2024-08-16 22:49:25 +02:00
var input_direction = 0 #To keep track of which direction we where moving in last frame
2024-08-16 21:03:06 +02:00
2024-08-17 22:26:53 +08:00
func get_input(delta):
2024-08-16 22:49:25 +02:00
2024-08-16 22:09:19 +02:00
var left = Input.is_action_pressed("player_left")
var right = Input.is_action_pressed("player_right")
2024-08-16 22:49:25 +02:00
2024-08-16 22:09:19 +02:00
if left and right:
2024-08-17 22:26:53 +08:00
input_direction = 0
2024-08-16 22:09:19 +02:00
elif left:
input_direction = -1
elif right:
input_direction = 1
else :
input_direction = 0
2024-08-17 22:26:53 +08:00
# This line updates the player's velocity
2024-08-16 21:03:06 +02:00
velocity.x = input_direction * speed
2024-08-16 22:49:25 +02:00
2024-08-17 22:26:53 +08:00
if is_on_floor():
# reset the jump count
is_touching_floor = true
jump_count = 1
coyote_timer = 0.2
if not is_on_floor():
if coyote_timer > 0.0:
coyote_timer -= delta
else:
jump_count = 0
2024-08-17 22:26:53 +08:00
if Input.is_action_just_pressed("player_jump") and jump_count > 0:
2024-08-16 22:49:25 +02:00
velocity.y = -1 * jump_strength
2024-08-17 22:26:53 +08:00
jump_count = 0
is_touching_floor = false
elif Input.is_action_pressed("player_jump"): #Fall less fast if we keep holding the button
velocity.y += -1 * 25
2024-08-16 22:49:25 +02:00
2024-08-16 21:03:06 +02:00
func _physics_process(delta):
2024-08-17 22:26:53 +08:00
get_input(delta)
2024-08-16 21:42:27 +02:00
velocity.y += gravity
2024-08-16 21:03:06 +02:00
move_and_slide()
2024-08-17 22:26:53 +08:00
print(jump_count)