Advertisement
thatenbykiki

player.gd

Apr 13th, 2024
47
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.  
  8. var lastMove := Vector2.ZERO
  9.  
  10. # set sprite textures
  11. var sprite_idle = preload("res://assets/sprites/player/idle.png")
  12. var sprite_run = preload("res://assets/sprites/player/run.png")
  13. var sprite_attack = preload("res://assets/sprites/player/attack1.png")
  14.  
  15. func _physics_process(delta):
  16.     inputHandler()
  17.     move_and_slide()
  18.     update_anim()
  19.  
  20. func inputHandler():
  21.     var direction = Input.get_vector("move_left", "move_right", "move_up", "move_down")
  22.     velocity = direction * speed
  23.  
  24. func update_anim():
  25.     if velocity.y >= 0 && lastMove.is_equal_approx(Vector2(0, 1)):
  26.         sprite.texture = sprite_idle
  27.         anim.play("IDLE_DOWN") 
  28.     elif velocity.y <= 0 && lastMove.is_equal_approx(Vector2(0, -1)):
  29.         sprite.texture = sprite_idle
  30.         anim.play("IDLE_UP")   
  31.     elif velocity.x <=0 && lastMove.is_equal_approx(Vector2(-1, 0)):
  32.         sprite.texture = sprite_idle
  33.         sprite.flip_h = true
  34.         anim.play("IDLE_LEFT-RIGHT")   
  35.     elif velocity.x >= 0 && lastMove.is_equal_approx(Vector2(1, 0)):
  36.         sprite.texture = sprite_idle
  37.         sprite.flip_h = false
  38.         anim.play("IDLE_LEFT-RIGHT")   
  39.    
  40.     if velocity.x > 0:
  41.         sprite.texture = sprite_run
  42.         sprite.flip_h = true
  43.         anim.play("RUN_LEFT-RIGHT")
  44.     elif velocity.x < 0:
  45.         sprite.texture = sprite_run
  46.         sprite.flip_h = false
  47.         anim.play("RUN_LEFT-RIGHT")
  48.     elif velocity.y > 0:
  49.         sprite.texture = sprite_run
  50.         anim.play("RUN_DOWN")
  51.     elif velocity.y < 0:
  52.         sprite.texture = sprite_run
  53.         anim.play("RUN_UP")
  54.    
  55.     lastMove = get_last_motion()
  56.     print(lastMove)
  57.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement