Search in sources :

Example 6 with Vector3d

use of org.joml.Vector3d in project chunkstories-core by Hugobros3.

the class ItemMeleeWeapon method onControllerInput.

@Override
public boolean onControllerInput(Entity owner, ItemPile pile, Input input, Controller controller) {
    if (input.getName().startsWith("mouse.left")) {
        // Checks current swing is done
        if (System.currentTimeMillis() - currentSwingStart > swingDuration) {
            currentSwingStart = System.currentTimeMillis();
            hasHitYet = false;
        }
        return true;
    } else if (input.getName().equals("shootGun") && owner.getWorld() instanceof WorldMaster) {
        // Actually hits
        EntityLiving shooter = (EntityLiving) owner;
        Vector3dc direction = shooter.getDirectionLookingAt();
        Vector3d eyeLocation = new Vector3d(shooter.getLocation());
        if (shooter instanceof EntityPlayer)
            eyeLocation.add(new Vector3d(0.0, ((EntityPlayer) shooter).eyePosition, 0.0));
        // Find wall collision
        Location shotBlock = owner.getWorld().collisionsManager().raytraceSolid(eyeLocation, direction, range);
        Vector3d nearestLocation = new Vector3d();
        // Loops to try and break blocks
        while (owner.getWorld() instanceof WorldMaster && shotBlock != null) {
            EditableCell peek = owner.getWorld().peekSafely(shotBlock);
            if (!peek.getVoxel().isAir() && peek.getVoxel().getMaterial().resolveProperty("bulletBreakable") != null && peek.getVoxel().getMaterial().resolveProperty("bulletBreakable").equals("true")) {
                // TODO: Spawn an event to check if it's okay
                // Destroy it
                peek.setVoxel(getDefinition().store().parent().voxels().air());
                for (int i = 0; i < 25; i++) {
                    Vector3d smashedVoxelParticleDirection = new Vector3d(direction);
                    smashedVoxelParticleDirection.mul(2.0);
                    smashedVoxelParticleDirection.add(Math.random() - 0.5, Math.random() - 0.5, Math.random() - 0.5);
                    smashedVoxelParticleDirection.normalize();
                    owner.getWorld().getParticlesManager().spawnParticleAtPositionWithVelocity("voxel_frag", shotBlock, smashedVoxelParticleDirection);
                }
                owner.getWorld().getSoundManager().playSoundEffect("sounds/environment/glass.ogg", Mode.NORMAL, shotBlock, (float) Math.random() * 0.2f + 0.9f, 1.0f);
                // Re-raytrace the ray
                shotBlock = owner.getWorld().collisionsManager().raytraceSolid(eyeLocation, direction, range);
            } else
                break;
        }
        if (shotBlock != null) {
            Location shotBlockOuter = owner.getWorld().collisionsManager().raytraceSolidOuter(eyeLocation, direction, range);
            if (shotBlockOuter != null) {
                Vector3d normal = shotBlockOuter.sub(shotBlock);
                double NbyI2x = 2.0 * direction.dot(normal);
                Vector3d NxNbyI2x = new Vector3d(normal);
                NxNbyI2x.mul(NbyI2x);
                Vector3d reflected = new Vector3d(direction);
                reflected.sub(NxNbyI2x);
                CellData peek = owner.getWorld().peekSafely(shotBlock);
                // This seems fine
                for (CollisionBox box : peek.getTranslatedCollisionBoxes()) {
                    Vector3dc thisLocation = box.lineIntersection(eyeLocation, direction);
                    if (thisLocation != null) {
                        if (nearestLocation == null || nearestLocation.distance(eyeLocation) > thisLocation.distance(eyeLocation))
                            nearestLocation.set(thisLocation);
                    }
                }
                // Position adjustements so shot blocks always shoot proper particles
                if (shotBlock.x() - nearestLocation.x() <= -1.0)
                    nearestLocation.add(-0.01, 0.0, 0.0);
                if (shotBlock.y() - nearestLocation.y() <= -1.0)
                    nearestLocation.add(0.0, -0.01, 0.0);
                if (shotBlock.z() - nearestLocation.z() <= -1.0)
                    nearestLocation.add(0.0, 0.0, -0.01);
                for (int i = 0; i < 25; i++) {
                    Vector3d untouchedReflection = new Vector3d(reflected);
                    Vector3d random = new Vector3d(Math.random() * 2.0 - 1.0, Math.random() * 2.0 - 1.0, Math.random() * 2.0 - 1.0);
                    random.mul(0.5);
                    untouchedReflection.add(random);
                    untouchedReflection.normalize();
                    untouchedReflection.mul(0.25);
                    Vector3d ppos = new Vector3d(nearestLocation);
                    owner.getWorld().getParticlesManager().spawnParticleAtPositionWithVelocity("voxel_frag", ppos, untouchedReflection);
                    owner.getWorld().getSoundManager().playSoundEffect(owner.getWorld().peekSafely(shotBlock).getVoxel().getMaterial().resolveProperty("landingSounds"), Mode.NORMAL, ppos, 1, 0.25f);
                }
                owner.getWorld().getDecalsManager().drawDecal(nearestLocation, normal.negate(), new Vector3d(0.5), "bullethole");
            }
        }
        // Hitreg takes place on server bois
        if (shooter.getWorld() instanceof WorldMaster) {
            // Iterate over each found entities
            Iterator<Entity> shotEntities = owner.getWorld().collisionsManager().rayTraceEntities(eyeLocation, direction, range);
            while (shotEntities.hasNext()) {
                Entity shotEntity = shotEntities.next();
                // Don't shoot itself & only living things get shot
                if (!shotEntity.equals(shooter) && shotEntity instanceof EntityLiving) {
                    // Get hit location
                    for (HitBox hitBox : ((EntityLiving) shotEntity).getHitBoxes()) {
                        Vector3dc hitPoint = hitBox.lineIntersection(eyeLocation, direction);
                        if (hitPoint == null)
                            continue;
                        // Deal damage
                        ((EntityLiving) shotEntity).damage(pileAsDamageCause(pile), hitBox, (float) damage);
                        // Spawn blood particles
                        Vector3d bloodDir = new Vector3d();
                        direction.normalize(bloodDir).mul(0.25);
                        for (int i = 0; i < 250; i++) {
                            Vector3d random = new Vector3d(Math.random() * 2.0 - 1.0, Math.random() * 2.0 - 1.0, Math.random() * 2.0 - 1.0);
                            random.mul(0.25);
                            random.add(bloodDir);
                            shooter.getWorld().getParticlesManager().spawnParticleAtPositionWithVelocity("blood", hitPoint, random);
                        }
                        // Spawn blood on walls
                        if (nearestLocation != null)
                            shooter.getWorld().getDecalsManager().drawDecal(nearestLocation, bloodDir, new Vector3d(3.0), "blood");
                    }
                }
            }
        }
    }
    return false;
}
Also used : Vector3dc(org.joml.Vector3dc) Entity(io.xol.chunkstories.api.entity.Entity) HitBox(io.xol.chunkstories.api.entity.EntityLiving.HitBox) EntityLiving(io.xol.chunkstories.api.entity.EntityLiving) Vector3d(org.joml.Vector3d) Iterator(java.util.Iterator) EntityPlayer(io.xol.chunkstories.core.entity.EntityPlayer) CellData(io.xol.chunkstories.api.world.cell.CellData) WorldMaster(io.xol.chunkstories.api.world.WorldMaster) EditableCell(io.xol.chunkstories.api.world.cell.EditableCell) Location(io.xol.chunkstories.api.Location) CollisionBox(io.xol.chunkstories.api.physics.CollisionBox)

Example 7 with Vector3d

use of org.joml.Vector3d 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 8 with Vector3d

use of org.joml.Vector3d in project chunkstories-core by Hugobros3.

the class PacketExplosionEffect method process.

@Override
public void process(PacketSender sender, DataInputStream in, PacketReceptionContext processor) throws IOException, PacketProcessingException {
    center = new Vector3d(in.readDouble(), in.readDouble(), in.readDouble());
    radius = in.readDouble();
    debrisSpeed = in.readDouble();
    f = in.readFloat();
    if (processor instanceof ClientPacketsProcessor) {
        ClientPacketsProcessor cpp = (ClientPacketsProcessor) processor;
        WorldEffects.createFireballFx(cpp.getWorld(), center, radius, debrisSpeed, f);
    }
}
Also used : ClientPacketsProcessor(io.xol.chunkstories.api.client.net.ClientPacketsProcessor) Vector3d(org.joml.Vector3d)

Example 9 with Vector3d

use of org.joml.Vector3d in project chunkstories-core by Hugobros3.

the class WorldEffects method createFireballFx.

public static void createFireballFx(World world, Vector3d center, double radius, double debrisSpeed, float f) {
    for (int z = 0; z < 250 * f; z++) {
        Vector3d lol = new Vector3d(Math.random() * 2.0 - 1.0, Math.random() * 2.0 - 1.0, Math.random() * 2.0 - 1.0);
        lol.normalize();
        Vector3d spd = new Vector3d(lol);
        spd.mul(debrisSpeed * (0.5 + Math.random()));
        lol.mul(radius);
        lol.add(center);
        world.getParticlesManager().spawnParticleAtPositionWithVelocity("fire", lol, spd);
    }
    world.getParticlesManager().spawnParticleAtPositionWithVelocity("fire_light", center, new Vector3d(1, 0, 0).normalize().mul(debrisSpeed * 1.5f));
}
Also used : Vector3d(org.joml.Vector3d)

Example 10 with Vector3d

use of org.joml.Vector3d in project chunkstories-core by Hugobros3.

the class VoxelSign method onPlace.

@Override
public void onPlace(FutureCell cell, WorldModificationCause cause) throws IllegalBlockModificationException {
    // We don't create the components here, as the cell isn't actually changed yet!
    int x = cell.getX();
    int y = cell.getY();
    int z = cell.getZ();
    if (cause != null && cause instanceof Entity) {
        Vector3d blockLocation = new Vector3d(x + 0.5, y, z + 0.5);
        blockLocation.sub(((Entity) cause).getLocation());
        blockLocation.negate();
        Vector2f direction = new Vector2f((float) (double) blockLocation.x(), (float) (double) blockLocation.z());
        direction.normalize();
        // System.out.println("x:"+direction.x+"y:"+direction.y);
        double asAngle = Math.acos(direction.y()) / Math.PI * 180;
        asAngle *= -1;
        if (direction.x() < 0)
            asAngle *= -1;
        // asAngle += 180.0;
        asAngle %= 360.0;
        asAngle += 360.0;
        asAngle %= 360.0;
        // System.out.println(asAngle);
        int meta = (int) (16 * asAngle / 360);
        cell.setMetaData(meta);
    }
}
Also used : Entity(io.xol.chunkstories.api.entity.Entity) Vector3d(org.joml.Vector3d) Vector2f(org.joml.Vector2f)

Aggregations

Vector3d (org.joml.Vector3d)117 Vector3dc (org.joml.Vector3dc)33 PhysicsObject (org.valkyrienskies.mod.common.ships.ship_world.PhysicsObject)20 BlockPos (net.minecraft.util.math.BlockPos)19 ShipTransform (org.valkyrienskies.mod.common.ships.ship_transform.ShipTransform)18 Location (io.xol.chunkstories.api.Location)12 Entity (io.xol.chunkstories.api.entity.Entity)11 AxisAlignedBB (net.minecraft.util.math.AxisAlignedBB)9 World (net.minecraft.world.World)9 ShipData (org.valkyrienskies.mod.common.ships.ShipData)9 WorldClient (io.xol.chunkstories.api.world.WorldClient)8 WorldMaster (io.xol.chunkstories.api.world.WorldMaster)8 EntityPlayer (net.minecraft.entity.player.EntityPlayer)8 EntityShipMovementData (org.valkyrienskies.mod.common.entity.EntityShipMovementData)7 EntityControllable (io.xol.chunkstories.api.entity.interfaces.EntityControllable)6 IBlockState (net.minecraft.block.state.IBlockState)6 Vec3d (net.minecraft.util.math.Vec3d)6 Vector3f (org.joml.Vector3f)6 EntityLiving (io.xol.chunkstories.api.entity.EntityLiving)5 CellData (io.xol.chunkstories.api.world.cell.CellData)5