Skip to main content

CaveLayerStreamer

A class in Root.Township, Alta.Chunks.

Streams one procedurally generated cave layer as its own location chunk below the surface. The CaveLayerManager places it at its entrance; it spawns the layer's generator content, positions the culling portal and syncs the layout to clients.

Constructors​

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

Fields​

MemberWhat it is
const int MaxNumberOfLayersUpper bound used when syncing the layer number.
22 private and internal fields
MemberWhat it is
private Material androidTerrainMaterialTerrain material used on Android while the local player is in the layer.
private Material androidTerrainMaterialLOD1Cheaper terrain material used on Android while the local player is elsewhere.
private LocationChunk chunkBacking field for Chunk.
private Vector3 chunkBoundsExtentsHalf size of the layer chunk's bounds, which sit below the layer's position. 200 by 7.5 by 200 by default.
private Ecosystem ecosystemBacking field for Ecosystem.
private Vector2 entranceDirectionBacking field for EntranceDirection.
private GenerationBase generatorCache for Generator.
private bool isConnectTrue only while sending the setup to a joining player.
private bool isGeneratedWhether the layer has been generated before. Saved; first generation sets IsGeneratingDynamic on the server.
private int layerBacking field for LayerIdentifier.
private float layerContentOffsetLocal Y of the spawned layer content. -7.5 by default.
private NetworkEntity layerEntityThe spawned layer content entity (not saved). Null when unloaded.
private bool loadedTrue once Generate has spawned the content, so it runs only once per load.
private static Logger loggerNLog logger for this class.
private RecastGraph navGraphThe A* recast graph for this layer. Null until GeneratePathfinding builds it.
private bool needsMatrixAlways true, so the world to local matrix is worked out on every renderer register call.
private ICaveLayer neighborStreamerThe cave layer in front of the portal (the one above).
private CullingPortal portalThe culling portal between this layer and the one above. Backs PortalTransform.
private MethodSync syncLayerMethod sync for the manager and layer number, sent to joining players.
private MethodSyncStruct<Vector3> syncLayerSetupMethod sync for the layer position and portal pose. Sent on generate and to joining players.
private Renderer terrainRendererThe layer's terrain renderer, set by RegisterTerrainRenderer.
private Matrix4x4 worldToLocalThe streamer's world to local matrix, used to pick renderers in the layer's height band.

Properties​

MemberWhat it is
Dictionary<Renderer, Vector3> AllStaticRenderers { get; }Static renderers in the layer's band, with their positions. Clients only; shown only while the local player is in the chunk.
Bounds Bounds { get; set; }Bounds used for the layer's pathfinding graph.
LocationChunk Chunk { get; private set; }The location chunk on the same object.
float Depth { get; set; }Depth of the layer below the manager. Set with SetEntrance.
Ecosystem Ecosystem { get; }Ecosystem asset for this layer.
Vector2 Entrance { get; set; }Horizontal position of the layer's entrance.
Vector2 EntranceDirection { get; }Horizontal direction the entrance faces.
GenerationBase Generator { get; }The generation component in the spawned layer content, or null before Generate.
bool IsGeneratingDynamic { get; private set; }True on the server when the layer is being generated for the first time.
int LayerIdentifier { get; }The layer number.
CaveLayerManager Manager { get; private set; }The cave layer manager that owns this layer.
bool NeedToGeneratePathFinding { get; }True until the pathfinding graph has been built.
Transform PortalTransform { get; private set; }Transform of the culling portal between this layer and the one above.

Events​

MemberWhat it is
event Action ClientLoadingFinishedRaised by OnClientLoadingFinished.
event Action<int, float, Vector2, Vector2> ExitReadyRaised with the layer, the next layer's depth, and the exit position and direction when the exit is placed.
event Action hasModifiedRenderersRaised when the set of static renderers changes.

Methods​

MemberWhat it is
void DeregisterRenderers(Renderer[] renderers)Removes renderers from AllStaticRenderers. Does nothing on a headless server.
async Task Generate()Spawns the layer content prefab once, positions the portal, syncs setup to clients and runs the generator.
IEnumerator GeneratePathfinding() (iterator)Coroutine that scans an A* graph over the layer's bounds, if not already built.
string GetIdentifier(int layer)Returns the chunk identifier for a layer number, such as "Cave Layer 2".
override void Initialize()Sets up the network messages for the layer number and layout.
void InitializeCaveLayer(CaveLayerManager manager, int layerId)Links the streamer to its manager and layer number and sets the chunk's bounds and identifier.
void OnClientLoadingFinished()Raises ClientLoadingFinished.
override void OnConnected(Player player)Server: sends the layer number and layout to a joining player.
void OnExitReady(Vector2 exit, Vector2 exitDirection)Called by the generator when the exit is placed; raises ExitReady for the layer below.
void RegisterPortal(LocationChunk chunk, ICaveLayer other)Links the portal to the chunk in front of it and its cave layer.
void RegisterRenderers(Renderer[] renderers)Adds renderers that sit in the layer's vertical band to AllStaticRenderers. Does nothing on a headless server.
void RegisterTerrainRenderer(Renderer renderer)Stores the layer's terrain renderer and sets its visibility and Android material.
void SetEntrance(float depth, Vector2 entrance, Vector2 direction)Moves the layer to the given depth and entrance and recentres its bounds.
void SetGeneratorChunk()Puts the spawned layer content into this layer's chunk.
void ToggleTerrain(bool isToRenderTerrain)Shows or hides the terrain renderer; always shown while the local player is in the chunk.
14 private and internal methods
MemberWhat it is
private void Awake()Finds the LocationChunk on the same object and caches the portal's transform.
private void FinishPositionPortal()Finds the chunk in front of the portal and makes this layer (and that neighbour) the chunks' portal applier.
private void HandleLocalPlayer()Clients: hooks the chunk's local player and fully unloaded events. Called one frame after Initialize.
private async Task Alta.Chunks.IProceduralGenerationStreamer.LoadDynamic(bool hasGenerated)Marks the layer generated, waits for its entrance from the manager, then runs Generate. First generation only spawns dynamic content on the server.
private Task Alta.Chunks.IProceduralGenerationStreamer.LoadStatic(CancellationToken cancellationToken)Does nothing; the layer has no static stage.
private void OnDrawGizmos()Editor: draws the entrance direction and the portal's up from the portal.
private void PositionPortal()Moves the portal to the entrance, facing down and along the entrance direction.
private void RecenterBounds(Vector3 boundsCenter)Moves the chunk's bounds to a new centre.
private void SyncLayer(IPlayer player, Stream stream)Method sync handler for the manager and layer number. Clients run InitializeCaveLayer and register in the manager.
private void SyncLayerSetup(IPlayer player, Stream stream, Vector3 position)Method sync handler for the layer position, entrance direction and portal pose. Clients set up bounds and the portal from it.
private void ToggleLocalPlayer(bool hasLocalPlayer)Clients: shows the terrain and static renderers only while the local player is in the chunk, swapping Android LOD materials.
private void Alta.Chunks.IProceduralGenerationStreamer.UnloadDynamic()Despawns the layer content through the manager and forgets the generator.
private void Unloaded(Chunk chunk)Clears AllStaticRenderers when the chunk fully unloads.
private void Alta.Chunks.IProceduralGenerationStreamer.UnloadStatic()Clears AllStaticRenderers.

Used in​

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