135 lines
2.7 KiB
GDScript
135 lines
2.7 KiB
GDScript
extends CharacterBody2D
|
|
|
|
@export var acceleration = 6000.0
|
|
@export var maxSpeed = 600.0
|
|
@export var decelleration = 4000.0
|
|
|
|
@export var maxJumpHeight = 310.0
|
|
@export var minJumpHeight = 90.0
|
|
@export var peakTime = 0.5
|
|
|
|
@export var coyoteTime = 0.2
|
|
|
|
# Calculated physics variables
|
|
var holdGravity = 0
|
|
var tapGravity = 0
|
|
var jumpVelocity = 0
|
|
|
|
# Input variables set by controller
|
|
var direction = 0
|
|
var startJump = false
|
|
var jumpHeld = false
|
|
|
|
var jumpWindowRemaining = 0
|
|
|
|
func jump(pressed):
|
|
startJump = startJump or (!jumpHeld and pressed)
|
|
jumpHeld = pressed
|
|
|
|
func move(dir):
|
|
direction = dir
|
|
|
|
func updateVars():
|
|
jumpVelocity = - 2 * maxJumpHeight / peakTime
|
|
holdGravity = 2 * maxJumpHeight / (peakTime * peakTime)
|
|
tapGravity = (jumpVelocity * jumpVelocity) / (2 * minJumpHeight)
|
|
|
|
func _ready():
|
|
updateVars()
|
|
|
|
func _process(delta):
|
|
# We need to keep updating the vars because the inspector might have changed the settings
|
|
# Would be better to have a function we call on update instead of running every tick, but I can't be bothered right now
|
|
updateVars()
|
|
|
|
func _physics_process(delta):
|
|
if direction != 0:
|
|
velocity.x += acceleration * direction * delta
|
|
else:
|
|
velocity.x = move_toward(velocity.x, 0, decelleration*delta)
|
|
|
|
velocity.x = clampf(velocity.x, -maxSpeed, maxSpeed)
|
|
|
|
if is_on_floor():
|
|
jumpWindowRemaining = coyoteTime
|
|
else:
|
|
jumpWindowRemaining -= delta
|
|
|
|
if startJump:
|
|
startJump = false
|
|
if jumpWindowRemaining > 0:
|
|
velocity.y = jumpVelocity
|
|
jumpWindowRemaining = 0
|
|
|
|
if not is_on_floor():
|
|
var gravity = holdGravity if jumpHeld and velocity.y < 0 else tapGravity
|
|
velocity.y += gravity * delta
|
|
|
|
move_and_slide()
|
|
|
|
func getInspecctorDefinition():
|
|
return [
|
|
{
|
|
label = "Max Speed",
|
|
editable = true,
|
|
property = "maxSpeed",
|
|
max = 1000
|
|
},
|
|
{
|
|
label = "Acceleration",
|
|
editable = true,
|
|
property = "acceleration",
|
|
min = 1000,
|
|
max = 10000
|
|
},
|
|
{
|
|
label = "Decelleration",
|
|
editable = true,
|
|
property = "decelleration",
|
|
min = 1000,
|
|
max = 10000
|
|
},
|
|
{
|
|
label = "Max Jump Height",
|
|
editable = true,
|
|
property = "maxJumpHeight",
|
|
max = 1000
|
|
},
|
|
{
|
|
label = "Min Jump Height",
|
|
editable = true,
|
|
property = "minJumpHeight",
|
|
max = 1000
|
|
},
|
|
{
|
|
label = "Peak Time",
|
|
editable = true,
|
|
property = "peakTime",
|
|
max = 1
|
|
},
|
|
{
|
|
label = "Coyote Time",
|
|
editable = true,
|
|
property = "coyoteTime",
|
|
max = 0.5
|
|
},
|
|
{
|
|
type = 'spacer'
|
|
},
|
|
{
|
|
label = "HoldGravity",
|
|
editable = false,
|
|
property = "holdGravity",
|
|
},
|
|
{
|
|
label = "TapGravity",
|
|
editable = false,
|
|
property = "tapGravity",
|
|
},
|
|
{
|
|
label = "Jump Velocity",
|
|
editable = false,
|
|
property = "jumpVelocity",
|
|
}
|
|
]
|