Player.gd 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. update_dirxy()
  42. tween.connect_into(self)
  43. func _unhandled_input(event):
  44. if event.is_pressed():
  45. if event.is_action(turn_left_action):
  46. tween.rotate_char(self, rotation_degrees - 90)
  47. tween.start()
  48. dire_delta = Side.LEFT
  49. get_tree().set_input_as_handled()
  50. elif event.is_action(turn_right_action):
  51. tween.rotate_char(self, rotation_degrees + 90)
  52. tween.start()
  53. dire_delta = Side.RIGHT
  54. get_tree().set_input_as_handled()
  55. func _on_game_start():
  56. move()
  57. func _on_tween_completed(_o, key):
  58. if (key == ":position"):
  59. move()
  60. func _on_crash(body):
  61. die()
  62. generate_wall()
  63. grid.set_cell(posix, posiy, 2)
  64. func generate_wall():
  65. grid.set_cell(posix - dirx, posiy - diry, 1)
  66. func move():
  67. if !alive:
  68. return
  69. generate_wall()
  70. if dire_delta == Side.LEFT and !has_block_on(Side.LEFT):
  71. # TODO Undo sprite turn if blocked
  72. dire += dire_delta
  73. if dire < 0:
  74. dire = 3
  75. elif dire_delta == Side.RIGHT and !has_block_on(Side.RIGHT):
  76. dire += dire_delta
  77. if dire > 3:
  78. dire = 0
  79. update_dirxy()
  80. dire_delta = 0
  81. posix += dirx
  82. posiy += diry
  83. target_pos = Vector2(posix * cell_size + cell_half_size, posiy * cell_size + cell_half_size)
  84. tween.move_char(self, target_pos)
  85. tween.start()
  86. func has_block_on(left_or_right:int):
  87. var bposx:int = posix - diry * left_or_right
  88. var bposy:int = posiy + dirx * left_or_right
  89. # TODO : use enum for blocks
  90. return grid.get_cell(bposx, bposy) == 1
  91. func update_dirxy():
  92. dirx = 0
  93. diry = 0
  94. if dire == Direction.UP:
  95. diry -= 1
  96. elif dire == Direction.RIGHT:
  97. dirx += 1
  98. elif dire == Direction.DOWN:
  99. diry += 1
  100. elif dire == Direction.LEFT:
  101. dirx -= 1
  102. else:
  103. push_error("dire out of range")
  104. func die():
  105. alive = false
  106. remove_from_group("living")
  107. func spring():
  108. alive = true
  109. add_to_group("living")
  110. func is_alive():
  111. return alive