use of org.terasology.entitySystem.prefab.Prefab in project Terasology by MovingBlocks.
the class AnimationScreen method initialise.
@Override
public void initialise() {
spawnEntityIdButton = find("spawnEntityIdButton", UIButton.class);
entityDropdown = find("entityDropdown", UIDropdownScrollable.class);
logger.info("Number of available skeletal meshes: " + assetManager.getAvailableAssets(SkeletalMesh.class).size());
ArrayList skeletalMesh = new ArrayList(assetManager.getAvailableAssets(SkeletalMesh.class));
if (entityDropdown != null) {
entityDropdown.setOptions(skeletalMesh);
}
animationSpeedSlider = find("entityAnimationSpeedSlider", UISlider.class);
if (animationSpeedSlider != null) {
animationSpeedSlider.setMinimum(-0.0f);
animationSpeedSlider.setIncrement(0.1f);
animationSpeedSlider.setRange(10.0f);
animationSpeedSlider.setPrecision(1);
}
spawnEntityIdButton.subscribe(widget -> {
Vector3f localPlayerPosition = localPlayer.getPosition();
Quat4f localPlayerRotation = localPlayer.getRotation();
Vector3f offset = localPlayer.getViewDirection();
offset.scale(2.0f);
offset.y = 0;
localPlayerPosition.add(offset);
Optional<Prefab> prefab = assetManager.getAsset(entityDropdown.getSelection(), Prefab.class);
if (prefab.isPresent() && prefab.get().getComponent(LocationComponent.class) != null) {
entityRef = entityManager.create(prefab.get(), localPlayerPosition, localPlayerRotation);
SkeletalMeshComponent skeletalMeshComponent = entityRef.getComponent(SkeletalMeshComponent.class);
skeletalMeshComponent.animationRate = animationSpeedSlider.getValue();
entityRef.saveComponent(skeletalMeshComponent);
CharacterMovementComponent movementComponent = entityRef.getComponent(CharacterMovementComponent.class);
movementComponent.speedMultiplier = animationSpeedSlider.getValue();
entityRef.saveComponent(movementComponent);
}
});
}
use of org.terasology.entitySystem.prefab.Prefab in project Terasology by MovingBlocks.
the class HealthCommands method damageWithType.
@Command(shortDescription = "Reduce the player's health by an amount by a specific type of damage", runOnServer = true, requiredPermission = PermissionManager.CHEAT_PERMISSION)
public String damageWithType(@Sender EntityRef client, @CommandParam("damageType") String damageType, @CommandParam("amount") int amount) {
ClientComponent clientComp = client.getComponent(ClientComponent.class);
Prefab damageTypePrefab = prefabManager.getPrefab(damageType);
if (damageTypePrefab != null) {
clientComp.character.send(new DoDamageEvent(amount, damageTypePrefab, clientComp.character));
return "Inflicted " + amount + " damage of type " + damageType;
} else {
return "Specified damage type does not exist.";
}
}
use of org.terasology.entitySystem.prefab.Prefab in project Terasology by MovingBlocks.
the class BlockTypeEntityGenerator method generateBlockTypeEntity.
private void generateBlockTypeEntity(Block block) {
EntityBuilder builder = entityManager.newBuilder(blockTypePrefab);
builder.getComponent(BlockTypeComponent.class).block = block;
// TODO: Copy across settings as necessary
Optional<Prefab> prefab = block.getPrefab();
if (prefab.isPresent()) {
for (Component comp : prefab.get().iterateComponents()) {
if (!(comp instanceof NetworkComponent)) {
builder.addComponent(entityManager.getComponentLibrary().copy(comp));
}
}
}
block.setEntity(builder.build());
}
use of org.terasology.entitySystem.prefab.Prefab in project Terasology by MovingBlocks.
the class EntityAwareWorldProvider method cleanUpTemporaryEntity.
private void cleanUpTemporaryEntity(EntityRef entity) {
Prefab prefab = entity.getParentPrefab();
for (Component comp : entity.iterateComponents()) {
if (!COMMON_BLOCK_COMPONENTS.contains(comp.getClass()) && (prefab == null || !prefab.hasComponent(comp.getClass()))) {
entity.removeComponent(comp.getClass());
}
}
entity.removeComponent(NetworkComponent.class);
if (prefab != null) {
for (Component comp : prefab.iterateComponents()) {
Component currentComp = entity.getComponent(comp.getClass());
if (currentComp == null) {
entity.addComponent(entityManager.getComponentLibrary().copy(comp));
} else {
ComponentMetadata<?> metadata = entityManager.getComponentLibrary().getMetadata(comp.getClass());
boolean changed = false;
for (FieldMetadata field : metadata.getFields()) {
Object expected = field.getValue(comp);
if (!Objects.equal(expected, field.getValue(currentComp))) {
field.setValue(currentComp, expected);
changed = true;
}
}
if (changed) {
entity.saveComponent(currentComp);
}
}
}
}
entityManager.destroyEntityWithoutEvents(entity);
}
use of org.terasology.entitySystem.prefab.Prefab in project Terasology by MovingBlocks.
the class EntityAwareWorldProvider method updateBlockEntityComponents.
/**
* Transforms a block entity with the change of block type. This is driven from the delta between the old and new
* block type prefabs, but takes into account changes made to the block entity.
*
* @param blockEntity The entity to update
* @param oldType The previous type of the block
* @param type The new type of the block
*/
private void updateBlockEntityComponents(EntityRef blockEntity, Block oldType, Block type, Set<Class<? extends Component>> retainComponents) {
BlockComponent blockComponent = blockEntity.getComponent(BlockComponent.class);
Optional<Prefab> oldPrefab = oldType.getPrefab();
EntityBuilder oldEntityBuilder = entityManager.newBuilder(oldPrefab.orElse(null));
oldEntityBuilder.addComponent(new BlockComponent(oldType, new Vector3i(blockComponent.getPosition())));
BeforeEntityCreated oldEntityEvent = new BeforeEntityCreated(oldPrefab.orElse(null), oldEntityBuilder.iterateComponents());
blockEntity.send(oldEntityEvent);
for (Component comp : oldEntityEvent.getResultComponents()) {
oldEntityBuilder.addComponent(comp);
}
Optional<Prefab> newPrefab = type.getPrefab();
EntityBuilder newEntityBuilder = entityManager.newBuilder(newPrefab.orElse(null));
newEntityBuilder.addComponent(new BlockComponent(type, new Vector3i(blockComponent.getPosition())));
BeforeEntityCreated newEntityEvent = new BeforeEntityCreated(newPrefab.orElse(null), newEntityBuilder.iterateComponents());
blockEntity.send(newEntityEvent);
for (Component comp : newEntityEvent.getResultComponents()) {
newEntityBuilder.addComponent(comp);
}
for (Component component : blockEntity.iterateComponents()) {
if (!COMMON_BLOCK_COMPONENTS.contains(component.getClass()) && !entityManager.getComponentLibrary().getMetadata(component.getClass()).isRetainUnalteredOnBlockChange() && !newEntityBuilder.hasComponent(component.getClass()) && !retainComponents.contains(component.getClass())) {
blockEntity.removeComponent(component.getClass());
}
}
blockComponent.setBlock(type);
blockEntity.saveComponent(blockComponent);
for (Component comp : newEntityBuilder.iterateComponents()) {
copyIntoPrefab(blockEntity, comp, retainComponents);
}
}
Aggregations