Search in sources :

Example 71 with Vector3d

use of org.joml.Vector3d in project chunkstories-core by Hugobros3.

the class EntityLivingImplementation method damage.

@Override
public float damage(DamageCause cause, HitBox osef, float damage) {
    if (damageCooldown > System.currentTimeMillis())
        return 0f;
    EntityDamageEvent event = new EntityDamageEvent(this, cause, damage);
    this.getWorld().getGameLogic().getPluginsManager().fireEvent(event);
    if (!event.isCancelled()) {
        entityHealthComponent.damage(event.getDamageDealt());
        lastDamageCause = cause;
        damageCooldown = System.currentTimeMillis() + cause.getCooldownInMs();
        float damageDealt = event.getDamageDealt();
        // Applies knockback
        if (cause instanceof Entity) {
            Entity attacker = (Entity) cause;
            Vector3d attackKnockback = this.getLocation().sub(attacker.getLocation().add(0d, 0d, 0d));
            attackKnockback.y = (0d);
            attackKnockback.normalize();
            float knockback = (float) Math.max(1f, Math.pow(damageDealt, 0.5f));
            attackKnockback.mul(knockback / 50d);
            attackKnockback.y = (knockback / 50d);
            /*
				attackKnockback.scale(damageDealt / 500d);
				attackKnockback.scale(1.0 / (1.0 + 5 * this.getVelocityComponent().getVelocity().length()));*/
            // .scale(1/60d).scale(damageDealt / 10f);
            this.getVelocityComponent().addVelocity(attackKnockback);
        }
        return damageDealt;
    }
    return 0f;
}
Also used : Entity(io.xol.chunkstories.api.entity.Entity) Vector3d(org.joml.Vector3d) EntityDamageEvent(io.xol.chunkstories.api.events.entity.EntityDamageEvent)

Example 72 with Vector3d

use of org.joml.Vector3d 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 73 with Vector3d

use of org.joml.Vector3d in project chunkstories-core by Hugobros3.

the class EntityPlayer method getBlockLookingAt.

@Override
public Location getBlockLookingAt(boolean inside) {
    Vector3d initialPosition = new Vector3d(getLocation());
    initialPosition.add(new Vector3d(0, eyePosition, 0));
    Vector3d direction = new Vector3d(getDirectionLookingAt());
    if (inside)
        return world.collisionsManager().raytraceSelectable(new Location(world, initialPosition), direction, 256.0);
    else
        return world.collisionsManager().raytraceSolidOuter(new Location(world, initialPosition), direction, 256.0);
}
Also used : Vector3d(org.joml.Vector3d) Location(io.xol.chunkstories.api.Location)

Example 74 with Vector3d

use of org.joml.Vector3d 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 75 with Vector3d

use of org.joml.Vector3d 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)

Aggregations

Vector3d (org.joml.Vector3d)117 Vector3dc (org.joml.Vector3dc)33 PhysicsObject (org.valkyrienskies.mod.common.ships.ship_world.PhysicsObject)20 BlockPos (net.minecraft.util.math.BlockPos)19 ShipTransform (org.valkyrienskies.mod.common.ships.ship_transform.ShipTransform)18 Location (io.xol.chunkstories.api.Location)12 Entity (io.xol.chunkstories.api.entity.Entity)11 AxisAlignedBB (net.minecraft.util.math.AxisAlignedBB)9 World (net.minecraft.world.World)9 ShipData (org.valkyrienskies.mod.common.ships.ShipData)9 WorldClient (io.xol.chunkstories.api.world.WorldClient)8 WorldMaster (io.xol.chunkstories.api.world.WorldMaster)8 EntityPlayer (net.minecraft.entity.player.EntityPlayer)8 EntityShipMovementData (org.valkyrienskies.mod.common.entity.EntityShipMovementData)7 EntityControllable (io.xol.chunkstories.api.entity.interfaces.EntityControllable)6 IBlockState (net.minecraft.block.state.IBlockState)6 Vec3d (net.minecraft.util.math.Vec3d)6 Vector3f (org.joml.Vector3f)6 EntityLiving (io.xol.chunkstories.api.entity.EntityLiving)5 CellData (io.xol.chunkstories.api.world.cell.CellData)5