Skip to content

Commit

Permalink
Update world and chunk
Browse files Browse the repository at this point in the history
  • Loading branch information
squid233 committed Mar 17, 2024
1 parent 5b0cb45 commit 1f9c112
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@

import java.lang.foreign.MemorySegment;
import java.lang.invoke.MethodHandles;
import java.util.UUID;

/**
* Client logic
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Chunk> 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<Entity> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 1f9c112

Please sign in to comment.