Search in sources :

Example 41 with Entity

use of io.xol.chunkstories.api.entity.Entity 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 42 with Entity

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

the class VoxelDoor method onRemove.

@Override
public void onRemove(ChunkCell context, WorldModificationCause cause) {
    // Don't interfere with system pokes, else we get stuck in a loop
    if (cause == null || !(cause instanceof Entity))
        return;
    World world = context.getWorld();
    int x = context.getX();
    int y = context.getY();
    int z = context.getZ();
    // Ignore all that crap on a slave world
    if (!(world instanceof WorldMaster))
        return;
    int otherPartOfTheDoorY = y;
    if (top)
        otherPartOfTheDoorY--;
    else
        otherPartOfTheDoorY++;
    Voxel restOfTheDoorVoxel = world.peekSimple(x, otherPartOfTheDoorY, z);
    // Remove the other part as well, if it still exists
    if (restOfTheDoorVoxel instanceof VoxelDoor)
        world.pokeSimple(x, otherPartOfTheDoorY, z, store().air(), -1, -1, 0);
}
Also used : Entity(io.xol.chunkstories.api.entity.Entity) ItemVoxel(io.xol.chunkstories.api.item.ItemVoxel) Voxel(io.xol.chunkstories.api.voxel.Voxel) World(io.xol.chunkstories.api.world.World) WorldMaster(io.xol.chunkstories.api.world.WorldMaster)

Example 43 with Entity

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

the class VoxelDoor method onPlace.

@Override
public void onPlace(FutureCell cell, WorldModificationCause cause) throws IllegalBlockModificationException {
    // Ignore all that crap on a slave world
    if (!(cell.getWorld() instanceof WorldMaster))
        return;
    // We should only place the lower part, prevent entities from doing so !
    if (top && cause != null && cause instanceof Entity)
        throw new IllegalBlockModificationException(cell, "Entities can't place upper doors parts");
    // If the system adds the upper part, no modifications to be done on it
    if (top)
        return;
    World world = cell.getWorld();
    int x = cell.getX();
    int y = cell.getY();
    int z = cell.getZ();
    // Check top is free
    int topData = world.peekRaw(x, y + 1, z);
    if (VoxelFormat.id(topData) != 0)
        throw new IllegalBlockModificationException(cell, "Top part isn't free");
    // grab our attributes
    boolean isOpen = ((cell.getMetaData() >> 0) & 0x1) == 1;
    boolean hingeSide = ((cell.getMetaData() >> 1) & 0x1) == 1;
    int facingPassed = (cell.getMetaData() >> 2) & 0x3;
    // Default face is given by passed metadata
    VoxelSide doorSideFacing = VoxelSide.values()[facingPassed];
    // Determine side if placed by an entity and not internal code
    if (cause != null && cause instanceof Entity) {
        Location loc = ((Entity) cause).getLocation();
        double dx = loc.x() - (x + 0.5);
        double dz = loc.z() - (z + 0.5);
        if (Math.abs(dx) > Math.abs(dz)) {
            if (dx > 0)
                doorSideFacing = VoxelSide.RIGHT;
            else
                doorSideFacing = VoxelSide.LEFT;
        } else {
            if (dz > 0)
                doorSideFacing = VoxelSide.FRONT;
            else
                doorSideFacing = VoxelSide.BACK;
        }
        // If there is an adjacent one, set the hinge to right
        Voxel adjacent = null;
        switch(doorSideFacing) {
            case LEFT:
                adjacent = world.peekSimple(x, y, z - 1);
                break;
            case RIGHT:
                adjacent = world.peekSimple(x, y, z + 1);
                break;
            case FRONT:
                adjacent = world.peekSimple(x - 1, y, z);
                break;
            case BACK:
                adjacent = world.peekSimple(x + 1, y, z);
                break;
            default:
                break;
        }
        if (adjacent instanceof VoxelDoor) {
            hingeSide = true;
        }
        cell.setMetaData(computeMeta(isOpen, hingeSide, doorSideFacing));
    }
    // Place the upper part and we're good to go
    world.pokeSimple(x, y + 1, z, this.getUpperPart(), -1, -1, cell.getMetaData());
}
Also used : Entity(io.xol.chunkstories.api.entity.Entity) VoxelSide(io.xol.chunkstories.api.voxel.VoxelSide) ItemVoxel(io.xol.chunkstories.api.item.ItemVoxel) Voxel(io.xol.chunkstories.api.voxel.Voxel) IllegalBlockModificationException(io.xol.chunkstories.api.exceptions.world.voxel.IllegalBlockModificationException) World(io.xol.chunkstories.api.world.World) WorldMaster(io.xol.chunkstories.api.world.WorldMaster) Location(io.xol.chunkstories.api.Location)

Example 44 with Entity

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

the class ItemsLogicListener method onDroppedItem.

@EventHandler
public void onDroppedItem(EventItemDroppedToWorld event) {
    // Create an EntityGroundItem and add it to the event
    Location throwLocation = event.getLocation();
    Vector3d throwForce = new Vector3d(0.0);
    // Throw it when dropping it from a player's inventory ?
    System.out.println(event.getInventoryFrom());
    if (event.getInventoryFrom() != null && event.getInventoryFrom().getHolder() != null && event.getInventoryFrom().getHolder() instanceof Entity) {
        System.out.println("from som 1");
        EntityWithInventory entity = ((EntityWithInventory) event.getInventoryFrom().getHolder());
        Location pos = entity.getLocation();
        if (entity instanceof EntityLiving) {
            System.out.println("he l i v e s");
            EntityLiving owner = (EntityLiving) entity;
            throwLocation = new Location(pos.getWorld(), pos.x(), pos.y() + ((EntityPlayer) owner).eyePosition, pos.z());
            throwForce = new Vector3d(((EntityPlayer) owner).getDirectionLookingAt()).mul(0.15 - Math2.clampd(((EntityPlayer) owner).getEntityRotationComponent().getVerticalRotation(), -45, 20) / 45f * 0.0f);
            throwForce.add(((EntityPlayer) owner).getVelocityComponent().getVelocity());
        }
    }
    EntityGroundItem thrownItem = (EntityGroundItem) core.getPluginExecutionContext().getContent().entities().getEntityDefinition("groundItem").create(throwLocation);
    thrownItem.positionComponent.setPosition(throwLocation);
    thrownItem.velocityComponent.setVelocity(throwForce);
    thrownItem.setItemPile(event.getItemPile());
    // EntityGroundItem entity = new EntityGroundItem(core.getPluginExecutionContext().getContent().entities().getEntityDefinitionByName("groundItem"), event.getLocation(), event.getItemPile());
    event.setItemEntity(thrownItem);
}
Also used : Entity(io.xol.chunkstories.api.entity.Entity) EntityLiving(io.xol.chunkstories.api.entity.EntityLiving) EntityGroundItem(io.xol.chunkstories.core.entity.EntityGroundItem) Vector3d(org.joml.Vector3d) EntityWithInventory(io.xol.chunkstories.api.entity.interfaces.EntityWithInventory) EntityPlayer(io.xol.chunkstories.core.entity.EntityPlayer) Location(io.xol.chunkstories.api.Location) EventHandler(io.xol.chunkstories.api.events.EventHandler)

Aggregations

Entity (io.xol.chunkstories.api.entity.Entity)44 Location (io.xol.chunkstories.api.Location)17 WorldMaster (io.xol.chunkstories.api.world.WorldMaster)12 Vector3d (org.joml.Vector3d)11 Player (io.xol.chunkstories.api.player.Player)9 EntityLiving (io.xol.chunkstories.api.entity.EntityLiving)7 EntityControllable (io.xol.chunkstories.api.entity.interfaces.EntityControllable)5 Voxel (io.xol.chunkstories.api.voxel.Voxel)4 World (io.xol.chunkstories.api.world.World)4 EntityPlayer (io.xol.chunkstories.core.entity.EntityPlayer)4 IOException (java.io.IOException)4 EntityBase (io.xol.chunkstories.api.entity.EntityBase)3 HitBox (io.xol.chunkstories.api.entity.EntityLiving.HitBox)3 EntityComponentInventory (io.xol.chunkstories.api.entity.components.EntityComponentInventory)3 WorldException (io.xol.chunkstories.api.exceptions.world.WorldException)3 ItemVoxel (io.xol.chunkstories.api.item.ItemVoxel)3 CollisionBox (io.xol.chunkstories.api.physics.CollisionBox)3 CellData (io.xol.chunkstories.api.world.cell.CellData)3 Vector3dc (org.joml.Vector3dc)3 LocalPlayer (io.xol.chunkstories.api.client.LocalPlayer)2