hexmapper/hexmapper/scenes/MainMap/map/map_layer_logic.gd

38 lines
1.1 KiB
GDScript

extends TileMapLayer
signal hex_clicked(cell_pos:Vector2, atlas_coords:Vector2)
var interaction_enabled:bool = true
func _ready():
print("TileMapLayer is alive")
func _input(event: InputEvent):
if not interaction_enabled: return
if %MapMenuContainer.visible==true: return
if event is InputEventMouseButton:
if event.pressed==false: return
var mouse_pos = get_local_mouse_position()
#print("Local Mouse Position:", mouse_pos)
if mouse_pos.y < 0: return # outside of TileMap
var cell_pos = local_to_map(mouse_pos)
var source_id = get_cell_source_id(cell_pos)
if source_id > -1:
var atlas_coords = get_cell_atlas_coords(cell_pos)
hex_clicked.emit(cell_pos, atlas_coords)
#Example: Place/erase tile on click
if event.button_index == MOUSE_BUTTON_LEFT:
print("Click left")
var atlas_source_id = self.tile_set.get_source_id(0) # source id of first atlas in TileMapLayer
set_cell(cell_pos, atlas_source_id, Vector2i(1, 2)) # Place tile
elif event.button_index == MOUSE_BUTTON_RIGHT:
print("Click right")
set_cell(cell_pos, -1) # Erase tile