Search in sources :

Example 16 with EntityRef

use of org.terasology.engine.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.

the class StorageManagerTest method testEntitySurvivesStorageInChunkStore.

@Test
public void testEntitySurvivesStorageInChunkStore() throws Exception {
    Chunk chunk = new ChunkImpl(CHUNK_POS, blockManager, extraDataManager);
    chunk.setBlock(0, 0, 0, testBlock);
    chunk.markReady();
    ChunkProvider chunkProvider = mock(ChunkProvider.class);
    when(chunkProvider.getAllChunks()).thenReturn(Arrays.asList(chunk));
    CoreRegistry.put(ChunkProvider.class, chunkProvider);
    EntityRef entity = entityManager.create();
    long id = entity.getId();
    LocationComponent locationComponent = new LocationComponent();
    AABBfc aabb = chunk.getAABB();
    Vector3f positionInChunk = new Vector3f(aabb.minX(), aabb.minY(), aabb.minZ());
    positionInChunk.x += 1;
    positionInChunk.y += 1;
    positionInChunk.z += 1;
    locationComponent.setWorldPosition(positionInChunk);
    entity.addComponent(locationComponent);
    esm.waitForCompletionOfPreviousSaveAndStartSaving();
    esm.finishSavingAndShutdown();
    EntitySystemSetupUtil.addReflectionBasedLibraries(context);
    EntitySystemSetupUtil.addEntityManagementRelatedClasses(context);
    EngineEntityManager newEntityManager = context.get(EngineEntityManager.class);
    StorageManager newSM = new ReadWriteStorageManager(savePath, moduleEnvironment, newEntityManager, blockManager, extraDataManager, false, recordAndReplaySerializer, recordAndReplayUtils, recordAndReplayCurrentStatus);
    newSM.loadGlobalStore();
    ChunkStore restored = newSM.loadChunkStore(CHUNK_POS);
    restored.restoreEntities();
    EntityRef ref = newEntityManager.getEntity(id);
    assertTrue(ref.exists());
    assertTrue(ref.isActive());
}
Also used : AABBfc(org.terasology.joml.geom.AABBfc) EngineEntityManager(org.terasology.engine.entitySystem.entity.internal.EngineEntityManager) ChunkImpl(org.terasology.engine.world.chunks.internal.ChunkImpl) Vector3f(org.joml.Vector3f) StorageManager(org.terasology.engine.persistence.StorageManager) Chunk(org.terasology.engine.world.chunks.Chunk) ChunkProvider(org.terasology.engine.world.chunks.ChunkProvider) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) LocationComponent(org.terasology.engine.logic.location.LocationComponent) ChunkStore(org.terasology.engine.persistence.ChunkStore) Test(org.junit.jupiter.api.Test)

Example 17 with EntityRef

use of org.terasology.engine.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.

the class BindableButtonImpl method update.

@Override
public void update(EntityRef[] inputEntities, float delta, EntityRef target, Vector3ic targetBlockPos, Vector3fc hitPosition, Vector3fc hitNormal, long gameTimeInMs) {
    long activateTime = gameTimeInMs;
    if (repeating && getState() == ButtonState.DOWN && mode.isActivatedOnPress() && activateTime - lastActivateTime > repeatTime) {
        lastActivateTime = activateTime;
        if (!consumedActivation) {
            boolean consumed = triggerOnRepeat(delta, target);
            if (!consumed) {
                buttonEvent.prepare(id, ButtonState.REPEAT, delta);
                buttonEvent.setTargetInfo(target, targetBlockPos, hitPosition, hitNormal);
                for (EntityRef entity : inputEntities) {
                    entity.send(buttonEvent);
                    if (buttonEvent.isConsumed()) {
                        break;
                    }
                }
            }
        }
    }
}
Also used : EntityRef(org.terasology.engine.entitySystem.entity.EntityRef)

Example 18 with EntityRef

use of org.terasology.engine.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.

the class BindableButtonImpl method updateBindState.

@Override
public boolean updateBindState(Input input, boolean pressed, float delta, EntityRef[] inputEntities, EntityRef target, Vector3ic targetBlockPos, Vector3fc hitPosition, Vector3fc hitNormal, boolean initialKeyConsumed, long gameTimeInMs) {
    boolean keyConsumed = initialKeyConsumed;
    if (pressed) {
        boolean previouslyEmpty = activeInputs.isEmpty();
        activeInputs.add(input);
        if (previouslyEmpty && mode.isActivatedOnPress()) {
            lastActivateTime = gameTimeInMs;
            consumedActivation = keyConsumed;
            if (!keyConsumed) {
                keyConsumed = triggerOnPress(delta, target);
            }
            if (!keyConsumed) {
                buttonEvent.prepare(id, ButtonState.DOWN, delta);
                buttonEvent.setTargetInfo(target, targetBlockPos, hitPosition, hitNormal);
                for (EntityRef entity : inputEntities) {
                    entity.send(buttonEvent);
                    if (buttonEvent.isConsumed()) {
                        break;
                    }
                }
                keyConsumed = buttonEvent.isConsumed();
            }
        }
    } else if (!activeInputs.isEmpty()) {
        activeInputs.remove(input);
        if (activeInputs.isEmpty() && mode.isActivatedOnRelease()) {
            if (!keyConsumed) {
                keyConsumed = triggerOnRelease(delta, target);
            }
            if (!keyConsumed) {
                buttonEvent.prepare(id, ButtonState.UP, delta);
                buttonEvent.setTargetInfo(target, targetBlockPos, hitPosition, hitNormal);
                for (EntityRef entity : inputEntities) {
                    entity.send(buttonEvent);
                    if (buttonEvent.isConsumed()) {
                        break;
                    }
                }
                keyConsumed = buttonEvent.isConsumed();
            }
        }
    }
    return keyConsumed;
}
Also used : EntityRef(org.terasology.engine.entitySystem.entity.EntityRef)

Example 19 with EntityRef

use of org.terasology.engine.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.

the class ConditionAction method condition.

protected boolean condition(Actor actor) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException {
    boolean passing = true;
    if (componentAbsent != null && actor.hasComponent(componentLibrary.resolve(componentAbsent).getType())) {
        passing = false;
    }
    if (componentPresent != null) {
        Component component = actor.getComponent(componentLibrary.resolve(componentPresent).getType());
        if (component == null) {
            passing = false;
        } else {
            // Check values
            if (values != null) {
                for (String value : values) {
                    String[] tokens = value.split(" ");
                    Object fieldValue = component.getClass().getDeclaredField(tokens[1]).get(component);
                    String secondValue;
                    switch(tokens[0]) {
                        // Read second value from the definition
                        case "V":
                            secondValue = tokens[3];
                            break;
                        // Read second value from a field of the component
                        case "F":
                            secondValue = component.getClass().getDeclaredField(tokens[3]).get(component).toString();
                            break;
                        // No second value needed.
                        case "N":
                            secondValue = "";
                            break;
                        default:
                            logger.error("Unsupported guard value type: {}", tokens[0]);
                            secondValue = "";
                    }
                    // Can't use a switch for this :(
                    if (fieldValue instanceof Boolean) {
                        switch(tokens[2]) {
                            case "=":
                            case "==":
                                passing = (Boolean) fieldValue == Boolean.parseBoolean(secondValue);
                                break;
                            case "!":
                            case "!=":
                                passing = (Boolean) fieldValue != Boolean.parseBoolean(secondValue);
                                break;
                            default:
                                logger.error("Unsupported operation for boolean values: {}", tokens[2]);
                        }
                    } else if (fieldValue instanceof Number) {
                        switch(tokens[2]) {
                            case "=":
                            case "==":
                                passing = (Double) fieldValue == Double.parseDouble(secondValue);
                                break;
                            case "!":
                            case "!=":
                                passing = (Double) fieldValue == Double.parseDouble(secondValue);
                                break;
                            case "<=":
                                passing = ((Number) fieldValue).doubleValue() <= Double.parseDouble(secondValue);
                                break;
                            case ">=":
                                passing = ((Number) fieldValue).doubleValue() >= Double.parseDouble(secondValue);
                                break;
                            case ">":
                                passing = ((Number) fieldValue).doubleValue() > Double.parseDouble(secondValue);
                                break;
                            case "<":
                                passing = ((Number) fieldValue).doubleValue() < Double.parseDouble(secondValue);
                                break;
                            default:
                                logger.error("Unsupported operation for numeric values: {}", tokens[2]);
                        }
                    } else if (fieldValue instanceof String) {
                        switch(tokens[2]) {
                            case "=":
                            case "==":
                                passing = fieldValue.equals(secondValue);
                                break;
                            case "!":
                            case "!=":
                                passing = !fieldValue.equals(secondValue);
                                break;
                            default:
                                logger.error("Unsupported operation for strings: {}", tokens[2]);
                        }
                    } else {
                        if (fieldValue == null) {
                            if (!tokens[2].equals("null")) {
                                // If a more complex check is requested and the field is null, fail
                                passing = false;
                            }
                        } else {
                            switch(tokens[2]) {
                                // Null check
                                case "exists":
                                    if (fieldValue instanceof EntityRef && fieldValue == EntityRef.NULL) {
                                        passing = false;
                                    }
                                    break;
                                // Collection checks
                                case "empty":
                                    if (fieldValue instanceof Collection) {
                                        passing = ((Collection<?>) fieldValue).isEmpty();
                                    }
                                    break;
                                case "nonEmpty":
                                    if (fieldValue instanceof Collection) {
                                        passing = !((Collection<?>) fieldValue).isEmpty();
                                    }
                                    break;
                                default:
                                    logger.error("Unknown field type or operation: {} {}", fieldValue.getClass(), tokens[2]);
                            }
                        }
                    }
                }
            }
        }
    }
    return passing;
}
Also used : Collection(java.util.Collection) Component(org.terasology.gestalt.entitysystem.component.Component) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef)

Example 20 with EntityRef

use of org.terasology.engine.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.

the class CollectiveBehaviorSystem method update.

@Override
public void update(float delta) {
    Iterable<EntityRef> entities = entityManager.getEntitiesWith(CollectiveBehaviorComponent.class);
    for (EntityRef entity : entities) {
        CollectiveBehaviorComponent collectiveBehaviorComponent = entity.getComponent(CollectiveBehaviorComponent.class);
        if (collectiveBehaviorComponent.collectiveInterpreter == null) {
            logger.warn("Found a null interpreter during tick updates, skipping for entity {}", entity);
            continue;
        }
        collectiveBehaviorComponent.collectiveInterpreter.tick(delta);
    }
}
Also used : 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