Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extends CharacterBody2D
- @export var speed := 300.0
- @onready var anim = $AnimationPlayer
- @onready var sprite = $Sprite2D
- @onready var lastMove = get_last_motion()
- # sprite textures
- var sprite_idle = preload("res://assets/sprites/player/idle.png")
- var sprite_run = preload("res://assets/sprites/player/run.png")
- var sprite_attack = preload("res://assets/sprites/player/attack1.png")
- func _ready():
- pass
- func _physics_process(delta):
- inputHandler()
- move_and_slide()
- update_anim()
- print(lastMove)
- func inputHandler():
- var direction = Input.get_vector("move_left", "move_right", "move_up", "move_down")
- velocity = direction * speed
- func update_anim():
- if velocity.x > 0:
- sprite.texture = sprite_run
- sprite.flip_h = true
- anim.play("RUN_LEFT-RIGHT")
- elif velocity.x < 0:
- sprite.texture = sprite_run
- sprite.flip_h = false
- anim.play("RUN_LEFT-RIGHT")
- elif velocity.y > 0:
- sprite.texture = sprite_run
- anim.play("RUN_DOWN")
- elif velocity.y < 0:
- sprite.texture = sprite_run
- anim.play("RUN_UP")
- if velocity == Vector2(0.0, 0.0) && lastMove == Vector2(0, 1):
- sprite.texture = sprite_idle
- anim.play("IDLE_DOWN")
- elif velocity == Vector2(0.0, 0.0) && lastMove == Vector2(0, -1):
- sprite.texture = sprite_idle
- anim.play("IDLE_UP")
- elif velocity == Vector2(0.0, 0.0) && lastMove == Vector2(-1, 0):
- sprite.texture = sprite_idle
- sprite.flip_h = true
- anim.play("IDLE_LEFT-RIGHT")
- elif velocity == Vector2(0.0, 0.0) && lastMove == Vector2(1, 0):
- sprite.texture = sprite_idle
- sprite.flip_h = false
- anim.play("IDLE_LEFT-RIGHT")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement