use of org.terasology.entitySystem.entity.EntityBuilder in project Terasology by MovingBlocks.
the class FirstPersonClientSystem method ensureClientSideEntityOnHeldItemMountPoint.
// ensures held item mount point entity exists, attaches it to the camera and sets its transform
@ReceiveEvent
public void ensureClientSideEntityOnHeldItemMountPoint(OnActivatedComponent event, EntityRef camera, FirstPersonHeldItemMountPointComponent firstPersonHeldItemMountPointComponent) {
if (!firstPersonHeldItemMountPointComponent.mountPointEntity.exists()) {
EntityBuilder builder = entityManager.newBuilder("engine:FirstPersonHeldItemMountPoint");
builder.setPersistent(false);
firstPersonHeldItemMountPointComponent.mountPointEntity = builder.build();
camera.saveComponent(firstPersonHeldItemMountPointComponent);
}
// link the mount point entity to the camera
Location.removeChild(camera, firstPersonHeldItemMountPointComponent.mountPointEntity);
Location.attachChild(camera, firstPersonHeldItemMountPointComponent.mountPointEntity, firstPersonHeldItemMountPointComponent.translate, new Quat4f(TeraMath.DEG_TO_RAD * firstPersonHeldItemMountPointComponent.rotateDegrees.y, TeraMath.DEG_TO_RAD * firstPersonHeldItemMountPointComponent.rotateDegrees.x, TeraMath.DEG_TO_RAD * firstPersonHeldItemMountPointComponent.rotateDegrees.z), firstPersonHeldItemMountPointComponent.scale);
}
use of org.terasology.entitySystem.entity.EntityBuilder in project Terasology by MovingBlocks.
the class PlayerFactory method newInstance.
/**
* Creates a new player character entity. The desired spawning location is derived from
* the {@link LocationComponent} of the controller.
* @param controller the controlling client entity
* @return a new player character entity
*/
public EntityRef newInstance(EntityRef controller) {
EntityBuilder builder = entityManager.newBuilder("engine:player");
LocationComponent location = controller.getComponent(LocationComponent.class);
Vector3f spawnPosition = findSpawnPositionFromLocationComponent(location);
location.setWorldPosition(spawnPosition);
controller.saveComponent(location);
logger.debug("Spawing player at: {}", spawnPosition);
builder.getComponent(LocationComponent.class).setWorldPosition(spawnPosition);
builder.setOwner(controller);
CharacterComponent playerComponent = builder.getComponent(CharacterComponent.class);
playerComponent.controller = controller;
EntityRef player = builder.build();
Location.attachChild(player, controller, new Vector3f(), new Quat4f(0, 0, 0, 1));
return player;
}
use of org.terasology.entitySystem.entity.EntityBuilder in project Terasology by MovingBlocks.
the class BlockDropGrammarSystem method onDestroyed.
@ReceiveEvent
public void onDestroyed(DoDestroyEvent event, EntityRef entity, BlockDropGrammarComponent blockDrop, LocationComponent locationComp) {
BlockDamageModifierComponent blockDamageModifierComponent = event.getDamageType().getComponent(BlockDamageModifierComponent.class);
float chanceOfBlockDrop = 1;
if (blockDamageModifierComponent != null) {
chanceOfBlockDrop = 1 - blockDamageModifierComponent.blockAnnihilationChance;
}
if (random.nextFloat() < chanceOfBlockDrop) {
List<String> blockDrops = blockDrop.blockDrops;
List<String> itemDrops = blockDrop.itemDrops;
if (blockDamageModifierComponent != null && blockDrop.droppedWithTool != null) {
for (String toolType : blockDamageModifierComponent.materialDamageMultiplier.keySet()) {
if (blockDrop.droppedWithTool.containsKey(toolType)) {
BlockDropGrammarComponent.DropDefinition dropDefinition = blockDrop.droppedWithTool.get(toolType);
blockDrops = dropDefinition.blockDrops;
itemDrops = dropDefinition.itemDrops;
break;
}
}
}
if (blockDrops != null) {
for (String drop : blockDrops) {
String dropResult = drop;
boolean dropping = true;
int pipeIndex = dropResult.indexOf('|');
if (pipeIndex > -1) {
float chance = Float.parseFloat(dropResult.substring(0, pipeIndex));
if (random.nextFloat() >= chance) {
dropping = false;
}
dropResult = dropResult.substring(pipeIndex + 1);
}
if (dropping) {
DropParser dropParser = new DropParser(random, dropResult).invoke();
EntityRef dropItem = blockItemFactory.newInstance(blockManager.getBlockFamily(dropParser.getDrop()), dropParser.getCount());
if (shouldDropToWorld(event, blockDamageModifierComponent, dropItem)) {
createDrop(dropItem, locationComp.getWorldPosition(), true);
}
}
}
}
if (itemDrops != null) {
for (String drop : itemDrops) {
String dropResult = drop;
boolean dropping = true;
int pipeIndex = dropResult.indexOf('|');
if (pipeIndex > -1) {
float chance = Float.parseFloat(dropResult.substring(0, pipeIndex));
if (random.nextFloat() >= chance) {
dropping = false;
}
dropResult = dropResult.substring(pipeIndex + 1);
}
if (dropping) {
DropParser dropParser = new DropParser(random, dropResult).invoke();
EntityBuilder dropEntity = entityManager.newBuilder(dropParser.getDrop());
if (dropParser.getCount() > 1) {
ItemComponent itemComponent = dropEntity.getComponent(ItemComponent.class);
itemComponent.stackCount = (byte) dropParser.getCount();
}
EntityRef dropItem = dropEntity.build();
if (shouldDropToWorld(event, blockDamageModifierComponent, dropItem)) {
createDrop(dropItem, locationComp.getWorldPosition(), false);
}
}
}
}
}
}
use of org.terasology.entitySystem.entity.EntityBuilder in project Terasology by MovingBlocks.
the class ArrowAction method onActivate.
@ReceiveEvent
public void onActivate(ActivateEvent event, EntityRef entity, ArrowActionComponent arrowActionComponent) {
if (time.getGameTime() > lastTime + 1.0f / arrowActionComponent.arrowsPerSecond) {
Vector3f target = event.getHitNormal();
Vector3i blockPos = new Vector3i(target);
Vector3f position = new Vector3f(event.getOrigin());
Vector3f dir = new Vector3f(event.getDirection());
HitResult result;
result = physicsRenderer.rayTrace(position, dir, arrowActionComponent.maxDistance, filter);
Block currentBlock = worldProvider.getBlock(blockPos);
if (currentBlock.isDestructible()) {
EntityBuilder builder = entityManager.newBuilder("Core:defaultBlockParticles");
builder.getComponent(LocationComponent.class).setWorldPosition(target);
builder.build();
}
EntityRef blockEntity = result.getEntity();
blockEntity.send(new DoDamageEvent(arrowActionComponent.damageAmount, arrowActionComponent.damageType));
lastTime = time.getGameTime();
}
}
Aggregations