Terrain generation

This commit is contained in:
2026-03-13 16:34:09 -03:00
parent 96a485743f
commit 5348cd3d06
15 changed files with 320 additions and 98 deletions
+50 -19
View File
@@ -2,7 +2,8 @@ import { Component } from "$/common/ecs/mod.ts";
import { EverythingRegistry, TileRegistry } from "$/common/everything_registry.ts";
import { AssetManager } from "../assets.ts";
import { ClientWorld } from "../client_world.ts";
import { Texture } from "../renderer/mod.ts";
import { generate_chunk } from "../generation.ts";
import { gl, Texture } from "../renderer/mod.ts";
export interface Block<T = unknown> {
x: number;
@@ -13,17 +14,18 @@ export interface Block<T = unknown> {
tickable?: boolean;
}
export const CHUNK_SIDE_SIZE = 16;
export const CHUNK_SIZE = 16;
export const CHUNK_HEIGHT = 128;
export const CHUNK_AREA = CHUNK_SIDE_SIZE * CHUNK_SIDE_SIZE;
export const CHUNK_AREA = CHUNK_SIZE * CHUNK_SIZE;
export interface Chunk {
x: number;
z: number;
blocks: Int32Array;
generated: boolean;
dirty: boolean;
vertexBuffer?: WebGLBuffer;
vertexCount?: number;
dirty: boolean;
vertexTransparentBuffer?: WebGLBuffer;
vertexTransparentCount?: number;
}
@@ -35,14 +37,14 @@ export class Dimension extends Component {
tick_timer = 0;
add_chunk(x: number, z: number) {
const chunk = { x, z, blocks: new Int32Array(CHUNK_AREA * CHUNK_HEIGHT), dirty: true };
const chunk = { x, z, blocks: new Int32Array(CHUNK_AREA * CHUNK_HEIGHT), dirty: true, generated: false };
this.chunks.push(chunk);
return chunk;
}
add_block(world: ClientWorld, block: Block) {
const block_chunk_x = Math.floor(block.x / CHUNK_SIDE_SIZE);
const block_chunk_z = Math.floor(block.z / CHUNK_SIDE_SIZE);
add_block(block: Block) {
const block_chunk_x = Math.floor(block.x / CHUNK_SIZE);
const block_chunk_z = Math.floor(block.z / CHUNK_SIZE);
let chunk = this.chunks.find((chunk) => chunk.x === block_chunk_x && chunk.z === block_chunk_z);
if (!chunk) {
chunk = this.add_chunk(block_chunk_x, block_chunk_z);
@@ -50,21 +52,21 @@ export class Dimension extends Component {
const [nid, block_info] = EverythingRegistry.get_full<TileRegistry>("blocks", block.id)!;
const lx = block.x - block_chunk_x * CHUNK_SIDE_SIZE;
const lz = block.z - block_chunk_z * CHUNK_SIDE_SIZE;
const lx = block.x - block_chunk_x * CHUNK_SIZE;
const lz = block.z - block_chunk_z * CHUNK_SIZE;
const ly = block.y;
const index = ly * CHUNK_SIDE_SIZE * CHUNK_SIDE_SIZE + lz * CHUNK_SIDE_SIZE + lx;
const index = ly * CHUNK_SIZE * CHUNK_SIZE + lz * CHUNK_SIZE + lx;
chunk.blocks[index] = nid;
if (block_info?.on_create) {
block_info?.on_create(world, block);
block_info?.on_create(this, block);
}
}
get_block(x: number, y: number, z: number) {
const chunk_x = Math.floor(x / CHUNK_SIDE_SIZE);
const chunk_z = Math.floor(z / CHUNK_SIDE_SIZE);
const chunk_x = Math.floor(x / CHUNK_SIZE);
const chunk_z = Math.floor(z / CHUNK_SIZE);
const chunk = this.chunks.find(
(c) => c.x === chunk_x && c.z === chunk_z,
@@ -74,11 +76,11 @@ export class Dimension extends Component {
return 0;
}
const lx = x - chunk_x * CHUNK_SIDE_SIZE;
const lz = z - chunk_z * CHUNK_SIDE_SIZE;
const lx = x - chunk_x * CHUNK_SIZE;
const lz = z - chunk_z * CHUNK_SIZE;
const ly = y;
const index = ly * CHUNK_SIDE_SIZE * CHUNK_SIDE_SIZE + lz * CHUNK_SIDE_SIZE + lx;
const index = ly * CHUNK_SIZE * CHUNK_SIZE + lz * CHUNK_SIZE + lx;
return chunk.blocks[index];
}
@@ -99,9 +101,38 @@ export class Dimension extends Component {
const y = Math.floor(index / CHUNK_AREA);
const rem = index % CHUNK_AREA;
const z = Math.floor(rem / CHUNK_SIDE_SIZE);
const x = rem % CHUNK_SIDE_SIZE;
const z = Math.floor(rem / CHUNK_SIZE);
const x = rem % CHUNK_SIZE;
return [x, y, z];
}
load_chunk(cx: number, cz: number) {
generate_chunk(this, cx, cz);
const chunk = this.chunks.find((c) => c.x === cx && c.z === cz);
if (chunk) {
chunk.generated = true;
chunk.dirty = true;
}
}
unload_chunk(cx: number, cz: number) {
const chunk_i = this.chunks.findIndex((c) => c.x === cx && c.z === cz);
if (chunk_i === -1) {
console.warn("Tried unloading a chunk that doesn't exist dumbass");
return;
}
this.delete_chunk_mesh(this.chunks[chunk_i]);
this.chunks.splice(chunk_i, 1);
}
delete_chunk_mesh(chunk: Chunk) {
if (chunk.vertexBuffer) {
gl.deleteBuffer(chunk.vertexBuffer);
}
if (chunk.vertexTransparentBuffer) {
gl.deleteBuffer(chunk.vertexTransparentBuffer);
}
}
}