Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extends KinematicBody2D
- #refs
- onready var sprite: AnimatedSprite = get_node("Sprite")
- # Constants
- const DEFAULT_SNAP = Vector2(0, 32)
- const SPEEDY = 250
- const GRAVITY = 900
- const JUMP_FORCE = 500
- const slope_slide_threshold = 50.0
- # State
- var velocity = Vector2(0, 0)
- var snap = false
- var friction = 0.5
- var animation = "idle_1"
- var flip = false
- # Input
- var action_jump = false
- var jump_release = false;
- var direction_x = 0
- func _ready():
- set_process_input(true)
- pass
- func update_animation(velocity: Vector2):
- var walking = abs(velocity.x) > 10
- if walking and is_on_floor():
- animation = "run"
- elif is_on_floor():
- animation = "idle_1"
- elif not is_on_floor():
- animation = "jump" if velocity.y < 0 else "fall";
- if walking:
- sprite.flip_h = velocity.x < 0
- if sprite.animation != animation:
- sprite.play(animation)
- ################################################################################################
- # Handle Physics
- #
- ################################################################################################
- func _physics_process(delta):
- # Handle left and right
- direction_x = Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left")
- velocity.x = direction_x * SPEEDY
- # Handle Jump
- if is_on_floor() and snap:
- if Input.is_action_just_pressed("ui_jump"):
- velocity.y = -JUMP_FORCE
- snap = false
- # Normalizations
- velocity.y += GRAVITY * delta;
- velocity.x = lerp(velocity.x, 0, friction)
- var snap_vector = DEFAULT_SNAP if snap else Vector2()
- # Physics
- velocity = move_and_slide(velocity, Vector2.UP, true)
- var just_landed = is_on_floor() and not snap
- if just_landed:
- snap = true
- update_animation(velocity)
- action_jump = false
- jump_release = false
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement