59 lines
1.4 KiB
GDScript
59 lines
1.4 KiB
GDScript
extends CharacterBody2D
|
|
|
|
# Serialized Variables ------------------
|
|
@export var MAX_SPEED = 300
|
|
@export var ACCELERATION = 1500
|
|
@export var FRICTION = 1200
|
|
# ---------------------------------------
|
|
|
|
|
|
|
|
# Calculation Variables -----------------
|
|
@onready var axis = Vector2.ZERO
|
|
# ---------------------------------------
|
|
|
|
|
|
|
|
# Engine Callbacks ----------------------
|
|
func _physics_process(delta):
|
|
move(delta)
|
|
# ---------------------------------------
|
|
|
|
|
|
|
|
# Input ---------------------------------
|
|
func get_input():
|
|
# Makes the function equivalent to unity's Input.GetAxis("Horizontal")
|
|
axis.x = int(Input.is_action_pressed("move_right")) - int(Input.is_action_pressed("move_left"))
|
|
# Makes the function equivalent to unity's Input.GetAxis("Vertical")
|
|
axis.y = int(Input.is_action_pressed("move_down")) - int(Input.is_action_pressed("move-up"))
|
|
|
|
return axis.normalized()
|
|
# ---------------------------------------
|
|
|
|
|
|
|
|
# Movement ------------------------------
|
|
func move(delta):
|
|
axis = get_input() # Get the input
|
|
|
|
# If player is not moving
|
|
if axis == Vector2.ZERO:
|
|
apply_friction(FRICTION * delta)
|
|
else :
|
|
apply_movement(axis * ACCELERATION * delta)
|
|
|
|
move_and_slide()
|
|
|
|
|
|
func apply_movement(accel):
|
|
velocity += accel
|
|
velocity = velocity.limit_length(MAX_SPEED)
|
|
|
|
|
|
func apply_friction(amount):
|
|
if velocity.length() > amount :
|
|
velocity -= velocity.normalized() * amount
|
|
else :
|
|
velocity = Vector2.ZERO
|
|
# ----------------------------------------
|