Player.gd 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 blocks = []
  9. var is_blocked:bool = false
  10. export (NodePath) var rayU
  11. export (NodePath) var rayD
  12. export (NodePath) var rayL
  13. export (NodePath) var rayR
  14. var raycast
  15. func _ready():
  16. grid = get_parent()
  17. turn(Vector2(0,1))
  18. pass
  19. func _physics_process(delta):
  20. direction = Vector2()
  21. if Input.is_action_pressed("ui_up"):
  22. direction.y -= 1
  23. elif Input.is_action_pressed("ui_down"):
  24. direction.y += 1
  25. elif Input.is_action_pressed("ui_left"):
  26. direction.x -= 1
  27. elif Input.is_action_pressed("ui_right"):
  28. direction.x += 1
  29. if direction != Vector2():
  30. turn(direction)
  31. if !raycast.is_colliding():
  32. position = get_position() + direction * grid.get_cell_size()
  33. pass
  34. func _on_area_entered(a):
  35. if a.get_parent() != $Position2D:
  36. print(a)
  37. blocks.append(a)
  38. is_blocked = true
  39. pass
  40. func _on_area_exited(a):
  41. blocks.erase(a)
  42. is_blocked = blocks.size()
  43. pass
  44. func turn(dir:Vector2):
  45. if dir.y < 0:
  46. raycast=get_node(rayU)
  47. elif dir.x < 0:
  48. raycast=get_node(rayL)
  49. elif dir.x > 0:
  50. raycast=get_node(rayR)
  51. else:
  52. raycast=get_node(rayD)
  53. pass