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());
}
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;
}
}
}
}
}
}
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;
}
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;
}
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);
}
}
Aggregations