Search in sources :

Example 41 with EntityRef

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

the class LocalPlayerBlockSelectionByItemSystem method onCamTargetChanged.

@ReceiveEvent(components = { LocationComponent.class })
public void onCamTargetChanged(CameraTargetChangedEvent event, EntityRef entity) {
    // This method will update the block selection to whatever block is targeted in the players view
    if (null == blockSelectionComponentEntity) {
        return;
    }
    BlockSelectionComponent blockSelectionComponent = blockSelectionComponentEntity.getComponent(BlockSelectionComponent.class);
    if (blockSelectionComponent.startPosition == null) {
        return;
    }
    EntityRef target = event.getNewTarget();
    target.send(new SetBlockSelectionEndingPointEvent(blockSelectionComponentEntity));
}
Also used : SetBlockSelectionEndingPointEvent(org.terasology.world.selection.event.SetBlockSelectionEndingPointEvent) BlockSelectionComponent(org.terasology.world.selection.BlockSelectionComponent) EntityRef(org.terasology.entitySystem.entity.EntityRef) ReceiveEvent(org.terasology.entitySystem.event.ReceiveEvent)

Example 42 with EntityRef

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

the class CharacterMovementSystemUtility method setToState.

/**
 * Sets the state of the given entity to the state represented by the
 * CharacterStateEvent. The state of the entity is determined by its
 * LocationComponent (location and orientation of physics body),
 * CharacterMovementComponent (velocity and various movement state
 * variables) and CharacterComponent for pitch and yaw (used for the camera).
 *
 * @param entity
 * @param state
 */
public void setToState(EntityRef entity, CharacterStateEvent state) {
    LocationComponent location = entity.getComponent(LocationComponent.class);
    CharacterMovementComponent movementComp = entity.getComponent(CharacterMovementComponent.class);
    if (location == null || movementComp == null) {
        return;
    }
    location.setWorldPosition(state.getPosition());
    location.setWorldRotation(state.getRotation());
    entity.saveComponent(location);
    movementComp.mode = state.getMode();
    movementComp.setVelocity(state.getVelocity());
    movementComp.grounded = state.isGrounded();
    movementComp.footstepDelta = state.getFootstepDelta();
    entity.saveComponent(movementComp);
    setPhysicsLocation(entity, state.getPosition());
    // set the pitch to the character's gaze entity
    Quat4f rotation = new Quat4f(0f, TeraMath.DEG_TO_RAD * state.getPitch(), 0f);
    EntityRef gazeEntity = GazeAuthoritySystem.getGazeEntityForCharacter(entity);
    if (!gazeEntity.equals(entity)) {
        // Only set the gaze entity rotation if it is not the same as the main entity.
        // The character is assumed to only rotate side to side, introducing pitch makes things act strangely
        LocationComponent gazeLocation = gazeEntity.getComponent(LocationComponent.class);
        gazeLocation.setLocalRotation(rotation);
        gazeEntity.saveComponent(gazeLocation);
    }
}
Also used : LocationComponent(org.terasology.logic.location.LocationComponent) EntityRef(org.terasology.entitySystem.entity.EntityRef) Quat4f(org.terasology.math.geom.Quat4f) BaseQuat4f(org.terasology.math.geom.BaseQuat4f)

Example 43 with EntityRef

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

the class CharacterSystem method getInstigatorName.

/**
 * Extracts the name from an entity.
 * If the entity is a character, then the display name from the {@link ClientComponent#clientInfo} is used.
 * Otherwise the entity itself is checked for a {@link DisplayNameComponent}.
 * In the last case, the prefab name of the entity is used, e.g. "engine:player" will be parsed to "Player".
 * @param instigator The entity for which an instigator name is needed.
 * @return The instigator name.
 */
public String getInstigatorName(EntityRef instigator) {
    if (instigator.hasComponent(CharacterComponent.class)) {
        EntityRef instigatorClient = instigator.getComponent(CharacterComponent.class).controller;
        EntityRef instigatorClientInfo = instigatorClient.getComponent(ClientComponent.class).clientInfo;
        DisplayNameComponent displayNameComponent = instigatorClientInfo.getComponent(DisplayNameComponent.class);
        return displayNameComponent.name;
    } else if (instigator.getParentPrefab() != null) {
        // A DisplayName can be specified in the entity prefab
        // Otherwise, the game will attempt to generate one from the name of that prefab
        Prefab parentPrefab = instigator.getParentPrefab();
        if (parentPrefab.hasComponent(DisplayNameComponent.class)) {
            DisplayNameComponent displayNameComponent = parentPrefab.getComponent(DisplayNameComponent.class);
            return displayNameComponent.name;
        } else {
            String instigatorName = parentPrefab.getName();
            // getParentPrefab.getName() returns a ResourceUrn String such as "engine:player"
            // The following calls change the damage type to be more readable
            // For instance, "engine:player" becomes "Player"
            instigatorName = instigatorName.replaceAll(".*:(.*)", "$1");
            instigatorName = Character.toUpperCase(instigatorName.charAt(0)) + instigatorName.substring(1);
            return instigatorName;
        }
    } else {
        return null;
    }
}
Also used : DisplayNameComponent(org.terasology.logic.common.DisplayNameComponent) PlayerCharacterComponent(org.terasology.logic.players.PlayerCharacterComponent) EntityRef(org.terasology.entitySystem.entity.EntityRef) ClientComponent(org.terasology.network.ClientComponent) Prefab(org.terasology.entitySystem.prefab.Prefab)

Example 44 with EntityRef

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

the class CharacterSystem method onAttackRequest.

@ReceiveEvent(components = LocationComponent.class, netFilter = RegisterMode.AUTHORITY)
public void onAttackRequest(AttackRequest event, EntityRef character, CharacterComponent characterComponent) {
    // if an item is used,  make sure this entity is allowed to attack with it
    if (event.getItem().exists()) {
        if (!character.equals(event.getItem().getOwner())) {
            return;
        }
    }
    OnItemUseEvent onItemUseEvent = new OnItemUseEvent();
    character.send(onItemUseEvent);
    if (!onItemUseEvent.isConsumed()) {
        EntityRef gazeEntity = GazeAuthoritySystem.getGazeEntityForCharacter(character);
        LocationComponent gazeLocation = gazeEntity.getComponent(LocationComponent.class);
        Vector3f direction = gazeLocation.getWorldDirection();
        Vector3f originPos = gazeLocation.getWorldPosition();
        HitResult result = physics.rayTrace(originPos, direction, characterComponent.interactionRange, Sets.newHashSet(character), DEFAULTPHYSICSFILTER);
        if (result.isHit()) {
            result.getEntity().send(new AttackEvent(character, event.getItem()));
        }
    }
}
Also used : HitResult(org.terasology.physics.HitResult) OnItemUseEvent(org.terasology.logic.characters.events.OnItemUseEvent) Vector3f(org.terasology.math.geom.Vector3f) EntityRef(org.terasology.entitySystem.entity.EntityRef) LocationComponent(org.terasology.logic.location.LocationComponent) AttackEvent(org.terasology.logic.characters.events.AttackEvent) ReceiveEvent(org.terasology.entitySystem.event.ReceiveEvent)

Example 45 with EntityRef

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

the class CharacterSystem method update.

@Override
public void update(float delta) {
    Iterable<EntityRef> characterEntities = entityManager.getEntitiesWith(CharacterComponent.class, LocationComponent.class);
    for (EntityRef characterEntity : characterEntities) {
        CharacterComponent characterComponent = characterEntity.getComponent(CharacterComponent.class);
        if (characterComponent == null) {
            // could have changed during events below
            continue;
        }
        LocationComponent characterLocation = characterEntity.getComponent(LocationComponent.class);
        if (characterLocation == null) {
            // could have changed during events below
            continue;
        }
        EntityRef target = characterComponent.authorizedInteractionTarget;
        if (target.isActive()) {
            LocationComponent targetLocation = target.getComponent(LocationComponent.class);
            if (targetLocation == null) {
                // could have changed during events below
                continue;
            }
            float maxInteractionRange = characterComponent.interactionRange;
            if (isDistanceToLarge(characterLocation, targetLocation, maxInteractionRange)) {
                InteractionUtil.cancelInteractionAsServer(characterEntity);
            }
        }
    }
}
Also used : PlayerCharacterComponent(org.terasology.logic.players.PlayerCharacterComponent) EntityRef(org.terasology.entitySystem.entity.EntityRef) LocationComponent(org.terasology.logic.location.LocationComponent)

Aggregations

EntityRef (org.terasology.entitySystem.entity.EntityRef)337 Test (org.junit.Test)106 ClientComponent (org.terasology.network.ClientComponent)49 LocationComponent (org.terasology.logic.location.LocationComponent)45 Vector3f (org.terasology.math.geom.Vector3f)44 ReceiveEvent (org.terasology.entitySystem.event.ReceiveEvent)36 Vector3i (org.terasology.math.geom.Vector3i)34 Command (org.terasology.logic.console.commandSystem.annotations.Command)28 StringComponent (org.terasology.entitySystem.stubs.StringComponent)26 NetworkComponent (org.terasology.network.NetworkComponent)21 EntityData (org.terasology.protobuf.EntityData)21 DisplayNameComponent (org.terasology.logic.common.DisplayNameComponent)17 Block (org.terasology.world.block.Block)16 Component (org.terasology.entitySystem.Component)15 EntityBuilder (org.terasology.entitySystem.entity.EntityBuilder)15 CharacterComponent (org.terasology.logic.characters.CharacterComponent)14 Quat4f (org.terasology.math.geom.Quat4f)14 BlockComponent (org.terasology.world.block.BlockComponent)13 Map (java.util.Map)11 LocalPlayer (org.terasology.logic.players.LocalPlayer)11