extends Object class_name PathGenerator var map_config : MapGeneratorResource = preload("res://resources/map_generator_resource.tres") var _tile_map : TileMapLayer var _path : Array[Vector2i] enum TileTransform { ROTATE_0 = 0, ROTATE_90 = TileSetAtlasSource.TRANSFORM_TRANSPOSE | TileSetAtlasSource.TRANSFORM_FLIP_H, ROTATE_180 = TileSetAtlasSource.TRANSFORM_FLIP_H | TileSetAtlasSource.TRANSFORM_FLIP_V, ROTATE_270 = TileSetAtlasSource.TRANSFORM_TRANSPOSE | TileSetAtlasSource.TRANSFORM_FLIP_V, } func _init(tile_map_layer: TileMapLayer): _tile_map = tile_map_layer func generate_path() -> Array[Vector2i]: _path.clear() var x : int = 0 var y : int = randi_range(1, map_config.grid_height - 2) while x < map_config.grid_width: if not _path.has(Vector2i(x,y)): _path.append(Vector2i(x, y)) var choice : int = randi_range(0,2) # every even tile goes right to leave room for towers if choice == 0 || x % 2 == 0 || x == map_config.grid_width - 1: x += 1 elif choice == 1 && y < map_config.grid_height - 2 && !_path.has(Vector2i(x, y + 1)): y += 1 elif choice == 2 && y > 1 && !_path.has(Vector2i(x, y - 1)): y -= 1 return _path func draw_path(): for i in _path: var score : int = _get_tile_score(i) var atlas_coords : Vector2i = map_config.atlas_coords["PATH_EMPTY"] var rot : TileTransform = TileTransform.ROTATE_0 match score: 2, 8, 10: atlas_coords = map_config.atlas_coords["PATH_STRAIGHT"] rot = TileTransform.ROTATE_90 1, 4, 5: atlas_coords = map_config.atlas_coords["PATH_STRAIGHT"] 3: atlas_coords = map_config.atlas_coords["PATH_CORNER"] rot = TileTransform.ROTATE_270 6: atlas_coords = map_config.atlas_coords["PATH_CORNER"] 12: atlas_coords = map_config.atlas_coords["PATH_CORNER"] rot = TileTransform.ROTATE_90 9: atlas_coords = map_config.atlas_coords["PATH_CORNER"] rot = TileTransform.ROTATE_180 _display_tile(atlas_coords, rot, i) func _display_tile(coords : Vector2i, rot : TileTransform, pos : Vector2i): _tile_map.set_cell(pos, 0, coords, rot) func _get_tile_score(tile : Vector2i) -> int: var score : int = 0 var x : int = tile.x var y : int = tile.y score += 1 if _path.has(Vector2i(x, y - 1)) else 0 score += 2 if _path.has(Vector2i(x + 1, y)) else 0 score += 4 if _path.has(Vector2i(x, y + 1)) else 0 score += 8 if _path.has(Vector2i(x - 1, y)) else 0 return score