Search in sources :

Example 6 with EntityControllable

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

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

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

the class PacketVelocityDelta method process.

@Override
public void process(PacketSender sender, DataInputStream in, PacketReceptionContext processor) throws IOException, PacketProcessingException {
    Vector3d delta = new Vector3d(in.readDouble(), in.readDouble(), in.readDouble());
    EntityControllable entity = ((ClientPacketsProcessor) processor).getContext().getPlayer().getControlledEntity();
    if (entity != null && entity instanceof EntityWithVelocity) {
        System.out.println("Debug: received velocity delta " + delta);
        ((EntityWithVelocity) entity).getVelocityComponent().addVelocity(delta);
    }
}
Also used : ClientPacketsProcessor(io.xol.chunkstories.api.client.net.ClientPacketsProcessor) EntityWithVelocity(io.xol.chunkstories.api.entity.interfaces.EntityWithVelocity) Vector3d(org.joml.Vector3d) EntityControllable(io.xol.chunkstories.api.entity.interfaces.EntityControllable)

Example 9 with EntityControllable

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

the class ChunkHolderImplementation method unloadChunk.

private void unloadChunk() {
    chunkLock.writeLock().lock();
    CubicChunk chunk = this.chunk;
    if (chunk == null) {
        chunkLock.writeLock().unlock();
        return;
    }
    // Unlist it immediately
    regionLoadedChunks.remove(chunk);
    this.chunk = null;
    // Remove the entities from this chunk from the world
    region.world.entitiesLock.writeLock().lock();
    Iterator<Entity> i = chunk.localEntities.iterator();
    while (i.hasNext()) {
        Entity entity = i.next();
        if (entity instanceof EntityControllable && ((EntityControllable) entity).getController() != null) {
            // give grace to controlled entities
            continue;
        } else {
            region.world.removeEntityFromList(entity);
        }
    }
    region.world.entitiesLock.writeLock().unlock();
    // Lock it down
    chunk.entitiesLock.lock();
    // Kill any load chunk operation that is still scheduled
    if (loadChunkTask != null) {
        IOTask task = loadChunkTask;
        if (task != null)
            task.cancel();
        loadChunkTask = null;
    }
    // Compress chunk one last time before it has to go
    setCompressedData(compressChunkData(chunk));
    // destroy it (returns any internal data using up ressources)
    chunk.destroy();
    // unlock it (whoever messes with it now, his problem)
    chunk.entitiesLock.unlock();
    chunkLock.writeLock().unlock();
}
Also used : Entity(io.xol.chunkstories.api.entity.Entity) IOTask(io.xol.chunkstories.world.io.IOTasks.IOTask) EntityControllable(io.xol.chunkstories.api.entity.interfaces.EntityControllable)

Example 10 with EntityControllable

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

the class PacketInput method process.

public void process(PacketSender sender, DataInputStream in, PacketReceptionContext processor) throws IOException {
    long code = in.readLong();
    boolean pressed = in.readBoolean();
    if (processor instanceof ServerPlayerPacketsProcessor) {
        ServerPlayerPacketsProcessor sppc = (ServerPlayerPacketsProcessor) processor;
        // Look for the controller handling this buisness
        EntityControllable entity = (EntityControllable) sppc.getPlayer().getControlledEntity();
        if (entity != null) {
            // Get input of the client
            input = sppc.getPlayer().getInputsManager().getInputFromHash(code);
            if (input == null)
                throw new NullPointerException("Unknown input hash : " + code);
            // System.out.println(processor.getServerClient().getProfile() +
            // " "+input + " "+pressed);
            // Update it's state
            ((InputVirtual) input).setPressed(pressed);
            // Fire appropriate event
            if (pressed) {
                PlayerInputPressedEvent event = new PlayerInputPressedEvent(sppc.getPlayer(), input);
                entity.getWorld().getGameLogic().getPluginsManager().fireEvent(event);
                if (!event.isCancelled())
                    entity.onControllerInput(input, entity.getControllerComponent().getController());
            } else {
                PlayerInputReleasedEvent event = new PlayerInputReleasedEvent(sppc.getPlayer(), input);
                entity.getWorld().getGameLogic().getPluginsManager().fireEvent(event);
            }
        // TODO why is this disabled and still there ?
        // If we pressed the input, apply game logic
        // if(pressed)
        // entity.handleInteraction(input,
        // entity.getControllerComponent().getController());
        }
    }
}
Also used : PlayerInputReleasedEvent(io.xol.chunkstories.api.events.player.PlayerInputReleasedEvent) ServerPlayerPacketsProcessor(io.xol.chunkstories.api.server.ServerPacketsProcessor.ServerPlayerPacketsProcessor) EntityControllable(io.xol.chunkstories.api.entity.interfaces.EntityControllable) PlayerInputPressedEvent(io.xol.chunkstories.api.events.player.PlayerInputPressedEvent) InputVirtual(io.xol.chunkstories.input.InputVirtual)

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