Search in sources :

Example 1 with ActivationRequest

use of org.terasology.logic.characters.events.ActivationRequest in project Terasology by MovingBlocks.

the class LocalPlayer method activateTargetOrOwnedEntity.

/**
 * @param usedOwnedEntity if it does not exist it is not an item usage.
 * @return true if an activation request got sent. Returns always true if usedItem exists.
 */
private boolean activateTargetOrOwnedEntity(EntityRef usedOwnedEntity) {
    EntityRef character = getCharacterEntity();
    CharacterComponent characterComponent = character.getComponent(CharacterComponent.class);
    Vector3f direction = getViewDirection();
    Vector3f originPos = getViewPosition();
    boolean ownedEntityUsage = usedOwnedEntity.exists();
    int activationId = nextActivationId++;
    Physics physics = CoreRegistry.get(Physics.class);
    HitResult result = physics.rayTrace(originPos, direction, characterComponent.interactionRange, Sets.newHashSet(character), CharacterSystem.DEFAULTPHYSICSFILTER);
    boolean eventWithTarget = result.isHit();
    if (eventWithTarget) {
        EntityRef activatedObject = usedOwnedEntity.exists() ? usedOwnedEntity : result.getEntity();
        activatedObject.send(new ActivationPredicted(character, result.getEntity(), originPos, direction, result.getHitPoint(), result.getHitNormal(), activationId));
        character.send(new ActivationRequest(character, ownedEntityUsage, usedOwnedEntity, eventWithTarget, result.getEntity(), originPos, direction, result.getHitPoint(), result.getHitNormal(), activationId));
        return true;
    } else if (ownedEntityUsage) {
        usedOwnedEntity.send(new ActivationPredicted(character, EntityRef.NULL, originPos, direction, originPos, new Vector3f(), activationId));
        character.send(new ActivationRequest(character, ownedEntityUsage, usedOwnedEntity, eventWithTarget, EntityRef.NULL, originPos, direction, originPos, new Vector3f(), activationId));
        return true;
    }
    return false;
}
Also used : HitResult(org.terasology.physics.HitResult) ActivationPredicted(org.terasology.logic.characters.events.ActivationPredicted) ActivationRequest(org.terasology.logic.characters.events.ActivationRequest) Vector3f(org.terasology.math.geom.Vector3f) CharacterComponent(org.terasology.logic.characters.CharacterComponent) EntityRef(org.terasology.entitySystem.entity.EntityRef) Physics(org.terasology.physics.Physics)

Aggregations

EntityRef (org.terasology.entitySystem.entity.EntityRef)1 CharacterComponent (org.terasology.logic.characters.CharacterComponent)1 ActivationPredicted (org.terasology.logic.characters.events.ActivationPredicted)1 ActivationRequest (org.terasology.logic.characters.events.ActivationRequest)1 Vector3f (org.terasology.math.geom.Vector3f)1 HitResult (org.terasology.physics.HitResult)1 Physics (org.terasology.physics.Physics)1