Skip to content

Instantly share code, notes, and snippets.

@NovemberDev
Created September 22, 2020 00:12
Show Gist options
  • Save NovemberDev/1fd7b8d2e4bba265479415a5cdc060cb to your computer and use it in GitHub Desktop.
Save NovemberDev/1fd7b8d2e4bba265479415a5cdc060cb to your computer and use it in GitHub Desktop.
extends KinematicBody2D
const HALT_SPEED = 0.325
const MAX_SPEED = 750.0
const GRAVITY = 1200.0
const MAX_JUMP = 725.0
const MAX_JUMP_TIME = 1.5
const MAX_JUMP_FORGIVENESS_TIME = 0.5
var jump_time = 0.0
var jump_accel = 0.0
var jump_forgiveness_time = 0.0
var jump_force = Vector2.ZERO
var direction = Vector2.ZERO
var direction_input = Vector2.ZERO
func _ready():
$AnimationTree.active = true
func _process(delta):
direction_input = Vector2.ZERO
if Input.is_key_pressed(KEY_A):
direction_input.x = -1
$Sprite.flip_h = true
if Input.is_key_pressed(KEY_D):
direction_input.x = 1
$Sprite.flip_h = false
if jump_time <= MAX_JUMP_TIME or jump_forgiveness_time <= MAX_JUMP_FORGIVENESS_TIME:
if Input.is_action_just_pressed("ui_select"):
jump_accel = -MAX_JUMP
if Input.is_action_pressed("ui_select"):
jump_accel -= MAX_JUMP * delta
jump_force.y = jump_accel
$AnimationTree.set("parameters/in_air/current", 1)
if Input.is_action_just_released("ui_select"):
$AnimationTree.set("parameters/in_air/current", 0)
if direction_input == Vector2.ZERO:
direction = direction.linear_interpolate(Vector2.ZERO, delta / HALT_SPEED)
else:
direction = direction.linear_interpolate(direction_input * MAX_SPEED, delta)
move_and_slide(direction + jump_force, Vector2.UP)
if is_on_floor():
jump_accel = 0.0
jump_force.y = 0.0
jump_forgiveness_time = 0.0
$AnimationTree.set("parameters/movement/current", int(direction.length() > 50))
else:
if !Input.is_action_pressed("ui_select"):
jump_time = MAX_JUMP_TIME + 1
else:
jump_forgiveness_time = MAX_JUMP_FORGIVENESS_TIME + 1
jump_time += delta
jump_forgiveness_time += delta
jump_force.y += GRAVITY * delta
if is_on_floor() or (jump_forgiveness_time <= MAX_JUMP_FORGIVENESS_TIME):
jump_time = 0.0
$AnimationTree.set("parameters/movement_time/scale", 1 + abs(direction.x)/100)
$AnimationTree.set("parameters/in_air_state/current", int(!$RayCast2D.is_colliding()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment