Search in sources :

Example 6 with WorldClient

use of io.xol.chunkstories.api.world.WorldClient in project chunkstories-core by Hugobros3.

the class EntityPlayer method onControllerInput.

@Override
public boolean onControllerInput(Input input, Controller controller) {
    // We are moving inventory bringup here !
    if (input.getName().equals("inventory") && world instanceof WorldClient) {
        if (creativeMode.get()) {
            ((WorldClient) getWorld()).getClient().openInventories(this.inventoryComponent.getInventory(), new InventoryLocalCreativeMenu(world));
        } else {
            ((WorldClient) getWorld()).getClient().openInventories(this.inventoryComponent.getInventory(), this.armor.getInventory());
        }
        return true;
    }
    Location blockLocation = this.getBlockLookingAt(true);
    double maxLen = 1024;
    if (blockLocation != null) {
        Vector3d diff = new Vector3d(blockLocation).sub(this.getLocation());
        // Vector3d dir = diff.clone().normalize();
        maxLen = diff.length();
    }
    Vector3d initialPosition = new Vector3d(getLocation());
    initialPosition.add(new Vector3d(0, eyePosition, 0));
    Vector3dc direction = getDirectionLookingAt();
    Iterator<Entity> i = world.collisionsManager().rayTraceEntities(initialPosition, direction, maxLen);
    while (i.hasNext()) {
        Entity e = i.next();
        if (e.handleInteraction(this, input))
            return true;
    }
    ItemPile itemSelected = getSelectedItemComponent().getSelectedItem();
    if (itemSelected != null) {
        // See if the item handles the interaction
        if (itemSelected.getItem().onControllerInput(this, itemSelected, input, controller))
            return true;
    }
    if (getWorld() instanceof WorldMaster) {
        // Creative mode features building and picking.
        if (this.getCreativeModeComponent().get()) {
            if (input.getName().equals("mouse.left")) {
                if (blockLocation != null) {
                    // Player events mod
                    if (controller instanceof Player) {
                        Player player = (Player) controller;
                        WorldCell cell = world.peekSafely(blockLocation);
                        FutureCell future = new FutureCell(cell);
                        future.setVoxel(this.getDefinition().store().parent().voxels().air());
                        future.setBlocklight(0);
                        future.setSunlight(0);
                        future.setMetaData(0);
                        PlayerVoxelModificationEvent event = new PlayerVoxelModificationEvent(cell, future, EntityCreative.CREATIVE_MODE, player);
                        // Anyone has objections ?
                        world.getGameContext().getPluginManager().fireEvent(event);
                        if (event.isCancelled())
                            return true;
                        Vector3d rnd = new Vector3d();
                        for (int k = 0; k < 40; k++) {
                            rnd.set(blockLocation);
                            rnd.add(Math.random() * 0.98, Math.random() * 0.98, Math.random() * 0.98);
                            world.getParticlesManager().spawnParticleAtPosition("voxel_frag", rnd);
                        }
                        world.getSoundManager().playSoundEffect("sounds/gameplay/voxel_remove.ogg", Mode.NORMAL, blockLocation, 1.0f, 1.0f);
                        try {
                            world.poke(future, this);
                        // world.poke((int)blockLocation.x, (int)blockLocation.y, (int)blockLocation.z, null, 0, 0, 0, this);
                        } catch (WorldException e) {
                        // Discard but maybe play some effect ?
                        }
                        return true;
                    }
                }
            } else if (input.getName().equals("mouse.middle")) {
                if (blockLocation != null) {
                    CellData ctx = this.getWorld().peekSafely(blockLocation);
                    if (!ctx.getVoxel().isAir()) {
                        // Spawn new itemPile in his inventory
                        ItemVoxel item = (ItemVoxel) world.getGameContext().getContent().items().getItemDefinition("item_voxel").newItem();
                        item.voxel = ctx.getVoxel();
                        item.voxelMeta = ctx.getMetaData();
                        ItemPile itemVoxel = new ItemPile(item);
                        this.getInventory().setItemPileAt(getSelectedItemComponent().getSelectedSlot(), 0, itemVoxel);
                        return true;
                    }
                }
            }
        }
    }
    // Then we check if the world minds being interacted with
    return world.handleInteraction(this, blockLocation, input);
}
Also used : Entity(io.xol.chunkstories.api.entity.Entity) Player(io.xol.chunkstories.api.player.Player) LocalPlayer(io.xol.chunkstories.api.client.LocalPlayer) ItemVoxel(io.xol.chunkstories.api.item.ItemVoxel) WorldCell(io.xol.chunkstories.api.world.World.WorldCell) WorldException(io.xol.chunkstories.api.exceptions.world.WorldException) WorldClient(io.xol.chunkstories.api.world.WorldClient) ItemPile(io.xol.chunkstories.api.item.inventory.ItemPile) CellData(io.xol.chunkstories.api.world.cell.CellData) Vector3dc(org.joml.Vector3dc) FutureCell(io.xol.chunkstories.api.world.cell.FutureCell) PlayerVoxelModificationEvent(io.xol.chunkstories.api.events.player.voxel.PlayerVoxelModificationEvent) Vector3d(org.joml.Vector3d) InventoryLocalCreativeMenu(io.xol.chunkstories.core.item.inventory.InventoryLocalCreativeMenu) WorldMaster(io.xol.chunkstories.api.world.WorldMaster) Location(io.xol.chunkstories.api.Location)

Example 7 with WorldClient

use of io.xol.chunkstories.api.world.WorldClient in project chunkstories-core by Hugobros3.

the class EntityPlayer method tick.

// Server-side updating
@Override
public void tick() {
    // This is a controllable entity, take care of controlling
    if (world instanceof WorldClient && ((WorldClient) getWorld()).getClient().getPlayer().getControlledEntity() == this)
        tickClientController(((WorldClient) getWorld()).getClient().getPlayer());
    // Tick item in hand if one such exists
    ItemPile pileSelected = getSelectedItemComponent().getSelectedItem();
    if (pileSelected != null)
        pileSelected.getItem().tickInHand(this, pileSelected);
    // Auto-pickups items on the ground
    if (world instanceof WorldMaster && (world.getTicksElapsed() % 60L) == 0L) {
        // TODO Use more precise, regional functions to not iterate over the entire world like a retard
        for (Entity e : world.getEntitiesInBox(getLocation(), new Vector3d(3.0))) {
            if (e instanceof EntityGroundItem && e.getLocation().distance(this.getLocation()) < 3.0f) {
                EntityGroundItem eg = (EntityGroundItem) e;
                if (!eg.canBePickedUpYet())
                    continue;
                world.getSoundManager().playSoundEffect("sounds/item/pickup.ogg", Mode.NORMAL, getLocation(), 1.0f, 1.0f);
                ItemPile pile = eg.getItemPile();
                if (pile != null) {
                    ItemPile left = this.inventoryComponent.getInventory().addItemPile(pile);
                    if (left == null)
                        world.removeEntity(eg);
                    else
                        eg.setItemPile(left);
                }
            }
        }
    }
    if (world instanceof WorldMaster) {
        // Take damage when starving
        if ((world.getTicksElapsed() % 100L) == 0L) {
            if (this.getFoodLevel() == 0)
                this.damage(EntityComponentFoodLevel.HUNGER_DAMAGE_CAUSE, 1);
            else {
                // 27 minutes to start starving at 0.1 starveFactor
                // Takes 100hp / ( 0.6rtps * 0.1 hp/hit )
                // Starve slowly if inactive
                float starve = 0.03f;
                // Walking drains you
                if (this.getVelocityComponent().getVelocity().length() > 0.3) {
                    starve = 0.06f;
                    // Running is even worse
                    if (this.getVelocityComponent().getVelocity().length() > 0.7)
                        starve = 0.15f;
                }
                float newfoodLevel = this.getFoodLevel() - starve;
                this.setFoodLevel(newfoodLevel);
            // System.out.println("new food level:"+newfoodLevel);
            }
        }
        // It restores hp
        if (getFoodLevel() > 20 && !this.isDead()) {
            if (this.getHealth() < this.getMaxHealth()) {
                this.setHealth(this.getHealth() + 0.01f);
                float newfoodLevel = this.getFoodLevel() - 0.01f;
                this.setFoodLevel(newfoodLevel);
            }
        }
        // Being on a ladder resets your jump height
        if (onLadder)
            lastStandingHeight = this.positionComponent.getLocation().y();
        if (this.getFlyingComponent().get())
            lastStandingHeight = Double.NaN;
    // else
    // System.out.println("prout"+(world.getTicksElapsed() % 10L));
    // System.out.println(this.getVelocityComponent().getVelocity().length());
    }
    super.tick();
}
Also used : Entity(io.xol.chunkstories.api.entity.Entity) Vector3d(org.joml.Vector3d) WorldClient(io.xol.chunkstories.api.world.WorldClient) ItemPile(io.xol.chunkstories.api.item.inventory.ItemPile) WorldMaster(io.xol.chunkstories.api.world.WorldMaster)

Example 8 with WorldClient

use of io.xol.chunkstories.api.world.WorldClient in project chunkstories-core by Hugobros3.

the class HitBoxImpl method draw.

/**
 * Debug method to figure out if the hitbox match with the model
 */
public void draw(RenderingInterface context) {
    if (!context.currentShader().getShaderName().equals("overlay")) {
        context.useShader("overlay");
        context.getCamera().setupShader(context.currentShader());
    }
    context.currentShader().setUniform1i("doTransform", 1);
    Matrix4f boneTransormation = new Matrix4f(entity.getAnimatedSkeleton().getBoneHierarchyTransformationMatrix(skeletonPart, System.currentTimeMillis() % 1000000));
    Matrix4f worldPositionTransformation = new Matrix4f();
    Location loc = entity.getLocation();
    Vector3f pos = new Vector3f((float) loc.x, (float) loc.y, (float) loc.z);
    worldPositionTransformation.translate(pos);
    worldPositionTransformation.mul(boneTransormation, boneTransormation);
    // Scales/moves the identity box to reflect collisionBox shape
    boneTransormation.translate(new Vector3f((float) box.xpos, (float) box.ypos, (float) box.zpos));
    boneTransormation.scale(new Vector3f((float) box.xw, (float) box.h, (float) box.zw));
    context.currentShader().setUniformMatrix4f("transform", boneTransormation);
    context.unbindAttributes();
    context.bindAttribute("vertexIn", context.meshes().getIdentityCube().asAttributeSource(VertexFormat.FLOAT, 3));
    context.currentShader().setUniform4f("colorIn", 0.0, 1.0, 0.0, 1.0);
    // Check for intersection with player
    EntityControllable ec = ((WorldClient) entity.getWorld()).getClient().getPlayer().getControlledEntity();
    if (ec != null) {
        if (lineIntersection((Vector3d) context.getCamera().getCameraPosition(), ((EntityPlayer) ec).getDirectionLookingAt()) != null)
            context.currentShader().setUniform4f("colorIn", 1.0, 0.0, 0.0, 1.0);
    }
    context.draw(Primitive.LINE, 0, 24);
    context.currentShader().setUniform1i("doTransform", 0);
}
Also used : Matrix4f(org.joml.Matrix4f) Vector3d(org.joml.Vector3d) Vector3f(org.joml.Vector3f) EntityControllable(io.xol.chunkstories.api.entity.interfaces.EntityControllable) WorldClient(io.xol.chunkstories.api.world.WorldClient) Location(io.xol.chunkstories.api.Location)

Example 9 with WorldClient

use of io.xol.chunkstories.api.world.WorldClient in project chunkstories-core by Hugobros3.

the class CachedLodSkeletonAnimator method getBoneHierarchyTransformationMatrixWithOffset.

@Override
public Matrix4fc getBoneHierarchyTransformationMatrixWithOffset(String nameOfEndBone, double animationTime) {
    // Don't mess with the player animation, it should NEVER be cached
    if (entity.getWorld() instanceof WorldClient && ((WorldClient) entity.getWorld()).getClient() != null && ((WorldClient) entity.getWorld()).getClient().getPlayer().getControlledEntity() == entity)
        return dataSource.getBoneHierarchyTransformationMatrixWithOffset(nameOfEndBone, animationTime);
    CachedData cachedData = cachedBones.get(nameOfEndBone);
    // If the matrix exists and doesn't need an update
    if (cachedData != null && !cachedData.needsUpdate) {
        cachedData.needsUpdate = false;
        return cachedData.matrix;
    }
    // Obtains the matrix and caches it
    Matrix4fc matrix = dataSource.getBoneHierarchyTransformationMatrixWithOffset(nameOfEndBone, animationTime);
    cachedBones.put(nameOfEndBone, new CachedData(matrix, System.currentTimeMillis()));
    return matrix;
}
Also used : Matrix4fc(org.joml.Matrix4fc) WorldClient(io.xol.chunkstories.api.world.WorldClient)

Example 10 with WorldClient

use of io.xol.chunkstories.api.world.WorldClient in project chunkstories-core by Hugobros3.

the class EntityGroundItem method tick.

@Override
public void tick() {
    // this.moveWithCollisionRestrain(0, -0.05, 0);
    Vector3d velocity = velocityComponent.getVelocity();
    if (world instanceof WorldMaster) {
        Voxel voxelIn = world.peekSafely(positionComponent.getLocation()).getVoxel();
        boolean inWater = voxelIn.getDefinition().isLiquid();
        double terminalVelocity = inWater ? -0.25 : -0.5;
        if (velocity.y() > terminalVelocity && !this.isOnGround())
            velocity.y = (velocity.y() - 0.016);
        if (velocity.y() < terminalVelocity)
            velocity.y = (terminalVelocity);
        Vector3dc remainingToMove = moveWithCollisionRestrain(velocity.x(), velocity.y(), velocity.z());
        if (remainingToMove.y() < -0.02 && this.isOnGround()) {
            if (remainingToMove.y() < -0.01) {
                // Bounce
                double originalDownardsVelocity = velocity.y();
                double bounceFactor = 0.15;
                velocity.mul(bounceFactor);
                velocity.y = (-originalDownardsVelocity * bounceFactor);
            // world.getSoundManager().playSoundEffect("./sounds/dogez/weapon/grenades/grenade_bounce.ogg", Mode.NORMAL, getLocation(), 1, 1, 10, 35);
            } else
                velocity.mul(0d);
        }
        if (Math.abs(velocity.x()) < 0.02)
            velocity.x = (0.0);
        if (Math.abs(velocity.z()) < 0.02)
            velocity.z = (0.0);
        if (Math.abs(velocity.y()) < 0.01)
            velocity.y = (0.0);
        velocityComponent.setVelocity(velocity);
    }
    if (world instanceof WorldClient) {
        if (this.isOnGround()) {
            rotation += 1.0f;
            rotation %= 360;
        }
    }
    super.tick();
}
Also used : Vector3dc(org.joml.Vector3dc) Vector3d(org.joml.Vector3d) Voxel(io.xol.chunkstories.api.voxel.Voxel) WorldClient(io.xol.chunkstories.api.world.WorldClient) WorldMaster(io.xol.chunkstories.api.world.WorldMaster)

Aggregations

WorldClient (io.xol.chunkstories.api.world.WorldClient)11 Vector3d (org.joml.Vector3d)8 WorldMaster (io.xol.chunkstories.api.world.WorldMaster)6 Location (io.xol.chunkstories.api.Location)4 Vector3dc (org.joml.Vector3dc)4 Entity (io.xol.chunkstories.api.entity.Entity)3 EntityControllable (io.xol.chunkstories.api.entity.interfaces.EntityControllable)3 Voxel (io.xol.chunkstories.api.voxel.Voxel)3 Controller (io.xol.chunkstories.api.entity.Controller)2 ItemVoxel (io.xol.chunkstories.api.item.ItemVoxel)2 ItemPile (io.xol.chunkstories.api.item.inventory.ItemPile)2 RemotePlayer (io.xol.chunkstories.api.server.RemotePlayer)2 WorldCell (io.xol.chunkstories.api.world.World.WorldCell)2 LocalPlayer (io.xol.chunkstories.api.client.LocalPlayer)1 EntityLiving (io.xol.chunkstories.api.entity.EntityLiving)1 HitBox (io.xol.chunkstories.api.entity.EntityLiving.HitBox)1 EntityComponentRotation (io.xol.chunkstories.api.entity.components.EntityComponentRotation)1 EntityCreative (io.xol.chunkstories.api.entity.interfaces.EntityCreative)1 EntityFlying (io.xol.chunkstories.api.entity.interfaces.EntityFlying)1 PlayerVoxelModificationEvent (io.xol.chunkstories.api.events.player.voxel.PlayerVoxelModificationEvent)1