Created
September 18, 2023 20:13
-
-
Save Ratstail91/f9599d8c96f7b56823395580e25a9e9f to your computer and use it in GitHub Desktop.
First thing I've gotten done in months - I should be happy, but I'm still disappointed...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extends CharacterBody2D | |
const MOVE_FORCE: int = 150 | |
const JUMP_FORCE: int = 500 | |
const FRICTION_FORCE: float = 0.9 | |
const GRAVITY_DOWN: int = 25 | |
const GRAVITY_UP: int = 15 | |
const MAX_SPEED: int = 350 | |
#TODO: buffered jumps | |
#TODO: coyote time | |
func _process(_delta): | |
var inputForce: Vector2 = Vector2.ZERO | |
#left | |
if Input.is_action_pressed("player_1_left"): | |
inputForce.x -= MOVE_FORCE | |
if Input.is_action_just_released("player_1_left") && velocity.x < 0: | |
inputForce.x += MOVE_FORCE | |
#right | |
if Input.is_action_pressed("player_1_right"): | |
inputForce.x += MOVE_FORCE | |
if Input.is_action_just_released("player_1_right") && velocity.x > 0: | |
inputForce.x -= MOVE_FORCE | |
#jump | |
if Input.is_action_just_pressed("player_1_jump") && is_on_floor(): | |
inputForce.y -= JUMP_FORCE | |
#lock it in | |
velocity += inputForce | |
func _physics_process(_delta): | |
#gravity | |
if !is_on_floor() && velocity.y < 0: | |
velocity.y += GRAVITY_UP | |
else: | |
velocity.y += GRAVITY_DOWN | |
#limits | |
if is_on_floor(): | |
velocity.x *= FRICTION_FORCE | |
if abs(velocity.x) > MAX_SPEED: | |
velocity.x = sign(velocity.x) * MAX_SPEED | |
#if abs(velocity.y) > MAX_SPEED: | |
# velocity.y = sign(velocity.y) * MAX_SPEED | |
#do the thing | |
move_and_slide() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment