Search in sources :

Example 11 with BlockComponent

use of org.terasology.engine.world.block.BlockComponent in project Terasology by MovingBlocks.

the class EntityDestructionAuthoritySystem method recordDestroyed.

private void recordDestroyed(DestroyEvent event, EntityRef entityRef) {
    EntityRef instigator = event.getInstigator();
    if (instigator != null) {
        if (entityRef.hasComponent(BlockComponent.class)) {
            BlockComponent blockComponent = entityRef.getComponent(BlockComponent.class);
            String blockName = blockComponent.getBlock().getDisplayName();
            if (instigator.hasComponent(GamePlayStatsComponent.class)) {
                GamePlayStatsComponent gamePlayStatsComponent = instigator.getComponent(GamePlayStatsComponent.class);
                Map<String, Integer> blockDestroyedMap = gamePlayStatsComponent.blockDestroyedMap;
                if (blockDestroyedMap.containsKey(blockName)) {
                    blockDestroyedMap.put(blockName, blockDestroyedMap.get(blockName) + 1);
                } else {
                    blockDestroyedMap.put(blockName, 1);
                }
                instigator.saveComponent(gamePlayStatsComponent);
            } else {
                GamePlayStatsComponent gamePlayStatsComponent = new GamePlayStatsComponent();
                Map<String, Integer> blockDestroyedMap = gamePlayStatsComponent.blockDestroyedMap;
                blockDestroyedMap.put(blockName, 1);
                instigator.addOrSaveComponent(gamePlayStatsComponent);
            }
        } else if (entityRef.hasComponent(CharacterComponent.class)) {
            String creatureName = entityRef.getParentPrefab().getName();
            if (instigator.hasComponent(GamePlayStatsComponent.class)) {
                GamePlayStatsComponent gamePlayStatsComponent = instigator.getComponent(GamePlayStatsComponent.class);
                Map<String, Integer> creatureKilled = gamePlayStatsComponent.creatureKilled;
                if (creatureKilled.containsKey(creatureName)) {
                    creatureKilled.put(creatureName, creatureKilled.get(creatureName) + 1);
                } else {
                    creatureKilled.put(creatureName, 1);
                }
                instigator.saveComponent(gamePlayStatsComponent);
            } else {
                GamePlayStatsComponent gamePlayStatsComponent = new GamePlayStatsComponent();
                Map<String, Integer> creatureKilled = gamePlayStatsComponent.creatureKilled;
                creatureKilled.put(creatureName, 1);
                instigator.addOrSaveComponent(gamePlayStatsComponent);
            }
        }
    }
}
Also used : BlockComponent(org.terasology.engine.world.block.BlockComponent) GamePlayStatsComponent(org.terasology.engine.telemetry.GamePlayStatsComponent) CharacterComponent(org.terasology.engine.logic.characters.CharacterComponent) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) Map(java.util.Map)

Example 12 with BlockComponent

use of org.terasology.engine.world.block.BlockComponent in project Terasology by MovingBlocks.

the class NetClient method sendInitialEntities.

private void sendInitialEntities(NetData.NetMessage.Builder message) {
    int[] initial = netInitial.toArray();
    netInitial.clear();
    Arrays.sort(initial);
    for (int netId : initial) {
        netRelevant.add(netId);
        EntityRef entity = networkSystem.getEntity(netId);
        if (!entity.hasComponent(NetworkComponent.class)) {
            logger.error("Sending net entity with no network component: {} - {}", netId, entity);
            continue;
        }
        // Note: Send owner->server fields on initial create
        Client owner = networkSystem.getOwner(entity);
        EntityData.PackedEntity entityData = entitySerializer.serialize(entity, true, new ServerComponentFieldCheck(owner == this, true)).build();
        NetData.CreateEntityMessage.Builder createMessage = NetData.CreateEntityMessage.newBuilder().setEntity(entityData);
        BlockComponent blockComponent = entity.getComponent(BlockComponent.class);
        if (blockComponent != null) {
            createMessage.setBlockPos(NetMessageUtil.convert(blockComponent.getPosition()));
        }
        message.addCreateEntity(createMessage);
    }
}
Also used : BlockComponent(org.terasology.engine.world.block.BlockComponent) NetworkComponent(org.terasology.engine.network.NetworkComponent) EntityData(org.terasology.protobuf.EntityData) ServerComponentFieldCheck(org.terasology.engine.network.serialization.ServerComponentFieldCheck) Client(org.terasology.engine.network.Client) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef)

Example 13 with BlockComponent

use of org.terasology.engine.world.block.BlockComponent in project Terasology by MovingBlocks.

the class CameraTargetSystem method update.

public void update(float delta) {
    // Repair lost target
    // TODO: Improvements to temporary chunk handling will remove the need for this
    boolean lostTarget = false;
    updateTarget();
    if (!target.exists()) {
        targetBlockPos = null;
        lostTarget = true;
    }
    HitResult hitInfo = physics.rayTrace(localPlayer.getViewPosition(new Vector3f()), localPlayer.getViewDirection(new Vector3f()), targetDistance, filter);
    updateFocalDistance(hitInfo, delta);
    Vector3i newBlockPos = null;
    EntityRef newTarget = EntityRef.NULL;
    if (hitInfo.isHit()) {
        newTarget = hitInfo.getEntity();
        hitPosition = hitInfo.getHitPoint();
        hitNormal = hitInfo.getHitNormal();
        if (hitInfo.isWorldHit()) {
            newBlockPos = new Vector3i(hitInfo.getBlockPosition());
        }
    }
    if (!Objects.equal(target, newTarget) || lostTarget) {
        EntityRef oldTarget = target;
        oldTarget.send(new CameraOutEvent());
        newTarget.send(new CameraOverEvent());
        localPlayer.getCharacterEntity().send(new CameraTargetChangedEvent(oldTarget, newTarget));
        // Set isBlock to false if the hit-entity does not have a BlockComponent
        isBlock = !(isTargetAvailable() && !newTarget.hasComponent(BlockComponent.class));
    }
    target = newTarget;
    targetBlockPos = newBlockPos;
}
Also used : HitResult(org.terasology.engine.physics.HitResult) BlockComponent(org.terasology.engine.world.block.BlockComponent) Vector3f(org.joml.Vector3f) Vector3i(org.joml.Vector3i) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef)

Example 14 with BlockComponent

use of org.terasology.engine.world.block.BlockComponent in project Terasology by MovingBlocks.

the class NetEntityRefTypeHandler method serializeNonNull.

@Override
public PersistedData serializeNonNull(EntityRef value, PersistedDataSerializer serializer) {
    BlockComponent blockComponent = value.getComponent(BlockComponent.class);
    if (blockComponent != null) {
        Vector3ic pos = blockComponent.getPosition();
        return serializer.serialize(pos.x(), pos.y(), pos.z());
    }
    NetworkComponent netComponent = value.getComponent(NetworkComponent.class);
    if (netComponent != null) {
        return serializer.serialize(netComponent.getNetworkId());
    }
    return serializer.serializeNull();
}
Also used : BlockComponent(org.terasology.engine.world.block.BlockComponent) NetworkComponent(org.terasology.engine.network.NetworkComponent) Vector3ic(org.joml.Vector3ic)

Example 15 with BlockComponent

use of org.terasology.engine.world.block.BlockComponent in project Terasology by MovingBlocks.

the class NeighbourBlockFamilyUpdateSystem method onBlockPlaced.

/**
 * Notifies the adjacent block families when a block is placed next to them.
 * @param event
 * @param entity
 */
@ReceiveEvent
public void onBlockPlaced(OnBlockItemPlaced event, EntityRef entity) {
    BlockComponent blockComponent = event.getPlacedBlock().getComponent(BlockComponent.class);
    if (blockComponent == null) {
        return;
    }
    processUpdateForBlockLocation(blockComponent.getPosition());
}
Also used : BlockComponent(org.terasology.engine.world.block.BlockComponent) ReceiveEvent(org.terasology.engine.entitySystem.event.ReceiveEvent)

Aggregations

BlockComponent (org.terasology.engine.world.block.BlockComponent)15 EntityRef (org.terasology.engine.entitySystem.entity.EntityRef)8 NetworkComponent (org.terasology.engine.network.NetworkComponent)6 Vector3f (org.joml.Vector3f)4 Vector3i (org.joml.Vector3i)4 ReceiveEvent (org.terasology.engine.entitySystem.event.ReceiveEvent)4 LocationComponent (org.terasology.engine.logic.location.LocationComponent)4 Block (org.terasology.engine.world.block.Block)4 Vector3ic (org.joml.Vector3ic)2 EntityBuilder (org.terasology.engine.entitySystem.entity.EntityBuilder)2 Client (org.terasology.engine.network.Client)2 BlockFamily (org.terasology.engine.world.block.family.BlockFamily)2 BlockRegionComponent (org.terasology.engine.world.block.regions.BlockRegionComponent)2 Iterator (java.util.Iterator)1 Map (java.util.Map)1 Matrix4f (org.joml.Matrix4f)1 Quaternionf (org.joml.Quaternionf)1 Vector2f (org.joml.Vector2f)1 PlaySoundEvent (org.terasology.engine.audio.events.PlaySoundEvent)1 BeforeDeactivateComponent (org.terasology.engine.entitySystem.entity.lifecycleEvents.BeforeDeactivateComponent)1