use of org.terasology.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 Quat4f(0, 0, 0, 1));
return player;
}
use of org.terasology.logic.location.LocationComponent 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.logic.location.LocationComponent in project Terasology by MovingBlocks.
the class PlayerSystem method onConnect.
@ReceiveEvent(components = ClientComponent.class)
public void onConnect(ConnectedEvent connected, EntityRef entity) {
LocationComponent loc = entity.getComponent(LocationComponent.class);
// for new clients, the player store will return default values
PlayerStore playerStore = connected.getPlayerStore();
Client owner = networkSystem.getOwner(entity);
Vector3i minViewDist = ViewDistance.LEGALLY_BLIND.getChunkDistance();
if (playerStore.hasCharacter()) {
Vector3f storedLocation = playerStore.getRelevanceLocation();
loc.setWorldPosition(storedLocation);
entity.saveComponent(loc);
if (worldProvider.isBlockRelevant(storedLocation)) {
// chunk for spawning location is ready, so spawn right now
playerStore.restoreEntities();
EntityRef character = playerStore.getCharacter();
Vector3i viewDist = owner.getViewDistance().getChunkDistance();
addRelevanceEntity(entity, viewDist, owner);
restoreCharacter(entity, character);
} else {
// otherwise wait until chunk is ready
addRelevanceEntity(entity, minViewDist, owner);
clientsPreparingToSpawn.add(new SpawningClientInfo(entity, storedLocation, playerStore));
}
} else {
Vector3f spawnPosition = worldGenerator.getSpawnPosition(entity);
loc.setWorldPosition(spawnPosition);
entity.saveComponent(loc);
addRelevanceEntity(entity, minViewDist, owner);
if (worldProvider.isBlockRelevant(spawnPosition)) {
spawnPlayer(entity);
} else {
clientsPreparingToSpawn.add(new SpawningClientInfo(entity, spawnPosition));
}
}
}
Aggregations