Search in sources :

Example 1 with Controller

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

the class EntityHumanoid method tick.

@Override
public void tick() {
    eyePosition = stance.get() == EntityHumanoidStance.CROUCHING ? 1.15 : 1.65;
    // Only  if we are allowed to
    boolean tick = false;
    if (this instanceof EntityControllable) {
        Controller controller = ((EntityControllable) this).getControllerComponent().getController();
        if (controller == null)
            tick = (getWorld() instanceof WorldMaster);
        else if (getWorld() instanceof WorldClient && ((WorldClient) getWorld()).getClient().getPlayer().equals(controller))
            tick = true;
    } else
        tick = (getWorld() instanceof WorldMaster);
    if (tick) {
        // The actual moment the jump takes effect
        // voxelIn != null && voxelIn.getType().isLiquid();
        boolean inWater = isInWater();
        if (jumpForce > 0.0 && (!justJumped || inWater)) {
            // Set the velocity
            getVelocityComponent().setVelocityY(jumpForce);
            justJumped = true;
            metersWalked = 0.0;
            jumpForce = 0.0;
        }
        // Set acceleration vector to wanted speed - actual speed
        if (isDead())
            targetVelocity = new Vector3d(0.0);
        acceleration = new Vector3d(targetVelocity.x() - getVelocityComponent().getVelocity().x(), 0, targetVelocity.z() - getVelocityComponent().getVelocity().z());
        // Limit maximal acceleration depending if we're on the groud or not, we accelerate 2x faster on ground
        double maxAcceleration = isOnGround() ? 0.010 : 0.005;
        if (inWater)
            maxAcceleration = 0.005;
        if (acceleration.length() > maxAcceleration) {
            acceleration.normalize();
            acceleration.mul(maxAcceleration);
        }
    }
    // Plays the walking sounds
    handleWalkingEtcSounds();
    // Tick : will move the entity, solve velocity/acceleration and so on
    super.tick();
}
Also used : Vector3d(org.joml.Vector3d) Controller(io.xol.chunkstories.api.entity.Controller) EntityControllable(io.xol.chunkstories.api.entity.interfaces.EntityControllable) WorldClient(io.xol.chunkstories.api.world.WorldClient) WorldMaster(io.xol.chunkstories.api.world.WorldMaster)

Example 2 with Controller

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

the class EntityPlayer method handleInteraction.

public boolean handleInteraction(Entity entity, Input input) {
    if (isDead() && input.getName().equals("mouse.right") && entity instanceof EntityControllable) {
        EntityControllable ctrla = (EntityControllable) entity;
        Controller ctrlr = ctrla.getController();
        if (ctrlr != null && ctrlr instanceof Player) {
            Player p = (Player) ctrlr;
            p.openInventory(this.getInventory());
            // p.sendMessage("HELLO THIS MY CADAVERER, PLZ FUCK OFF");
            return true;
        }
    }
    return false;
}
Also used : Player(io.xol.chunkstories.api.player.Player) LocalPlayer(io.xol.chunkstories.api.client.LocalPlayer) Controller(io.xol.chunkstories.api.entity.Controller) EntityComponentController(io.xol.chunkstories.api.entity.components.EntityComponentController) EntityControllable(io.xol.chunkstories.api.entity.interfaces.EntityControllable)

Example 3 with Controller

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

the class EntityComponentHealth method handleDeath.

private void handleDeath() {
    EntityDeathEvent entityDeathEvent = new EntityDeathEvent(entityLiving);
    entity.getWorld().getGameLogic().getPluginsManager().fireEvent(entityDeathEvent);
    // Handles cases of controlled player death
    if (entity instanceof EntityControllable) {
        Controller controller = ((EntityControllable) entity).getControllerComponent().getController();
        if (controller != null) {
            controller.setControlledEntity(null);
            // Serverside stuff
            if (controller instanceof Player && entity.getWorld() instanceof WorldMaster) {
                Player player = (Player) controller;
                PlayerDeathEvent event = new PlayerDeathEvent(player);
                entity.getWorld().getGameLogic().getPluginsManager().fireEvent(event);
                // When a player dies, delete his save as well
                File playerSavefile = new File(((WorldMaster) entity.getWorld()).getFolderPath() + "/players/" + player.getName().toLowerCase() + ".csf");
                if (playerSavefile.exists()) {
                    // Player save file is deleted upon death
                    playerSavefile.delete();
                }
                if (event.getDeathMessage() != null) {
                    GameContext gc = player.getContext();
                    if (gc instanceof ServerInterface)
                        ((ServerInterface) gc).broadcastMessage(event.getDeathMessage());
                }
            } else {
            // Weird, undefined cases ( controller wasn't a player, maybe some weird mod logic here
            }
        }
    }
}
Also used : Player(io.xol.chunkstories.api.player.Player) ServerInterface(io.xol.chunkstories.api.server.ServerInterface) EntityDeathEvent(io.xol.chunkstories.api.events.entity.EntityDeathEvent) GameContext(io.xol.chunkstories.api.GameContext) PlayerDeathEvent(io.xol.chunkstories.api.events.player.PlayerDeathEvent) Controller(io.xol.chunkstories.api.entity.Controller) EntityControllable(io.xol.chunkstories.api.entity.interfaces.EntityControllable) File(java.io.File) WorldMaster(io.xol.chunkstories.api.world.WorldMaster)

Example 4 with Controller

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

the class ItemFirearm method tickInHand.

/**
 * Should be called when the owner has this item selected
 *
 * @param owner
 */
@Override
public void tickInHand(Entity owner, ItemPile itemPile) {
    if (owner instanceof EntityControllable && ((EntityControllable) owner).getController() != null) {
        EntityControllable owner2 = ((EntityControllable) owner);
        Controller controller = owner2.getController();
        // For now only client-side players can trigger shooting actions
        if (controller instanceof LocalPlayer) {
            if (!((LocalPlayer) controller).hasFocus())
                return;
            LocalPlayer LocalPlayer = (LocalPlayer) controller;
            if (LocalPlayer.getInputsManager().getInputByName("mouse.left").isPressed()) {
                // Check for bullet presence (or creative mode)
                boolean bulletPresence = (owner instanceof EntityCreative && ((EntityCreative) owner).isCreativeMode()) || checkBullet(itemPile);
                if (!bulletPresence && !wasTriggerPressedLastTick) {
                    // Play sounds
                    if (LocalPlayer != null)
                        LocalPlayer.getSoundManager().playSoundEffect("sounds/dogez/weapon/default/dry.ogg", Mode.NORMAL, owner.getLocation(), 1.0f, 1.0f, 1f, (float) soundRange);
                // Dry.ogg
                // return;
                } else if ((automatic || !wasTriggerPressedLastTick) && (System.currentTimeMillis() - lastShot) / 1000.0d > 1.0 / (rpm / 60.0)) {
                    // Fire virtual input
                    // ClientInputPressedEvent event = new ClientInputPressedEvent(controller.getInputsManager().getInputByName("shootGun"));
                    // Client.getInstance().getPluginManager().fireEvent(event);
                    LocalPlayer.getInputsManager().onInputPressed(controller.getInputsManager().getInputByName("shootGun"));
                    lastShot = System.currentTimeMillis();
                }
            }
            isScoped = this.isScopedWeapon() && controller.getInputsManager().getInputByName("mouse.right").isPressed();
            wasTriggerPressedLastTick = controller.getInputsManager().getInputByName("mouse.left").isPressed();
        }
    }
}
Also used : LocalPlayer(io.xol.chunkstories.api.client.LocalPlayer) EntityCreative(io.xol.chunkstories.api.entity.interfaces.EntityCreative) Controller(io.xol.chunkstories.api.entity.Controller) EntityControllable(io.xol.chunkstories.api.entity.interfaces.EntityControllable)

Example 5 with Controller

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

Aggregations

Controller (io.xol.chunkstories.api.entity.Controller)11 EntityControllable (io.xol.chunkstories.api.entity.interfaces.EntityControllable)10 WorldMaster (io.xol.chunkstories.api.world.WorldMaster)6 Player (io.xol.chunkstories.api.player.Player)4 Vector3d (org.joml.Vector3d)3 Location (io.xol.chunkstories.api.Location)2 LocalPlayer (io.xol.chunkstories.api.client.LocalPlayer)2 WorldClient (io.xol.chunkstories.api.world.WorldClient)2 GameContext (io.xol.chunkstories.api.GameContext)1 ClientInterface (io.xol.chunkstories.api.client.ClientInterface)1 Entity (io.xol.chunkstories.api.entity.Entity)1 EntityBase (io.xol.chunkstories.api.entity.EntityBase)1 EntityComponentController (io.xol.chunkstories.api.entity.components.EntityComponentController)1 EntityCreative (io.xol.chunkstories.api.entity.interfaces.EntityCreative)1 EntityFlying (io.xol.chunkstories.api.entity.interfaces.EntityFlying)1 EntityWorldModifier (io.xol.chunkstories.api.entity.interfaces.EntityWorldModifier)1 EntityDeathEvent (io.xol.chunkstories.api.events.entity.EntityDeathEvent)1 PlayerDeathEvent (io.xol.chunkstories.api.events.player.PlayerDeathEvent)1 PlayerVoxelModificationEvent (io.xol.chunkstories.api.events.player.voxel.PlayerVoxelModificationEvent)1 UnauthorizedClientActionException (io.xol.chunkstories.api.exceptions.UnauthorizedClientActionException)1