From 1eb02505d129a63ee80ecee0b814647701a0d031 Mon Sep 17 00:00:00 2001 From: squid233 <60126026+squid233@users.noreply.github.com> Date: Wed, 31 Jul 2024 00:42:06 +0800 Subject: [PATCH] Add CubeEntityRenderer --- .../main/java/freeworld/client/Freeworld.java | 133 ++++++++++-------- .../freeworld/client/render/GameRenderer.java | 22 ++- .../render/model/block/BlockModelManager.java | 2 +- .../client/render/world/WorldRenderer.java | 1 + .../world/{ => block}/BlockRenderer.java | 4 +- .../world/{ => chunk}/ChunkCompiler.java | 4 +- .../world/entity/CubeEntityRenderer.java | 45 ++++++ .../render/world/entity/EntityRenderer.java | 34 +++++ .../render/world/entity/EntityRenderers.java | 42 ++++++ .../chunk}/ChunkVertexData.java | 2 +- .../client/world/chunk/ClientChunk.java | 3 +- .../src/main/java/module-info.java | 2 + .../freeworld/registry/DefaultedRegistry.java | 9 +- .../freeworld/registry/MappedRegistry.java | 9 +- .../java/freeworld/registry/Registry.java | 4 +- .../src/main/java/freeworld/world/World.java | 6 +- .../world/block}/BlockHitResult.java | 3 +- .../java/freeworld/world/entity/Entity.java | 24 +++- .../freeworld/world/entity/EntityTypes.java | 2 +- .../world/entity/system/MotionSystem.java | 3 +- 20 files changed, 272 insertions(+), 82 deletions(-) rename modules/freeworld.client/src/main/java/freeworld/client/render/world/{ => block}/BlockRenderer.java (97%) rename modules/freeworld.client/src/main/java/freeworld/client/render/world/{ => chunk}/ChunkCompiler.java (95%) create mode 100644 modules/freeworld.client/src/main/java/freeworld/client/render/world/entity/CubeEntityRenderer.java create mode 100644 modules/freeworld.client/src/main/java/freeworld/client/render/world/entity/EntityRenderer.java create mode 100644 modules/freeworld.client/src/main/java/freeworld/client/render/world/entity/EntityRenderers.java rename modules/freeworld.client/src/main/java/freeworld/client/{render/world => world/chunk}/ChunkVertexData.java (94%) rename modules/{freeworld.client/src/main/java/freeworld/client/render/world => freeworld.core/src/main/java/freeworld/world/block}/BlockHitResult.java (87%) diff --git a/modules/freeworld.client/src/main/java/freeworld/client/Freeworld.java b/modules/freeworld.client/src/main/java/freeworld/client/Freeworld.java index 99d3723..01f4ad1 100644 --- a/modules/freeworld.client/src/main/java/freeworld/client/Freeworld.java +++ b/modules/freeworld.client/src/main/java/freeworld/client/Freeworld.java @@ -19,7 +19,8 @@ import freeworld.client.render.screen.Screen; import freeworld.client.render.screen.ingame.CreativeTabScreen; import freeworld.client.render.screen.ingame.PauseScreen; -import freeworld.client.render.world.BlockHitResult; +import freeworld.client.render.world.entity.EntityRenderers; +import freeworld.world.block.BlockHitResult; import freeworld.client.render.world.WorldRenderer; import freeworld.math.Vector2d; import freeworld.math.Vector3d; @@ -124,11 +125,6 @@ public void start() { BlockTypes.bootstrap(); EntityTypes.bootstrap(); - - blockModelManager = new BlockModelManager(); - blockModelManager.bootstrap(); - - setScreen(new PauseScreen(this)); world = new World("New world", new Random().nextLong()); player = world.createEntity(EntityTypes.PLAYER, new Vector3d(0.0, 64.0, 0.0)); @@ -236,65 +232,73 @@ private void onScroll(double x, double y) { } } + private void worldInput() { + double speed; + if (player.flying()) { + speed = 0.5; // TODO: this value is only for test + } else { + if (player.onGround()) { + speed = 0.1; + } 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; + 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 ((player.onGround() || player.flying()) && glfw.getKey(window, GLFW.KEY_SPACE) == GLFW.PRESS) { + yo += 1.0; + } + if (player.flying() && glfw.getKey(window, GLFW.KEY_LEFT_SHIFT) == GLFW.PRESS) { + yo -= 1.0; + } + player.acceleration = MathUtil.moveRelative(xo, yo * (player.flying() ? speed : 0.5), zo, player.rotation().y(), speed); + + if (blockDestroyTimer >= 2) { + final BlockHitResult hitResult = gameRenderer.hitResult(); + if (!hitResult.missed() && + glfw.getMouseButton(window, GLFW.MOUSE_BUTTON_LEFT) == GLFW.PRESS) { + Vector3i position = hitResult.position(); + world.setBlockType(position.x(), position.y(), position.z(), BlockTypes.AIR); + blockDestroyTimer = 0; + } + } + if (blockPlaceTimer >= 2) { + final BlockHitResult hitResult = gameRenderer.hitResult(); + if (!hitResult.missed() && + glfw.getMouseButton(window, GLFW.MOUSE_BUTTON_RIGHT) == GLFW.PRESS) { + final Direction face = hitResult.face(); + final BlockType type = player.getHandItem(); + if (!type.air()) { + Vector3i axis = face.axis(); + Vector3i position = hitResult.position(); + Vector3i add = position.add(axis); + if (world.getBlockType(position.x(), position.y(), position.z()).replaceable() || + world.getBlockType(add.x(), add.y(), add.z()).replaceable()) { + world.setBlockType(add.x(), add.y(), add.z(), type); + } + } + blockPlaceTimer = 0; + } + } + blockDestroyTimer++; + blockPlaceTimer++; + + if (glfw.getKey(window, GLFW.KEY_G) == GLFW.PRESS) { + world.createEntity(EntityTypes.CUBE, player.position()); + } + } + private void tick() { if (world != null) { camera.preUpdate(); if (screen == null) { - double speed; - if (player.flying()) { - speed = 0.5; // TODO: this value is only for test - } else { - if (player.onGround()) { - speed = 0.1; - } 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; - 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 ((player.onGround() || player.flying()) && glfw.getKey(window, GLFW.KEY_SPACE) == GLFW.PRESS) { - yo += 1.0; - } - if (player.flying() && glfw.getKey(window, GLFW.KEY_LEFT_SHIFT) == GLFW.PRESS) { - yo -= 1.0; - } - player.acceleration = MathUtil.moveRelative(xo, yo * (player.flying() ? speed : 0.5), zo, player.rotation().y(), speed); - - if (blockDestroyTimer >= 2) { - final BlockHitResult hitResult = gameRenderer.hitResult(); - if (!hitResult.missed() && - glfw.getMouseButton(window, GLFW.MOUSE_BUTTON_LEFT) == GLFW.PRESS) { - Vector3i position = hitResult.position(); - world.setBlockType(position.x(), position.y(), position.z(), BlockTypes.AIR); - blockDestroyTimer = 0; - } - } - if (blockPlaceTimer >= 2) { - final BlockHitResult hitResult = gameRenderer.hitResult(); - if (!hitResult.missed() && - glfw.getMouseButton(window, GLFW.MOUSE_BUTTON_RIGHT) == GLFW.PRESS) { - final Direction face = hitResult.face(); - final BlockType type = player.getHandItem(); - if (!type.air()) { - Vector3i axis = face.axis(); - Vector3i position = hitResult.position(); - Vector3i add = position.add(axis); - if (world.getBlockType(position.x(), position.y(), position.z()).replaceable() || - world.getBlockType(add.x(), add.y(), add.z()).replaceable()) { - world.setBlockType(add.x(), add.y(), add.z(), type); - } - } - blockPlaceTimer = 0; - } - } - blockDestroyTimer++; - blockPlaceTimer++; + worldInput(); } world.tick(); } @@ -310,8 +314,15 @@ private void initGL() { RenderSystem.initialize(gl); + blockModelManager = new BlockModelManager(); + blockModelManager.bootstrap(); + + EntityRenderers.bootstrap(); + gameRenderer = new GameRenderer(this); gameRenderer.init(gl); + + setScreen(new PauseScreen(this)); } public void run() { diff --git a/modules/freeworld.client/src/main/java/freeworld/client/render/GameRenderer.java b/modules/freeworld.client/src/main/java/freeworld/client/render/GameRenderer.java index 719de76..d494cf1 100644 --- a/modules/freeworld.client/src/main/java/freeworld/client/render/GameRenderer.java +++ b/modules/freeworld.client/src/main/java/freeworld/client/render/GameRenderer.java @@ -25,16 +25,18 @@ import freeworld.client.render.texture.TextureManager; import freeworld.client.render.vertex.VertexLayout; import freeworld.client.render.vertex.VertexLayouts; -import freeworld.client.render.world.BlockHitResult; -import freeworld.client.render.world.BlockRenderer; import freeworld.client.render.world.WorldRenderer; +import freeworld.client.render.world.block.BlockRenderer; +import freeworld.client.render.world.entity.EntityRenderers; import freeworld.client.world.chunk.ClientChunk; -import freeworld.util.Identifier; import freeworld.math.Matrix4f; import freeworld.math.Vector3i; +import freeworld.registry.Registries; import freeworld.util.Direction; +import freeworld.util.Identifier; import freeworld.util.Logging; import freeworld.util.math.Lined; +import freeworld.world.block.BlockHitResult; import freeworld.world.entity.Entity; import org.slf4j.Logger; import overrungl.opengl.GL10C; @@ -194,6 +196,20 @@ private void renderWorld(GLStateMgr gl, double partialTick) { } tessellator.end(gl); } + + renderWorldEntities(gl, partialTick); + } + + private void renderWorldEntities(GLStateMgr gl, double partialTick) { + RenderSystem.useProgram(positionColorProgram); + RenderSystem.updateMatrices(); + for (Entity entity : client.world().entities()) { + entity.interpolatePosition(partialTick); + var factory = EntityRenderers.registry().getById(Registries.ENTITY_TYPE.getId(entity.entityType())); + if (factory != null) { + factory.create(client).render(gl, partialTick, Matrix4f.translation(entity.interpolatedPosition().toVector3f()), entity); + } + } } private void renderHud(GLStateMgr gl, double partialTick) { diff --git a/modules/freeworld.client/src/main/java/freeworld/client/render/model/block/BlockModelManager.java b/modules/freeworld.client/src/main/java/freeworld/client/render/model/block/BlockModelManager.java index ec72db3..888cf44 100644 --- a/modules/freeworld.client/src/main/java/freeworld/client/render/model/block/BlockModelManager.java +++ b/modules/freeworld.client/src/main/java/freeworld/client/render/model/block/BlockModelManager.java @@ -70,7 +70,7 @@ public void bootstrap() { } public BlockModel get(Identifier identifier) { - return registry.get(identifier); + return registry.getById(identifier); } public DefaultedRegistry registry() { diff --git a/modules/freeworld.client/src/main/java/freeworld/client/render/world/WorldRenderer.java b/modules/freeworld.client/src/main/java/freeworld/client/render/world/WorldRenderer.java index 51cc7ac..019fcc4 100644 --- a/modules/freeworld.client/src/main/java/freeworld/client/render/world/WorldRenderer.java +++ b/modules/freeworld.client/src/main/java/freeworld/client/render/world/WorldRenderer.java @@ -25,6 +25,7 @@ import freeworld.util.math.HitResult; import freeworld.world.World; import freeworld.world.WorldListener; +import freeworld.world.block.BlockHitResult; import freeworld.world.block.BlockType; import freeworld.world.entity.Entity; import org.slf4j.Logger; diff --git a/modules/freeworld.client/src/main/java/freeworld/client/render/world/BlockRenderer.java b/modules/freeworld.client/src/main/java/freeworld/client/render/world/block/BlockRenderer.java similarity index 97% rename from modules/freeworld.client/src/main/java/freeworld/client/render/world/BlockRenderer.java rename to modules/freeworld.client/src/main/java/freeworld/client/render/world/block/BlockRenderer.java index ef6911d..5d42944 100644 --- a/modules/freeworld.client/src/main/java/freeworld/client/render/world/BlockRenderer.java +++ b/modules/freeworld.client/src/main/java/freeworld/client/render/world/block/BlockRenderer.java @@ -8,7 +8,7 @@ * only version 2.1 of the License. */ -package freeworld.client.render.world; +package freeworld.client.render.world.block; import freeworld.client.render.model.block.BlockModel; import freeworld.client.render.model.block.BlockModelFace; @@ -47,6 +47,7 @@ private void emitVertices(VertexBuilder builder, Matrix4f matrix, Vector3f from, case NORTH, SOUTH -> 0.8f; }; Vector4i vertexIndices = direction.vertexIndices(); + builder.indices(0, 1, 2, 2, 3, 0); builder.position(matrix, AABBox.getPoint(from, to, vertexIndices.x())).color(color, color, color).texCoord(uvFrom.x(), uvFrom.y()).emit(); builder.position(matrix, AABBox.getPoint(from, to, vertexIndices.y())).color(color, color, color).texCoord(uvFrom.x(), uvTo.y()).emit(); builder.position(matrix, AABBox.getPoint(from, to, vertexIndices.z())).color(color, color, color).texCoord(uvTo.x(), uvTo.y()).emit(); @@ -73,7 +74,6 @@ public void renderBlockModel(VertexBuilder builder, BlockModel model, Matrix4f m final Vector2f uvFrom = face.uvFrom().mul(region.width(), region.height()).add(region.x(), region.y()).div(width, height); final Vector2f uvTo = face.uvTo().mul(region.width(), region.height()).add(region.x(), region.y()).div(width, height); - builder.indices(0, 1, 2, 2, 3, 0); emitVertices(builder, matrix, from, to, uvFrom, uvTo, e.getKey()); } } diff --git a/modules/freeworld.client/src/main/java/freeworld/client/render/world/ChunkCompiler.java b/modules/freeworld.client/src/main/java/freeworld/client/render/world/chunk/ChunkCompiler.java similarity index 95% rename from modules/freeworld.client/src/main/java/freeworld/client/render/world/ChunkCompiler.java rename to modules/freeworld.client/src/main/java/freeworld/client/render/world/chunk/ChunkCompiler.java index a03a845..74f9da3 100644 --- a/modules/freeworld.client/src/main/java/freeworld/client/render/world/ChunkCompiler.java +++ b/modules/freeworld.client/src/main/java/freeworld/client/render/world/chunk/ChunkCompiler.java @@ -8,11 +8,13 @@ * only version 2.1 of the License. */ -package freeworld.client.render.world; +package freeworld.client.render.world.chunk; import freeworld.client.render.vertex.VertexBuilder; import freeworld.client.render.model.block.BlockModel; import freeworld.client.render.model.block.BlockModelManager; +import freeworld.client.render.world.block.BlockRenderer; +import freeworld.client.world.chunk.ChunkVertexData; import freeworld.registry.Registries; import freeworld.math.Vector3i; import freeworld.world.chunk.Chunk; diff --git a/modules/freeworld.client/src/main/java/freeworld/client/render/world/entity/CubeEntityRenderer.java b/modules/freeworld.client/src/main/java/freeworld/client/render/world/entity/CubeEntityRenderer.java new file mode 100644 index 0000000..b105c13 --- /dev/null +++ b/modules/freeworld.client/src/main/java/freeworld/client/render/world/entity/CubeEntityRenderer.java @@ -0,0 +1,45 @@ +/* + * 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; + * only version 2.1 of the License. + */ + +package freeworld.client.render.world.entity; + +import freeworld.client.Freeworld; +import freeworld.client.render.RenderSystem; +import freeworld.client.render.Tessellator; +import freeworld.client.render.gl.GLDrawMode; +import freeworld.client.render.gl.GLStateMgr; +import freeworld.math.Matrix4f; +import freeworld.world.entity.CubeEntity; + +/** + * @author squid233 + * @since 0.1.0 + */ +public class CubeEntityRenderer extends EntityRenderer { + public CubeEntityRenderer(Freeworld client) { + super(client); + } + + @Override + public void render(GLStateMgr gl, double partialTick, Matrix4f positionMatrix, CubeEntity entity) { + RenderSystem.useProgram(client.gameRenderer().positionColorProgram()); + Tessellator t = Tessellator.getInstance(); + t.begin(GLDrawMode.TRIANGLES); + + // +z + t.indices(0, 1, 2, 2, 3, 0); + t.position(positionMatrix, 0.0f, 1.0f, 1.0f).color(0, 148, 255).texCoord(0, 0).emit(); + t.position(positionMatrix, 0.0f, 0.0f, 1.0f).color(0, 148, 255).texCoord(0, 0).emit(); + t.position(positionMatrix, 1.0f, 0.0f, 1.0f).color(0, 148, 255).texCoord(0, 0).emit(); + t.position(positionMatrix, 1.0f, 1.0f, 1.0f).color(0, 148, 255).texCoord(0, 0).emit(); + + t.end(gl); + } +} diff --git a/modules/freeworld.client/src/main/java/freeworld/client/render/world/entity/EntityRenderer.java b/modules/freeworld.client/src/main/java/freeworld/client/render/world/entity/EntityRenderer.java new file mode 100644 index 0000000..6ef5e2a --- /dev/null +++ b/modules/freeworld.client/src/main/java/freeworld/client/render/world/entity/EntityRenderer.java @@ -0,0 +1,34 @@ +/* + * 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; + * only version 2.1 of the License. + */ + +package freeworld.client.render.world.entity; + +import freeworld.client.Freeworld; +import freeworld.client.render.gl.GLStateMgr; +import freeworld.math.Matrix4f; +import freeworld.world.entity.Entity; + +/** + * @author squid233 + * @since 0.1.0 + */ +public abstract class EntityRenderer { + protected final Freeworld client; + + protected EntityRenderer(Freeworld client) { + this.client = client; + } + + public interface Factory { + EntityRenderer create(Freeworld client); + } + + public abstract void render(GLStateMgr gl, double partialTick, Matrix4f positionMatrix, T entity); +} diff --git a/modules/freeworld.client/src/main/java/freeworld/client/render/world/entity/EntityRenderers.java b/modules/freeworld.client/src/main/java/freeworld/client/render/world/entity/EntityRenderers.java new file mode 100644 index 0000000..368eb79 --- /dev/null +++ b/modules/freeworld.client/src/main/java/freeworld/client/render/world/entity/EntityRenderers.java @@ -0,0 +1,42 @@ +/* + * 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; + * only version 2.1 of the License. + */ + +package freeworld.client.render.world.entity; + +import freeworld.registry.MappedRegistry; +import freeworld.registry.Registries; +import freeworld.registry.Registry; +import freeworld.util.Identifier; +import freeworld.world.entity.Entity; +import freeworld.world.entity.EntityType; +import freeworld.world.entity.EntityTypes; + +/** + * @author squid233 + * @since 0.1.0 + */ +public final class EntityRenderers { + private static final MappedRegistry> REGISTRY = new MappedRegistry<>(Identifier.ofBuiltin("entity_renderer")); + + private EntityRenderers() { + } + + public static void bootstrap() { + register(EntityTypes.CUBE, CubeEntityRenderer::new); + } + + public static void register(EntityType entityType, EntityRenderer.Factory renderer) { + Registry.register(REGISTRY, Registries.ENTITY_TYPE.getId(entityType), (EntityRenderer.Factory) renderer); + } + + public static Registry> registry() { + return REGISTRY; + } +} diff --git a/modules/freeworld.client/src/main/java/freeworld/client/render/world/ChunkVertexData.java b/modules/freeworld.client/src/main/java/freeworld/client/world/chunk/ChunkVertexData.java similarity index 94% rename from modules/freeworld.client/src/main/java/freeworld/client/render/world/ChunkVertexData.java rename to modules/freeworld.client/src/main/java/freeworld/client/world/chunk/ChunkVertexData.java index d5223aa..85162d6 100644 --- a/modules/freeworld.client/src/main/java/freeworld/client/render/world/ChunkVertexData.java +++ b/modules/freeworld.client/src/main/java/freeworld/client/world/chunk/ChunkVertexData.java @@ -8,7 +8,7 @@ * only version 2.1 of the License. */ -package freeworld.client.render.world; +package freeworld.client.world.chunk; import freeworld.client.render.vertex.VertexLayout; diff --git a/modules/freeworld.client/src/main/java/freeworld/client/world/chunk/ClientChunk.java b/modules/freeworld.client/src/main/java/freeworld/client/world/chunk/ClientChunk.java index 201fb3e..9d3e84d 100644 --- a/modules/freeworld.client/src/main/java/freeworld/client/world/chunk/ClientChunk.java +++ b/modules/freeworld.client/src/main/java/freeworld/client/world/chunk/ClientChunk.java @@ -14,8 +14,7 @@ import freeworld.client.render.RenderSystem; import freeworld.client.render.gl.GLStateMgr; import freeworld.client.render.vertex.VertexLayout; -import freeworld.client.render.world.ChunkCompiler; -import freeworld.client.render.world.ChunkVertexData; +import freeworld.client.render.world.chunk.ChunkCompiler; import freeworld.client.render.world.WorldRenderer; import freeworld.world.World; import freeworld.world.chunk.Chunk; diff --git a/modules/freeworld.client/src/main/java/module-info.java b/modules/freeworld.client/src/main/java/module-info.java index 9bac55b..3090b6b 100644 --- a/modules/freeworld.client/src/main/java/module-info.java +++ b/modules/freeworld.client/src/main/java/module-info.java @@ -29,6 +29,8 @@ exports freeworld.client.render.texture; exports freeworld.client.render.vertex; exports freeworld.client.render.world; + exports freeworld.client.render.world.block; + exports freeworld.client.render.world.chunk; exports freeworld.client.util; exports freeworld.client.world.chunk; diff --git a/modules/freeworld.core/src/main/java/freeworld/registry/DefaultedRegistry.java b/modules/freeworld.core/src/main/java/freeworld/registry/DefaultedRegistry.java index aa312c5..f6fddeb 100644 --- a/modules/freeworld.core/src/main/java/freeworld/registry/DefaultedRegistry.java +++ b/modules/freeworld.core/src/main/java/freeworld/registry/DefaultedRegistry.java @@ -35,8 +35,8 @@ public T getDefaultValue() { } @Override - public T get(Identifier identifier) { - final T t = super.get(identifier); + public T getById(Identifier identifier) { + final T t = super.getById(identifier); return t != null ? t : getDefaultValue(); } @@ -51,4 +51,9 @@ public Identifier getId(T entry) { final Identifier id = super.getId(entry); return id != null ? id : super.getId(getDefaultValue()); } + + @Override + public int getRawId(Identifier identifier) { + return idToRawIdMap.containsKey(identifier) ? idToRawIdMap.get(identifier) : super.getRawId(getId(getDefaultValue())); + } } diff --git a/modules/freeworld.core/src/main/java/freeworld/registry/MappedRegistry.java b/modules/freeworld.core/src/main/java/freeworld/registry/MappedRegistry.java index 732340e..b46d161 100644 --- a/modules/freeworld.core/src/main/java/freeworld/registry/MappedRegistry.java +++ b/modules/freeworld.core/src/main/java/freeworld/registry/MappedRegistry.java @@ -33,7 +33,7 @@ public class MappedRegistry implements MutableRegistry { private final Map idToEntryMap = HashMap.newHashMap(DEFAULT_CAPACITY); private final Map entryToIdMap = HashMap.newHashMap(DEFAULT_CAPACITY); private final Map rawIdToEntryMap = HashMap.newHashMap(DEFAULT_CAPACITY); - private final Map idToRawIdMap = HashMap.newHashMap(DEFAULT_CAPACITY); + protected final Map idToRawIdMap = HashMap.newHashMap(DEFAULT_CAPACITY); private int nextId = -1; public MappedRegistry(Identifier registryName) { @@ -80,7 +80,7 @@ public T remove(Identifier identifier) { } @Override - public T get(Identifier identifier) { + public T getById(Identifier identifier) { return idToEntryMap.get(identifier); } @@ -94,6 +94,11 @@ public Identifier getId(T entry) { return entryToIdMap.get(entry); } + @Override + public int getRawId(Identifier identifier) { + return idToRawIdMap.get(identifier); + } + @Override public int size() { return idToEntryMap.size(); diff --git a/modules/freeworld.core/src/main/java/freeworld/registry/Registry.java b/modules/freeworld.core/src/main/java/freeworld/registry/Registry.java index 14ed73b..d6ee82a 100644 --- a/modules/freeworld.core/src/main/java/freeworld/registry/Registry.java +++ b/modules/freeworld.core/src/main/java/freeworld/registry/Registry.java @@ -37,11 +37,13 @@ static T register(MutableRegistry registry, String id, T entry) { * * @param identifier the identifier */ - T get(Identifier identifier); + T getById(Identifier identifier); T getByRawId(int rawId); Identifier getId(T entry); + int getRawId(Identifier identifier); + int size(); } diff --git a/modules/freeworld.core/src/main/java/freeworld/world/World.java b/modules/freeworld.core/src/main/java/freeworld/world/World.java index a14fd4c..59b5fa9 100644 --- a/modules/freeworld.core/src/main/java/freeworld/world/World.java +++ b/modules/freeworld.core/src/main/java/freeworld/world/World.java @@ -46,7 +46,7 @@ public World(String name, long seed) { } public static void forInChunkRange(Entity player, int chunkRadius, Int3Consumer consumer) { - Vector3i chunkPos = ChunkPos.absoluteToChunk(player.position.toVector3iFloor()); + Vector3i chunkPos = ChunkPos.absoluteToChunk(player.position().toVector3iFloor()); int minX = chunkPos.x() - chunkRadius; int maxX = chunkPos.x() + chunkRadius; int minY = chunkPos.y() - chunkRadius; @@ -147,4 +147,8 @@ public void setBlockType(int x, int y, int z, BlockType blockType) { public long seed() { return seed; } + + public List entities() { + return entities; + } } diff --git a/modules/freeworld.client/src/main/java/freeworld/client/render/world/BlockHitResult.java b/modules/freeworld.core/src/main/java/freeworld/world/block/BlockHitResult.java similarity index 87% rename from modules/freeworld.client/src/main/java/freeworld/client/render/world/BlockHitResult.java rename to modules/freeworld.core/src/main/java/freeworld/world/block/BlockHitResult.java index 8b77bf2..5b628c8 100644 --- a/modules/freeworld.client/src/main/java/freeworld/client/render/world/BlockHitResult.java +++ b/modules/freeworld.core/src/main/java/freeworld/world/block/BlockHitResult.java @@ -8,11 +8,10 @@ * only version 2.1 of the License. */ -package freeworld.client.render.world; +package freeworld.world.block; import freeworld.math.Vector3i; import freeworld.util.Direction; -import freeworld.world.block.BlockType; /** * @author squid233 diff --git a/modules/freeworld.core/src/main/java/freeworld/world/entity/Entity.java b/modules/freeworld.core/src/main/java/freeworld/world/entity/Entity.java index a2b6180..22982c4 100644 --- a/modules/freeworld.core/src/main/java/freeworld/world/entity/Entity.java +++ b/modules/freeworld.core/src/main/java/freeworld/world/entity/Entity.java @@ -30,7 +30,9 @@ public class Entity { public Vector3d eyePosition; public boolean flying = false; public boolean onGround = false; - public Vector3d position = Vector3d.ZERO; + private Vector3d previousPosition = Vector3d.ZERO; + private Vector3d position = Vector3d.ZERO; + private Vector3d interpolatedPosition = Vector3d.ZERO; public Vector2d rotation = Vector2d.ZERO; public Vector3d velocity = Vector3d.ZERO; @@ -62,6 +64,18 @@ public final void init(Vector3d position) { this.boundingBox = boundingBox(position, entityType.dimension()); } + public void updatePreviousPosition() { + this.previousPosition = position; + } + + public void setPosition(Vector3d position) { + this.position = position; + } + + public void interpolatePosition(double partialTick) { + interpolatedPosition = previousPosition.lerp(position, partialTick); + } + public World world() { return world; } @@ -94,10 +108,18 @@ public boolean onGround() { return onGround; } + public Vector3d previousPosition() { + return previousPosition; + } + public Vector3d position() { return position; } + public Vector3d interpolatedPosition() { + return interpolatedPosition; + } + public Vector2d rotation() { return rotation; } diff --git a/modules/freeworld.core/src/main/java/freeworld/world/entity/EntityTypes.java b/modules/freeworld.core/src/main/java/freeworld/world/entity/EntityTypes.java index c7b1ea4..aaa8709 100644 --- a/modules/freeworld.core/src/main/java/freeworld/world/entity/EntityTypes.java +++ b/modules/freeworld.core/src/main/java/freeworld/world/entity/EntityTypes.java @@ -24,7 +24,7 @@ public final class EntityTypes { /** * A cube entity is for test. */ - public static final EntityType CUBE = register("cube", new EntityType<>(new EntityType.Settings().dimension(new Vector3d(1.0)), CubeEntity::new)); + public static final EntityType CUBE = register("cube", new EntityType<>(new EntityType.Settings().dimension(new Vector3d(1.0)), CubeEntity::new)); private EntityTypes() { } diff --git a/modules/freeworld.core/src/main/java/freeworld/world/entity/system/MotionSystem.java b/modules/freeworld.core/src/main/java/freeworld/world/entity/system/MotionSystem.java index de396fc..67ff32e 100644 --- a/modules/freeworld.core/src/main/java/freeworld/world/entity/system/MotionSystem.java +++ b/modules/freeworld.core/src/main/java/freeworld/world/entity/system/MotionSystem.java @@ -99,7 +99,8 @@ public void process(World world, List entities) { } entity.velocity = new Vector3d(fvx, fvy, fvz); - entity.position = entity.position().add(moveX, moveY, moveZ); + entity.updatePreviousPosition(); + entity.setPosition(entity.position().add(moveX, moveY, moveZ)); entity.boundingBox = Entity.boundingBox(entity.position(), entity.boundingBox().dimension()); if (entity.flying()) {