class_name Board extends Node2D enum Direction { NONE, UP, DOWN, LEFT, RIGHT } var buildings: Dictionary[Vector2i, Building] = {} var active_building: Building var active_tile_id: int = -1 var current_map_coord: Vector2i var prev_map_coord: Vector2i var is_placing_walls: bool = false: set(value): is_placing_walls = value if !is_placing_walls: _clear_wall_preview() var current_wall_direction: Direction = Direction.DOWN var real_wall_coords: Array[Vector2i] = [] @onready var tile_map: SceneTileMapLayer = %Tiles @onready var tile_preview_map: SceneTileMapLayer = %TilesPreview @onready var wall_map: SceneTileMapLayer = %Walls @onready var wall_preview_map: SceneTileMapLayer = %WallPreviews func _input(event: InputEvent) -> void: if event is InputEventMouseMotion: current_map_coord = tile_map.local_to_map(tile_map.get_local_mouse_position()) _place_wall_preview() _place_tile_preview() if active_building != null: active_building.position = tile_map.map_to_local(current_map_coord) - active_building.placement_point.position prev_map_coord = current_map_coord if event.is_action_pressed("select"): if is_placing_walls: _place_wall(current_map_coord, current_wall_direction) match current_wall_direction: Direction.UP: _place_wall(current_map_coord + Vector2i.UP, Direction.DOWN) Direction.DOWN: _place_wall(current_map_coord + Vector2i.DOWN, Direction.UP) Direction.LEFT: _place_wall(current_map_coord + Vector2i.LEFT, Direction.RIGHT) Direction.RIGHT: _place_wall(current_map_coord + Vector2i.RIGHT, Direction.LEFT) elif active_tile_id != -1: tile_map.set_cell(current_map_coord, 0, Vector2i.ZERO, active_tile_id) active_tile_id = -1 elif active_building != null: active_building.starting_coord = current_map_coord for coord in active_building.get_tile_coords(): buildings[coord] = active_building active_building.position = tile_map.map_to_local(current_map_coord) - active_building.placement_point.position active_building.modulate = Color(1, 1, 1, 1) active_building = null if event.is_action_pressed("rotate_wall_up") and is_placing_walls: current_wall_direction = get_next_direction(current_wall_direction) var wall_preview = wall_preview_map.get_cell_scene(current_map_coord) if is_instance_valid(wall_preview): wall_preview.set_wall(current_wall_direction) elif event.is_action_pressed("rotate_wall_down") and is_placing_walls: current_wall_direction = get_previous_direction(current_wall_direction) var wall_preview = wall_preview_map.get_cell_scene(current_map_coord) if is_instance_valid(wall_preview): wall_preview.set_wall(current_wall_direction) func set_active_tile(tile: Tile) -> void: active_tile_id = tile_map.tile_set.get_tile_id(tile) func set_active_building(building: Building) -> void: active_building = building.duplicate() add_child(active_building) active_building.modulate = Color(1, 1, 1, 0.5) func _place_wall(wall_coord: Vector2i, wall_direction: Direction) -> void: var current_wall = wall_map.get_cell_scene(wall_coord) if is_instance_valid(current_wall): current_wall.add_wall(wall_direction) else: var wall_preview = wall_map.get_cell_scene(wall_coord) if is_instance_valid(wall_preview): wall_preview.set_wall(wall_direction) func _place_wall_preview() -> void: if is_placing_walls: if prev_map_coord != current_map_coord: var previous_preview = wall_preview_map.get_cell_scene(prev_map_coord) if is_instance_valid(previous_preview): previous_preview.set_wall(Direction.NONE) var wall_preview = wall_preview_map.get_cell_scene(current_map_coord) if is_instance_valid(wall_preview): wall_preview.set_wall(current_wall_direction) func _clear_wall_preview() -> void: wall_preview_map.get_cell_scene(prev_map_coord).set_wall(Direction.NONE) wall_preview_map.get_cell_scene(current_map_coord).set_wall(Direction.NONE) func _place_tile_preview() -> void: if active_tile_id != -1: if prev_map_coord != current_map_coord: var prev_preview_tile_id = tile_preview_map.get_cell_alternative_tile(prev_map_coord) if prev_preview_tile_id != -1: tile_preview_map.set_cell(prev_map_coord) tile_preview_map.set_cell(current_map_coord, 0, Vector2i.ZERO, active_tile_id) static func get_next_direction(direction: Direction) -> Direction: match direction: Direction.UP: return Direction.RIGHT Direction.RIGHT: return Direction.DOWN Direction.DOWN: return Direction.LEFT Direction.LEFT: return Direction.UP _: return direction static func get_previous_direction(direction: Direction) -> Direction: match direction: Direction.UP: return Direction.LEFT Direction.RIGHT: return Direction.UP Direction.DOWN: return Direction.RIGHT Direction.LEFT: return Direction.DOWN _: return direction static func get_direction_vector(direction: Direction) -> Vector2i: match direction: Direction.UP: return Vector2i.UP Direction.RIGHT: return Vector2i.RIGHT Direction.DOWN: return Vector2i.DOWN Direction.LEFT: return Vector2i.LEFT _: return Vector2i.ZERO #func generate() -> void: #for child in get_children(): #child.queue_free() #for x in range(width): #for y in range(height): #var tile: Control #if x == 1 and y == 1: #tile = spawn_scene.instantiate() #else: #tile = ground_scene.instantiate() #add_child(tile) #tile.position = get_coordinate(x, y) #tile.owner = self