Skip to content

Commit 8cf3e7b

Browse files
committed
Update terrain
1 parent 5586b88 commit 8cf3e7b

File tree

26 files changed

+718
-121
lines changed

26 files changed

+718
-121
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
# freeworld
22

33
A 3D sandbox game.
4+
5+
## Additions
6+
7+
Math codes are from [JOML](https://github.com/JOML-CI/JOML).
8+
9+
Block textures are from [Squareful](https://www.curseforge.com/minecraft/texture-packs/xekr-square-pattern) by XeKr.

gradle/wrapper/gradle-wrapper.properties

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
#
55
# This library is free software; you can redistribute it and/or
66
# modify it under the terms of the GNU Lesser General Public
7-
# License as published by the Free Software Foundation; either
8-
# version 2.1 of the License, or (at your option) any later version.
7+
# License as published by the Free Software Foundation;
8+
# only version 2.1 of the License.
99
#
1010

1111
distributionBase=GRADLE_USER_HOME
1212
distributionPath=wrapper/dists
13-
distributionUrl=https\://services.gradle.org/distributions/gradle-8.8-bin.zip
13+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.9-bin.zip
1414
networkTimeout=10000
1515
validateDistributionUrl=true
1616
zipStoreBase=GRADLE_USER_HOME

gradlew

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
#
77
# This library is free software; you can redistribute it and/or
88
# modify it under the terms of the GNU Lesser General Public
9-
# License as published by the Free Software Foundation; either
10-
# version 2.1 of the License, or (at your option) any later version.
9+
# License as published by the Free Software Foundation;
10+
# only version 2.1 of the License.
1111
#
1212

1313
##############################################################################

modules/freeworld.client/src/main/java/freeworld/client/Freeworld.java

Lines changed: 55 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
import freeworld.client.render.screen.ingame.PauseScreen;
2020
import freeworld.client.render.screen.Screen;
2121
import freeworld.client.render.world.HitResult;
22-
import freeworld.core.registry.BuiltinRegistries;
22+
import freeworld.client.render.world.WorldRenderer;
23+
import freeworld.core.registry.Registries;
2324
import freeworld.math.Vector2d;
2425
import freeworld.math.Vector3d;
2526
import freeworld.util.Direction;
@@ -46,6 +47,7 @@
4647

4748
import java.lang.foreign.MemorySegment;
4849
import java.lang.invoke.MethodHandles;
50+
import java.util.Random;
4951

5052
/**
5153
* Client logic
@@ -94,6 +96,8 @@ public final class Freeworld implements AutoCloseable {
9496
BlockTypes.AIR,
9597
BlockTypes.AIR
9698
};
99+
private int gameTick = 0;
100+
private int spaceTick = 0;
97101

98102
private Freeworld() {
99103
this.glfw = GLFW.INSTANCE;
@@ -140,15 +144,17 @@ public void start() {
140144
}
141145

142146
BlockTypes.bootstrap();
143-
BuiltinRegistries.BLOCK_TYPE.freeze();
147+
Registries.BLOCK_TYPE.freeze();
144148
EntityTypes.bootstrap();
145-
BuiltinRegistries.ENTITY_TYPE.freeze();
149+
Registries.ENTITY_TYPE.freeze();
146150

147151
blockModelManager = new BlockModelManager();
148152
blockModelManager.bootstrap();
149153

150-
world = new World("New world");
151-
player = world.createEntity(EntityTypes.PLAYER, new Vector3d(0.0, 0.0, 0.0));
154+
world = new World("New world", new Random().nextLong());
155+
player = world.createEntity(EntityTypes.PLAYER, new Vector3d(0.0, 128.0, 0.0));
156+
157+
World.forEachChunk(player, WorldRenderer.RENDER_RADIUS, (x, y, z) -> world.getOrCreateChunk(x, y, z));
152158

153159
initGL();
154160
run();
@@ -168,16 +174,6 @@ private void onKey(int key, int scancode, int action, int mods) {
168174
}
169175
case GLFW.PRESS -> {
170176
switch (key) {
171-
case GLFW.KEY_1 -> hotBarSelection = 0;
172-
case GLFW.KEY_2 -> hotBarSelection = 1;
173-
case GLFW.KEY_3 -> hotBarSelection = 2;
174-
case GLFW.KEY_4 -> hotBarSelection = 3;
175-
case GLFW.KEY_5 -> hotBarSelection = 4;
176-
case GLFW.KEY_6 -> hotBarSelection = 5;
177-
case GLFW.KEY_7 -> hotBarSelection = 6;
178-
case GLFW.KEY_8 -> hotBarSelection = 7;
179-
case GLFW.KEY_9 -> hotBarSelection = 8;
180-
case GLFW.KEY_0 -> hotBarSelection = 9;
181177
case GLFW.KEY_ESCAPE -> {
182178
if (screen != null) {
183179
if (screen.escapeCanClose()) {
@@ -191,7 +187,27 @@ private void onKey(int key, int scancode, int action, int mods) {
191187
if (screen == null) {
192188
if (world != null) {
193189
switch (key) {
190+
case GLFW.KEY_1 -> hotBarSelection = 0;
191+
case GLFW.KEY_2 -> hotBarSelection = 1;
192+
case GLFW.KEY_3 -> hotBarSelection = 2;
193+
case GLFW.KEY_4 -> hotBarSelection = 3;
194+
case GLFW.KEY_5 -> hotBarSelection = 4;
195+
case GLFW.KEY_6 -> hotBarSelection = 5;
196+
case GLFW.KEY_7 -> hotBarSelection = 6;
197+
case GLFW.KEY_8 -> hotBarSelection = 7;
198+
case GLFW.KEY_9 -> hotBarSelection = 8;
199+
case GLFW.KEY_0 -> hotBarSelection = 9;
194200
case GLFW.KEY_E -> openScreen(new CreativeTabScreen(this, null));
201+
case GLFW.KEY_SPACE -> {
202+
if (gameTick - spaceTick < 5) {
203+
if (player.hasComponent(EntityComponents.FLYING)) {
204+
player.removeComponent(EntityComponents.FLYING);
205+
} else {
206+
player.addComponent(EntityComponents.FLYING);
207+
}
208+
}
209+
spaceTick = gameTick;
210+
}
195211
}
196212
}
197213
} else {
@@ -253,17 +269,35 @@ private void tick() {
253269
camera.preUpdate();
254270
if (screen == null) {
255271
final boolean onGround = player.hasComponent(EntityComponents.ON_GROUND);
256-
double speed = onGround ? 0.1 : 0.02;
272+
final boolean flying = player.hasComponent(EntityComponents.FLYING);
273+
double speed;
274+
if (onGround) {
275+
speed = 0.1;
276+
} else if (flying) {
277+
speed = 0.5;
278+
} else {
279+
speed = 0.02;
280+
}
257281
if (glfw.getKey(window, GLFW.KEY_LEFT_CONTROL) == GLFW.PRESS) speed *= 2.0;
258282
double xo = 0.0;
283+
double yo = 0.0;
284+
boolean changedYo = false;
259285
double zo = 0.0;
260286
if (glfw.getKey(window, GLFW.KEY_W) == GLFW.PRESS) zo -= 1.0;
261287
if (glfw.getKey(window, GLFW.KEY_S) == GLFW.PRESS) zo += 1.0;
262288
if (glfw.getKey(window, GLFW.KEY_A) == GLFW.PRESS) xo -= 1.0;
263289
if (glfw.getKey(window, GLFW.KEY_D) == GLFW.PRESS) xo += 1.0;
264-
if (onGround && glfw.getKey(window, GLFW.KEY_SPACE) == GLFW.PRESS) {
265-
final Vector3d value = player.getComponent(EntityComponents.VELOCITY);
266-
player.setComponent(EntityComponents.VELOCITY, new Vector3d(value.x(), 0.5, value.z()));
290+
if ((onGround || flying) && glfw.getKey(window, GLFW.KEY_SPACE) == GLFW.PRESS) {
291+
yo += 0.5;
292+
changedYo = true;
293+
}
294+
if (flying && glfw.getKey(window, GLFW.KEY_LEFT_SHIFT) == GLFW.PRESS) {
295+
yo -= 0.5;
296+
changedYo = true;
297+
}
298+
if (changedYo) {
299+
double finalYo = yo;
300+
player.withComponent(EntityComponents.VELOCITY, v -> v.withY(finalYo));
267301
}
268302
player.setComponent(EntityComponents.ACCELERATION,
269303
MathUtil.moveRelative(xo, 0.0, zo, player.getComponent(EntityComponents.ROTATION).y(), speed));
@@ -299,6 +333,8 @@ private void tick() {
299333
world.tick();
300334
}
301335
gameRenderer.tick();
336+
337+
gameTick++;
302338
}
303339

304340
private void initGL() {

modules/freeworld.client/src/main/java/freeworld/client/render/gui/HudRenderer.java

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,12 @@
1414
import freeworld.client.render.GameRenderer;
1515
import freeworld.client.render.RenderSystem;
1616
import freeworld.client.render.Tessellator;
17-
import freeworld.client.render.animation.Animation;
1817
import freeworld.client.render.gl.GLDrawMode;
1918
import freeworld.client.render.gl.GLStateMgr;
2019
import freeworld.client.render.texture.TextureAtlas;
2120
import freeworld.client.render.texture.TextureManager;
2221
import freeworld.core.Identifier;
23-
import freeworld.core.registry.BuiltinRegistries;
24-
import freeworld.math.Maths;
22+
import freeworld.core.registry.Registries;
2523
import freeworld.math.Matrix4f;
2624
import freeworld.world.block.BlockType;
2725
import overrungl.opengl.GL10C;
@@ -35,11 +33,6 @@ public final class HudRenderer {
3533
public static final Identifier HOT_BAR_TEXTURE = Identifier.ofBuiltin("gui/hotbar");
3634
public static final Identifier HOT_BAR_SELECTED_TEXTURE = Identifier.ofBuiltin("gui/hotbar_selected");
3735
private final GameRenderer gameRenderer;
38-
private final Animation<Float> hotBarSelectorAnimation = new Animation<>(
39-
hotBarSelectorX(0),
40-
(start, end, progress) -> (float) Maths.lerp(start, end, progress)
41-
);
42-
private int prevHotBarSelection = 0;
4336
private float width = 0f;
4437
private float height = 0f;
4538

@@ -97,7 +90,7 @@ private void renderHotBar(GuiGraphics graphics, GLStateMgr gl, double partialTic
9790
);
9891
graphics.drawSprite(
9992
atlas.getRegion(HOT_BAR_SELECTED_TEXTURE),
100-
(float) Maths.lerp(hotBarSelectorAnimation.previous(), hotBarSelectorAnimation.current(), partialTick),
93+
hotBarSelectorX(gameRenderer.client().hotBarSelection()),
10194
-height * 0.5f,
10295
0.0f,
10396
0.0f
@@ -123,7 +116,7 @@ private void renderHotBarItems(GLStateMgr gl) {
123116
.rotateY((float) Math.toRadians(45.0))
124117
.scale(10));
125118
tessellator.begin(GLDrawMode.TRIANGLES);
126-
gameRenderer.blockRenderer().renderBlockModel(tessellator, client.blockModelManager().get(BuiltinRegistries.BLOCK_TYPE.getId(blockType)), 0, 0, 0, _ -> false);
119+
gameRenderer.blockRenderer().renderBlockModel(tessellator, client.blockModelManager().get(Registries.BLOCK_TYPE.getId(blockType)), 0, 0, 0, _ -> false);
127120
tessellator.end(gl);
128121
i++;
129122
}
@@ -135,14 +128,5 @@ private float hotBarSelectorX(int selection) {
135128
}
136129

137130
public void tick() {
138-
final int selection = gameRenderer.client().hotBarSelection();
139-
if (prevHotBarSelection != selection) {
140-
hotBarSelectorAnimation.reset(
141-
hotBarSelectorX(selection),
142-
1
143-
);
144-
prevHotBarSelection = selection;
145-
}
146-
hotBarSelectorAnimation.tick();
147131
}
148132
}

modules/freeworld.client/src/main/java/freeworld/client/render/model/block/BlockModelManager.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
import freeworld.client.render.texture.TextureAtlas;
1414
import freeworld.core.Identifier;
15-
import freeworld.core.registry.BuiltinRegistries;
15+
import freeworld.core.registry.Registries;
1616
import freeworld.core.registry.DefaultedRegistry;
1717
import freeworld.core.registry.Registry;
1818
import freeworld.world.block.BlockType;
@@ -57,7 +57,7 @@ public void register(Identifier identifier, BlockModel blockModel) {
5757
}
5858

5959
public void register(BlockType blockType, BlockModel blockModel) {
60-
register(BuiltinRegistries.BLOCK_TYPE.getId(blockType), blockModel);
60+
register(Registries.BLOCK_TYPE.getId(blockType), blockModel);
6161
}
6262

6363
public void bootstrap() {

modules/freeworld.client/src/main/java/freeworld/client/render/world/BlockRenderer.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,38 +36,45 @@ public BlockRenderer(TextureManager textureManager) {
3636
}
3737

3838
private void emitVertices(VertexBuilder builder, Vector3f from, Vector3f to, Vector2f uvFrom, Vector2f uvTo, Direction direction) {
39+
// TODO: 2024/7/6 squid233: color
3940
switch (direction) {
4041
case WEST -> {
42+
builder.color(0.7f, 0.7f, 0.7f);
4143
builder.position(from.x(), to.y(), from.z()).texCoord(uvFrom.x(), uvFrom.y()).emit();
4244
builder.position(from.x(), from.y(), from.z()).texCoord(uvFrom.x(), uvTo.y()).emit();
4345
builder.position(from.x(), from.y(), to.z()).texCoord(uvTo.x(), uvTo.y()).emit();
4446
builder.position(from.x(), to.y(), to.z()).texCoord(uvTo.x(), uvFrom.y()).emit();
4547
}
4648
case EAST -> {
49+
builder.color(1.0f, 1.0f, 1.0f);
4750
builder.position(to.x(), to.y(), to.z()).texCoord(uvFrom.x(), uvFrom.y()).emit();
4851
builder.position(to.x(), from.y(), to.z()).texCoord(uvFrom.x(), uvTo.y()).emit();
4952
builder.position(to.x(), from.y(), from.z()).texCoord(uvTo.x(), uvTo.y()).emit();
5053
builder.position(to.x(), to.y(), from.z()).texCoord(uvTo.x(), uvFrom.y()).emit();
5154
}
5255
case DOWN -> {
56+
builder.color(0.6f, 0.6f, 0.6f);
5357
builder.position(from.x(), from.y(), to.z()).texCoord(uvFrom.x(), uvFrom.y()).emit();
5458
builder.position(from.x(), from.y(), from.z()).texCoord(uvFrom.x(), uvTo.y()).emit();
5559
builder.position(to.x(), from.y(), from.z()).texCoord(uvTo.x(), uvTo.y()).emit();
5660
builder.position(to.x(), from.y(), to.z()).texCoord(uvTo.x(), uvFrom.y()).emit();
5761
}
5862
case UP -> {
63+
builder.color(0.9f, 0.9f, 0.9f);
5964
builder.position(from.x(), to.y(), from.z()).texCoord(uvFrom.x(), uvFrom.y()).emit();
6065
builder.position(from.x(), to.y(), to.z()).texCoord(uvFrom.x(), uvTo.y()).emit();
6166
builder.position(to.x(), to.y(), to.z()).texCoord(uvTo.x(), uvTo.y()).emit();
6267
builder.position(to.x(), to.y(), from.z()).texCoord(uvTo.x(), uvFrom.y()).emit();
6368
}
6469
case NORTH -> {
70+
builder.color(0.8f, 0.8f, 0.8f);
6571
builder.position(to.x(), to.y(), from.z()).texCoord(uvFrom.x(), uvFrom.y()).emit();
6672
builder.position(to.x(), from.y(), from.z()).texCoord(uvFrom.x(), uvTo.y()).emit();
6773
builder.position(from.x(), from.y(), from.z()).texCoord(uvTo.x(), uvTo.y()).emit();
6874
builder.position(from.x(), to.y(), from.z()).texCoord(uvTo.x(), uvFrom.y()).emit();
6975
}
7076
case SOUTH -> {
77+
builder.color(0.8f, 0.8f, 0.8f);
7178
builder.position(from.x(), to.y(), to.z()).texCoord(uvFrom.x(), uvFrom.y()).emit();
7279
builder.position(from.x(), from.y(), to.z()).texCoord(uvFrom.x(), uvTo.y()).emit();
7380
builder.position(to.x(), from.y(), to.z()).texCoord(uvTo.x(), uvTo.y()).emit();

modules/freeworld.client/src/main/java/freeworld/client/render/world/ChunkCompiler.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import freeworld.client.render.builder.VertexBuilder;
1414
import freeworld.client.render.model.block.BlockModel;
1515
import freeworld.client.render.model.block.BlockModelManager;
16-
import freeworld.core.registry.BuiltinRegistries;
16+
import freeworld.core.registry.Registries;
1717
import freeworld.world.chunk.Chunk;
1818
import freeworld.world.chunk.ChunkPos;
1919

@@ -45,7 +45,7 @@ public static ChunkVertexData compile(
4545
int finalX = x;
4646
int finalY = y;
4747
int finalZ = z;
48-
final BlockModel model = blockModelManager.get(BuiltinRegistries.BLOCK_TYPE.getId(chunk.getBlockType(x, y, z)));
48+
final BlockModel model = blockModelManager.get(Registries.BLOCK_TYPE.getId(chunk.getBlockType(x, y, z)));
4949
blockRenderer.renderBlockModel(
5050
vertexBuilder,
5151
model,
@@ -61,9 +61,10 @@ public static ChunkVertexData compile(
6161
final int absNz = ChunkPos.relativeToAbsolute(cz, nz);
6262
final boolean shouldRender =
6363
(chunk.isInBound(nx, ny, nz) &&
64-
chunk.getBlockType(nx, ny, nz).air()) ||
64+
chunk.getBlockType(nx, ny, nz).nonOpaque()) ||
6565
(chunk.world().isBlockLoaded(absNx, absNy, absNz) &&
66-
chunk.world().getBlockType(absNx, absNy, absNz).air());
66+
chunk.world().getBlockType(absNx, absNy, absNz).nonOpaque()) ||
67+
!chunk.world().isBlockLoaded(absNx, absNy, absNz) /* TODO: add method world::tryLoading() */;
6768
return !shouldRender;
6869
}
6970
);

0 commit comments

Comments
 (0)