goblin.gd 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. extends CharacterBody2D
  2. const MOTION_SPEED = 160 # Pixels/second.
  3. var last_direction = Vector2(1, 0)
  4. var anim_directions = {
  5. "idle": [ # list of [animation name, horizontal flip]
  6. ["side_right_idle", false],
  7. ["45front_right_idle", false],
  8. ["front_idle", false],
  9. ["45front_left_idle", false],
  10. ["side_left_idle", false],
  11. ["45back_left_idle", false],
  12. ["back_idle", false],
  13. ["45back_right_idle", false],
  14. ],
  15. "walk": [
  16. ["side_right_walk", false],
  17. ["45front_right_walk", false],
  18. ["front_walk", false],
  19. ["45front_left_walk", false],
  20. ["side_left_walk", false],
  21. ["45back_left_walk", false],
  22. ["back_walk", false],
  23. ["45back_right_walk", false],
  24. ],
  25. }
  26. func _physics_process(_delta):
  27. var motion = Vector2()
  28. motion.x = Input.get_action_strength("move_right") - Input.get_action_strength("move_left")
  29. motion.y = Input.get_action_strength("move_down") - Input.get_action_strength("move_up")
  30. motion.y /= 2
  31. motion = motion.normalized() * MOTION_SPEED
  32. #warning-ignore:return_value_discarded
  33. set_velocity(motion)
  34. move_and_slide()
  35. var dir = velocity
  36. if dir.length() > 0:
  37. last_direction = dir
  38. update_animation("walk")
  39. else:
  40. update_animation("idle")
  41. func update_animation(anim_set):
  42. var angle = rad_to_deg(last_direction.angle()) + 22.5
  43. var slice_dir = floor(angle / 45)
  44. $Sprite2D.play(anim_directions[anim_set][slice_dir][0])
  45. $Sprite2D.flip_h = anim_directions[anim_set][slice_dir][1]