Created
September 29, 2023 02:16
-
-
Save Ratstail91/8861ad06f7901a8b2444fc88362ba37f to your computer and use it in GitHub Desktop.
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 | |
#tweakable constants | |
const MOVE_FORCE: int = 600 | |
const JUMP_FORCE: int = 20_000 | |
const FRICTION_FORCE: float = 0.9 | |
const GRAVITY_DOWN: int = 500 | |
const GRAVITY_UP: int = 350 | |
const MAX_SPEED: int = 350 | |
const MAX_FALL: int = 500 | |
#for allowances | |
var lastGroundedTime: int = 0 | |
var lastJumpTime: int = 0 | |
var externalForce: Vector2 = Vector2.ZERO | |
@export var controlScheme : String = "" | |
func _process(delta): | |
var inputForce: Vector2 = Vector2.ZERO | |
#left | |
if Input.is_action_pressed(controlScheme + "_left"): | |
inputForce.x -= MOVE_FORCE | |
#right | |
if Input.is_action_pressed(controlScheme + "_right"): | |
inputForce.x += MOVE_FORCE | |
#jump, with coyote time | |
if is_on_floor(): | |
lastGroundedTime = Time.get_ticks_msec() | |
if Input.is_action_just_pressed(controlScheme + "_jump") && Time.get_ticks_msec() - lastGroundedTime < 100: | |
inputForce.y -= JUMP_FORCE | |
lastGroundedTime = 0 | |
#jump, with buffered input | |
if Input.is_action_just_pressed(controlScheme + "_jump"): | |
lastJumpTime = Time.get_ticks_msec() | |
if is_on_floor() && Time.get_ticks_msec() - lastJumpTime < 100: | |
inputForce.y -= JUMP_FORCE | |
lastJumpTime = 0 | |
#lock it in | |
velocity += inputForce * delta | |
func _physics_process(delta): | |
#rising | |
if !is_on_floor() && !Input.is_action_pressed(controlScheme + "_jump") && velocity.y < 0: | |
velocity.y *= FRICTION_FORCE | |
#gravity | |
if !is_on_floor() && velocity.y < 0: | |
velocity.y += GRAVITY_UP * delta | |
else: | |
velocity.y += GRAVITY_DOWN * delta | |
#external influence | |
velocity += externalForce * delta | |
externalForce = Vector2.ZERO | |
#limits | |
if is_on_floor() && !(Input.is_action_pressed(controlScheme + "_left") || Input.is_action_pressed(controlScheme + "_right")): | |
velocity.x *= FRICTION_FORCE | |
if !is_on_floor() && !(Input.is_action_pressed(controlScheme + "_left") || Input.is_action_pressed(controlScheme + "_right")): | |
velocity.x = 0 | |
if abs(velocity.x) > MAX_SPEED: | |
velocity.x = sign(velocity.x) * MAX_SPEED | |
if abs(velocity.y) > MAX_FALL: | |
velocity.y = sign(velocity.y) * MAX_FALL | |
move_and_slide() | |
for index in get_slide_collision_count(): | |
var collision = get_slide_collision(index) | |
print(collision.get_collider().name, " - ", is_on_ceiling()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment