use of org.terasology.engine.logic.location.LocationComponent in project Terasology by MovingBlocks.
the class CoreCommands method spawnPrefab.
/**
* Spawns an instance of a prefab in the world
*
* @param sender Sender of command
* @param prefabName String containing prefab name
* @return String containing final message
*/
@Command(shortDescription = "Spawns an instance of a prefab in the world", runOnServer = true, requiredPermission = PermissionManager.CHEAT_PERMISSION)
public String spawnPrefab(@Sender EntityRef sender, @CommandParam("prefabId") String prefabName) {
ClientComponent clientComponent = sender.getComponent(ClientComponent.class);
LocationComponent characterLocation = clientComponent.character.getComponent(LocationComponent.class);
Vector3f spawnPos = characterLocation.getWorldPosition(new Vector3f());
Vector3f offset = characterLocation.getWorldDirection(new Vector3f());
offset.mul(2);
spawnPos.add(offset);
Vector3f dir = characterLocation.getWorldDirection(new Vector3f());
dir.y = 0;
if (dir.lengthSquared() > 0.001f) {
dir.normalize();
} else {
dir.set(Direction.FORWARD.asVector3f());
}
return Assets.getPrefab(prefabName).map(prefab -> {
LocationComponent loc = prefab.getComponent(LocationComponent.class);
if (loc != null) {
entityManager.create(prefab, spawnPos);
return "Done";
} else {
return "Prefab cannot be spawned (no location component)";
}
}).orElse("Unknown prefab");
}
use of org.terasology.engine.logic.location.LocationComponent 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 Quaternionf(0, 0, 0, 1));
return player;
}
use of org.terasology.engine.logic.location.LocationComponent in project Terasology by MovingBlocks.
the class ThirdPersonRemoteClientSystem method update.
/**
* Modifies the remote players' held item mount points to show and move their held items at their location. Clean up
* no longer needed held item entities.
* <p>
* TODO: Also responsible for catching characters without current held item entities and then create them. Should be
* moved elsewhere
*/
@Override
public void update(float delta) {
// Make a set of all held items that exist so we can review them and later toss any no longer needed
Set<EntityRef> heldItemsForReview = Sets.newHashSet(entityManager.getEntitiesWith(ItemIsRemotelyHeldComponent.class));
// critters may also want held items
for (EntityRef remotePlayer : entityManager.getEntitiesWith(CharacterComponent.class, PlayerCharacterComponent.class)) {
if (relatesToLocalPlayer(remotePlayer)) {
continue;
}
// Find the associated held item entity for this player, if one exists
EntityRef currentHeldItem = EntityRef.NULL;
Iterator<EntityRef> heldItermsIterator = heldItemsForReview.iterator();
while (heldItermsIterator.hasNext()) {
EntityRef heldItemCandidate = heldItermsIterator.next();
ItemIsRemotelyHeldComponent itemIsRemotelyHeldComponent = heldItemCandidate.getComponent(ItemIsRemotelyHeldComponent.class);
if (itemIsRemotelyHeldComponent.remotePlayer.equals(remotePlayer)) {
currentHeldItem = heldItemCandidate;
heldItermsIterator.remove();
break;
}
}
// selected
if (currentHeldItem == EntityRef.NULL) {
if (remotePlayer.hasComponent(CharacterHeldItemComponent.class)) {
CharacterHeldItemComponent characterHeldItemComponent = remotePlayer.getComponent(CharacterHeldItemComponent.class);
if (characterHeldItemComponent != null && !characterHeldItemComponent.selectedItem.equals(EntityRef.NULL)) {
linkHeldItemLocationForRemotePlayer(remotePlayer.getComponent(CharacterHeldItemComponent.class).selectedItem, remotePlayer);
}
}
}
// get the remote person mount point
CharacterHeldItemComponent characterHeldItemComponent = remotePlayer.getComponent(CharacterHeldItemComponent.class);
RemotePersonHeldItemMountPointComponent mountPointComponent = remotePlayer.getComponent(RemotePersonHeldItemMountPointComponent.class);
if (characterHeldItemComponent == null || mountPointComponent == null) {
continue;
}
LocationComponent locationComponent = mountPointComponent.mountPointEntity.getComponent(LocationComponent.class);
if (locationComponent == null) {
continue;
}
long timeElapsedSinceLastUsed = time.getGameTimeInMs() - characterHeldItemComponent.lastItemUsedTime;
float animateAmount = 0f;
if (timeElapsedSinceLastUsed < USEANIMATIONLENGTH) {
// TODO add other easing functions into utilities and use here?
// 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 Quaternionf().rotationYXZ(Math.toRadians(mountPointComponent.rotateDegrees.y + addYaw), Math.toRadians(mountPointComponent.rotateDegrees.x + addPitch), Math.toRadians(mountPointComponent.rotateDegrees.z)));
Vector3f offset = new Vector3f(0.05f * animateAmount, -0.24f * animateAmount, 0f);
offset.add(mountPointComponent.translate);
locationComponent.setLocalPosition(offset);
mountPointComponent.mountPointEntity.saveComponent(locationComponent);
}
heldItemsForReview.forEach(remainingHeldItem -> {
if (remainingHeldItem.exists()) {
remainingHeldItem.destroy();
}
});
}
use of org.terasology.engine.logic.location.LocationComponent in project Terasology by MovingBlocks.
the class LocalPlayerSystem method onTargetChanged.
@ReceiveEvent
public void onTargetChanged(PlayerTargetChangedEvent event, EntityRef entity) {
EntityRef target = event.getNewTarget();
hasTarget = target.exists();
if (hasTarget) {
LocationComponent location = target.getComponent(LocationComponent.class);
if (location != null) {
BlockComponent blockComp = target.getComponent(BlockComponent.class);
BlockRegionComponent blockRegion = target.getComponent(BlockRegionComponent.class);
if (blockComp != null || blockRegion != null) {
Vector3f blockPos = location.getWorldPosition(new Vector3f());
Block block = worldProvider.getBlock(blockPos);
aabb.set(block.getBounds(blockPos));
} else {
MeshComponent mesh = target.getComponent(MeshComponent.class);
if (mesh != null && mesh.mesh != null) {
aabb.set(mesh.mesh.getAABB());
aabb.transform(new Matrix4f().translationRotateScale(location.getWorldPosition(new Vector3f()), location.getWorldRotation(new Quaternionf()), location.getWorldScale()));
}
}
}
}
}
use of org.terasology.engine.logic.location.LocationComponent in project Terasology by MovingBlocks.
the class MovementDebugCommands method teleportAllPlayersToPlayer.
@Command(shortDescription = "Teleport all users to specified user", runOnServer = true, requiredPermission = PermissionManager.USER_MANAGEMENT_PERMISSION)
public String teleportAllPlayersToPlayer(@CommandParam("username") String username) {
Vector3f vPlayerLocation = new Vector3f();
boolean bPlayerLocationWasFound = false;
EntityRef playerEntity = null;
for (EntityRef clientEntity : entityManager.getEntitiesWith(ClientComponent.class)) {
EntityRef clientInfo = clientEntity.getComponent(ClientComponent.class).clientInfo;
DisplayNameComponent name = clientInfo.getComponent(DisplayNameComponent.class);
if (username.equalsIgnoreCase(name.name)) {
LocationComponent locationComponent = clientEntity.getComponent(LocationComponent.class);
if (locationComponent != null) {
vPlayerLocation = locationComponent.getWorldPosition(new Vector3f());
bPlayerLocationWasFound = true;
playerEntity = clientEntity;
}
break;
}
}
if (!bPlayerLocationWasFound) {
throw new IllegalArgumentException("No such user '" + username + "'");
}
MovementMode playerMovementMode = MovementMode.NONE;
ClientComponent clientInfo = playerEntity.getComponent(ClientComponent.class);
if (clientInfo != null) {
CharacterMovementComponent playerMovementComponent = clientInfo.character.getComponent(CharacterMovementComponent.class);
if (playerMovementComponent != null) {
playerMovementMode = playerMovementComponent.mode;
}
}
for (EntityRef clientEntity : entityManager.getEntitiesWith(ClientComponent.class)) {
ClientComponent clientComp = clientEntity.getComponent(ClientComponent.class);
if (clientComp != null) {
clientComp.character.send(new CharacterTeleportEvent(vPlayerLocation));
CharacterMovementComponent characterMovementComponent = clientComp.character.getComponent(CharacterMovementComponent.class);
if (characterMovementComponent != null && playerMovementMode != MovementMode.NONE && playerMovementMode != characterMovementComponent.mode) {
clientComp.character.send(new SetMovementModeEvent(playerMovementMode));
}
}
}
return "All possible players teleported to " + username + " and set to " + playerMovementMode;
}
Aggregations