grid.gd 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. extends TileMap
  2. enum CellType { ACTOR, OBSTACLE, OBJECT }
  3. @export var dialogue_ui: Node
  4. func _ready():
  5. for child in get_children():
  6. set_cell(0, local_to_map(child.position), child.type, Vector2i.ZERO)
  7. func get_cell_pawn(cell, type = CellType.ACTOR):
  8. for node in get_children():
  9. if node.type != type:
  10. continue
  11. if local_to_map(node.position) == cell:
  12. return(node)
  13. func request_move(pawn, direction: Vector2i):
  14. var cell_start = local_to_map(pawn.position)
  15. var cell_target = cell_start + direction
  16. var cell_tile_id = get_cell_source_id(0, cell_target)
  17. match cell_tile_id:
  18. -1:
  19. set_cell(0, cell_target, CellType.ACTOR, Vector2i.ZERO)
  20. set_cell(0, cell_start, -1, Vector2i.ZERO)
  21. return map_to_local(cell_target)
  22. CellType.OBJECT, CellType.ACTOR:
  23. var target_pawn = get_cell_pawn(cell_target, cell_tile_id)
  24. #print("Cell %s contains %s" % [cell_target, target_pawn.name])
  25. if not target_pawn.has_node("DialoguePlayer"):
  26. return
  27. dialogue_ui.show_dialogue(pawn, target_pawn.get_node("DialoguePlayer"))