Basic jump arc working
This commit is contained in:
@@ -2,30 +2,49 @@ extends CharacterBody2D
|
||||
|
||||
@onready var _sprite = $AnimatedSprite2D
|
||||
|
||||
const GRAVITY: Vector2 = Vector2(0, 9.8)
|
||||
const MAX_SPEED: int = 500
|
||||
const IMPULSE: int = 80
|
||||
const JUMP: int = -400 #TODO: proper jump arch
|
||||
const MOVE_FORCE: int = 300
|
||||
const JUMP_FORCE: int = 800
|
||||
|
||||
const GRAVITY_UP: int = 15
|
||||
const GRAVITY_DOWN: int = 30
|
||||
|
||||
const MAX_MOVE_SPEED: int = 300
|
||||
const MAX_FALL_SPEED: int = 500
|
||||
|
||||
#boilerplate
|
||||
func _ready():
|
||||
_sprite.play("idle", 1)
|
||||
|
||||
#physics and controls
|
||||
#movement
|
||||
func _physics_process(_delta) -> void:
|
||||
#falling or jumping
|
||||
#input, jumps
|
||||
if is_on_floor() and Input.is_action_just_pressed("input_jump"):
|
||||
velocity.y = JUMP
|
||||
velocity.y -= JUMP_FORCE
|
||||
|
||||
velocity += GRAVITY
|
||||
#fall faster than you rise
|
||||
if is_airborne_rising() and Input.is_action_pressed("input_jump"):
|
||||
velocity.y += GRAVITY_UP
|
||||
else:
|
||||
velocity.y += GRAVITY_DOWN
|
||||
|
||||
var direction = Input.get_axis("input_left", "input_right")
|
||||
if direction:
|
||||
velocity.x += IMPULSE * direction
|
||||
if abs(velocity.x) > MAX_SPEED:
|
||||
velocity.x = MAX_SPEED * sign(velocity.x)
|
||||
if is_airborne_falling() and velocity.y > MAX_FALL_SPEED:
|
||||
velocity.y = MAX_FALL_SPEED
|
||||
|
||||
#input, walking
|
||||
var move_dir = Input.get_axis("input_left", "input_right")
|
||||
|
||||
#move with a maximum value
|
||||
if move_dir:
|
||||
velocity.x += MOVE_FORCE * move_dir
|
||||
if abs(velocity.x) > MAX_MOVE_SPEED:
|
||||
velocity.x = MAX_MOVE_SPEED * sign(velocity.x)
|
||||
|
||||
#no input, slow down
|
||||
elif velocity.x != 0:
|
||||
velocity.x = log(abs(velocity.x)) * sign(velocity.x)
|
||||
|
||||
print(velocity)
|
||||
#do the thing
|
||||
move_and_slide()
|
||||
|
||||
#animation stuff
|
||||
@@ -34,3 +53,8 @@ func _on_animation_finished() -> void:
|
||||
_sprite.play("idle_glance", 2)
|
||||
else:
|
||||
_sprite.play("idle", 2)
|
||||
|
||||
#utils
|
||||
func is_airborne() -> bool: return !is_on_floor()
|
||||
func is_airborne_rising() -> bool: return !is_on_floor() and velocity.y < 0
|
||||
func is_airborne_falling() -> bool: return !is_on_floor() and velocity.y >= 0
|
||||
|
||||
Reference in New Issue
Block a user