Search in sources :

Example 21 with EntityControllable

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

the class EntityComponentVelocity method addVelocity.

public void addVelocity(Vector3dc delta) {
    this.velocity.add(delta);
    this.pushComponentEveryoneButController();
    // Notify the controller otherwise:
    if (entity instanceof EntityControllable) {
        Controller controller = ((EntityControllable) entity).getControllerComponent().getController();
        if (controller != null) {
            PacketVelocityDelta packet = new PacketVelocityDelta(entity.getWorld(), delta);
            controller.pushPacket(packet);
        }
    }
}
Also used : PacketVelocityDelta(io.xol.chunkstories.api.net.packets.PacketVelocityDelta) Controller(io.xol.chunkstories.api.entity.Controller) EntityControllable(io.xol.chunkstories.api.entity.interfaces.EntityControllable)

Example 22 with EntityControllable

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

the class ItemVoxel method onControllerInput.

@Override
public boolean onControllerInput(Entity entity, ItemPile pile, Input input, Controller controller) {
    try {
        if (entity.getWorld() instanceof WorldMaster && input.getName().equals("mouse.right")) {
            // Require entities to be of the right kind
            if (!(entity instanceof EntityWorldModifier))
                return true;
            if (!(entity instanceof EntityControllable))
                return true;
            EntityWorldModifier modifierEntity = (EntityWorldModifier) entity;
            EntityControllable playerEntity = (EntityControllable) entity;
            boolean isEntityCreativeMode = (entity instanceof EntityCreative) && (((EntityCreative) entity).isCreativeMode());
            Location blockLocation = null;
            blockLocation = playerEntity.getBlockLookingAt(false);
            if (blockLocation != null) {
                FutureCell fvc = new FutureCell(entity.getWorld().peekSafely(blockLocation));
                fvc.setVoxel(voxel);
                // Opaque blocks overwrite the original light with zero.
                if (voxel.getDefinition().isOpaque()) {
                    fvc.setBlocklight(0);
                    fvc.setSunlight(0);
                }
                // Glowy stuff should glow
                // if(voxel.getDefinition().getEmittedLightLevel() > 0)
                fvc.setBlocklight(voxel.getEmittedLightLevel(fvc));
                // Player events mod
                if (controller instanceof Player) {
                    Player player = (Player) controller;
                    CellData ctx = entity.getWorld().peek(blockLocation);
                    PlayerVoxelModificationEvent event = new PlayerVoxelModificationEvent(ctx, fvc, isEntityCreativeMode ? EntityCreative.CREATIVE_MODE : this, player);
                    // Anyone has objections ?
                    entity.getWorld().getGameContext().getPluginManager().fireEvent(event);
                    if (event.isCancelled())
                        return true;
                    entity.getWorld().getSoundManager().playSoundEffect("sounds/gameplay/voxel_place.ogg", Mode.NORMAL, fvc.getLocation(), 1.0f, 1.0f);
                }
                entity.getWorld().poke(fvc, modifierEntity);
                // Decrease stack size
                if (!isEntityCreativeMode) {
                    int currentAmount = pile.getAmount();
                    currentAmount--;
                    pile.setAmount(currentAmount);
                }
            } else {
                // No space found :/
                return true;
            }
        }
    } catch (WorldException e) {
    }
    return false;
}
Also used : FutureCell(io.xol.chunkstories.api.world.cell.FutureCell) Player(io.xol.chunkstories.api.player.Player) PlayerVoxelModificationEvent(io.xol.chunkstories.api.events.player.voxel.PlayerVoxelModificationEvent) WorldException(io.xol.chunkstories.api.exceptions.world.WorldException) EntityWorldModifier(io.xol.chunkstories.api.entity.interfaces.EntityWorldModifier) EntityCreative(io.xol.chunkstories.api.entity.interfaces.EntityCreative) EntityControllable(io.xol.chunkstories.api.entity.interfaces.EntityControllable) CellData(io.xol.chunkstories.api.world.cell.CellData) WorldMaster(io.xol.chunkstories.api.world.WorldMaster) Location(io.xol.chunkstories.api.Location)

Example 23 with EntityControllable

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

the class ServerPlayer method updateTrackedEntities.

// Entity tracking
public void updateTrackedEntities() {
    EntityControllable controlledEntity = this.controlledEntity;
    if (controlledEntity == null)
        return;
    // Cache (idk if HotSpot makes it redudant but whatever)
    double world_size = controlledEntity.getWorld().getWorldSize();
    Location controlledEntityLocation = controlledEntity.getLocation();
    double ENTITY_VISIBILITY_SIZE = 192;
    Iterator<Entity> inRangeEntitiesIterator = controlledEntity.getWorld().getEntitiesInBox(controlledEntityLocation, new Vector3d(ENTITY_VISIBILITY_SIZE, ENTITY_VISIBILITY_SIZE, ENTITY_VISIBILITY_SIZE));
    while (inRangeEntitiesIterator.hasNext()) {
        Entity e = inRangeEntitiesIterator.next();
        // && chunk != null;
        boolean shouldTrack = e.shouldBeTrackedBy(this);
        boolean contains = subscribedEntities.contains(e);
        if (shouldTrack && !contains)
            this.subscribe(e);
        if (!shouldTrack && contains)
            this.unsubscribe(e);
    }
    Iterator<Entity> subscribedEntitiesIterator = subscribedEntities.iterator();
    while (subscribedEntitiesIterator.hasNext()) {
        Entity e = subscribedEntitiesIterator.next();
        Location loc = e.getLocation();
        // Distance calculations
        double dx = LoopingMathHelper.moduloDistance(controlledEntityLocation.x(), loc.x(), world_size);
        double dy = Math.abs(controlledEntityLocation.y() - loc.y());
        double dz = LoopingMathHelper.moduloDistance(controlledEntityLocation.z(), loc.z(), world_size);
        boolean inRange = (dx < ENTITY_VISIBILITY_SIZE && dz < ENTITY_VISIBILITY_SIZE && dy < ENTITY_VISIBILITY_SIZE);
        // Reasons other than distance to stop tracking this entity
        if (!e.shouldBeTrackedBy(this) || !inRange)
            this.unsubscribe(e);
    // No need to do anything as the component system handles the updates
    }
}
Also used : Entity(io.xol.chunkstories.api.entity.Entity) Vector3d(org.joml.Vector3d) EntityControllable(io.xol.chunkstories.api.entity.interfaces.EntityControllable) Location(io.xol.chunkstories.api.Location)

Example 24 with EntityControllable

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

the class ServerPlayer method unsubscribeAll.

@Override
public void unsubscribeAll() {
    Iterator<Entity> iterator = getSubscribedToList();
    while (iterator.hasNext()) {
        Entity entity = iterator.next();
        // If one of the entities is controllable ...
        if (entity instanceof EntityControllable) {
            EntityControllable controllableEntity = (EntityControllable) entity;
            Controller entityController = controllableEntity.getControllerComponent().getController();
            // If said entity is controlled by this subscriber/player
            if (entityController == this) {
                // Set the controller to null
                controllableEntity.getControllerComponent().setController(null);
            }
        }
        ((EntityBase) entity).unsubscribe(this);
        iterator.remove();
    }
}
Also used : EntityBase(io.xol.chunkstories.api.entity.EntityBase) Entity(io.xol.chunkstories.api.entity.Entity) Controller(io.xol.chunkstories.api.entity.Controller) EntityControllable(io.xol.chunkstories.api.entity.interfaces.EntityControllable)

Example 25 with EntityControllable

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

the class WorldImplementation method spawnPlayer.

public void spawnPlayer(Player player) {
    if (!(this instanceof WorldMaster))
        throw new UnsupportedOperationException("Only Master Worlds can do this");
    Entity savedEntity = null;
    SerializedEntityFile playerEntityFile = new SerializedEntityFile(this.getFolderPath() + "/players/" + player.getName().toLowerCase() + ".csf");
    if (playerEntityFile.exists())
        savedEntity = playerEntityFile.read(this);
    Location previousLocation = null;
    if (savedEntity != null)
        previousLocation = savedEntity.getLocation();
    PlayerSpawnEvent playerSpawnEvent = new PlayerSpawnEvent(player, (WorldMaster) this, savedEntity, previousLocation);
    getGameContext().getPluginManager().fireEvent(playerSpawnEvent);
    if (!playerSpawnEvent.isCancelled()) {
        Entity entity = playerSpawnEvent.getEntity();
        Location actualSpawnLocation = playerSpawnEvent.getSpawnLocation();
        if (actualSpawnLocation == null)
            actualSpawnLocation = this.getDefaultSpawnLocation();
        // TODO EntitySimplePlayer ?
        if (entity == null || ((entity instanceof EntityLiving) && (((EntityLiving) entity).isDead())))
            entity = this.gameContext.getContent().entities().getEntityDefinition("player").create(actualSpawnLocation);
        else
            // entity = new EntityPlayer(this, 0d, 0d, 0d, player.getName()); //Default entity
            entity.setUUID(-1);
        // Name your player !
        if (entity instanceof EntityNameable)
            ((EntityNameable) entity).getNameComponent().setName(player.getName());
        entity.setLocation(actualSpawnLocation);
        addEntity(entity);
        if (entity instanceof EntityControllable)
            player.setControlledEntity((EntityControllable) entity);
        else
            System.out.println("Error : entity is not controllable");
    }
}
Also used : Entity(io.xol.chunkstories.api.entity.Entity) EntityLiving(io.xol.chunkstories.api.entity.EntityLiving) SerializedEntityFile(io.xol.chunkstories.entity.SerializedEntityFile) PlayerSpawnEvent(io.xol.chunkstories.api.events.player.PlayerSpawnEvent) EntityNameable(io.xol.chunkstories.api.entity.interfaces.EntityNameable) EntityControllable(io.xol.chunkstories.api.entity.interfaces.EntityControllable) WorldMaster(io.xol.chunkstories.api.world.WorldMaster) 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