Player.gd 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. extends Area2D
  2. # Nodes
  3. var grid:TileMap
  4. onready var tween = $Tween
  5. # Misc
  6. var cell_size
  7. var cell_half_size
  8. var alive = false
  9. # Enum
  10. enum Direction {
  11. UP = 0
  12. RIGHT = 1
  13. DOWN = 2
  14. LEFT = 3
  15. }
  16. enum Side {
  17. LEFT = -1
  18. RIGHT = 1
  19. }
  20. # Movement
  21. var posix
  22. var posiy
  23. var dirx = 0
  24. var diry = 0
  25. export var dire = Direction.UP
  26. var dire_delta = 0
  27. var target_pos
  28. # Controls
  29. export var turn_left_action:String
  30. export var turn_right_action:String
  31. func _ready():
  32. add_to_group("players")
  33. spring()
  34. assert(grid)
  35. assert(turn_left_action)
  36. assert(turn_right_action)
  37. cell_size = int (grid.get_cell_size().x)
  38. cell_half_size = cell_size / 2
  39. posix = int (position.x / 64)
  40. posiy = int (position.y / 64)
  41. apply_turn()
  42. tween.connect_into(self)
  43. func _unhandled_input(event):
  44. if event.is_pressed():
  45. if event.is_action(turn_left_action):
  46. prepare_turn(Side.LEFT)
  47. get_tree().set_input_as_handled()
  48. elif event.is_action(turn_right_action):
  49. prepare_turn(Side.RIGHT)
  50. get_tree().set_input_as_handled()
  51. func _on_game_start():
  52. move()
  53. func _on_tween_completed(_o, key):
  54. if (key == ":position"):
  55. move()
  56. func _on_crash(body):
  57. die()
  58. generate_wall()
  59. # TODO : use enum for blocks
  60. grid.set_cell(posix, posiy, 2)
  61. func generate_wall():
  62. # TODO : use enum for blocks
  63. grid.set_cell(posix - dirx, posiy - diry, 1)
  64. func prepare_turn(left_or_right:int):
  65. var current_angle = (dire + dire_delta) * 90
  66. dire_delta += left_or_right
  67. if dire_delta > Side.RIGHT:
  68. dire_delta = Side.RIGHT
  69. return
  70. elif dire_delta < Side.LEFT:
  71. dire_delta = Side.LEFT
  72. return
  73. var aim_angle = current_angle + 90 * left_or_right
  74. tween.rotate_char(self, current_angle, aim_angle)
  75. tween.start()
  76. func can_turn():
  77. return dire_delta != 0 and !has_block_on(dire_delta)
  78. func apply_turn():
  79. dire += dire_delta
  80. dire_delta = 0
  81. if dire < 0:
  82. dire = 3
  83. elif dire > 3:
  84. dire = 0
  85. dirx = 0
  86. diry = 0
  87. if dire == Direction.UP:
  88. diry -= 1
  89. elif dire == Direction.RIGHT:
  90. dirx += 1
  91. elif dire == Direction.DOWN:
  92. diry += 1
  93. elif dire == Direction.LEFT:
  94. dirx -= 1
  95. else:
  96. push_error("dire out of range")
  97. func move():
  98. if !alive:
  99. return
  100. generate_wall()
  101. if can_turn():
  102. apply_turn()
  103. go_forward()
  104. func go_forward():
  105. posix += dirx
  106. posiy += diry
  107. target_pos = Vector2(posix * cell_size + cell_half_size, posiy * cell_size + cell_half_size)
  108. tween.move_char(self, target_pos)
  109. tween.start()
  110. func has_block_on(left_or_right:int):
  111. var bposx:int = posix - diry * left_or_right
  112. var bposy:int = posiy + dirx * left_or_right
  113. # TODO : use enum for blocks
  114. return grid.get_cell(bposx, bposy) == 1
  115. func die():
  116. alive = false
  117. remove_from_group("living")
  118. func spring():
  119. alive = true
  120. add_to_group("living")
  121. func is_alive():
  122. return alive