use of org.terasology.engine.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.
the class IterateComponentsBenchmark method iterateSingleComponent.
@Benchmark
public void iterateSingleComponent(StateObject state) {
for (EntityRef entity : state.entityManager.getEntitiesWith(LocationComponent.class)) {
LocationComponent loc = entity.getComponent(LocationComponent.class);
loc.getLocalPosition();
}
}
use of org.terasology.engine.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.
the class SectorSimulationSystem method chunkLoad.
/**
* Handles the OnChunkLoaded event for sector entities.
*
* Forwards the event to the appropriate sector-scope entities, if they are watching that chunk, and sends a
* {@link SectorEntityLoad} event if this is the first watched chunk to be loaded for that entity.
*
* @param event the event sent when any chunk is loaded
* @param worldEntity ignored
*/
@ReceiveEvent(components = WorldComponent.class)
public void chunkLoad(OnChunkLoaded event, EntityRef worldEntity) {
for (EntityRef entity : entityManager.getEntitiesWith(SectorSimulationComponent.class)) {
if (SectorUtil.getWatchedChunks(entity).contains(event.getChunkPos())) {
entity.send(new OnChunkLoaded(event.getChunkPos()));
if (SectorUtil.onlyWatchedChunk(entity, event.getChunkPos(), chunkProvider)) {
entity.send(new SectorEntityLoad());
}
sendLoadedSectorUpdateEvent(entity, simulationDelta(entity));
}
}
}
use of org.terasology.engine.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.
the class PlayerTargetSystem method update.
@Override
public void update(float delta) {
EntityRef charEntity = player.getCharacterEntity();
if (charEntity.exists()) {
Vector3f cameraPos = player.getViewPosition(new Vector3f());
CharacterComponent charComp = charEntity.getComponent(CharacterComponent.class);
if (charComp != null) {
Vector3f dir = player.getViewDirection(new Vector3f());
float maxDist = charComp.interactionRange;
FirstPersonHeldItemMountPointComponent heldItemMountPoint = player.getCameraEntity().getComponent(FirstPersonHeldItemMountPointComponent.class);
if (heldItemMountPoint != null && heldItemMountPoint.isTracked()) {
maxDist = heldItemMountPoint.translate.length() + 0.25f;
dir = new Vector3f(heldItemMountPoint.translate).normalize();
}
if (targetSystem.updateTarget(cameraPos, dir, maxDist)) {
EntityRef oldTarget = targetSystem.getPreviousTarget();
EntityRef newTarget = targetSystem.getTarget();
charEntity.send(new PlayerTargetChangedEvent(oldTarget, newTarget));
}
}
}
}
use of org.terasology.engine.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.
the class NetworkSystemImpl method processNewClient.
private void processNewClient(NetClient client) {
ServerConnectListManager serverConnectListManager = context.get(ServerConnectListManager.class);
if (!serverConnectListManager.isClientAllowedToConnect(client.getId())) {
String errorMessage = serverConnectListManager.getErrorMessage(client.getId());
client.send(NetData.NetMessage.newBuilder().setServerInfo(getServerInfoMessage(errorMessage)).build());
forceDisconnect(client);
// reset kicked status so the next connection is set correctly
kicked = false;
return;
}
client.connected(entityManager, entitySerializer, eventSerializer, eventLibrary);
client.send(NetData.NetMessage.newBuilder().setJoinComplete(NetData.JoinCompleteMessage.newBuilder().setClientId(client.getEntity().getComponent(NetworkComponent.class).getNetworkId())).build());
clientList.add(client);
netClientList.add(client);
clientPlayerLookup.put(client.getEntity(), client);
connectClient(client);
// log after connect so that the name has been set:
logger.info("New client entity: {}", client.getEntity());
for (EntityRef netEntity : entityManager.getEntitiesWith(NetworkComponent.class)) {
NetworkComponent netComp = netEntity.getComponent(NetworkComponent.class);
if (netComp.getNetworkId() != NULL_NET_ID) {
switch(netComp.replicateMode) {
case OWNER:
if (client.equals(getOwner(netEntity))) {
client.setNetInitial(netComp.getNetworkId());
}
break;
default:
// TODO: Relevance Check
client.setNetInitial(netComp.getNetworkId());
break;
}
}
}
}
use of org.terasology.engine.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.
the class NetworkSystemImpl method onEntityComponentRemoved.
@Override
public void onEntityComponentRemoved(EntityRef entity, Class<? extends Component> component) {
ComponentMetadata<? extends Component> metadata = componentLibrary.getMetadata(component);
NetworkComponent netComp = entity.getComponent(NetworkComponent.class);
if (netComp != null && netComp.getNetworkId() != NULL_NET_ID) {
if (mode.isServer()) {
if (metadata.isReplicated()) {
for (NetClient client : netClientList) {
logger.debug("Component {} removed from {}", component, entity);
client.setComponentRemoved(netComp.getNetworkId(), component);
}
}
}
}
if (mode.isAuthority() && metadata.isReferenceOwner()) {
ownershipHelper.listOwnedEntities(entity.getComponent(component)).forEach(EntityRef::destroy);
}
}
Aggregations