extends Node const GRID_SIZE := 64 var world_grid: TileMap var world_data: Dictionary = {} var res_locations := {} func init(grid: TileMap) -> void: world_grid = grid func world_to_grid(world_loc: Vector2) -> Vector2i: return Vector2i(floor(world_loc.x / GRID_SIZE), floor(world_loc.y / GRID_SIZE)) func grid_to_world_center(grid_loc: Vector2i) -> Vector2: return Vector2((grid_loc.x + 0.5) * GRID_SIZE, (grid_loc.y + 0.5) * GRID_SIZE) func set_selected_tile(tile: Vector2i) -> void: world_grid.clear_layer(Constants.TilemapLayers.CURSOR) world_grid.set_cell(Constants.TilemapLayers.CURSOR, tile,0,Vector2i(0,7)) func corrupt_location(loc: Vector2i) -> bool: return world_data[loc].change_layer(Constants.TilemapLayers.CORRUPTION, true) func get_corrupted_cells() -> Array[Vector2i]: return world_data.values().filter(func(cell: CellData): return cell.is_corrupted()).map(func(cell: CellData) -> Vector2i: return cell._pos) func change_location_data(pos: Vector2i, layer: int, data: Vector2i) -> void: if not world_data.has(pos): world_data[pos] = CellData.new(pos) world_data[pos].change_layer(layer, data) world_grid.set_cell(layer, pos, 0, data) func get_location_data(pos: Vector2i) -> CellData: if world_data.has(pos): return world_data[pos] return null func change_location_resource(pos: Vector2i, data: GameResource) -> void: world_data[pos].change_resource(data) world_grid.set_cell(Constants.TilemapLayers.ENVIRONMENT, pos, 0, data.atlas_location) if not res_locations.has(data): res_locations[data] = [] res_locations[data].append(Grid.grid_to_world_center(pos)) func get_nearest_resource(pos: Vector2, data: GameResource) -> Vector2: var distance: float = 9999 var nearest: Vector2 for location: Vector2 in res_locations[data]: var _dist = pos.distance_to(location) if _dist < distance: distance = _dist nearest = location return nearest func change_location_building(pos: Vector2i, data: BuildingBase) -> void: world_data[pos].change_building(data)