- Edited
I'm fairly new to godot and game dev in general. i have some python experience from school and i want to learn more.
my GDScript code is quite messy and i want to optimize it but i don't know how. any help please ?
here's the code:
extends KinematicBody2D
const TARGET_FPS = 60
const ACCELERATION = 8
const MAX_SPEED = 65
const FRICTION = 10
const AIR_RESISTANCE = 1
const GRAVITY = 6
const JUMP_FORCE = 150
var motion = Vector2.ZERO
export var on_ground = false
onready var animatedSprite = $AnimatedSprite
func _physics_process(delta):
#declaring variables
var is_rolling = false
var is_crouched = false
var x_input = Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left")
#input logic
if x_input != 0 :
motion.x += x_input * ACCELERATION * delta * TARGET_FPS
motion.x = clamp(motion.x, -MAX_SPEED, MAX_SPEED)
animatedSprite.flip_h = x_input < 0
motion.y += GRAVITY * delta * TARGET_FPS
if is_on_floor():
if on_ground == false:
on_ground = true
if x_input == 0:
motion.x = lerp(motion.x, 0, FRICTION * delta)
if Input.is_action_pressed("ui_down"):
is_crouched = true
motion.x = 0
else:
is_crouched = false
if Input.is_action_just_pressed("ui_up"):
motion.y = -JUMP_FORCE
if Input.is_action_just_released("ui_up") and motion.y < -JUMP_FORCE/2:
motion.y = -JUMP_FORCE/2
if x_input == 0:
motion.x = lerp(motion.x, 0, AIR_RESISTANCE * delta)
else:
if on_ground == true:
on_ground = false
#animation logic
if on_ground:
if is_crouched:
animatedSprite.play("crouch")
elif x_input!= 0:
animatedSprite.play("run")
elif x_input==0:
animatedSprite.play("idle")
elif motion.y < 0:
animatedSprite.play("jump_begin")
elif motion.y > 0:
animatedSprite.play("jump_loop")
motion = move_and_slide(motion, Vector2.UP)