Player.gd 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. const DEG_LIST = [DEG_UP, DEG_RIGHT, DEG_DOWN, DEG_LEFT]
  8. const DIR_UP = 0
  9. const DIR_RIGHT = 1
  10. const DIR_DOWN = 2
  11. const DIR_LEFT = 3
  12. var posix
  13. var posiy
  14. var dirx = 0
  15. var diry = 0
  16. var dire = DIR_UP
  17. var grid
  18. var blocks = []
  19. var is_blocked:bool = false
  20. export (NodePath) var rayU
  21. export (NodePath) var rayD
  22. export (NodePath) var rayL
  23. export (NodePath) var rayR
  24. var raycast
  25. func _ready():
  26. grid = get_parent()
  27. posix = int (position.x / 64)
  28. posiy = int (position.y / 64)
  29. turn(DIR_UP)
  30. pass
  31. func _physics_process(delta):
  32. if Input.is_action_pressed("ui_left"):
  33. dire -= 1
  34. if dire < 0:
  35. dire = 3
  36. update_avatar_dir()
  37. elif Input.is_action_pressed("ui_right"):
  38. dire += 1
  39. if dire > 3:
  40. dire = 0
  41. update_avatar_dir()
  42. if Input.is_action_pressed("ui_up"):
  43. turn(dire)
  44. if !raycast.is_colliding():
  45. posix += dirx
  46. posiy += diry
  47. position = Vector2(posix * 64 + 32, posiy * 64 + 32)
  48. pass
  49. func _on_area_entered(a):
  50. if a.get_parent() != $Position2D:
  51. print(a)
  52. blocks.append(a)
  53. is_blocked = true
  54. pass
  55. func _on_area_exited(a):
  56. blocks.erase(a)
  57. is_blocked = blocks.size()
  58. pass
  59. func turn(dir:int):
  60. dirx = 0
  61. diry = 0
  62. if dir == DIR_UP:
  63. raycast=get_node(rayU)
  64. diry -= 1
  65. elif dir == DIR_RIGHT:
  66. raycast=get_node(rayR)
  67. dirx += 1
  68. elif dir == DIR_DOWN:
  69. raycast=get_node(rayD)
  70. diry += 1
  71. else:
  72. raycast=get_node(rayL)
  73. dirx -= 1
  74. pass
  75. func update_avatar_dir():
  76. $Sprite.rotation_degrees = DEG_LIST[dire]