Skip to main content

CaveLayerManager

A class in Root.Township, Alta.Caves.

Owns the cave system: the seed, the layers that exist, each layer's entrance and depth, and their biomes. A scene singleton (Instance); mostly server side, with the seed synced to clients.

Constructors​

MemberWhat it is
CaveLayerManager()Unity makes components itself; use AddComponent or spawn a prefab instead of new.

Fields​

12 private and internal fields
MemberWhat it is
private List<GeneratedBiome> biomesGenerated biomes by layer, built in order by GetBiome.
private int currentLayerCountNumber of layers to spawn on start. Saved.
private List<float> depthsDepth of each layer's entrance below the manager.
private LocationChunk entranceChunkOverworld chunk the top cave layer connects to.
private List<Vector2> entranceDirectionsDirection of each layer's entrance.
private List<Vector2> entrancesHorizontal position of each layer's entrance.
private List<uint> layerPrefabsPrefab hash used for each layer, recorded as layers are created.
private static Logger loggerNLog logger for this class.
private PregeneratedCave pregeneratedBacking field for Pregenerated. When set, its seed is used.
private Dictionary<int, TaskCompletionSource<bool>> requestedLayers waiting for entrance information, by index.
private int seedBacking field for Seed. -1 means not chosen yet.
private MethodSync syncMethod sync for SyncSeed, sent on connect and after ClearCaveSystem.

Properties​

MemberWhat it is
static CaveLayerManager Instance { get; private set; }The scene's cave layer manager. Logs an error if a second one wakes.
Dictionary<int, ICaveLayer> Layers { get; }The layers made so far, by layer number (0 is the top).
HashFunction LayersSeedHash { get; private set; }Hash built from the seed that layers and biomes derive their own seeds from.
PregeneratedCave Pregenerated { get; }Optional pregenerated cave asset whose seed is used if set.
int Seed { get; }The cave seed. -1 until chosen; a random one wipes old cave saves.

Methods​

MemberWhat it is
async Task ClearCaveSystem()Server: unloads and wipes every layer's save, destroys the layers, picks a new seed and makes layer 0 again. Does not move players.
void Despawn(int layer)Drops a pending request for a layer that was never made. Does nothing to layers that exist.
async Task<GeneratedBiome> GetBiome(int layer)The generated biome for a layer, making the biomes above it first if needed.
override void Initialize()Sets up the network message that syncs the seed.
override void InitializeAsServer(SpawnData spawnData)Server: once the entity has a chunk, picks the seed and spawns the saved number of layers.
override void OnConnected(Player player)Server: sends the seed to a joining player.
void RequestPortalRegistering(ICaveLayer streamer)Connects a layer's chunk to the layer above (or the entrance chunk for layer 0) and registers its portal.
ICaveLayer SpawnWithoutGenerating(int layer)Returns the layer, making its streamer if it does not exist yet. Does not load or generate it.
async Task WaitForEntranceInformation(int layer)Waits until the layer's entrance position is known (set when the layer above finds its exit).
7 private and internal methods
MemberWhat it is
private void Awake()Sets Instance, logging an error if one exists, and initializes biome resources.
private ICaveLayer CreateLayer(int layer)Spawns the cave layer prefab for a layer index at its entrance and depth, and connects it to the layer above.
private void ExitReady(int layer, float depth, Vector2 exit, Vector2 direction)Called when a layer reports its exit; sets that as the entrance of the next layer down.
private void Prepare()Server: picks the seed (settings, pregenerated cave or random) and sets the top entrance if none is set.
private void PrepareSpawn(Chunk chunk)Server: once the manager is in a chunk, prepares and spawns the saved number of layers.
private void SetEntrance(int layer, float depth, Vector2 entrance, Vector2 direction)Records a layer's entrance, direction and depth, and wakes anything waiting on it in WaitForEntranceInformation.
private void SyncSeed(IPlayer player, Stream stream)Syncs the cave seed; clients build LayersSeedHash from it.

Used in​

Members of other types that take, return or hold a CaveLayerManager: