Search in sources :

Example 6 with CharacterComponent

use of org.terasology.engine.logic.characters.CharacterComponent 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 7 with CharacterComponent

use of org.terasology.engine.logic.characters.CharacterComponent 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 8 with CharacterComponent

use of org.terasology.engine.logic.characters.CharacterComponent 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(new Vector3f());
    Vector3f originPos = getViewPosition(new Vector3f());
    if (recordAndReplayCurrentStatus.getStatus() == RecordAndReplayStatus.RECORDING) {
        this.directionAndOriginPosRecorderList.getTargetOrOwnedEntityDirectionAndOriginPosRecorder().add(direction, originPos);
    } else if (recordAndReplayCurrentStatus.getStatus() == RecordAndReplayStatus.REPLAYING) {
        Vector3f[] data = this.directionAndOriginPosRecorderList.getTargetOrOwnedEntityDirectionAndOriginPosRecorder().poll();
        direction = data[0];
        originPos = data[1];
    }
    boolean ownedEntityUsage = usedOwnedEntity.exists();
    int activationId = nextActivationId++;
    Physics physics = CoreRegistry.get(Physics.class);
    // FIXME This is the same code as in CharacterSystem#isPredictionOfEventCorrect to derive the actual interaction range from the
    // player's character component and the used item's range component...
    float interactionRange;
    if (ownedEntityUsage && usedOwnedEntity.hasComponent(RangeComponent.class)) {
        interactionRange = Math.max(usedOwnedEntity.getComponent(RangeComponent.class).range, characterComponent.interactionRange);
    } else {
        interactionRange = characterComponent.interactionRange;
    }
    HitResult result = physics.rayTrace(originPos, direction, interactionRange, Sets.newHashSet(character), CharacterSystem.DEFAULTPHYSICSFILTER);
    boolean eventWithTarget = result.isHit();
    if (ownedEntityUsage || 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;
    }
    return false;
}
Also used : HitResult(org.terasology.engine.physics.HitResult) ActivationPredicted(org.terasology.engine.logic.characters.events.ActivationPredicted) ActivationRequest(org.terasology.engine.logic.characters.events.ActivationRequest) RangeComponent(org.terasology.engine.logic.common.RangeComponent) Vector3f(org.joml.Vector3f) CharacterComponent(org.terasology.engine.logic.characters.CharacterComponent) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) Physics(org.terasology.engine.physics.Physics)

Example 9 with CharacterComponent

use of org.terasology.engine.logic.characters.CharacterComponent 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 Quaternionf(0, 0, 0, 1));
    return player;
}
Also used : Vector3f(org.joml.Vector3f) Quaternionf(org.joml.Quaternionf) CharacterComponent(org.terasology.engine.logic.characters.CharacterComponent) EntityBuilder(org.terasology.engine.entitySystem.entity.EntityBuilder) LocationComponent(org.terasology.engine.logic.location.LocationComponent) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef)

Example 10 with CharacterComponent

use of org.terasology.engine.logic.characters.CharacterComponent in project Terasology by MovingBlocks.

the class InteractionUtil method getActiveInteractionScreenUri.

/**
 * @return the active interaction screen uri of the specified character.
 * The method returns null if the player has no interaction screen open.
 * The method is only intended to be called for the own character.
 */
public static ResourceUrn getActiveInteractionScreenUri(EntityRef character) {
    CharacterComponent characterComponent = character.getComponent(CharacterComponent.class);
    if (characterComponent == null) {
        return null;
    }
    EntityRef interactionTarget = characterComponent.predictedInteractionTarget;
    if (!interactionTarget.exists()) {
        return null;
    }
    InteractionScreenComponent screenComponent = interactionTarget.getComponent(InteractionScreenComponent.class);
    if (screenComponent == null) {
        return null;
    }
    return new ResourceUrn(screenComponent.screen);
}
Also used : CharacterComponent(org.terasology.engine.logic.characters.CharacterComponent) ResourceUrn(org.terasology.gestalt.assets.ResourceUrn) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef)

Aggregations

CharacterComponent (org.terasology.engine.logic.characters.CharacterComponent)12 EntityRef (org.terasology.engine.entitySystem.entity.EntityRef)11 Vector3f (org.joml.Vector3f)4 ReceiveEvent (org.terasology.engine.entitySystem.event.ReceiveEvent)3 Quaternionf (org.joml.Quaternionf)2 ClientComponent (org.terasology.engine.network.ClientComponent)2 EntityBuilder (org.terasology.engine.entitySystem.entity.EntityBuilder)1 AliveCharacterComponent (org.terasology.engine.logic.characters.AliveCharacterComponent)1 ActivationPredicted (org.terasology.engine.logic.characters.events.ActivationPredicted)1 ActivationRequest (org.terasology.engine.logic.characters.events.ActivationRequest)1 RangeComponent (org.terasology.engine.logic.common.RangeComponent)1 LocationComponent (org.terasology.engine.logic.location.LocationComponent)1 FirstPersonHeldItemMountPointComponent (org.terasology.engine.logic.players.FirstPersonHeldItemMountPointComponent)1 LocalPlayer (org.terasology.engine.logic.players.LocalPlayer)1 PlayerTargetChangedEvent (org.terasology.engine.logic.players.PlayerTargetChangedEvent)1 Client (org.terasology.engine.network.Client)1 HitResult (org.terasology.engine.physics.HitResult)1 Physics (org.terasology.engine.physics.Physics)1 ResourceUrn (org.terasology.gestalt.assets.ResourceUrn)1