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));
}
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);
}
}
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;
}
}
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()));
}
}
}
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);
}
}
}
}
Aggregations