56 lines
1.5 KiB
GDScript
56 lines
1.5 KiB
GDScript
extends KinematicBody2D
|
|
|
|
const ACCELERATION = 600
|
|
const MAX_SPEED = 64
|
|
const FRICTION = 0.25
|
|
const AIR_RESISTANCE = 0.02
|
|
const GRAVITY = 220
|
|
const JUMP_FORCE = 128
|
|
const COUNTER_MIN = 0.1
|
|
|
|
var motion = Vector2.ZERO
|
|
var counter = 0
|
|
|
|
onready var sprite = $Sprite
|
|
onready var animationPlayer = $AnimationPlayer
|
|
|
|
func _physics_process(delta):
|
|
print(delta)
|
|
if is_on_floor():
|
|
counter = 0
|
|
sprite.material.set_shader_param("amount", 0.3)
|
|
else:
|
|
counter += delta
|
|
|
|
var x_input = Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left")
|
|
|
|
if x_input != 0:
|
|
animationPlayer.play("Run")
|
|
motion.x += x_input * ACCELERATION * delta
|
|
motion.x = clamp(motion.x, -MAX_SPEED, MAX_SPEED)
|
|
sprite.flip_h = x_input < 0
|
|
else:
|
|
animationPlayer.play("Stand")
|
|
|
|
motion.y += GRAVITY * delta
|
|
|
|
# If on floor
|
|
if counter < COUNTER_MIN:
|
|
if x_input == 0: # & no input
|
|
motion.x = lerp(motion.x, 0, FRICTION) # add friction
|
|
|
|
if Input.is_action_just_pressed("ui_up"): # & jump input
|
|
motion.y = -JUMP_FORCE # move up
|
|
|
|
else: # If not on floor
|
|
animationPlayer.play("Jump")
|
|
if Input.is_action_just_released("ui_up") and motion.y < -JUMP_FORCE/2: # & moving up
|
|
motion.y = -JUMP_FORCE/2 # half speed
|
|
|
|
if x_input == 0: # & no input
|
|
motion.x = lerp(motion.x, 0, AIR_RESISTANCE) # add air resistance
|
|
|
|
sprite.material.set_shader_param("amount", clamp(abs(motion.y)/50,0.3,0.5))
|
|
$"/root/World/TileMap".material.set_shader_param("amount", clamp(abs(motion.y)/50,0.3,0.5))
|
|
motion = move_and_slide(motion, Vector2.UP)
|