Search in sources :

Example 1 with EntityMountable

use of org.valkyrienskies.mod.common.entity.EntityMountable in project Valkyrien-Warfare-Revamped by ValkyrienWarfare.

the class EventsCommon method onEntityJoinWorldEvent.

@SubscribeEvent(priority = EventPriority.HIGHEST)
public static void onEntityJoinWorldEvent(EntityJoinWorldEvent event) {
    Entity entity = event.getEntity();
    World world = entity.world;
    BlockPos posAt = new BlockPos(entity);
    Optional<PhysicsObject> physicsObject = ValkyrienUtils.getPhysoManagingBlock(world, posAt);
    if (!event.getWorld().isRemote && physicsObject.isPresent() && !(entity instanceof EntityFallingBlock)) {
        if (entity instanceof EntityArmorStand || entity instanceof EntityPig || entity instanceof EntityBoat) {
            EntityMountable entityMountable = new EntityMountable(world, entity.getPositionVector(), CoordinateSpaceType.SUBSPACE_COORDINATES, posAt);
            world.spawnEntity(entityMountable);
            entity.startRiding(entityMountable);
        }
        physicsObject.get().getShipTransformationManager().getCurrentTickTransform().transform(entity, TransformType.SUBSPACE_TO_GLOBAL, false);
    // TODO: This should work but it doesn't because of sponge. Instead we have to rely on MixinChunk.preAddEntity() to fix this
    // event.setCanceled(true);
    // event.getWorld().spawnEntity(entity);
    }
}
Also used : Entity(net.minecraft.entity.Entity) EntityFallingBlock(net.minecraft.entity.item.EntityFallingBlock) EntityPig(net.minecraft.entity.passive.EntityPig) EntityBoat(net.minecraft.entity.item.EntityBoat) EntityMountable(org.valkyrienskies.mod.common.entity.EntityMountable) BlockPos(net.minecraft.util.math.BlockPos) EntityArmorStand(net.minecraft.entity.item.EntityArmorStand) World(net.minecraft.world.World) SubscribeEvent(net.minecraftforge.fml.common.eventhandler.SubscribeEvent)

Example 2 with EntityMountable

use of org.valkyrienskies.mod.common.entity.EntityMountable in project Valkyrien-Warfare-Revamped by ValkyrienWarfare.

the class RegisterEvents method onRegisterEntitiesEvent.

/**
 * This method will be called by Forge when it is time for the mod to register its entity
 * entries.
 */
@SubscribeEvent
public static void onRegisterEntitiesEvent(@Nonnull final RegistryEvent.Register<EntityEntry> event) {
    final ResourceLocation physicsWrapperEntity = new ResourceLocation(ValkyrienSkiesMod.MOD_ID, "PhysWrapper");
    final ResourceLocation entityMountable = new ResourceLocation(ValkyrienSkiesMod.MOD_ID, "entity_mountable");
    final ResourceLocation entityMountableChair = new ResourceLocation(ValkyrienSkiesMod.MOD_ID, "entity_mountable_chair");
    // Used to ensure no duplicates of entity network id's
    int entityId = 0;
    event.getRegistry().registerAll(EntityEntryBuilder.create().entity(EntityMountable.class).id(entityMountable, entityId++).name(entityMountable.getPath()).tracker(ValkyrienSkiesMod.VS_ENTITY_LOAD_DISTANCE, 1, false).build(), EntityEntryBuilder.create().entity(EntityMountableChair.class).id(entityMountableChair, entityId++).name(entityMountableChair.getPath()).tracker(ValkyrienSkiesMod.VS_ENTITY_LOAD_DISTANCE, 1, false).build());
}
Also used : EntityMountableChair(org.valkyrienskies.mod.common.entity.EntityMountableChair) ResourceLocation(net.minecraft.util.ResourceLocation) EntityMountable(org.valkyrienskies.mod.common.entity.EntityMountable) SubscribeEvent(net.minecraftforge.fml.common.eventhandler.SubscribeEvent)

Example 3 with EntityMountable

use of org.valkyrienskies.mod.common.entity.EntityMountable in project Valkyrien-Warfare-Revamped by ValkyrienWarfare.

the class EntityCollisionInjector method alterEntityMovement.

// Returns false if game should use default collision
@Nullable
public static IntermediateMovementVariableStorage alterEntityMovement(Entity entity, MoverType type, double dx, double dy, double dz) {
    final double origDx = dx;
    final double origDy = dy;
    final double origDz = dz;
    final double origPosX = entity.posX;
    final double origPosY = entity.posY;
    final double origPosZ = entity.posZ;
    boolean isLiving = entity instanceof EntityLivingBase;
    Vec3d velocity = new Vec3d(dx, dy, dz);
    Polygon playerBeforeMove = new Polygon(entity.getEntityBoundingBox());
    List<Polygon> colPolys = getCollidingPolygonsAndDoBlockCols(entity, velocity);
    PhysicsObject worldBelow = null;
    IDraggable draggable = EntityDraggable.getDraggableFromEntity(entity);
    final EntityShipMovementData lastTickEntityShipMovementData = draggable.getEntityShipMovementData();
    Vector3d total = new Vector3d();
    // Used to reset the player position after collision processing, effectively
    // using the player to integrate their velocity
    double posOffestX = 0;
    double posOffestY = 0;
    double posOffestZ = 0;
    // True IFF the player is on a ladder
    boolean isPlayerOnLadder = false;
    // region Ladder movement
    if (entity instanceof EntityLivingBase) {
        final EntityLivingBase base = (EntityLivingBase) entity;
        final List<PhysicsObject> collidingShips = ((IHasShipManager) entity.getEntityWorld()).getManager().getPhysObjectsInAABB(base.getEntityBoundingBox());
        final Iterable<Triple<PhysicsObject, BlockPos, IBlockState>> ladderCollisions = getLadderCollisions(base, collidingShips);
        // For now, just ignore the y component. I may or may not use it later.
        final float forward = ((EntityLivingBase) entity).moveForward;
        final float strafe = ((EntityLivingBase) entity).moveStrafing;
        final double f1 = Math.sin(Math.toRadians(entity.rotationYaw));
        final double f2 = Math.cos(Math.toRadians(entity.rotationYaw));
        final double intendedXVel = strafe * f2 - forward * f1;
        final double intendedYVel = 0;
        final double intendedZVel = forward * f2 + strafe * f1;
        final Vector3dc originalVelocityDirection = new Vector3d(intendedXVel, intendedYVel, intendedZVel).normalize();
        final World world = entity.world;
        final Polygon playerPolygon = new Polygon(base.getEntityBoundingBox());
        for (final Triple<PhysicsObject, BlockPos, IBlockState> ladderCollision : ladderCollisions) {
            final IBlockState ladderState = ladderCollision.getRight();
            EnumFacing ladderFacing = null;
            // For now, we only support a few blocks
            if (ladderState.getPropertyKeys().contains(BlockHorizontal.FACING)) {
                ladderFacing = ladderState.getValue(BlockHorizontal.FACING);
            }
            // We need the EnumFacing of the ladder for the code to work. If we couldn't find it then just give up :/
            if (ladderFacing != null) {
                final Vector3d ladderNormal = JOML.convertDouble(ladderFacing.getDirectionVec());
                final ShipTransform shipTransform = ladderCollision.getLeft().getShipTransform();
                // Grow the ladder BB by a small margin (makes the ladder experience better imo)
                final AxisAlignedBB ladderBB = ladderCollision.getRight().getBoundingBox(world, ladderCollision.getMiddle()).offset(ladderCollision.getMiddle()).grow(.4);
                final Polygon ladderPoly = new Polygon(ladderBB, shipTransform.getSubspaceToGlobal());
                // Determine if the player is actually colliding with the ladder
                final PhysPolygonCollider collider = new PhysPolygonCollider(playerPolygon, ladderPoly, ladderCollision.getLeft().getShipTransformationManager().normals);
                collider.processData();
                shipTransform.transformDirection(ladderNormal, TransformType.SUBSPACE_TO_GLOBAL);
                // Don't use "floor ladders"
                final boolean isLadderFacingDown = ladderNormal.y > .8;
                if (isLadderFacingDown) {
                    continue;
                }
                // If the ladder is facing up, then let the player use them like monkey bars
                final boolean isLadderFacingUp = ladderNormal.y < -.8;
                // Whether or not the player is actually colliding with a ladder, since it is close to one we give the player ladder movement.
                dx = MathHelper.clamp(dx, -.15, .15);
                dz = MathHelper.clamp(dz, -.15, .15);
                base.fallDistance = 0;
                if (!isLadderFacingUp) {
                    // Use ladders like normal
                    if (dy < -.15) {
                        dy = -.15;
                    }
                    final boolean isPlayerGoingTowardsLadder = originalVelocityDirection.dot(ladderNormal) < -.1;
                    final boolean isPlayerSneakingOnLadder = base.isSneaking() && base instanceof EntityPlayer;
                    if (isPlayerSneakingOnLadder && dy < 0) {
                        dy = 0;
                    }
                    if (!collider.seperated && isPlayerGoingTowardsLadder) {
                        dy = .2;
                    }
                } else {
                    // Use ladders like monkey bars
                    dy = .2;
                }
                worldBelow = ladderCollision.getLeft();
                isPlayerOnLadder = true;
                break;
            }
        }
    }
    // endregion
    final Vector3dc velVec = new Vector3d(dx, dy, dz);
    for (Polygon poly : colPolys) {
        if (poly instanceof ShipPolygon) {
            ShipPolygon shipPoly = (ShipPolygon) poly;
            try {
                EntityPolygonCollider fast = new EntityPolygonCollider(playerBeforeMove, shipPoly, shipPoly.normals, velVec.add(total, new Vector3d()));
                if (!fast.arePolygonsSeparated()) {
                    // fastCollisions.add(fast);
                    worldBelow = shipPoly.shipFrom;
                    Vector3d response = fast.getCollisions()[fast.getMinDistanceIndex()].getResponse();
                    // TODO: Add more potential yResponses
                    double stepSquared = entity.stepHeight * entity.stepHeight;
                    // Do not do stair stepping if the player is on a ladder.
                    boolean isStep = isLiving && entity.onGround && !isPlayerOnLadder;
                    if (response.y >= 0 && VSMath.canStandOnNormal(fast.getCollisionAxes()[fast.getMinDistanceIndex()])) {
                        Vector3d slowButStopped = new Vector3d(0, -fast.getCollisions()[fast.getMinDistanceIndex()].getCollisionPenetrationDistance() / fast.getCollisionAxes()[fast.getMinDistanceIndex()].y(), 0);
                        response = slowButStopped;
                    }
                    if (isStep) {
                        EntityLivingBase living = (EntityLivingBase) entity;
                        if (Math.abs(living.moveForward) > .01 || Math.abs(living.moveStrafing) > .01) {
                            for (int i = 3; i < 6; i++) {
                                Vector3d tempResponse = fast.getCollisions()[i].getResponse();
                                if (tempResponse.y > 0 && VSMath.canStandOnNormal(fast.getCollisions()[i].getCollisionNormal()) && tempResponse.lengthSquared() < stepSquared) {
                                    if (tempResponse.lengthSquared() < .1) {
                                        // Too small to be a real step, let it through
                                        response = tempResponse;
                                    } else {
                                        // System.out.println("Try Stepping!");
                                        AxisAlignedBB axisalignedbb = entity.getEntityBoundingBox().offset(tempResponse.x, tempResponse.y, tempResponse.z);
                                        // Don't allow the player to step if the step will put them in another polygon.
                                        boolean collidesWithAnything = false;
                                        {
                                            final AxisAlignedBB newEntityBBShrunk = axisalignedbb.shrink(.15);
                                            final Polygon newEntityBBShrunkPolygon = new Polygon(newEntityBBShrunk);
                                            for (Polygon potentialStepCollision : colPolys) {
                                                if (potentialStepCollision == poly) {
                                                    // Don't run this on ourself
                                                    continue;
                                                }
                                                if (potentialStepCollision.getEnclosedAABB().intersects(newEntityBBShrunk)) {
                                                    // Finer check
                                                    ShipPolygon potentialStepCollisionShipPoly = (ShipPolygon) potentialStepCollision;
                                                    final EntityPolygonCollider checkIfStepCollidesWithBlock = new EntityPolygonCollider(newEntityBBShrunkPolygon, potentialStepCollisionShipPoly, potentialStepCollisionShipPoly.normals, new Vector3d());
                                                    checkIfStepCollidesWithBlock.processData();
                                                    if (!checkIfStepCollidesWithBlock.arePolygonsSeparated()) {
                                                        collidesWithAnything = true;
                                                        break;
                                                    }
                                                }
                                            }
                                        }
                                        if (!collidesWithAnything) {
                                            entity.setEntityBoundingBox(axisalignedbb);
                                            // I think this correct, but it may create more problems than it solves
                                            response.zero();
                                            entity.resetPositionToBB();
                                        }
                                    }
                                }
                            }
                        }
                    }
                    // total.add(response);
                    if (Math.abs(response.x) > .01D) {
                        total.x += response.x;
                    }
                    if (Math.abs(response.y) > .01D) {
                        total.y += response.y;
                    }
                    if (Math.abs(response.z) > .01D) {
                        total.z += response.z;
                    }
                    entity.posX += response.x;
                    entity.posY += response.y;
                    entity.posZ += response.z;
                    posOffestX += response.x;
                    posOffestY += response.y;
                    posOffestZ += response.z;
                    AxisAlignedBB axisalignedbb = entity.getEntityBoundingBox().offset(response.x, response.y, response.z);
                    entity.setEntityBoundingBox(axisalignedbb);
                    entity.resetPositionToBB();
                }
            } catch (Exception e) {
            // Do nothing
            }
        }
    }
    AxisAlignedBB axisalignedbb = entity.getEntityBoundingBox().offset(-posOffestX, -posOffestY, -posOffestZ);
    entity.setEntityBoundingBox(axisalignedbb);
    entity.resetPositionToBB();
    // We are on the ship that we are riding
    if (entity.ridingEntity instanceof EntityMountable) {
        final EntityMountable entityMountable = (EntityMountable) entity.ridingEntity;
        if (entityMountable.getReferencePosOptional().isPresent()) {
            final Optional<PhysicsObject> physicsObjectOptional = ValkyrienUtils.getPhysoManagingBlock(entity.world, entityMountable.getReferencePosOptional().get());
            if (physicsObjectOptional.isPresent()) {
                worldBelow = physicsObjectOptional.get();
            }
        }
    }
    if (worldBelow == null) {
        return null;
    }
    dx += total.x;
    dy += total.y;
    dz += total.z;
    boolean alreadyOnGround = entity.onGround && (dy == origDy) && origDy < 0;
    Vector3d original = new Vector3d(origDx, origDy, origDz);
    Vector3d newMov = new Vector3d(dx - origDx, dy - origDy, dz - origDz);
    entity.collidedHorizontally = original.dot(newMov) < 0;
    entity.collidedVertically = isDifSignificant(dy, origDy);
    entity.onGround = entity.collidedVertically && origDy < 0 || alreadyOnGround;
    entity.collided = entity.collidedHorizontally || entity.collidedVertically;
    // entity.resetPositionToBB();
    double motionYBefore = entity.motionY;
    float oldFallDistance = entity.fallDistance;
    Vector3d dxyz = new Vector3d(dx, dy, dz);
    ;
    Vector3d origDxyz = new Vector3d(origDx, origDy, origDz);
    Vector3d origPosXyz = new Vector3d(origPosX, origPosY, origPosZ);
    return new IntermediateMovementVariableStorage(dxyz, origDxyz, origPosXyz, alreadyOnGround, motionYBefore, oldFallDistance, worldBelow.getShipData());
}
Also used : EntityShipMovementData(org.valkyrienskies.mod.common.entity.EntityShipMovementData) AxisAlignedBB(net.minecraft.util.math.AxisAlignedBB) EnumFacing(net.minecraft.util.EnumFacing) EntityMountable(org.valkyrienskies.mod.common.entity.EntityMountable) World(net.minecraft.world.World) BlockPos(net.minecraft.util.math.BlockPos) Polygon(org.valkyrienskies.mod.common.collision.Polygon) ShipPolygon(org.valkyrienskies.mod.common.collision.ShipPolygon) IBlockState(net.minecraft.block.state.IBlockState) ShipPolygon(org.valkyrienskies.mod.common.collision.ShipPolygon) PhysicsObject(org.valkyrienskies.mod.common.ships.ship_world.PhysicsObject) EntityPolygonCollider(org.valkyrienskies.mod.common.collision.EntityPolygonCollider) Vec3d(net.minecraft.util.math.Vec3d) Triple(org.apache.commons.lang3.tuple.Triple) Vector3dc(org.joml.Vector3dc) Vector3d(org.joml.Vector3d) ShipTransform(org.valkyrienskies.mod.common.ships.ship_transform.ShipTransform) EntityLivingBase(net.minecraft.entity.EntityLivingBase) EntityPlayer(net.minecraft.entity.player.EntityPlayer) PhysPolygonCollider(org.valkyrienskies.mod.common.collision.PhysPolygonCollider) Nullable(javax.annotation.Nullable)

Example 4 with EntityMountable

use of org.valkyrienskies.mod.common.entity.EntityMountable in project Valkyrien-Warfare-Revamped by ValkyrienWarfare.

the class VSWorldEventListener method onEntityAdded.

// TODO: Fix conflicts with EventsCommon.onEntityJoinWorldEvent()
@Override
public void onEntityAdded(Entity entity) {
    // This is really only here because Sponge doesn't call the entity join event for some reason :/
    // So I basically just copied the event code here as well.
    World world = worldObj;
    BlockPos posAt = new BlockPos(entity);
    Optional<PhysicsObject> physicsObject = ValkyrienUtils.getPhysoManagingBlock(world, posAt);
    if (!worldObj.isRemote && physicsObject.isPresent() && !(entity instanceof EntityFallingBlock)) {
        if (entity instanceof EntityArmorStand || entity instanceof EntityPig || entity instanceof EntityBoat) {
            EntityMountable entityMountable = new EntityMountable(world, entity.getPositionVector(), CoordinateSpaceType.SUBSPACE_COORDINATES, posAt);
            world.spawnEntity(entityMountable);
            entity.startRiding(entityMountable);
        }
        world.getChunk(entity.getPosition().getX() >> 4, entity.getPosition().getZ() >> 4).removeEntity(entity);
        physicsObject.get().getShipTransformationManager().getCurrentTickTransform().transform(entity, TransformType.SUBSPACE_TO_GLOBAL, false);
        world.getChunk(entity.getPosition().getX() >> 4, entity.getPosition().getZ() >> 4).addEntity(entity);
    }
}
Also used : EntityFallingBlock(net.minecraft.entity.item.EntityFallingBlock) EntityPig(net.minecraft.entity.passive.EntityPig) EntityBoat(net.minecraft.entity.item.EntityBoat) EntityMountable(org.valkyrienskies.mod.common.entity.EntityMountable) BlockPos(net.minecraft.util.math.BlockPos) EntityArmorStand(net.minecraft.entity.item.EntityArmorStand) World(net.minecraft.world.World)

Example 5 with EntityMountable

use of org.valkyrienskies.mod.common.entity.EntityMountable in project Valkyrien-Warfare-Revamped by ValkyrienWarfare.

the class ValkyrienUtils method fixEntityToShip.

public void fixEntityToShip(Entity toFix, Vector3dc posInLocal, PhysicsObject mountingShip) {
    World world = mountingShip.getWorld();
    EntityMountable entityMountable = new EntityMountable(world, JOML.toMinecraft(posInLocal), CoordinateSpaceType.SUBSPACE_COORDINATES, mountingShip.getReferenceBlockPos());
    world.spawnEntity(entityMountable);
    toFix.startRiding(entityMountable);
}
Also used : EntityMountable(org.valkyrienskies.mod.common.entity.EntityMountable) IPhysObjectWorld(org.valkyrienskies.mod.common.ships.ship_world.IPhysObjectWorld) World(net.minecraft.world.World)

Aggregations

EntityMountable (org.valkyrienskies.mod.common.entity.EntityMountable)6 World (net.minecraft.world.World)4 BlockPos (net.minecraft.util.math.BlockPos)3 Entity (net.minecraft.entity.Entity)2 EntityArmorStand (net.minecraft.entity.item.EntityArmorStand)2 EntityBoat (net.minecraft.entity.item.EntityBoat)2 EntityFallingBlock (net.minecraft.entity.item.EntityFallingBlock)2 EntityPig (net.minecraft.entity.passive.EntityPig)2 SubscribeEvent (net.minecraftforge.fml.common.eventhandler.SubscribeEvent)2 PhysicsObject (org.valkyrienskies.mod.common.ships.ship_world.PhysicsObject)2 Nullable (javax.annotation.Nullable)1 IBlockState (net.minecraft.block.state.IBlockState)1 EntityLivingBase (net.minecraft.entity.EntityLivingBase)1 EntityPlayer (net.minecraft.entity.player.EntityPlayer)1 TileEntity (net.minecraft.tileentity.TileEntity)1 EnumFacing (net.minecraft.util.EnumFacing)1 ResourceLocation (net.minecraft.util.ResourceLocation)1 AxisAlignedBB (net.minecraft.util.math.AxisAlignedBB)1 Vec3d (net.minecraft.util.math.Vec3d)1 Triple (org.apache.commons.lang3.tuple.Triple)1