Search in sources :

Example 1 with ChunkCell

use of io.xol.chunkstories.api.world.chunk.Chunk.ChunkCell in project chunkstories-api by Hugobros3.

the class PacketVoxelUpdate method process.

public void process(PacketSender sender, DataInputStream in, PacketReceptionContext processor) throws IOException {
    if (processor instanceof ClientPacketsProcessor) {
        ClientPacketsProcessor cpp = (ClientPacketsProcessor) processor;
        int x = in.readInt();
        int y = in.readInt();
        int z = in.readInt();
        int data = in.readInt();
        Voxel voxel = world.getContentTranslator().getVoxelForId(VoxelFormat.id(data));
        byte nextComponent = in.readByte();
        try {
            ChunkCell context = cpp.getWorld().getChunkWorldCoordinates(x, y, z).poke(x, y, z, voxel, VoxelFormat.sunlight(data), VoxelFormat.blocklight(data), VoxelFormat.meta(data), null);
            while (nextComponent != 0) {
                String componentName = in.readUTF();
                context.components().get(componentName).pull(sender, in);
                nextComponent = in.readByte();
            }
        } catch (WorldException e) {
        // Maybe the world wasn't ready ?
        // Edge case: what happens we receive an update for a chunk we haven't received the data yet ?
        // The best option would be to delay the process but this is complicated for a rare instance
        // Maybe one day
        }
    } else {
    // Fail hard
    }
}
Also used : ClientPacketsProcessor(io.xol.chunkstories.api.client.net.ClientPacketsProcessor) WorldException(io.xol.chunkstories.api.exceptions.world.WorldException) Voxel(io.xol.chunkstories.api.voxel.Voxel) ChunkCell(io.xol.chunkstories.api.world.chunk.Chunk.ChunkCell)

Example 2 with ChunkCell

use of io.xol.chunkstories.api.world.chunk.Chunk.ChunkCell in project chunkstories-api by Hugobros3.

the class InventoryTranslator method obtainInventoryHandle.

public static Inventory obtainInventoryHandle(DataInputStream in, PacketReceptionContext context) throws IOException {
    byte holderType = in.readByte();
    if (holderType == 0x01) {
        long uuid = in.readLong();
        short componentId = in.readShort();
        Entity entity = context.getWorld().getEntityByUUID(uuid);
        EntityComponent cpn = entity.getComponents().getComponentById(componentId);
        if (cpn != null && cpn instanceof EntityComponentInventory) {
            return ((EntityComponentInventory) cpn).getInventory();
        }
    } else if (holderType == 0x03) {
        int x = in.readInt();
        int y = in.readInt();
        int z = in.readInt();
        String componentName = in.readUTF();
        try {
            ChunkCell voxelContext = context.getWorld().peek(x, y, z);
            VoxelComponent com = voxelContext.components().get(componentName);
            if (com != null && com instanceof VoxelInventoryComponent) {
                VoxelInventoryComponent comp = (VoxelInventoryComponent) com;
                return comp.getInventory();
            }
        } catch (WorldException e) {
        // TODO log as warning
        // Ignore and return null
        }
    }
    return null;
}
Also used : Entity(io.xol.chunkstories.api.entity.Entity) WorldException(io.xol.chunkstories.api.exceptions.world.WorldException) VoxelComponent(io.xol.chunkstories.api.voxel.components.VoxelComponent) ChunkCell(io.xol.chunkstories.api.world.chunk.Chunk.ChunkCell) EntityComponentInventory(io.xol.chunkstories.api.entity.components.EntityComponentInventory) EntityComponent(io.xol.chunkstories.api.entity.components.EntityComponent) VoxelInventoryComponent(io.xol.chunkstories.api.voxel.components.VoxelInventoryComponent)

Example 3 with ChunkCell

use of io.xol.chunkstories.api.world.chunk.Chunk.ChunkCell in project chunkstories by Hugobros3.

the class ChunkRenderDataHolder method renderExtras.

public void renderExtras(RenderingInterface renderingInterface) {
    noDrawDeleteConflicts.acquireUninterruptibly();
    if (this.currentData != null) {
        // For each type of voxel that requires dynamic rendering...
        for (DynamicallyRenderedVoxelType drvt : this.currentData.dynamicVoxelTypes) {
            VoxelDynamicRenderer voxelDynamicRenderer = drvt.renderer;
            // Build an iterator for the list of indexes that use that renderer
            IterableIterator<ChunkCell> relevantCellsIterator = new IterableIterator<ChunkCell>() {

                Iterator<Integer> iindex = drvt.indexes.iterator();

                ChunkCell cvc = null;

                @Override
                public boolean hasNext() {
                    while (iindex.hasNext()) {
                        if (cvc != null)
                            return true;
                        int index = iindex.next();
                        cvc = chunk.peek(index / 1024, (index / 32) % 32, index % 32);
                    }
                    if (cvc != null)
                        return true;
                    return false;
                }

                @Override
                public ChunkCell next() {
                    ChunkCell cvr = cvc;
                    cvc = null;
                    return cvr;
                }
            };
            // And we call the renderer
            voxelDynamicRenderer.renderVoxels(renderingInterface, relevantCellsIterator);
        }
    }
    noDrawDeleteConflicts.release();
}
Also used : DynamicallyRenderedVoxelType(io.xol.chunkstories.renderer.chunks.ChunkMeshDataSections.DynamicallyRenderedVoxelType) VoxelDynamicRenderer(io.xol.chunkstories.api.rendering.voxel.VoxelDynamicRenderer) Iterator(java.util.Iterator) IterableIterator(io.xol.chunkstories.api.util.IterableIterator) ChunkCell(io.xol.chunkstories.api.world.chunk.Chunk.ChunkCell) IterableIterator(io.xol.chunkstories.api.util.IterableIterator)

Example 4 with ChunkCell

use of io.xol.chunkstories.api.world.chunk.Chunk.ChunkCell in project chunkstories-core by Hugobros3.

the class SignRenderer method renderVoxels.

@Override
public void renderVoxels(RenderingInterface renderingContext, IterableIterator<ChunkCell> renderableEntitiesIterator) {
    setupRender(renderingContext);
    renderingContext.setObjectMatrix(null);
    for (// .getElementsInFrustrumOnly())
    ChunkCell context : // .getElementsInFrustrumOnly())
    renderableEntitiesIterator) {
        if (renderingContext.getCamera().getCameraPosition().distance(context.getLocation()) > 32)
            continue;
        Texture2D diffuse = renderingContext.textures().getTexture("./models/sign.png");
        diffuse.setLinearFiltering(false);
        renderingContext.bindAlbedoTexture(diffuse);
        renderingContext.bindNormalTexture(renderingContext.textures().getTexture("./textures/normalnormal.png"));
        renderingContext.currentShader().setUniform2f("worldLightIn", context.getBlocklight(), context.getSunlight());
        boolean isPost = context.getVoxel().getName().endsWith("_post");
        int facing = context.getMetaData();
        Matrix4f mutrix = new Matrix4f();
        mutrix.translate(new Vector3f(0.5f, 0.0f, 0.5f));
        Location loc = context.getLocation();
        mutrix.translate((float) loc.x, (float) loc.y, (float) loc.z);
        mutrix.rotate((float) Math.PI * 2.0f * (-facing) / 16f, new Vector3f(0, 1, 0));
        if (!isPost)
            mutrix.translate(new Vector3f(0.0f, 0.0f, -0.5f));
        renderingContext.setObjectMatrix(mutrix);
        if (!isPost)
            renderingContext.meshes().getRenderableMeshByName("./models/sign_post.obj").render(renderingContext);
        else
            renderingContext.meshes().getRenderableMeshByName("./models/sign.obj").render(renderingContext);
        VoxelComponentSignText signTextComponent = (VoxelComponentSignText) context.components().get("signData");
        if (signTextComponent == null)
            continue;
        // bake sign mesh
        if (signTextComponent.cachedText == null || !signTextComponent.cachedText.equals(signTextComponent.getSignText())) {
            // entitySign.renderData = new TextMeshObject(entitySign.signText.getSignText());
            signTextComponent.cachedText = signTextComponent.getSignText();
            signTextComponent.renderData = renderingContext.getFontRenderer().newTextMeshObject(renderingContext.getFontRenderer().defaultFont(), signTextComponent.cachedText);
        }
        // signTextComponent.setSignText("fuck");
        // System.out.println("cachedText:"+signTextComponent.getSignText());
        // Display it
        mutrix.translate(new Vector3f(0.0f, 1.15f, 0.055f));
        renderingContext.setObjectMatrix(mutrix);
        signTextComponent.renderData.render(renderingContext);
    }
}
Also used : VoxelComponentSignText(io.xol.chunkstories.core.voxel.components.VoxelComponentSignText) Texture2D(io.xol.chunkstories.api.rendering.textures.Texture2D) Matrix4f(org.joml.Matrix4f) Vector3f(org.joml.Vector3f) ChunkCell(io.xol.chunkstories.api.world.chunk.Chunk.ChunkCell) Location(io.xol.chunkstories.api.Location)

Aggregations

ChunkCell (io.xol.chunkstories.api.world.chunk.Chunk.ChunkCell)4 WorldException (io.xol.chunkstories.api.exceptions.world.WorldException)2 Location (io.xol.chunkstories.api.Location)1 ClientPacketsProcessor (io.xol.chunkstories.api.client.net.ClientPacketsProcessor)1 Entity (io.xol.chunkstories.api.entity.Entity)1 EntityComponent (io.xol.chunkstories.api.entity.components.EntityComponent)1 EntityComponentInventory (io.xol.chunkstories.api.entity.components.EntityComponentInventory)1 Texture2D (io.xol.chunkstories.api.rendering.textures.Texture2D)1 VoxelDynamicRenderer (io.xol.chunkstories.api.rendering.voxel.VoxelDynamicRenderer)1 IterableIterator (io.xol.chunkstories.api.util.IterableIterator)1 Voxel (io.xol.chunkstories.api.voxel.Voxel)1 VoxelComponent (io.xol.chunkstories.api.voxel.components.VoxelComponent)1 VoxelInventoryComponent (io.xol.chunkstories.api.voxel.components.VoxelInventoryComponent)1 VoxelComponentSignText (io.xol.chunkstories.core.voxel.components.VoxelComponentSignText)1 DynamicallyRenderedVoxelType (io.xol.chunkstories.renderer.chunks.ChunkMeshDataSections.DynamicallyRenderedVoxelType)1 Iterator (java.util.Iterator)1 Matrix4f (org.joml.Matrix4f)1 Vector3f (org.joml.Vector3f)1