Search in sources :

Example 26 with EntityControllable

use of io.xol.chunkstories.api.entity.interfaces.EntityControllable in project chunkstories-core by Hugobros3.

the class EntityLivingImplementation method tick.

@Override
public void tick() {
    if (getWorld() == null)
        return;
    // Despawn counter is strictly a client matter
    if (getWorld() instanceof WorldMaster) {
        if (isDead()) {
            deathDespawnTimer--;
            if (deathDespawnTimer < 0) {
                world.removeEntity(this);
                return;
            }
        }
        // Fall damage
        if (isOnGround()) {
            if (!wasStandingLastTick && !Double.isNaN(lastStandingHeight)) {
                double fallDistance = lastStandingHeight - this.positionComponent.getLocation().y();
                if (fallDistance > 0) {
                    // System.out.println("Fell "+fallDistance+" meters");
                    if (fallDistance > 5) {
                        float fallDamage = (float) (fallDistance * fallDistance / 2);
                        System.out.println(this + "Took " + fallDamage + " hp of fall damage");
                        this.damage(DAMAGE_CAUSE_FALL, fallDamage);
                    }
                }
            }
            lastStandingHeight = this.positionComponent.getLocation().y();
        }
        this.wasStandingLastTick = isOnGround();
    }
    boolean shouldDoTick = false;
    if (this instanceof EntityControllable) {
        Controller controller = ((EntityControllable) this).getControllerComponent().getController();
        if (controller == null)
            shouldDoTick = (getWorld() instanceof WorldMaster);
        else if (getWorld() instanceof WorldClient && ((WorldClient) getWorld()).getClient().getPlayer().equals(controller))
            shouldDoTick = true;
    } else
        shouldDoTick = (getWorld() instanceof WorldMaster);
    if (shouldDoTick) {
        Vector3dc ogVelocity = getVelocityComponent().getVelocity();
        Vector3d velocity = new Vector3d(ogVelocity);
        Vector2f headRotationVelocity = this.getEntityRotationComponent().tickInpulse();
        getEntityRotationComponent().addRotation(headRotationVelocity.x(), headRotationVelocity.y());
        // voxelIn = VoxelsStore.get().getVoxelById(VoxelFormat.id(world.getVoxelData(positionComponent.getLocation())));
        // voxelIn.getType().isLiquid();
        boolean inWater = isInWater();
        // Gravity
        if (!(this instanceof EntityFlying && ((EntityFlying) this).getFlyingComponent().get())) {
            double terminalVelocity = inWater ? -0.05 : -0.5;
            if (velocity.y() > terminalVelocity)
                velocity.y = (velocity.y() - 0.008);
            if (velocity.y() < terminalVelocity)
                velocity.y = (terminalVelocity);
            // Water limits your overall movement
            double targetSpeedInWater = 0.02;
            if (inWater) {
                if (velocity.length() > targetSpeedInWater) {
                    double decelerationThen = Math.pow((velocity.length() - targetSpeedInWater), 1.0);
                    // System.out.println(decelerationThen);
                    double maxDeceleration = 0.006;
                    if (decelerationThen > maxDeceleration)
                        decelerationThen = maxDeceleration;
                    // System.out.println(decelerationThen);
                    acceleration.add(new Vector3d(velocity).normalize().negate().mul(decelerationThen));
                // acceleration.add(0.0, decelerationThen * (velocity.y() > 0.0 ? 1.0 : -1.0), 0.0);
                }
            }
        }
        // Acceleration
        velocity.x = (velocity.x() + acceleration.x());
        velocity.y = (velocity.y() + acceleration.y());
        velocity.z = (velocity.z() + acceleration.z());
        // TODO ugly
        if (!world.isChunkLoaded((int) (double) positionComponent.getLocation().x() / 32, (int) (double) positionComponent.getLocation().y() / 32, (int) (double) positionComponent.getLocation().z() / 32)) {
            velocity.set(0d, 0d, 0d);
        }
        // Eventually moves
        Vector3dc remainingToMove = moveWithCollisionRestrain(velocity.x(), velocity.y(), velocity.z());
        Vector2d remaining2d = new Vector2d(remainingToMove.x(), remainingToMove.z());
        // Auto-step logic
        if (remaining2d.length() > 0.001 && isOnGround()) {
            // Cap max speed we can get through the bump ?
            if (remaining2d.length() > 0.20d) {
                System.out.println("Too fast, capping");
                remaining2d.normalize();
                remaining2d.mul(0.20);
            }
            // Get whatever we are colliding with
            // Test if setting yourself on top would be ok
            // Do it if possible
            // TODO remake proper
            Vector3d blockedMomentum = new Vector3d(remaining2d.x(), 0, remaining2d.y());
            for (double d = 0.25; d < 0.5; d += 0.05) {
                // I don't want any of this to reflect on the object, because it causes ugly jumps in the animation
                Vector3dc canMoveUp = this.canMoveWithCollisionRestrain(new Vector3d(0.0, d, 0.0));
                // It can go up that bit
                if (canMoveUp.length() == 0.0f) {
                    // Would it help with being stuck ?
                    Vector3d tryFromHigher = new Vector3d(this.getLocation());
                    tryFromHigher.add(new Vector3d(0.0, d, 0.0));
                    Vector3dc blockedMomentumRemaining = this.canMoveWithCollisionRestrain(tryFromHigher, blockedMomentum);
                    // If length of remaining momentum < of what we requested it to do, that means it *did* go a bit further away
                    if (blockedMomentumRemaining.length() < blockedMomentum.length()) {
                        // Where would this land ?
                        Vector3d afterJump = new Vector3d(tryFromHigher);
                        afterJump.add(blockedMomentum);
                        afterJump.sub(blockedMomentumRemaining);
                        // land distance = whatever is left of our -0.55 delta when it hits the ground
                        Vector3dc landDistance = this.canMoveWithCollisionRestrain(afterJump, new Vector3d(0.0, -d, 0.0));
                        afterJump.add(new Vector3d(0.0, -d, 0.0));
                        afterJump.sub(landDistance);
                        this.setLocation(new Location(world, afterJump));
                        remaining2d = new Vector2d(blockedMomentumRemaining.x(), blockedMomentumRemaining.z());
                        break;
                    }
                }
            }
        }
        // Collisions, snap to axises
        if (Math.abs(remaining2d.x()) >= 0.001d)
            velocity.x = (0d);
        if (Math.abs(remaining2d.y()) >= 0.001d)
            velocity.z = (0d);
        // Stap it
        if (isOnGround() && velocity.y() < 0)
            velocity.y = (0d);
        else if (isOnGround())
            velocity.y = (0d);
        getVelocityComponent().setVelocity(velocity);
    }
}
Also used : EntityFlying(io.xol.chunkstories.api.entity.interfaces.EntityFlying) Controller(io.xol.chunkstories.api.entity.Controller) WorldClient(io.xol.chunkstories.api.world.WorldClient) Vector3dc(org.joml.Vector3dc) Vector2d(org.joml.Vector2d) Vector3d(org.joml.Vector3d) Vector2f(org.joml.Vector2f) EntityControllable(io.xol.chunkstories.api.entity.interfaces.EntityControllable) WorldMaster(io.xol.chunkstories.api.world.WorldMaster) Location(io.xol.chunkstories.api.Location)

Example 27 with EntityControllable

use of io.xol.chunkstories.api.entity.interfaces.EntityControllable 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)

Aggregations

EntityControllable (io.xol.chunkstories.api.entity.interfaces.EntityControllable)27 Controller (io.xol.chunkstories.api.entity.Controller)10 Location (io.xol.chunkstories.api.Location)9 WorldMaster (io.xol.chunkstories.api.world.WorldMaster)9 Player (io.xol.chunkstories.api.player.Player)7 Vector3d (org.joml.Vector3d)6 LocalPlayer (io.xol.chunkstories.api.client.LocalPlayer)5 Entity (io.xol.chunkstories.api.entity.Entity)5 EntityCreative (io.xol.chunkstories.api.entity.interfaces.EntityCreative)4 EntityLiving (io.xol.chunkstories.api.entity.EntityLiving)3 World (io.xol.chunkstories.api.world.World)3 WorldClientRemote (io.xol.chunkstories.world.WorldClientRemote)3 ClientInterface (io.xol.chunkstories.api.client.ClientInterface)2 EntityWorldModifier (io.xol.chunkstories.api.entity.interfaces.EntityWorldModifier)2 PlayerInputPressedEvent (io.xol.chunkstories.api.events.player.PlayerInputPressedEvent)2 PlayerInputReleasedEvent (io.xol.chunkstories.api.events.player.PlayerInputReleasedEvent)2 PlayerVoxelModificationEvent (io.xol.chunkstories.api.events.player.voxel.PlayerVoxelModificationEvent)2 WorldException (io.xol.chunkstories.api.exceptions.world.WorldException)2 ItemPile (io.xol.chunkstories.api.item.inventory.ItemPile)2 ClientPluginManager (io.xol.chunkstories.api.plugin.ClientPluginManager)2