Search in sources :

Example 91 with EntityRef

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

the class InteractionSystem method onActivationPredicted.

@ReceiveEvent(components = InteractionTargetComponent.class)
public void onActivationPredicted(ActivationPredicted event, EntityRef target) {
    EntityRef character = event.getInstigator();
    CharacterComponent characterComponent = character.getComponent(CharacterComponent.class);
    if (characterComponent == null) {
        return;
    }
    if (characterComponent.predictedInteractionTarget.exists()) {
        InteractionUtil.cancelInteractionAsClient(character);
    }
    if (target.exists()) {
        characterComponent.predictedInteractionTarget = target;
        characterComponent.predictedInteractionId = event.getActivationId();
        character.saveComponent(characterComponent);
        target.send(new InteractionStartPredicted(character));
    }
}
Also used : CharacterComponent(org.terasology.engine.logic.characters.CharacterComponent) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) ReceiveEvent(org.terasology.engine.entitySystem.event.ReceiveEvent)

Example 92 with EntityRef

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

the class InteractionSystem method onInteractionStartPredicted.

@ReceiveEvent(components = InteractionScreenComponent.class)
public void onInteractionStartPredicted(InteractionStartPredicted event, EntityRef container, InteractionScreenComponent interactionScreenComponent) {
    EntityRef investigator = event.getInstigator();
    CharacterComponent characterComponent = investigator.getComponent(CharacterComponent.class);
    if (characterComponent == null) {
        logger.error("Interaction start predicted for entity without character component");
        return;
    }
    ClientComponent controller = characterComponent.controller.getComponent(ClientComponent.class);
    if (controller != null && controller.local) {
        nuiManager.closeAllScreens();
        nuiManager.pushScreen(interactionScreenComponent.screen);
    }
}
Also used : CharacterComponent(org.terasology.engine.logic.characters.CharacterComponent) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) ClientComponent(org.terasology.engine.network.ClientComponent) ReceiveEvent(org.terasology.engine.entitySystem.event.ReceiveEvent)

Example 93 with EntityRef

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

the class InteractionUtil method cancelInteractionAsServer.

public static void cancelInteractionAsServer(EntityRef character) {
    CharacterComponent characterComponent = character.getComponent(CharacterComponent.class);
    if (characterComponent == null) {
        logger.error("Interaction end request instigator has no character component");
        return;
    }
    int oldInteractionId = characterComponent.authorizedInteractionId;
    EntityRef oldTarget = characterComponent.authorizedInteractionTarget;
    if (oldTarget.exists()) {
        characterComponent.authorizedInteractionTarget = EntityRef.NULL;
        character.saveComponent(characterComponent);
    }
    character.send(new InteractionEndEvent(oldInteractionId));
}
Also used : CharacterComponent(org.terasology.engine.logic.characters.CharacterComponent) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef)

Example 94 with EntityRef

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

the class InteractionUtil method cancelInteractionAsClient.

static void cancelInteractionAsClient(EntityRef character, boolean notifyServer) {
    CharacterComponent characterComponent = character.getComponent(CharacterComponent.class);
    if (characterComponent == null) {
        logger.error("Interaction instigator has no character component");
        return;
    }
    EntityRef oldTarget = characterComponent.predictedInteractionTarget;
    if (oldTarget.exists()) {
        characterComponent.predictedInteractionTarget = EntityRef.NULL;
        character.saveComponent(characterComponent);
        oldTarget.send(new InteractionEndPredicted(character));
        if (notifyServer) {
            character.send(new InteractionEndRequest());
        }
    }
}
Also used : CharacterComponent(org.terasology.engine.logic.characters.CharacterComponent) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef)

Example 95 with EntityRef

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

the class PhysicsSystem method update.

@Override
public void update(float delta) {
    PerformanceMonitor.startActivity("Physics Renderer");
    physics.update(time.getGameDelta());
    PerformanceMonitor.endActivity();
    // Update the velocity from physics engine bodies to Components:
    Iterator<EntityRef> iter = physics.physicsEntitiesIterator();
    while (iter.hasNext()) {
        EntityRef entity = iter.next();
        RigidBodyComponent comp = entity.getComponent(RigidBodyComponent.class);
        RigidBody body = physics.getRigidBody(entity);
        // force location component to update and sync trigger state
        if (entity.hasComponent(TriggerComponent.class)) {
            physics.updateTrigger(entity);
        }
        if (body.isActive()) {
            body.getLinearVelocity(comp.velocity);
            body.getAngularVelocity(comp.angularVelocity);
            Vector3f vLocation = body.getLocation(new Vector3f());
            Vector3f vDirection = new Vector3f(comp.velocity);
            float fDistanceThisFrame = vDirection.length();
            vDirection.normalize();
            fDistanceThisFrame = fDistanceThisFrame * delta;
            while (true) {
                HitResult hitInfo = physics.rayTrace(vLocation, vDirection, fDistanceThisFrame + 0.5f, DEFAULT_COLLISION_GROUP);
                if (hitInfo.isHit()) {
                    Block hitBlock = worldProvider.getBlock(hitInfo.getBlockPosition());
                    if (hitBlock != null) {
                        Vector3f vTravelledDistance = vLocation.sub(hitInfo.getHitPoint());
                        float fTravelledDistance = vTravelledDistance.length();
                        if (fTravelledDistance > fDistanceThisFrame) {
                            break;
                        }
                        if (hitBlock.isPenetrable()) {
                            if (!hitInfo.getEntity().hasComponent(BlockComponent.class)) {
                                entity.send(new EntityImpactEvent(hitInfo.getHitPoint(), hitInfo.getHitNormal(), comp.velocity, fDistanceThisFrame, hitInfo.getEntity()));
                                break;
                            }
                            // decrease the remaining distance to check if we hit a block
                            fDistanceThisFrame = fDistanceThisFrame - fTravelledDistance;
                            vLocation = hitInfo.getHitPoint();
                        } else {
                            entity.send(new BlockImpactEvent(hitInfo.getHitPoint(), hitInfo.getHitNormal(), comp.velocity, fDistanceThisFrame, hitInfo.getEntity()));
                            break;
                        }
                    } else {
                        break;
                    }
                } else {
                    break;
                }
            }
        }
    }
    if (networkSystem.getMode().isServer() && time.getGameTimeInMs() - TIME_BETWEEN_NETSYNCS > lastNetsync) {
        sendSyncMessages();
        lastNetsync = time.getGameTimeInMs();
    }
    List<CollisionPair> collisionPairs = physics.getCollisionPairs();
    for (CollisionPair pair : collisionPairs) {
        if (pair.b.exists()) {
            short bCollisionGroup = getCollisionGroupFlag(pair.b);
            short aCollidesWith = getCollidesWithGroupFlag(pair.a);
            if ((bCollisionGroup & aCollidesWith) != 0 || (pair.b.hasComponent(BlockComponent.class) && !pair.a.hasComponent(BlockComponent.class))) {
                pair.a.send(new CollideEvent(pair.b, pair.pointA, pair.pointB, pair.distance, pair.normal));
            }
        }
        if (pair.a.exists()) {
            short aCollisionGroup = getCollisionGroupFlag(pair.a);
            short bCollidesWith = getCollidesWithGroupFlag(pair.b);
            if ((aCollisionGroup & bCollidesWith) != 0 || (pair.a.hasComponent(BlockComponent.class) && !pair.b.hasComponent(BlockComponent.class))) {
                pair.b.send(new CollideEvent(pair.a, pair.pointB, pair.pointA, pair.distance, new Vector3f(pair.normal).mul(-1.0f)));
            }
        }
    }
}
Also used : RigidBodyComponent(org.terasology.engine.physics.components.RigidBodyComponent) HitResult(org.terasology.engine.physics.HitResult) BlockComponent(org.terasology.engine.world.block.BlockComponent) CollideEvent(org.terasology.engine.physics.events.CollideEvent) Vector3f(org.joml.Vector3f) BlockImpactEvent(org.terasology.engine.physics.events.BlockImpactEvent) OnChangedBlock(org.terasology.engine.world.OnChangedBlock) Block(org.terasology.engine.world.block.Block) EntityImpactEvent(org.terasology.engine.physics.events.EntityImpactEvent) 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