Advertisement
thatenbykiki

player.gd (12APR24 22:50)

Apr 12th, 2024
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. extends CharacterBody2D
  2.  
  3. @export var speed := 300.0
  4.  
  5. @onready var anim = $AnimationPlayer
  6. @onready var sprite = $Sprite2D
  7. @onready var lastMove = get_last_motion()
  8.  
  9. # sprite textures
  10. var sprite_idle = preload("res://assets/sprites/player/idle.png")
  11. var sprite_run = preload("res://assets/sprites/player/run.png")
  12. var sprite_attack = preload("res://assets/sprites/player/attack1.png")
  13.  
  14. func _ready():
  15.     pass
  16.  
  17. func _physics_process(delta):
  18.     inputHandler()
  19.     move_and_slide()
  20.     update_anim()
  21.     print(lastMove)
  22.  
  23. func inputHandler():
  24.     var direction = Input.get_vector("move_left", "move_right", "move_up", "move_down")
  25.     velocity = direction * speed
  26.  
  27. func update_anim():
  28.     if velocity.x > 0:
  29.         sprite.texture = sprite_run
  30.         sprite.flip_h = true
  31.         anim.play("RUN_LEFT-RIGHT")
  32.     elif velocity.x < 0:
  33.         sprite.texture = sprite_run
  34.         sprite.flip_h = false
  35.         anim.play("RUN_LEFT-RIGHT")
  36.     elif velocity.y > 0:
  37.         sprite.texture = sprite_run
  38.         anim.play("RUN_DOWN")
  39.     elif velocity.y < 0:
  40.         sprite.texture = sprite_run
  41.         anim.play("RUN_UP")
  42.    
  43.     if velocity == Vector2(0.0, 0.0) && lastMove == Vector2(0, 1):
  44.         sprite.texture = sprite_idle
  45.         anim.play("IDLE_DOWN") 
  46.     elif velocity == Vector2(0.0, 0.0) && lastMove == Vector2(0, -1):
  47.         sprite.texture = sprite_idle
  48.         anim.play("IDLE_UP")   
  49.     elif velocity == Vector2(0.0, 0.0) && lastMove == Vector2(-1, 0):
  50.         sprite.texture = sprite_idle
  51.         sprite.flip_h = true
  52.         anim.play("IDLE_LEFT-RIGHT")   
  53.     elif velocity == Vector2(0.0, 0.0) && lastMove == Vector2(1, 0):
  54.         sprite.texture = sprite_idle
  55.         sprite.flip_h = false
  56.         anim.play("IDLE_LEFT-RIGHT")   
  57.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement