Search in sources :

Example 61 with EntityRef

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();
    }
}
Also used : EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) LocationComponent(org.terasology.engine.logic.location.LocationComponent) Benchmark(org.openjdk.jmh.annotations.Benchmark)

Example 62 with EntityRef

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));
        }
    }
}
Also used : OnChunkLoaded(org.terasology.engine.world.chunks.event.OnChunkLoaded) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) BaseEntityRef(org.terasology.engine.entitySystem.entity.internal.BaseEntityRef) ReceiveEvent(org.terasology.engine.entitySystem.event.ReceiveEvent)

Example 63 with EntityRef

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));
            }
        }
    }
}
Also used : PlayerTargetChangedEvent(org.terasology.engine.logic.players.PlayerTargetChangedEvent) Vector3f(org.joml.Vector3f) CharacterComponent(org.terasology.engine.logic.characters.CharacterComponent) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) FirstPersonHeldItemMountPointComponent(org.terasology.engine.logic.players.FirstPersonHeldItemMountPointComponent)

Example 64 with EntityRef

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;
            }
        }
    }
}
Also used : NetworkComponent(org.terasology.engine.network.NetworkComponent) ByteString(com.google.protobuf.ByteString) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef)

Example 65 with EntityRef

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);
    }
}
Also used : NetworkComponent(org.terasology.engine.network.NetworkComponent) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef)

Aggregations

EntityRef (org.terasology.engine.entitySystem.entity.EntityRef)298 Test (org.junit.jupiter.api.Test)88 ClientComponent (org.terasology.engine.network.ClientComponent)55 Vector3f (org.joml.Vector3f)51 LocationComponent (org.terasology.engine.logic.location.LocationComponent)44 Vector3i (org.joml.Vector3i)36 Command (org.terasology.engine.logic.console.commandSystem.annotations.Command)34 ReceiveEvent (org.terasology.engine.entitySystem.event.ReceiveEvent)29 StringComponent (org.terasology.unittest.stubs.StringComponent)26 NetworkComponent (org.terasology.engine.network.NetworkComponent)23 EntityData (org.terasology.protobuf.EntityData)23 Quaternionf (org.joml.Quaternionf)19 DisplayNameComponent (org.terasology.engine.logic.common.DisplayNameComponent)19 Component (org.terasology.gestalt.entitysystem.component.Component)19 CharacterComponent (org.terasology.engine.logic.characters.CharacterComponent)15 Map (java.util.Map)14 EntityBuilder (org.terasology.engine.entitySystem.entity.EntityBuilder)13 BlockComponent (org.terasology.engine.world.block.BlockComponent)13 Block (org.terasology.engine.world.block.Block)11 Prefab (org.terasology.engine.entitySystem.prefab.Prefab)10