Last active
January 21, 2021 00:27
-
-
Save Sciman101/dc0a693a059d8051ff52a7ff393e07d8 to your computer and use it in GitHub Desktop.
Godot Platformer Starter Code
This file contains hidden or 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 KinematicBody2D | |
const ACC_INSTANT = 1000000 # If acceleration time is 0, default to this | |
# Exposed movement parameters | |
export var move_speed : float | |
export var acceleration_time : float # How many seconds does it take to go from 0 to max speed? | |
export var air_acceleration_time : float # How many seconds does it take to go from 0 to max speed, when we're midair? | |
export var friction_time : float # How many seconds does it take to go from max speed to 0? | |
export var jump_height : float # How many pixels vertically should we jump | |
export var jump_apex_time : float # How many seconds should it take to reach the peak of our jump | |
export var falling_grav_multiplier : float # When falling, we fall this much faster | |
export var min_jump_speed : float # Minimum jump speed for variable jumping | |
export var max_air_jumps : int | |
export var jump_buffer_time : float # How early can we press jump | |
export var edge_buffer_time : float # How late can we press jump | |
# Calculated parameters | |
var _gravity : float | |
var _jump_speed : float | |
var _acceleration : float | |
var _air_acceleration : float | |
var _friction : float | |
# Buffers | |
var jump_buffer := 0.0 # When we press jump, set this to some value - if we hit the ground while it's still positive, jump. | |
var edge_buffer := 0.0 # When we leave a ledge, set this to some value. if jump_buffer > 0 when this is positive, jump | |
# Motion variables | |
var motion : Vector2 | |
var grounded : bool | |
var was_grounded : bool | |
var jump_count : int | |
func _ready() -> void: | |
calculate_movement_parameters() | |
# Move | |
func _physics_process(delta:float) -> void: | |
grounded = is_on_floor() | |
_handle_movement(delta) | |
motion = move_and_slide(motion,Vector2.UP) | |
was_grounded = grounded | |
func _handle_movement(delta:float) -> void: | |
# Get horizontal input | |
var hor = 0.0 | |
if Input.is_action_pressed("right"): hor += 1 | |
if Input.is_action_pressed("left"): hor -= 1 | |
# Determine acceleration | |
var acc = _acceleration | |
if grounded: | |
if hor == 0: | |
acc = _friction | |
else: | |
if hor == 0: | |
acc = 0 | |
else: | |
acc = _air_acceleration | |
# Accelerate | |
motion.x = move_toward(motion.x,hor * move_speed,acc*delta) | |
# Gravity | |
motion.y += _gravity * delta * (1 if motion.y <= 0 else falling_grav_multiplier) | |
# Jump input | |
if Input.is_action_just_pressed("jump"): | |
jump_buffer = jump_buffer_time | |
# Edge buffer | |
if grounded and not was_grounded: | |
jump_count = 0 | |
elif not grounded and was_grounded: | |
edge_buffer = edge_buffer_time | |
# # Jumping | |
if jump_buffer > 0 and \ | |
(grounded or edge_buffer > 0) and (jump_count <= max_air_jumps): | |
# Apply speed | |
motion.y = -_jump_speed | |
# Clear buffers | |
edge_buffer = 0 | |
jump_buffer = 0 | |
jump_count += 1 | |
# Cancel jump | |
if Input.is_action_just_released("jump") and motion.y < 0: | |
motion.y = max(motion.y,-min_jump_speed) | |
# Decrement buffers | |
if jump_buffer > 0: jump_buffer = max(jump_buffer-delta,0) | |
if edge_buffer > 0: edge_buffer = max(edge_buffer-delta,0) | |
func calculate_movement_parameters() -> void: | |
# Calculate jump vars using projectile motion | |
_gravity = 2*jump_height/(jump_apex_time*jump_apex_time) | |
_jump_speed = _gravity*jump_apex_time | |
if acceleration_time != 0: | |
_acceleration = move_speed / acceleration_time | |
else: | |
_acceleration = ACC_INSTANT | |
if air_acceleration_time != 0: | |
_air_acceleration = move_speed / air_acceleration_time | |
else: | |
_air_acceleration = ACC_INSTANT | |
if friction_time != 0: | |
_friction = move_speed / friction_time | |
else: | |
_friction = ACC_INSTANT |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment