Player.gd 3.2 KB

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