diff --git a/modules/io.github.xenfork.freeworld.client/src/main/java/io/github/xenfork/freeworld/client/Freeworld.java b/modules/io.github.xenfork.freeworld.client/src/main/java/io/github/xenfork/freeworld/client/Freeworld.java index f9328b0..27f28b7 100644 --- a/modules/io.github.xenfork.freeworld.client/src/main/java/io/github/xenfork/freeworld/client/Freeworld.java +++ b/modules/io.github.xenfork.freeworld.client/src/main/java/io/github/xenfork/freeworld/client/Freeworld.java @@ -33,7 +33,6 @@ import java.lang.foreign.MemorySegment; import java.lang.invoke.MethodHandles; -import java.util.UUID; /** * Client logic @@ -113,9 +112,7 @@ public void start() { BuiltinRegistries.ENTITY_TYPE.freeze(); world = new World("New world"); - player = new Entity(world, UUID.randomUUID(), EntityTypes.PLAYER); - player.position().position().set(1.5, 16.0, 1.5); - world.entities.add(player); + player = world.createEntity(EntityTypes.PLAYER, 32.0, 16.0, 32.0); initGL(); run(); diff --git a/modules/io.github.xenfork.freeworld.client/src/main/java/io/github/xenfork/freeworld/client/render/world/WorldRenderer.java b/modules/io.github.xenfork.freeworld.client/src/main/java/io/github/xenfork/freeworld/client/render/world/WorldRenderer.java index c2f1f88..1ef553c 100644 --- a/modules/io.github.xenfork.freeworld.client/src/main/java/io/github/xenfork/freeworld/client/render/world/WorldRenderer.java +++ b/modules/io.github.xenfork.freeworld.client/src/main/java/io/github/xenfork/freeworld/client/render/world/WorldRenderer.java @@ -18,6 +18,7 @@ import io.github.xenfork.freeworld.util.Direction; import io.github.xenfork.freeworld.world.World; import io.github.xenfork.freeworld.world.chunk.Chunk; +import io.github.xenfork.freeworld.world.chunk.ChunkPos; import java.util.concurrent.ExecutorService; import java.util.concurrent.LinkedBlockingDeque; @@ -55,12 +56,21 @@ public void render(GLStateMgr gl, Tessellator tessellator) { final BlockRenderer blockRenderer = gameRenderer.blockRenderer(); tessellator.begin(GLDrawMode.TRIANGLES); for (Chunk chunk : world.chunks) { + final int cx = chunk.x(); + final int cy = chunk.y(); + final int cz = chunk.z(); for (Direction direction : Direction.LIST) { - for (int x = chunk.fromX(), x1 = chunk.toX(); x < x1; x++) { - for (int y = chunk.fromY(), y1 = chunk.toY(); y < y1; y++) { - for (int z = chunk.fromZ(), z1 = chunk.toZ(); z < z1; z++) { + for (int x = 0; x < Chunk.SIZE; x++) { + for (int y = 0; y < Chunk.SIZE; y++) { + for (int z = 0; z < Chunk.SIZE; z++) { if (chunk.getBlockType(x + direction.axisX(), y + direction.axisY(), z + direction.axisZ()).air()) { - blockRenderer.renderBlockFace(gl, tessellator, chunk.getBlockType(x, y, z), x, y, z, direction); + blockRenderer.renderBlockFace(gl, + tessellator, + chunk.getBlockType(x, y, z), + ChunkPos.relativeToAbsolute(cx, x), + ChunkPos.relativeToAbsolute(cy, y), + ChunkPos.relativeToAbsolute(cz, z), + direction); } } } diff --git a/modules/io.github.xenfork.freeworld.core/src/main/java/io/github/xenfork/freeworld/world/World.java b/modules/io.github.xenfork.freeworld.core/src/main/java/io/github/xenfork/freeworld/world/World.java index ae37025..ba711c7 100644 --- a/modules/io.github.xenfork.freeworld.core/src/main/java/io/github/xenfork/freeworld/world/World.java +++ b/modules/io.github.xenfork.freeworld.core/src/main/java/io/github/xenfork/freeworld/world/World.java @@ -12,32 +12,54 @@ import io.github.xenfork.freeworld.world.chunk.Chunk; import io.github.xenfork.freeworld.world.entity.Entity; +import io.github.xenfork.freeworld.world.entity.EntityType; +import io.github.xenfork.freeworld.world.entity.component.PositionComponent; import io.github.xenfork.freeworld.world.entity.system.MotionSystem; import java.util.ArrayList; import java.util.List; +import java.util.UUID; /** * @author squid233 * @since 0.1.0 */ public final class World { - public final Chunk c0 = new Chunk(this, 0, 0, 0); - public final Chunk c1 = new Chunk(this, 1, 0, 0); - public final Chunk c2 = new Chunk(this, 0, 0, 1); - public final Chunk c3 = new Chunk(this, 1, 0, 1); - public final List chunks = List.of(c0, c1, c2, c3); + private final int width = 64; + private final int height = 64; + private final int depth = 64; + private final int xChunks = width / Chunk.SIZE; + private final int yChunks = height / Chunk.SIZE; + private final int zChunks = depth / Chunk.SIZE; + public final Chunk[] chunks = new Chunk[xChunks * yChunks * zChunks]; public final List entities = new ArrayList<>(); public final MotionSystem motionSystem = new MotionSystem(); public World(String name) { - chunks.forEach(Chunk::generateTerrain); + for (int x = 0; x < xChunks; x++) { + for (int y = 0; y < yChunks; y++) { + for (int z = 0; z < zChunks; z++) { + final Chunk chunk = new Chunk(this, x, y, z); + chunks[(y * zChunks + z) * xChunks + x] = chunk; + chunk.generateTerrain(); + } + } + } } public void tick() { motionSystem.process(entities); } + public Entity createEntity(EntityType type, double x, double y, double z) { + final Entity entity = new Entity(this, UUID.randomUUID(), type); + if (entity.hasComponent(PositionComponent.ID)) { + entity.position().position().set(x, y, z); + } + entities.add(entity); + return entity; + } + public Chunk createChunk(int x, int y, int z) { return new Chunk(this, x, y, z); } diff --git a/modules/io.github.xenfork.freeworld.core/src/main/java/io/github/xenfork/freeworld/world/chunk/Chunk.java b/modules/io.github.xenfork.freeworld.core/src/main/java/io/github/xenfork/freeworld/world/chunk/Chunk.java index a8079bb..e5435b3 100644 --- a/modules/io.github.xenfork.freeworld.core/src/main/java/io/github/xenfork/freeworld/world/chunk/Chunk.java +++ b/modules/io.github.xenfork.freeworld.core/src/main/java/io/github/xenfork/freeworld/world/chunk/Chunk.java @@ -57,6 +57,7 @@ public Chunk(World world, int x, int y, int z) { } public void generateTerrain() { + if (y != 0) return; for (int bx = 0; bx < width; bx++) { for (int bz = 0; bz < depth; bz++) { for (int by = 0; by < 8; by++) { diff --git a/modules/io.github.xenfork.freeworld.core/src/main/java/io/github/xenfork/freeworld/world/chunk/ChunkPos.java b/modules/io.github.xenfork.freeworld.core/src/main/java/io/github/xenfork/freeworld/world/chunk/ChunkPos.java new file mode 100644 index 0000000..733ca61 --- /dev/null +++ b/modules/io.github.xenfork.freeworld.core/src/main/java/io/github/xenfork/freeworld/world/chunk/ChunkPos.java @@ -0,0 +1,21 @@ +/* + * freeworld - 3D sandbox game + * Copyright (C) 2024 XenFork Union + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + */ + +package io.github.xenfork.freeworld.world.chunk; + +/** + * @author squid233 + * @since 0.1.0 + */ +public record ChunkPos(int x, int y, int z) { + public static int relativeToAbsolute(int chunkPos, int relativePos) { + return chunkPos * Chunk.SIZE + relativePos; + } +}