Skip to content

Commit

Permalink
Merge pull request #6 from XenFork/terrain
Browse files Browse the repository at this point in the history
  • Loading branch information
squid233 authored Jul 18, 2024
2 parents 5586b88 + 8cf3e7b commit 19c9965
Show file tree
Hide file tree
Showing 26 changed files with 718 additions and 121 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# freeworld

A 3D sandbox game.

## Additions

Math codes are from [JOML](https://github.com/JOML-CI/JOML).

Block textures are from [Squareful](https://www.curseforge.com/minecraft/texture-packs/xekr-square-pattern) by XeKr.
6 changes: 3 additions & 3 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
#
# 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.
# License as published by the Free Software Foundation;
# only version 2.1 of the License.
#

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.8-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.9-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
4 changes: 2 additions & 2 deletions gradlew
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
#
# 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.
# License as published by the Free Software Foundation;
# only version 2.1 of the License.
#

##############################################################################
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
import freeworld.client.render.screen.ingame.PauseScreen;
import freeworld.client.render.screen.Screen;
import freeworld.client.render.world.HitResult;
import freeworld.core.registry.BuiltinRegistries;
import freeworld.client.render.world.WorldRenderer;
import freeworld.core.registry.Registries;
import freeworld.math.Vector2d;
import freeworld.math.Vector3d;
import freeworld.util.Direction;
Expand All @@ -46,6 +47,7 @@

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

/**
* Client logic
Expand Down Expand Up @@ -94,6 +96,8 @@ public final class Freeworld implements AutoCloseable {
BlockTypes.AIR,
BlockTypes.AIR
};
private int gameTick = 0;
private int spaceTick = 0;

private Freeworld() {
this.glfw = GLFW.INSTANCE;
Expand Down Expand Up @@ -140,15 +144,17 @@ public void start() {
}

BlockTypes.bootstrap();
BuiltinRegistries.BLOCK_TYPE.freeze();
Registries.BLOCK_TYPE.freeze();
EntityTypes.bootstrap();
BuiltinRegistries.ENTITY_TYPE.freeze();
Registries.ENTITY_TYPE.freeze();

blockModelManager = new BlockModelManager();
blockModelManager.bootstrap();

world = new World("New world");
player = world.createEntity(EntityTypes.PLAYER, new Vector3d(0.0, 0.0, 0.0));
world = new World("New world", new Random().nextLong());
player = world.createEntity(EntityTypes.PLAYER, new Vector3d(0.0, 128.0, 0.0));

World.forEachChunk(player, WorldRenderer.RENDER_RADIUS, (x, y, z) -> world.getOrCreateChunk(x, y, z));

initGL();
run();
Expand All @@ -168,16 +174,6 @@ private void onKey(int key, int scancode, int action, int mods) {
}
case GLFW.PRESS -> {
switch (key) {
case GLFW.KEY_1 -> hotBarSelection = 0;
case GLFW.KEY_2 -> hotBarSelection = 1;
case GLFW.KEY_3 -> hotBarSelection = 2;
case GLFW.KEY_4 -> hotBarSelection = 3;
case GLFW.KEY_5 -> hotBarSelection = 4;
case GLFW.KEY_6 -> hotBarSelection = 5;
case GLFW.KEY_7 -> hotBarSelection = 6;
case GLFW.KEY_8 -> hotBarSelection = 7;
case GLFW.KEY_9 -> hotBarSelection = 8;
case GLFW.KEY_0 -> hotBarSelection = 9;
case GLFW.KEY_ESCAPE -> {
if (screen != null) {
if (screen.escapeCanClose()) {
Expand All @@ -191,7 +187,27 @@ private void onKey(int key, int scancode, int action, int mods) {
if (screen == null) {
if (world != null) {
switch (key) {
case GLFW.KEY_1 -> hotBarSelection = 0;
case GLFW.KEY_2 -> hotBarSelection = 1;
case GLFW.KEY_3 -> hotBarSelection = 2;
case GLFW.KEY_4 -> hotBarSelection = 3;
case GLFW.KEY_5 -> hotBarSelection = 4;
case GLFW.KEY_6 -> hotBarSelection = 5;
case GLFW.KEY_7 -> hotBarSelection = 6;
case GLFW.KEY_8 -> hotBarSelection = 7;
case GLFW.KEY_9 -> hotBarSelection = 8;
case GLFW.KEY_0 -> hotBarSelection = 9;
case GLFW.KEY_E -> openScreen(new CreativeTabScreen(this, null));
case GLFW.KEY_SPACE -> {
if (gameTick - spaceTick < 5) {
if (player.hasComponent(EntityComponents.FLYING)) {
player.removeComponent(EntityComponents.FLYING);
} else {
player.addComponent(EntityComponents.FLYING);
}
}
spaceTick = gameTick;
}
}
}
} else {
Expand Down Expand Up @@ -253,17 +269,35 @@ private void tick() {
camera.preUpdate();
if (screen == null) {
final boolean onGround = player.hasComponent(EntityComponents.ON_GROUND);
double speed = onGround ? 0.1 : 0.02;
final boolean flying = player.hasComponent(EntityComponents.FLYING);
double speed;
if (onGround) {
speed = 0.1;
} else if (flying) {
speed = 0.5;
} else {
speed = 0.02;
}
if (glfw.getKey(window, GLFW.KEY_LEFT_CONTROL) == GLFW.PRESS) speed *= 2.0;
double xo = 0.0;
double yo = 0.0;
boolean changedYo = false;
double zo = 0.0;
if (glfw.getKey(window, GLFW.KEY_W) == GLFW.PRESS) zo -= 1.0;
if (glfw.getKey(window, GLFW.KEY_S) == GLFW.PRESS) zo += 1.0;
if (glfw.getKey(window, GLFW.KEY_A) == GLFW.PRESS) xo -= 1.0;
if (glfw.getKey(window, GLFW.KEY_D) == GLFW.PRESS) xo += 1.0;
if (onGround && glfw.getKey(window, GLFW.KEY_SPACE) == GLFW.PRESS) {
final Vector3d value = player.getComponent(EntityComponents.VELOCITY);
player.setComponent(EntityComponents.VELOCITY, new Vector3d(value.x(), 0.5, value.z()));
if ((onGround || flying) && glfw.getKey(window, GLFW.KEY_SPACE) == GLFW.PRESS) {
yo += 0.5;
changedYo = true;
}
if (flying && glfw.getKey(window, GLFW.KEY_LEFT_SHIFT) == GLFW.PRESS) {
yo -= 0.5;
changedYo = true;
}
if (changedYo) {
double finalYo = yo;
player.withComponent(EntityComponents.VELOCITY, v -> v.withY(finalYo));
}
player.setComponent(EntityComponents.ACCELERATION,
MathUtil.moveRelative(xo, 0.0, zo, player.getComponent(EntityComponents.ROTATION).y(), speed));
Expand Down Expand Up @@ -299,6 +333,8 @@ private void tick() {
world.tick();
}
gameRenderer.tick();

gameTick++;
}

private void initGL() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,12 @@
import freeworld.client.render.GameRenderer;
import freeworld.client.render.RenderSystem;
import freeworld.client.render.Tessellator;
import freeworld.client.render.animation.Animation;
import freeworld.client.render.gl.GLDrawMode;
import freeworld.client.render.gl.GLStateMgr;
import freeworld.client.render.texture.TextureAtlas;
import freeworld.client.render.texture.TextureManager;
import freeworld.core.Identifier;
import freeworld.core.registry.BuiltinRegistries;
import freeworld.math.Maths;
import freeworld.core.registry.Registries;
import freeworld.math.Matrix4f;
import freeworld.world.block.BlockType;
import overrungl.opengl.GL10C;
Expand All @@ -35,11 +33,6 @@ public final class HudRenderer {
public static final Identifier HOT_BAR_TEXTURE = Identifier.ofBuiltin("gui/hotbar");
public static final Identifier HOT_BAR_SELECTED_TEXTURE = Identifier.ofBuiltin("gui/hotbar_selected");
private final GameRenderer gameRenderer;
private final Animation<Float> hotBarSelectorAnimation = new Animation<>(
hotBarSelectorX(0),
(start, end, progress) -> (float) Maths.lerp(start, end, progress)
);
private int prevHotBarSelection = 0;
private float width = 0f;
private float height = 0f;

Expand Down Expand Up @@ -97,7 +90,7 @@ private void renderHotBar(GuiGraphics graphics, GLStateMgr gl, double partialTic
);
graphics.drawSprite(
atlas.getRegion(HOT_BAR_SELECTED_TEXTURE),
(float) Maths.lerp(hotBarSelectorAnimation.previous(), hotBarSelectorAnimation.current(), partialTick),
hotBarSelectorX(gameRenderer.client().hotBarSelection()),
-height * 0.5f,
0.0f,
0.0f
Expand All @@ -123,7 +116,7 @@ private void renderHotBarItems(GLStateMgr gl) {
.rotateY((float) Math.toRadians(45.0))
.scale(10));
tessellator.begin(GLDrawMode.TRIANGLES);
gameRenderer.blockRenderer().renderBlockModel(tessellator, client.blockModelManager().get(BuiltinRegistries.BLOCK_TYPE.getId(blockType)), 0, 0, 0, _ -> false);
gameRenderer.blockRenderer().renderBlockModel(tessellator, client.blockModelManager().get(Registries.BLOCK_TYPE.getId(blockType)), 0, 0, 0, _ -> false);
tessellator.end(gl);
i++;
}
Expand All @@ -135,14 +128,5 @@ private float hotBarSelectorX(int selection) {
}

public void tick() {
final int selection = gameRenderer.client().hotBarSelection();
if (prevHotBarSelection != selection) {
hotBarSelectorAnimation.reset(
hotBarSelectorX(selection),
1
);
prevHotBarSelection = selection;
}
hotBarSelectorAnimation.tick();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import freeworld.client.render.texture.TextureAtlas;
import freeworld.core.Identifier;
import freeworld.core.registry.BuiltinRegistries;
import freeworld.core.registry.Registries;
import freeworld.core.registry.DefaultedRegistry;
import freeworld.core.registry.Registry;
import freeworld.world.block.BlockType;
Expand Down Expand Up @@ -57,7 +57,7 @@ public void register(Identifier identifier, BlockModel blockModel) {
}

public void register(BlockType blockType, BlockModel blockModel) {
register(BuiltinRegistries.BLOCK_TYPE.getId(blockType), blockModel);
register(Registries.BLOCK_TYPE.getId(blockType), blockModel);
}

public void bootstrap() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,38 +36,45 @@ public BlockRenderer(TextureManager textureManager) {
}

private void emitVertices(VertexBuilder builder, Vector3f from, Vector3f to, Vector2f uvFrom, Vector2f uvTo, Direction direction) {
// TODO: 2024/7/6 squid233: color
switch (direction) {
case WEST -> {
builder.color(0.7f, 0.7f, 0.7f);
builder.position(from.x(), to.y(), from.z()).texCoord(uvFrom.x(), uvFrom.y()).emit();
builder.position(from.x(), from.y(), from.z()).texCoord(uvFrom.x(), uvTo.y()).emit();
builder.position(from.x(), from.y(), to.z()).texCoord(uvTo.x(), uvTo.y()).emit();
builder.position(from.x(), to.y(), to.z()).texCoord(uvTo.x(), uvFrom.y()).emit();
}
case EAST -> {
builder.color(1.0f, 1.0f, 1.0f);
builder.position(to.x(), to.y(), to.z()).texCoord(uvFrom.x(), uvFrom.y()).emit();
builder.position(to.x(), from.y(), to.z()).texCoord(uvFrom.x(), uvTo.y()).emit();
builder.position(to.x(), from.y(), from.z()).texCoord(uvTo.x(), uvTo.y()).emit();
builder.position(to.x(), to.y(), from.z()).texCoord(uvTo.x(), uvFrom.y()).emit();
}
case DOWN -> {
builder.color(0.6f, 0.6f, 0.6f);
builder.position(from.x(), from.y(), to.z()).texCoord(uvFrom.x(), uvFrom.y()).emit();
builder.position(from.x(), from.y(), from.z()).texCoord(uvFrom.x(), uvTo.y()).emit();
builder.position(to.x(), from.y(), from.z()).texCoord(uvTo.x(), uvTo.y()).emit();
builder.position(to.x(), from.y(), to.z()).texCoord(uvTo.x(), uvFrom.y()).emit();
}
case UP -> {
builder.color(0.9f, 0.9f, 0.9f);
builder.position(from.x(), to.y(), from.z()).texCoord(uvFrom.x(), uvFrom.y()).emit();
builder.position(from.x(), to.y(), to.z()).texCoord(uvFrom.x(), uvTo.y()).emit();
builder.position(to.x(), to.y(), to.z()).texCoord(uvTo.x(), uvTo.y()).emit();
builder.position(to.x(), to.y(), from.z()).texCoord(uvTo.x(), uvFrom.y()).emit();
}
case NORTH -> {
builder.color(0.8f, 0.8f, 0.8f);
builder.position(to.x(), to.y(), from.z()).texCoord(uvFrom.x(), uvFrom.y()).emit();
builder.position(to.x(), from.y(), from.z()).texCoord(uvFrom.x(), uvTo.y()).emit();
builder.position(from.x(), from.y(), from.z()).texCoord(uvTo.x(), uvTo.y()).emit();
builder.position(from.x(), to.y(), from.z()).texCoord(uvTo.x(), uvFrom.y()).emit();
}
case SOUTH -> {
builder.color(0.8f, 0.8f, 0.8f);
builder.position(from.x(), to.y(), to.z()).texCoord(uvFrom.x(), uvFrom.y()).emit();
builder.position(from.x(), from.y(), to.z()).texCoord(uvFrom.x(), uvTo.y()).emit();
builder.position(to.x(), from.y(), to.z()).texCoord(uvTo.x(), uvTo.y()).emit();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import freeworld.client.render.builder.VertexBuilder;
import freeworld.client.render.model.block.BlockModel;
import freeworld.client.render.model.block.BlockModelManager;
import freeworld.core.registry.BuiltinRegistries;
import freeworld.core.registry.Registries;
import freeworld.world.chunk.Chunk;
import freeworld.world.chunk.ChunkPos;

Expand Down Expand Up @@ -45,7 +45,7 @@ public static ChunkVertexData compile(
int finalX = x;
int finalY = y;
int finalZ = z;
final BlockModel model = blockModelManager.get(BuiltinRegistries.BLOCK_TYPE.getId(chunk.getBlockType(x, y, z)));
final BlockModel model = blockModelManager.get(Registries.BLOCK_TYPE.getId(chunk.getBlockType(x, y, z)));
blockRenderer.renderBlockModel(
vertexBuilder,
model,
Expand All @@ -61,9 +61,10 @@ public static ChunkVertexData compile(
final int absNz = ChunkPos.relativeToAbsolute(cz, nz);
final boolean shouldRender =
(chunk.isInBound(nx, ny, nz) &&
chunk.getBlockType(nx, ny, nz).air()) ||
chunk.getBlockType(nx, ny, nz).nonOpaque()) ||
(chunk.world().isBlockLoaded(absNx, absNy, absNz) &&
chunk.world().getBlockType(absNx, absNy, absNz).air());
chunk.world().getBlockType(absNx, absNy, absNz).nonOpaque()) ||
!chunk.world().isBlockLoaded(absNx, absNy, absNz) /* TODO: add method world::tryLoading() */;
return !shouldRender;
}
);
Expand Down
Loading

0 comments on commit 19c9965

Please sign in to comment.