Search in sources :

Example 11 with CharacterComponent

use of org.terasology.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();
    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)

Example 12 with CharacterComponent

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

Aggregations

CharacterComponent (org.terasology.logic.characters.CharacterComponent)12 EntityRef (org.terasology.entitySystem.entity.EntityRef)11 Vector3f (org.terasology.math.geom.Vector3f)4 ReceiveEvent (org.terasology.entitySystem.event.ReceiveEvent)3 Quat4f (org.terasology.math.geom.Quat4f)2 ClientComponent (org.terasology.network.ClientComponent)2 ArrayList (java.util.ArrayList)1 ResourceUrn (org.terasology.assets.ResourceUrn)1 EntityBuilder (org.terasology.entitySystem.entity.EntityBuilder)1 AliveCharacterComponent (org.terasology.logic.characters.AliveCharacterComponent)1 ActivationPredicted (org.terasology.logic.characters.events.ActivationPredicted)1 ActivationRequest (org.terasology.logic.characters.events.ActivationRequest)1 InventoryComponent (org.terasology.logic.inventory.InventoryComponent)1 InventoryManager (org.terasology.logic.inventory.InventoryManager)1 LocationComponent (org.terasology.logic.location.LocationComponent)1 FirstPersonHeldItemMountPointComponent (org.terasology.logic.players.FirstPersonHeldItemMountPointComponent)1 LocalPlayer (org.terasology.logic.players.LocalPlayer)1 PlayerTargetChangedEvent (org.terasology.logic.players.PlayerTargetChangedEvent)1 Client (org.terasology.network.Client)1 HitResult (org.terasology.physics.HitResult)1