Search in sources :

Example 6 with WorldMaster

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

the class ItemMiningTool method tickInHand.

@Override
public void tickInHand(Entity owner, ItemPile itemPile) {
    World world = owner.getWorld();
    if (owner instanceof EntityControllable && owner instanceof EntityWorldModifier) {
        EntityControllable entityControllable = (EntityControllable) owner;
        Controller controller = entityControllable.getController();
        if (controller != null && controller instanceof Player) {
            InputsManager inputs = controller.getInputsManager();
            Location lookingAt = entityControllable.getBlockLookingAt(true);
            if (lookingAt != null && lookingAt.distance(owner.getLocation()) > 7f)
                lookingAt = null;
            if (inputs.getInputByName("mouse.left").isPressed() && lookingAt != null) {
                WorldCell cell = world.peekSafely(lookingAt);
                // Cancel mining if looking away or the block changed by itself
                if (lookingAt == null || (progress != null && (lookingAt.distance(progress.loc) > 0 || !cell.getVoxel().sameKind(progress.voxel)))) {
                    progress = null;
                }
                if (progress == null) {
                    // Try starting mining something
                    if (lookingAt != null)
                        progress = new MiningProgress(world.peekSafely(lookingAt));
                } else {
                    // Progress using efficiency / ticks per second
                    progress.progress += ItemMiningTool.this.miningEfficiency / 60f / progress.materialHardnessForThisTool;
                    if (progress.progress >= 1.0f) {
                        if (owner.getWorld() instanceof WorldMaster) {
                            FutureCell future = new FutureCell(cell);
                            future.setVoxel(this.getDefinition().store().parent().voxels().air());
                            // Check no one minds
                            PlayerVoxelModificationEvent event = new PlayerVoxelModificationEvent(cell, future, (WorldModificationCause) entityControllable, (Player) controller);
                            owner.getWorld().getGameContext().getPluginManager().fireEvent(event);
                            // Break the block
                            if (!event.isCancelled()) {
                                Vector3d rnd = new Vector3d();
                                for (int i = 0; i < 40; i++) {
                                    rnd.set(progress.loc);
                                    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, progress.loc, 1.0f, 1.0f);
                                Location itemSpawnLocation = new Location(world, progress.loc);
                                itemSpawnLocation.add(0.5, 0.0, 0.5);
                                // ItemPile droppedItemPile = null;
                                for (ItemPile droppedItemPile : cell.getVoxel().getLoot(cell, (WorldModificationCause) entityControllable)) {
                                    EntityGroundItem thrownItem = (EntityGroundItem) getDefinition().store().parent().entities().getEntityDefinition("groundItem").create(itemSpawnLocation);
                                    thrownItem.positionComponent.setPosition(itemSpawnLocation);
                                    thrownItem.velocityComponent.setVelocity(new Vector3d(Math.random() * 0.125 - 0.0625, 0.1, Math.random() * 0.125 - 0.0625));
                                    thrownItem.setItemPile(droppedItemPile);
                                    world.addEntity(thrownItem);
                                }
                                try {
                                    world.poke(future, (WorldModificationCause) entityControllable);
                                } catch (WorldException e) {
                                // Didn't work
                                // TODO make some ingame effect so as to clue in the player why it failed
                                }
                            }
                        }
                        progress = null;
                    }
                }
            } else {
                progress = null;
            }
            Player player = (Player) controller;
            if (player.getContext() instanceof ClientInterface) {
                Player me = ((ClientInterface) player.getContext()).getPlayer();
                if (me.equals(player)) {
                    myProgress = progress;
                }
            }
        }
    }
}
Also used : Player(io.xol.chunkstories.api.player.Player) WorldCell(io.xol.chunkstories.api.world.World.WorldCell) EntityGroundItem(io.xol.chunkstories.core.entity.EntityGroundItem) WorldException(io.xol.chunkstories.api.exceptions.world.WorldException) EntityWorldModifier(io.xol.chunkstories.api.entity.interfaces.EntityWorldModifier) ClientInterface(io.xol.chunkstories.api.client.ClientInterface) World(io.xol.chunkstories.api.world.World) Controller(io.xol.chunkstories.api.entity.Controller) ItemPile(io.xol.chunkstories.api.item.inventory.ItemPile) FutureCell(io.xol.chunkstories.api.world.cell.FutureCell) PlayerVoxelModificationEvent(io.xol.chunkstories.api.events.player.voxel.PlayerVoxelModificationEvent) Vector3d(org.joml.Vector3d) EntityControllable(io.xol.chunkstories.api.entity.interfaces.EntityControllable) WorldMaster(io.xol.chunkstories.api.world.WorldMaster) InputsManager(io.xol.chunkstories.api.input.InputsManager) Location(io.xol.chunkstories.api.Location)

Example 7 with WorldMaster

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

the class VoxelChest method handleInteraction.

@Override
public boolean handleInteraction(Entity entity, ChunkCell voxelContext, Input input) {
    if (input.getName().equals("mouse.right") && voxelContext.getWorld() instanceof WorldMaster) {
        // Only actual players can open that kind of stuff
        if (entity instanceof EntityControllable) {
            EntityControllable e = (EntityControllable) entity;
            Controller c = e.getController();
            if (c instanceof Player && ((Player) c).getLocation().distance(voxelContext.getLocation()) <= 5) {
                Player p = (Player) c;
                p.openInventory(getInventory(voxelContext));
            }
        }
    }
    return false;
}
Also used : Player(io.xol.chunkstories.api.player.Player) Controller(io.xol.chunkstories.api.entity.Controller) EntityControllable(io.xol.chunkstories.api.entity.interfaces.EntityControllable) WorldMaster(io.xol.chunkstories.api.world.WorldMaster)

Example 8 with WorldMaster

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

the class VoxelDoor method handleInteraction.

// Meta
// 0x0 -> open/close
// 0x1 -> left/right hinge || left = 0 right = 1 (left is default)
// 0x2-0x4 -> side ( VoxelSide << 2 )
@Override
public boolean handleInteraction(Entity entity, ChunkCell voxelContext, Input input) {
    if (!input.getName().equals("mouse.right"))
        return false;
    if (!(entity.getWorld() instanceof WorldMaster))
        return true;
    boolean isOpen = ((voxelContext.getMetaData() >> 0) & 0x1) == 1;
    boolean hingeSide = ((voxelContext.getMetaData() >> 1) & 0x1) == 1;
    int facingPassed = (voxelContext.getMetaData() >> 2) & 0x3;
    boolean newState = !isOpen;
    int newData = computeMeta(newState, hingeSide, facingPassed);
    Location otherPartLocation = voxelContext.getLocation();
    if (top)
        otherPartLocation.add(0.0, -1.0, 0.0);
    else
        otherPartLocation.add(0.0, 1.0, 0.0);
    EditableCell otherLocationPeek = voxelContext.getWorld().peekSafely(otherPartLocation);
    if (otherLocationPeek.getVoxel() instanceof VoxelDoor) {
        System.out.println("new door status : " + newState);
        voxelContext.getWorld().getSoundManager().playSoundEffect("sounds/voxels/door.ogg", Mode.NORMAL, voxelContext.getLocation(), 1.0f, 1.0f);
        voxelContext.setMetaData(newData);
        otherLocationPeek.setMetaData(newData);
    // otherPartLocation.setVoxelDataAtLocation(VoxelFormat.changeMeta(otherPartLocation.getVoxelDataAtLocation(), newData));
    } else {
        store.parent().logger().error("Incomplete door @ " + otherPartLocation);
    }
    return true;
}
Also used : WorldMaster(io.xol.chunkstories.api.world.WorldMaster) EditableCell(io.xol.chunkstories.api.world.cell.EditableCell) Location(io.xol.chunkstories.api.Location)

Example 9 with WorldMaster

use of io.xol.chunkstories.api.world.WorldMaster in project chunkstories by Hugobros3.

the class WorldImplementation method addEntity.

@Override
public void addEntity(final Entity entity) {
    // Assign an UUID to entities lacking one
    if (this instanceof WorldMaster && entity.getUUID() == -1) {
        long nextUUID = nextEntityId();
        entity.setUUID(nextUUID);
    }
    Entity check = this.getEntityByUUID(entity.getUUID());
    if (check != null) {
        logger().error("Added an entity twice " + check + " conflits with " + entity + " UUID: " + entity.getUUID());
        // logger().save();
        Thread.dumpStack();
        // System.exit(-1);
        return;
    }
    // Add it to the world
    ((EntityBase) entity).markHasSpawned();
    assert entity.getWorld() == this;
    Chunk chunk = this.getChunkWorldCoordinates(entity.getLocation());
    if (chunk != null) {
        ((EntityBase) entity).positionComponent.trySnappingToChunk();
    }
    this.entities.insertEntity(entity);
}
Also used : EntityBase(io.xol.chunkstories.api.entity.EntityBase) Entity(io.xol.chunkstories.api.entity.Entity) Chunk(io.xol.chunkstories.api.world.chunk.Chunk) CubicChunk(io.xol.chunkstories.world.chunk.CubicChunk) WorldMaster(io.xol.chunkstories.api.world.WorldMaster)

Example 10 with WorldMaster

use of io.xol.chunkstories.api.world.WorldMaster in project chunkstories by Hugobros3.

the class Ingame method destroy.

@Override
public void destroy() {
    // Logout sequence: Save the player entity
    if (world instanceof WorldMaster) {
        Player player = getPlayer();
        PlayerLogoutEvent playerDisconnectionEvent = new PlayerLogoutEvent(player);
        world.getPluginManager().fireEvent(playerDisconnectionEvent);
        if (this.playerEntity != null) {
            SerializedEntityFile playerEntityFile = new SerializedEntityFile(world.getFolderPath() + "/players/" + getPlayer().getName().toLowerCase() + ".csf");
            playerEntityFile.write(this.playerEntity);
        }
    }
    // Stop the game logic and save
    if (world instanceof WorldMaster) {
        // TODO: Stop simulation
        Fence fence = ((WorldMaster) world).stopLogic();
        // exitButton.text = "#{world.saving}";
        fence.traverse();
        fence = world.saveEverything();
        // exitButton.text = "#{world.saving}";
        fence.traverse();
    }
    // Disables plugins
    world.getPluginManager().disablePlugins();
    this.world.getWorldRenderer().destroy();
}
Also used : Player(io.xol.chunkstories.api.player.Player) LocalPlayer(io.xol.chunkstories.api.client.LocalPlayer) PlayerLogoutEvent(io.xol.chunkstories.api.events.player.PlayerLogoutEvent) SerializedEntityFile(io.xol.chunkstories.entity.SerializedEntityFile) Fence(io.xol.chunkstories.api.util.concurrency.Fence) WorldMaster(io.xol.chunkstories.api.world.WorldMaster)

Aggregations

WorldMaster (io.xol.chunkstories.api.world.WorldMaster)27 Location (io.xol.chunkstories.api.Location)12 Entity (io.xol.chunkstories.api.entity.Entity)12 EntityControllable (io.xol.chunkstories.api.entity.interfaces.EntityControllable)9 Player (io.xol.chunkstories.api.player.Player)8 Vector3d (org.joml.Vector3d)8 Controller (io.xol.chunkstories.api.entity.Controller)6 WorldClient (io.xol.chunkstories.api.world.WorldClient)6 ItemPile (io.xol.chunkstories.api.item.inventory.ItemPile)5 Voxel (io.xol.chunkstories.api.voxel.Voxel)5 World (io.xol.chunkstories.api.world.World)5 Vector3dc (org.joml.Vector3dc)5 WorldException (io.xol.chunkstories.api.exceptions.world.WorldException)4 EntityLiving (io.xol.chunkstories.api.entity.EntityLiving)3 PlayerVoxelModificationEvent (io.xol.chunkstories.api.events.player.voxel.PlayerVoxelModificationEvent)3 ItemVoxel (io.xol.chunkstories.api.item.ItemVoxel)3 CellData (io.xol.chunkstories.api.world.cell.CellData)3 FutureCell (io.xol.chunkstories.api.world.cell.FutureCell)3 SerializedEntityFile (io.xol.chunkstories.entity.SerializedEntityFile)3 LocalPlayer (io.xol.chunkstories.api.client.LocalPlayer)2