use of org.joml.Vector3i in project Terasology by MovingBlocks.
the class KinematicCharacterMover method checkBlockEntry.
/*
* Figure out if our position has put us into a new set of blocks and fire the appropriate events.
*/
private void checkBlockEntry(EntityRef entity, Vector3i oldPosition, Vector3i newPosition, float characterHeight) {
if (!oldPosition.equals(newPosition)) {
int characterHeightInBlocks = (int) Math.ceil(characterHeight);
// get the old position's blocks
Block[] oldBlocks = new Block[characterHeightInBlocks];
for (int y = 0; y < characterHeightInBlocks; y++) {
oldBlocks[y] = worldProvider.getBlock(oldPosition.x, oldPosition.y + y, oldPosition.z);
}
// get the new position's blocks
Block[] newBlocks = new Block[characterHeightInBlocks];
for (int y = 0; y < characterHeightInBlocks; y++) {
newBlocks[y] = worldProvider.getBlock(newPosition.x, newPosition.y + y, newPosition.z);
}
for (int y = 0; y < characterHeightInBlocks; y++) {
// send a block enter/leave event for this character
entity.send(new OnEnterBlockEvent(oldBlocks[y], newBlocks[y], new Vector3i(0, y, 0)));
}
}
}
use of org.joml.Vector3i in project Terasology by MovingBlocks.
the class KinematicCharacterMover method checkMode.
/**
* Checks whether a character should change movement mode (from being underwater or in a ladder). A higher and lower point of the
* character is tested for being in water, only if both points are in water does the character count as swimming.
* <br><br>
* Sends the OnEnterLiquidEvent and OnLeaveLiquidEvent events.
*
* @param movementComp The movement component of the character.
* @param state The current state of the character.
*/
private void checkMode(final CharacterMovementComponent movementComp, final CharacterStateEvent state, final CharacterStateEvent oldState, EntityRef entity, boolean firstRun, boolean isCrouching) {
// If we are ghosting or we can't move, the mode cannot be changed.
if (!state.getMode().respondToEnvironment) {
return;
}
Vector3f worldPos = state.getPosition();
Vector3f top = new Vector3f(worldPos);
Vector3f bottom = new Vector3f(worldPos);
top.y += 0.5f * movementComp.height;
bottom.y -= 0.5f * movementComp.height;
final boolean topUnderwater = worldProvider.getBlock(top).isLiquid();
final boolean bottomUnderwater = worldProvider.getBlock(bottom).isLiquid();
final boolean newSwimming = !topUnderwater && bottomUnderwater;
final boolean newDiving = topUnderwater && bottomUnderwater;
boolean newClimbing = false;
if (isClimbingAllowed(newSwimming, newDiving)) {
Vector3i finalDir;
finalDir = findClimbable(movementComp, worldPos, newSwimming, newDiving);
if (finalDir != null) {
newClimbing = true;
state.setClimbDirection(finalDir);
}
}
updateMode(state, newSwimming, newDiving, newClimbing, isCrouching);
}
use of org.joml.Vector3i in project Terasology by MovingBlocks.
the class KinematicCharacterMover method climb.
private void climb(final CharacterStateEvent state, CharacterMoveInputEvent input, Vector3f desiredVelocity) {
if (state.getClimbDirection() == null) {
return;
}
Vector3f tmp;
Vector3i climbDir3i = state.getClimbDirection();
Vector3f climbDir3f = new Vector3f(climbDir3i);
Quaternionf rotation = new Quaternionf().rotationYXZ(TeraMath.DEG_TO_RAD * state.getYaw(), 0, 0);
tmp = new Vector3f(0.0f, 0.0f, -1.0f);
tmp.rotate(rotation);
float angleToClimbDirection = tmp.angle(climbDir3f);
boolean clearMovementToDirection = !state.isGrounded();
boolean jumpOrCrouchActive = desiredVelocity.y != 0;
// facing the ladder or looking down or up
if (angleToClimbDirection < Math.PI / 4.0 || Math.abs(input.getPitch()) > 60f) {
if (jumpOrCrouchActive) {
desiredVelocity.x = 0;
desiredVelocity.z = 0;
clearMovementToDirection = false;
} else {
float pitchAmount = state.isGrounded() ? 45f : 90f;
float pitch = input.getPitch() > 30f ? pitchAmount : -pitchAmount;
rotation.rotationYXZ(TeraMath.DEG_TO_RAD * state.getYaw(), TeraMath.DEG_TO_RAD * pitch, 0);
desiredVelocity.rotate(rotation);
}
// looking sidewards from ladder
} else if (angleToClimbDirection < Math.PI * 3.0 / 4.0) {
float rollAmount = state.isGrounded() ? 45f : 90f;
tmp = new Vector3f();
climbDir3f.rotate(rotation, tmp);
float leftOrRight = tmp.x;
float plusOrMinus = (leftOrRight < 0f ? -1.0f : 1.0f) * (climbDir3i.x != 0 ? -1.0f : 1.0f);
if (jumpOrCrouchActive) {
rotation.rotationY(TeraMath.DEG_TO_RAD * state.getYaw());
} else {
rotation.rotationYXZ(TeraMath.DEG_TO_RAD * input.getYaw(), 0f, TeraMath.DEG_TO_RAD * rollAmount * plusOrMinus);
}
desiredVelocity.rotate(rotation);
// facing away from ladder
} else {
rotation.rotationYXZ(TeraMath.DEG_TO_RAD * state.getYaw(), 0, 0);
desiredVelocity.rotate(rotation);
clearMovementToDirection = false;
}
// clear out movement towards or away from the ladder
if (clearMovementToDirection) {
if (climbDir3i.x != 0) {
desiredVelocity.x = 0f;
}
if (climbDir3i.z != 0) {
desiredVelocity.z = 0f;
}
}
}
use of org.joml.Vector3i in project Terasology by MovingBlocks.
the class BlockEntitySystem method defaultDropsHandling.
@ReceiveEvent(priority = EventPriority.PRIORITY_TRIVIAL)
public void defaultDropsHandling(CreateBlockDropsEvent event, EntityRef entity, ActAsBlockComponent blockComponent) {
if (blockComponent.block != null) {
if (entity.hasComponent(BlockRegionComponent.class)) {
BlockRegionComponent blockRegion = entity.getComponent(BlockRegionComponent.class);
if (blockComponent.dropBlocksInRegion) {
// loop through all the blocks in this region and drop them
for (Vector3ic location : blockRegion.region) {
Block blockInWorld = worldProvider.getBlock(location);
commonDefaultDropsHandling(event, entity, location, blockInWorld.getBlockFamily().getArchetypeBlock());
}
} else {
// just drop the ActAsBlock block
Vector3i location = new Vector3i(blockRegion.region.center(new Vector3f()), RoundingMode.HALF_UP);
commonDefaultDropsHandling(event, entity, location, blockComponent.block.getArchetypeBlock());
}
} else if (entity.hasComponent(LocationComponent.class)) {
LocationComponent locationComponent = entity.getComponent(LocationComponent.class);
Vector3i location = new Vector3i(locationComponent.getWorldPosition(new Vector3f()), RoundingMode.HALF_UP);
commonDefaultDropsHandling(event, entity, location, blockComponent.block.getArchetypeBlock());
}
}
}
use of org.joml.Vector3i in project Terasology by MovingBlocks.
the class NeighbourBlockFamilyUpdateSystem method processUpdateForBlockLocation.
private void processUpdateForBlockLocation(Vector3ic blockLocation) {
for (Side side : Side.values()) {
Vector3i neighborLocation = blockLocation.add(side.direction(), new Vector3i());
if (worldProvider.isBlockRelevant(neighborLocation)) {
Block neighborBlock = worldProvider.getBlock(neighborLocation);
final BlockFamily blockFamily = neighborBlock.getBlockFamily();
if (blockFamily instanceof UpdatesWithNeighboursFamily) {
UpdatesWithNeighboursFamily neighboursFamily = (UpdatesWithNeighboursFamily) blockFamily;
Block neighborBlockAfterUpdate = neighboursFamily.getBlockForNeighborUpdate(neighborLocation, neighborBlock);
if (neighborBlock != neighborBlockAfterUpdate) {
worldProvider.setBlock(neighborLocation, neighborBlockAfterUpdate);
}
}
}
}
}
Aggregations