Player.gd 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. extends Area2D
  2. var direction = Vector2()
  3. const DEG_UP = 0
  4. const DEG_RIGHT = 90
  5. const DEG_DOWN = 180
  6. const DEG_LEFT = 270
  7. var grid
  8. var is_moving = false
  9. var tween
  10. var target_pos = Vector2()
  11. var blocks = []
  12. var is_blocked:bool = false
  13. export (NodePath) var rayU
  14. export (NodePath) var rayD
  15. export (NodePath) var rayL
  16. export (NodePath) var rayR
  17. var raycast
  18. func _ready():
  19. grid = get_parent()
  20. tween = $Tween
  21. tween.connect_into(self)
  22. turn(Vector2(0,1))
  23. pass
  24. func _physics_process(delta):
  25. direction = Vector2()
  26. if Input.is_action_pressed("ui_up"):
  27. direction.y -= 1
  28. elif Input.is_action_pressed("ui_down"):
  29. direction.y += 1
  30. elif Input.is_action_pressed("ui_left"):
  31. direction.x -= 1
  32. elif Input.is_action_pressed("ui_right"):
  33. direction.x += 1
  34. if !is_moving and direction != Vector2():
  35. turn(direction)
  36. if !raycast.is_colliding():
  37. target_pos = get_position() + direction * grid.get_cell_size()
  38. tween.move_char(self, target_pos)
  39. is_moving = true
  40. pass
  41. func _on_tween_completed(o, k):
  42. is_moving = false
  43. pass
  44. func _on_area_entered(a):
  45. if a.get_parent() != $Position2D:
  46. print(a)
  47. blocks.append(a)
  48. is_blocked = true
  49. pass
  50. func _on_area_exited(a):
  51. blocks.erase(a)
  52. is_blocked = blocks.size()
  53. pass
  54. func turn(dir:Vector2):
  55. if dir.y < 0:
  56. raycast=get_node(rayU)
  57. elif dir.x < 0:
  58. raycast=get_node(rayL)
  59. elif dir.x > 0:
  60. raycast=get_node(rayR)
  61. else:
  62. raycast=get_node(rayD)
  63. pass