Liner.gd 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. extends Area2D
  2. # Handle liner logic and events
  3. signal crash
  4. signal arena_removed
  5. signal drift_started
  6. signal drift_ended
  7. enum Direction {
  8. UP = 0
  9. RIGHT = 1
  10. DOWN = 2
  11. LEFT = 3
  12. }
  13. enum Side {
  14. LEFT = -1
  15. RIGHT = 1
  16. }
  17. const cell_size = 64
  18. const cell_half_size = 32
  19. const LEVEL_SPAWN_DURATION = 2
  20. # State
  21. var running: bool = false
  22. var drifting: bool = false
  23. var landed: bool = false
  24. # Movement
  25. var posix
  26. var posiy
  27. var dirx = 0
  28. var diry = 0
  29. var dire = 0
  30. var dire_delta = 0
  31. var target_pos
  32. # Controls
  33. var turn_left_action:String
  34. var turn_right_action:String
  35. var grid:TileMap
  36. onready var mover = $Mover
  37. func _ready():
  38. assert(turn_left_action)
  39. assert(turn_right_action)
  40. func _unhandled_input(event):
  41. if event.is_pressed():
  42. if event.is_action(turn_left_action):
  43. prepare_turn(Side.LEFT)
  44. get_tree().set_input_as_handled()
  45. elif event.is_action(turn_right_action):
  46. prepare_turn(Side.RIGHT)
  47. get_tree().set_input_as_handled()
  48. func _on_round_start():
  49. assert(grid)
  50. run()
  51. func _on_round_won():
  52. stop()
  53. func _on_arena_removed():
  54. grid = null
  55. landed = false
  56. emit_signal("arena_removed")
  57. func _on_move_completed(_o, key):
  58. if (key == ":position"):
  59. move()
  60. func _on_crash(body):
  61. if landed and running:
  62. stop()
  63. emit_signal("crash")
  64. func spawn(newGrid, newPosition, newOrientation):
  65. # Update arena
  66. grid = newGrid
  67. # Adjust spawn situation
  68. dire_delta = 0
  69. posix = int (newPosition.x / cell_size)
  70. posiy = int (newPosition.y / cell_size)
  71. dire = int((newOrientation + 45) / 90)
  72. apply_turn()
  73. # Animate spawning
  74. mover.rotate_char_during(self, rotation_degrees, newOrientation, LEVEL_SPAWN_DURATION)
  75. mover.move_char_during(self, newPosition, LEVEL_SPAWN_DURATION)
  76. mover.start()
  77. func generate_wall():
  78. # TODO : use enum for blocks
  79. grid.set_cell(posix - dirx, posiy - diry, 1)
  80. func prepare_turn(left_or_right: int):
  81. if !running:
  82. return
  83. var current_angle = (dire + dire_delta) * 90
  84. dire_delta += left_or_right
  85. if dire_delta > Side.RIGHT:
  86. dire_delta = Side.RIGHT
  87. return
  88. elif dire_delta < Side.LEFT:
  89. dire_delta = Side.LEFT
  90. return
  91. var aim_angle = current_angle + 90 * left_or_right
  92. mover.rotate_char(self, current_angle, aim_angle)
  93. mover.start()
  94. func can_turn():
  95. return !has_block_on(dire_delta)
  96. func apply_turn():
  97. dire += dire_delta
  98. dire_delta = 0
  99. if dire < 0:
  100. dire = 3
  101. elif dire > 3:
  102. dire = 0
  103. dirx = 0
  104. diry = 0
  105. if dire == Direction.UP:
  106. diry -= 1
  107. elif dire == Direction.RIGHT:
  108. dirx += 1
  109. elif dire == Direction.DOWN:
  110. diry += 1
  111. elif dire == Direction.LEFT:
  112. dirx -= 1
  113. else:
  114. push_error("dire out of range")
  115. func apply_drift():
  116. if !drifting:
  117. drifting = true
  118. emit_signal("drift_started")
  119. func end_drift():
  120. if drifting:
  121. drifting = false
  122. emit_signal("drift_ended")
  123. func apply_wall_boost():
  124. var n := 0
  125. if has_block_on(Side.LEFT):
  126. n += 1
  127. if has_block_on(Side.RIGHT):
  128. n += 1
  129. mover.set_boost(n)
  130. func end_wall_boost():
  131. mover.set_boost(0)
  132. func move():
  133. if !running:
  134. return
  135. generate_wall()
  136. if is_transversal():
  137. if can_turn():
  138. end_drift()
  139. apply_turn()
  140. else:
  141. apply_drift()
  142. end_wall_boost()
  143. else:
  144. end_drift()
  145. apply_wall_boost()
  146. go_forward()
  147. func go_forward():
  148. posix += dirx
  149. posiy += diry
  150. target_pos = Vector2(posix * cell_size + cell_half_size, posiy * cell_size + cell_half_size)
  151. mover.move_char(self, target_pos)
  152. mover.start()
  153. func has_block_on(left_or_right:int):
  154. var bposx:int = posix - diry * left_or_right
  155. var bposy:int = posiy + dirx * left_or_right
  156. # TODO : use enum for blocks
  157. return grid.get_cell(bposx, bposy) == 1
  158. func stop():
  159. if running:
  160. remove_from_group("running")
  161. generate_wall()
  162. end_wall_boost()
  163. running = false
  164. func run():
  165. landed = true # TODO : land at game signal `land`
  166. running = true
  167. add_to_group("running")
  168. move()
  169. func is_running():
  170. return running
  171. func is_linear():
  172. return dire_delta == 0
  173. func is_transversal():
  174. return !is_linear()