Skip to content

Commit ddff2ce

Browse files
authored
feat(voxel-renderer): implement moveLayer to VoxelRenderer and expose markAllChunksDirty() (#230)
1 parent 95e3e77 commit ddff2ce

File tree

5 files changed

+32
-4
lines changed

5 files changed

+32
-4
lines changed

.changeset/five-boxes-melt.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@jolly-pixel/voxel.renderer": minor
3+
---
4+
5+
Implement moveLayer() in VoxelRenderer and expose markAllChunksDirty()

packages/voxel-renderer/docs/Hooks.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ export type VoxelLayerHookAction =
3131
| "updated"
3232
| "offset-updated"
3333
| "voxel-set"
34-
| "voxel-removed";
34+
| "voxel-removed"
35+
| "reordered";
3536

3637
// Describes a change related to a layer.
3738
export interface VoxelLayerHookEvent {

packages/voxel-renderer/docs/VoxelRenderer.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,10 @@ is re-evaluated on the next frame. No-op if the layer is not found.
186186
Adds `delta` to the layer's current offset. Equivalent to `setLayerOffset` with
187187
`layer.offset + delta`. No-op if the layer is not found.
188188

189+
#### `moveLayer(name: string, direction: "up" | "down"): void`
190+
191+
Swaps `order` with the neighbouring layer in the given direction.
192+
189193
#### `setVoxel(layerName: string, options: VoxelSetOptions): void`
190194

191195
Places a voxel at a world-space position.
@@ -259,6 +263,10 @@ Serialises the full world state (layers, voxels, tileset metadata) to a plain JS
259263
Clears the current world, restores state from a JSON snapshot, and reloads any
260264
referenced tilesets that are not already loaded.
261265

266+
#### `markAllChunksDirty(source?: string): void`
267+
268+
Mark all the chunks as dirty and rebuild them in the next frame
269+
262270
### Hooks
263271

264272
See [Hooks](./Hooks.md) for more information

packages/voxel-renderer/src/VoxelRenderer.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,19 @@ export class VoxelRenderer extends ActorComponent {
453453
});
454454
}
455455

456+
moveLayer(
457+
name: string,
458+
direction: "up" | "down"
459+
): void {
460+
this.world.moveLayer(name, direction);
461+
this.markAllChunksDirty("moveLayer");
462+
this.#onLayerUpdated?.({
463+
action: "reordered",
464+
layerName: name,
465+
metadata: { direction }
466+
});
467+
}
468+
456469
async loadTileset(
457470
def: TilesetDefinition
458471
): Promise<void> {
@@ -473,7 +486,7 @@ export class VoxelRenderer extends ActorComponent {
473486
this.#materials.delete(def.id);
474487

475488
// Force all chunks to rebuild geometry (UV offsets may have changed).
476-
this.#markAllChunksDirty("loadTileset");
489+
this.markAllChunksDirty("loadTileset");
477490
}
478491

479492
// --- Serialization --- //
@@ -705,7 +718,7 @@ export class VoxelRenderer extends ActorComponent {
705718
}
706719
}
707720

708-
#markAllChunksDirty(
721+
markAllChunksDirty(
709722
source?: string
710723
): void {
711724
this.#logger.debug("Marking all chunks dirty...", { source });

packages/voxel-renderer/src/hooks.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ export type VoxelLayerHookAction =
44
| "updated"
55
| "offset-updated"
66
| "voxel-set"
7-
| "voxel-removed";
7+
| "voxel-removed"
8+
| "reordered";
89

910
export interface VoxelLayerHookEvent {
1011
layerName: string;

0 commit comments

Comments
 (0)