Player.gd 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. extends Area2D
  2. # Nodes
  3. var grid
  4. var tween
  5. var rayFront
  6. var rayLeft
  7. var rayRight
  8. # Enum
  9. const DIR_UP = 0
  10. const DIR_RIGHT = 1
  11. const DIR_DOWN = 2
  12. const DIR_LEFT = 3
  13. # Movement
  14. var posix
  15. var posiy
  16. var dirx = 0
  17. var diry = 0
  18. export var dire = DIR_UP
  19. var dire_delta = 0
  20. var is_moving = false
  21. var target_pos
  22. func _ready():
  23. grid = get_parent()
  24. posix = int (position.x / 64)
  25. posiy = int (position.y / 64)
  26. turn(dire)
  27. tween = $Tween
  28. tween.connect_into(self)
  29. rayFront = $RayFront
  30. rayLeft = $RayLeft
  31. rayRight = $RayRight
  32. func _physics_process(delta):
  33. if Input.is_action_just_pressed("ui_left"):
  34. dire_delta = -1
  35. elif Input.is_action_just_pressed("ui_right"):
  36. dire_delta = 1
  37. if is_moving:
  38. return
  39. dire += dire_delta
  40. if dire_delta == -1:
  41. tween.rotate_char(self, rotation_degrees - 90)
  42. if dire < 0:
  43. dire = 3
  44. elif dire_delta == 1:
  45. tween.rotate_char(self, rotation_degrees + 90)
  46. if dire > 3:
  47. dire = 0
  48. turn(dire)
  49. dire_delta = 0
  50. posix += dirx
  51. posiy += diry
  52. target_pos = Vector2(posix * 64 + 32, posiy * 64 + 32)
  53. tween.move_char(self, target_pos)
  54. is_moving = true
  55. tween.start()
  56. func _on_tween_completed(o, k):
  57. is_moving = false
  58. func turn(dir:int):
  59. dirx = 0
  60. diry = 0
  61. if dir == DIR_UP:
  62. diry -= 1
  63. elif dir == DIR_RIGHT:
  64. dirx += 1
  65. elif dir == DIR_DOWN:
  66. diry += 1
  67. else:
  68. dirx -= 1