Player.gd 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 is_moving = false
  20. var target_pos
  21. func _ready():
  22. grid = get_parent()
  23. posix = int (position.x / 64)
  24. posiy = int (position.y / 64)
  25. turn(dire)
  26. tween = $Tween
  27. tween.connect_into(self)
  28. rayFront = $RayFront
  29. rayLeft = $RayLeft
  30. rayRight = $RayRight
  31. func _physics_process(delta):
  32. if is_moving:
  33. return
  34. if Input.is_action_pressed("ui_left"):
  35. dire -= 1
  36. if dire < 0:
  37. dire = 3
  38. tween.rotate_char(self, rotation_degrees - 90)
  39. elif Input.is_action_pressed("ui_right"):
  40. dire += 1
  41. if dire > 3:
  42. dire = 0
  43. tween.rotate_char(self, rotation_degrees + 90)
  44. turn(dire)
  45. posix += dirx
  46. posiy += diry
  47. target_pos = Vector2(posix * 64 + 32, posiy * 64 + 32)
  48. tween.move_char(self, target_pos)
  49. is_moving = true
  50. tween.start()
  51. func _on_tween_completed(o, k):
  52. is_moving = false
  53. func turn(dir:int):
  54. dirx = 0
  55. diry = 0
  56. if dir == DIR_UP:
  57. diry -= 1
  58. elif dir == DIR_RIGHT:
  59. dirx += 1
  60. elif dir == DIR_DOWN:
  61. diry += 1
  62. else:
  63. dirx -= 1