Skip to content

Commit

Permalink
Update cube entity
Browse files Browse the repository at this point in the history
  • Loading branch information
squid233 committed Jul 31, 2024
1 parent 1eb0250 commit 61ab0f8
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,11 @@ private void renderWorldEntities(GLStateMgr gl, double partialTick) {
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);
factory.create(client).render(gl,
partialTick,
Matrix4f.translation(entity.interpolatedPosition().toVector3f())
.rotateY((float) Math.toRadians(entity.rotation().y())),
entity);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,54 @@ public void render(GLStateMgr gl, double partialTick, Matrix4f positionMatrix, C
RenderSystem.useProgram(client.gameRenderer().positionColorProgram());
Tessellator t = Tessellator.getInstance();
t.begin(GLDrawMode.TRIANGLES);
float x0 = -0.5f;
float y0 = 0.0f;
float z0 = -0.5f;
float x1 = 0.5f;
float y1 = 1.0f;
float z1 = 0.5f;

// -x
t.indices(0, 1, 2, 2, 3, 0);
t.position(positionMatrix, x0, y1, z0).color(0, 255, 255).texCoord(0, 0).emit();
t.position(positionMatrix, x0, y0, z0).color(0, 255, 255).texCoord(0, 0).emit();
t.position(positionMatrix, x0, y0, z1).color(0, 255, 255).texCoord(0, 0).emit();
t.position(positionMatrix, x0, y1, z1).color(0, 255, 255).texCoord(0, 0).emit();

// +x
t.indices(0, 1, 2, 2, 3, 0);
t.position(positionMatrix, x1, y1, z1).color(255, 0, 0).texCoord(0, 0).emit();
t.position(positionMatrix, x1, y0, z1).color(255, 0, 0).texCoord(0, 0).emit();
t.position(positionMatrix, x1, y0, z0).color(255, 0, 0).texCoord(0, 0).emit();
t.position(positionMatrix, x1, y1, z0).color(255, 0, 0).texCoord(0, 0).emit();

// -y
t.indices(0, 1, 2, 2, 3, 0);
t.position(positionMatrix, x0, y0, z1).color(255, 0, 220).texCoord(0, 0).emit();
t.position(positionMatrix, x0, y0, z0).color(255, 0, 220).texCoord(0, 0).emit();
t.position(positionMatrix, x1, y0, z0).color(255, 0, 220).texCoord(0, 0).emit();
t.position(positionMatrix, x1, y0, z1).color(255, 0, 220).texCoord(0, 0).emit();

// +y
t.indices(0, 1, 2, 2, 3, 0);
t.position(positionMatrix, x0, y1, z0).color(0, 255, 33).texCoord(0, 0).emit();
t.position(positionMatrix, x0, y1, z1).color(0, 255, 33).texCoord(0, 0).emit();
t.position(positionMatrix, x1, y1, z1).color(0, 255, 33).texCoord(0, 0).emit();
t.position(positionMatrix, x1, y1, z0).color(0, 255, 33).texCoord(0, 0).emit();

// -z
t.indices(0, 1, 2, 2, 3, 0);
t.position(positionMatrix, x1, y1, z0).color(255, 216, 0).texCoord(0, 0).emit();
t.position(positionMatrix, x1, y0, z0).color(255, 216, 0).texCoord(0, 0).emit();
t.position(positionMatrix, x0, y0, z0).color(255, 216, 0).texCoord(0, 0).emit();
t.position(positionMatrix, x0, y1, z0).color(255, 216, 0).texCoord(0, 0).emit();

// +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.position(positionMatrix, x0, y1, z1).color(0, 148, 255).texCoord(0, 0).emit();
t.position(positionMatrix, x0, y0, z1).color(0, 148, 255).texCoord(0, 0).emit();
t.position(positionMatrix, x1, y0, z1).color(0, 148, 255).texCoord(0, 0).emit();
t.position(positionMatrix, x1, y1, z1).color(0, 148, 255).texCoord(0, 0).emit();

t.end(gl);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public static void bootstrap() {
register(EntityTypes.CUBE, CubeEntityRenderer::new);
}

@SuppressWarnings("unchecked")
public static <T extends Entity> void register(EntityType<T> entityType, EntityRenderer.Factory<T> renderer) {
Registry.register(REGISTRY, Registries.ENTITY_TYPE.getId(entityType), (EntityRenderer.Factory<Entity>) renderer);
}
Expand Down
14 changes: 14 additions & 0 deletions modules/freeworld.core/src/main/java/freeworld/world/World.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@

package freeworld.world;

import freeworld.math.Vector2d;
import freeworld.math.Vector3d;
import freeworld.math.Vector3i;
import freeworld.util.Int3Consumer;
import freeworld.util.math.ChunkPos;
import freeworld.util.math.MathUtil;
import freeworld.world.block.BlockType;
import freeworld.world.block.BlockTypes;
import freeworld.world.chunk.Chunk;
import freeworld.world.entity.CubeEntity;
import freeworld.world.entity.Entity;
import freeworld.world.entity.EntityType;
import freeworld.world.entity.PlayerEntity;
Expand Down Expand Up @@ -69,6 +72,17 @@ public void addListener(WorldListener listener) {
public void tick() {
motionSystem.process(this, players);
motionSystem.process(this, entities);
// TODO: test
for (Entity entity : entities) {
if (entity instanceof CubeEntity cubeEntity) {
cubeEntity.rotation = new Vector2d(0.0, cubeEntity.rotation().y() + Math.random() * 2 - 1);
cubeEntity.acceleration = MathUtil.moveRelative(
0.0, cubeEntity.onGround() && Math.random() > 0.5 ? 0.5 : 0.0, 1.0,
cubeEntity.rotation().y(),
cubeEntity.onGround() ? 0.1 : 0.02
);
}
}
}

public <T extends Entity> T createEntity(EntityType<T> type, Vector3d position) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,10 @@ public Matrix4f translate(float x, float y, float z) {
return translateGeneric(x, y, z);
}

public Matrix4f translate(Vector3f v) {
return translate(v.x(), v.y(), v.z());
}

private Matrix4f translateGeneric(float x, float y, float z) {
return new Matrix4f(
properties & ~(PROPERTY_PERSPECTIVE | PROPERTY_IDENTITY),
Expand Down

0 comments on commit 61ab0f8

Please sign in to comment.