Player.gd 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. extends Area2D
  2. # Nodes
  3. var grid:TileMap
  4. onready var tween = $Tween
  5. # Misc
  6. const cell_size = 64
  7. const cell_half_size = 32
  8. var running = 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. var dire
  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. assert(turn_left_action)
  34. assert(turn_right_action)
  35. tween.connect_into(self)
  36. func _unhandled_input(event):
  37. if event.is_pressed():
  38. if event.is_action(turn_left_action):
  39. prepare_turn(Side.LEFT)
  40. get_tree().set_input_as_handled()
  41. elif event.is_action(turn_right_action):
  42. prepare_turn(Side.RIGHT)
  43. get_tree().set_input_as_handled()
  44. func spawn(newGrid, newPosition, newOrientation):
  45. # Update arena
  46. grid = newGrid
  47. # Adjust spawn situation
  48. posix = int (newPosition.x / cell_size)
  49. posiy = int (newPosition.y / cell_size)
  50. dire = int((newOrientation + 45) / 90)
  51. apply_turn()
  52. # Animate spawning
  53. tween.rotate_char(self, rotation_degrees, newOrientation)
  54. tween.move_char(self, newPosition)
  55. tween.start()
  56. func _on_round_start():
  57. assert(grid)
  58. spring()
  59. func _on_tween_completed(_o, key):
  60. if (key == ":position"):
  61. move()
  62. func _on_crash(body):
  63. die()
  64. generate_wall()
  65. # TODO : use enum for blocks
  66. grid.set_cell(posix, posiy, 2)
  67. func generate_wall():
  68. # TODO : use enum for blocks
  69. grid.set_cell(posix - dirx, posiy - diry, 1)
  70. func prepare_turn(left_or_right:int):
  71. var current_angle = (dire + dire_delta) * 90
  72. dire_delta += left_or_right
  73. if dire_delta > Side.RIGHT:
  74. dire_delta = Side.RIGHT
  75. return
  76. elif dire_delta < Side.LEFT:
  77. dire_delta = Side.LEFT
  78. return
  79. var aim_angle = current_angle + 90 * left_or_right
  80. tween.rotate_char(self, current_angle, aim_angle)
  81. tween.start()
  82. func can_turn():
  83. return dire_delta != 0 and !has_block_on(dire_delta)
  84. func apply_turn():
  85. dire += dire_delta
  86. dire_delta = 0
  87. if dire < 0:
  88. dire = 3
  89. elif dire > 3:
  90. dire = 0
  91. dirx = 0
  92. diry = 0
  93. if dire == Direction.UP:
  94. diry -= 1
  95. elif dire == Direction.RIGHT:
  96. dirx += 1
  97. elif dire == Direction.DOWN:
  98. diry += 1
  99. elif dire == Direction.LEFT:
  100. dirx -= 1
  101. else:
  102. push_error("dire out of range")
  103. func move():
  104. if !running:
  105. return
  106. generate_wall()
  107. if can_turn():
  108. apply_turn()
  109. go_forward()
  110. func go_forward():
  111. posix += dirx
  112. posiy += diry
  113. target_pos = Vector2(posix * cell_size + cell_half_size, posiy * cell_size + cell_half_size)
  114. tween.move_char(self, target_pos)
  115. tween.start()
  116. func has_block_on(left_or_right:int):
  117. var bposx:int = posix - diry * left_or_right
  118. var bposy:int = posiy + dirx * left_or_right
  119. # TODO : use enum for blocks
  120. return grid.get_cell(bposx, bposy) == 1
  121. func die():
  122. if running:
  123. remove_from_group("running")
  124. running = false
  125. func spring():
  126. running = true
  127. add_to_group("running")
  128. move()
  129. func is_running():
  130. return running