use of org.terasology.math.geom.Quat4f in project Terasology by MovingBlocks.
the class FirstPersonClientSystem method update.
/**
* modifies the held item mount point to move the held item in first person view
*/
@Override
public void update(float delta) {
// ensure empty hand is shown if no item is hold at the moment
if (!currentHeldItem.exists() && currentHeldItem != getHandEntity()) {
linkHeldItemLocationForLocalPlayer(getHandEntity());
}
// ensure that there are no lingering items that are marked as still held. This situation happens with client side predicted items
for (EntityRef entityRef : entityManager.getEntitiesWith(ItemIsHeldComponent.class)) {
if (!entityRef.equals(currentHeldItem) && !entityRef.equals(handEntity)) {
entityRef.destroy();
}
}
// get the first person mount point and rotate it away from the camera
CharacterHeldItemComponent characterHeldItemComponent = localPlayer.getCharacterEntity().getComponent(CharacterHeldItemComponent.class);
FirstPersonHeldItemMountPointComponent mountPointComponent = localPlayer.getCameraEntity().getComponent(FirstPersonHeldItemMountPointComponent.class);
if (characterHeldItemComponent == null || mountPointComponent == null) {
return;
}
LocationComponent locationComponent = mountPointComponent.mountPointEntity.getComponent(LocationComponent.class);
if (locationComponent == null) {
return;
}
long timeElapsedSinceLastUsed = time.getGameTimeInMs() - characterHeldItemComponent.lastItemUsedTime;
float animateAmount = 0f;
if (timeElapsedSinceLastUsed < USEANIMATIONLENGTH) {
// half way through the animation will be the maximum extent of rotation and translation
animateAmount = 1f - Math.abs(((float) timeElapsedSinceLastUsed / (float) USEANIMATIONLENGTH) - 0.5f);
}
float addPitch = 15f * animateAmount;
float addYaw = 10f * animateAmount;
locationComponent.setLocalRotation(new Quat4f(TeraMath.DEG_TO_RAD * (mountPointComponent.rotateDegrees.y + addYaw), TeraMath.DEG_TO_RAD * (mountPointComponent.rotateDegrees.x + addPitch), TeraMath.DEG_TO_RAD * mountPointComponent.rotateDegrees.z));
Vector3f offset = new Vector3f(0.25f * animateAmount, -0.12f * animateAmount, 0f);
offset.add(mountPointComponent.translate);
locationComponent.setLocalPosition(offset);
mountPointComponent.mountPointEntity.saveComponent(locationComponent);
}
use of org.terasology.math.geom.Quat4f in project Terasology by MovingBlocks.
the class LocalPlayer method getViewRotation.
public Quat4f getViewRotation() {
ClientComponent clientComponent = getClientEntity().getComponent(ClientComponent.class);
if (clientComponent == null) {
return new Quat4f(Quat4f.IDENTITY);
}
LocationComponent location = clientComponent.camera.getComponent(LocationComponent.class);
if (location == null) {
return getRotation();
}
return location.getWorldRotation();
}
use of org.terasology.math.geom.Quat4f in project Terasology by MovingBlocks.
the class PlayerFactory method newInstance.
/**
* Creates a new player character entity. The desired spawning location is derived from
* the {@link LocationComponent} of the controller.
* @param controller the controlling client entity
* @return a new player character entity
*/
public EntityRef newInstance(EntityRef controller) {
EntityBuilder builder = entityManager.newBuilder("engine:player");
LocationComponent location = controller.getComponent(LocationComponent.class);
Vector3f spawnPosition = findSpawnPositionFromLocationComponent(location);
location.setWorldPosition(spawnPosition);
controller.saveComponent(location);
logger.debug("Spawing player at: {}", spawnPosition);
builder.getComponent(LocationComponent.class).setWorldPosition(spawnPosition);
builder.setOwner(controller);
CharacterComponent playerComponent = builder.getComponent(CharacterComponent.class);
playerComponent.controller = controller;
EntityRef player = builder.build();
Location.attachChild(player, controller, new Vector3f(), new Quat4f(0, 0, 0, 1));
return player;
}
use of org.terasology.math.geom.Quat4f in project Terasology by MovingBlocks.
the class PlayerSystem method setSpawnLocationOnRespawnRequest.
@ReceiveEvent(priority = EventPriority.PRIORITY_CRITICAL, components = { ClientComponent.class })
public void setSpawnLocationOnRespawnRequest(RespawnRequestEvent event, EntityRef entity) {
EntityRef clientInfo = entity.getComponent(ClientComponent.class).clientInfo;
Vector3f spawnPosition;
if (clientInfo.hasComponent(StaticSpawnLocationComponent.class)) {
spawnPosition = clientInfo.getComponent(StaticSpawnLocationComponent.class).position;
} else {
spawnPosition = worldGenerator.getSpawnPosition(entity);
}
LocationComponent loc = entity.getComponent(LocationComponent.class);
loc.setWorldPosition(spawnPosition);
// reset rotation
loc.setLocalRotation(new Quat4f());
entity.saveComponent(loc);
}
use of org.terasology.math.geom.Quat4f in project Terasology by MovingBlocks.
the class LocationComponentTest method setup.
@Before
public void setup() {
loc = new LocationComponent();
entity = createFakeEntityWith(loc);
yawRotation = new Quat4f(TeraMath.DEG_TO_RAD * 90, 0, 0);
pitchRotation = new Quat4f(0, TeraMath.DEG_TO_RAD * 45, 0);
yawPitch = new Quat4f(TeraMath.DEG_TO_RAD * 90, TeraMath.DEG_TO_RAD * 45, 0);
}
Aggregations