use of org.terasology.logic.location.LocationComponent in project Terasology by MovingBlocks.
the class LocalPlayerSystem method onTargetChanged.
@ReceiveEvent
public void onTargetChanged(PlayerTargetChangedEvent event, EntityRef entity) {
EntityRef target = event.getNewTarget();
if (target.exists()) {
LocationComponent location = target.getComponent(LocationComponent.class);
if (location != null) {
BlockComponent blockComp = target.getComponent(BlockComponent.class);
BlockRegionComponent blockRegion = target.getComponent(BlockRegionComponent.class);
if (blockComp != null || blockRegion != null) {
Vector3f blockPos = location.getWorldPosition();
Block block = worldProvider.getBlock(blockPos);
aabb = block.getBounds(blockPos);
} else {
MeshComponent mesh = target.getComponent(MeshComponent.class);
if (mesh != null && mesh.mesh != null) {
aabb = mesh.mesh.getAABB();
aabb = aabb.transform(location.getWorldRotation(), location.getWorldPosition(), location.getWorldScale());
}
}
}
} else {
aabb = null;
}
}
use of org.terasology.logic.location.LocationComponent in project Terasology by MovingBlocks.
the class PlayerSystem method respawnPlayer.
private void respawnPlayer(EntityRef clientEntity) {
ClientComponent client = clientEntity.getComponent(ClientComponent.class);
EntityRef playerCharacter = client.character;
LocationComponent location = clientEntity.getComponent(LocationComponent.class);
PlayerFactory playerFactory = new PlayerFactory(entityManager, worldProvider);
Vector3f spawnPosition = playerFactory.findSpawnPositionFromLocationComponent(location);
location.setWorldPosition(spawnPosition);
clientEntity.saveComponent(location);
playerCharacter.addComponent(new AliveCharacterComponent());
playerCharacter.send(new CharacterTeleportEvent(spawnPosition));
logger.debug("Re-spawing player at: {}", spawnPosition);
Client clientListener = networkSystem.getOwner(clientEntity);
Vector3i distance = clientListener.getViewDistance().getChunkDistance();
updateRelevanceEntity(clientEntity, distance);
playerCharacter.send(new OnPlayerRespawnedEvent());
}
use of org.terasology.logic.location.LocationComponent in project Terasology by MovingBlocks.
the class CharacterMovementSystemUtility method extrapolateLocationComponent.
private void extrapolateLocationComponent(EntityRef entity, CharacterStateEvent state, Vector3f newPos) {
LocationComponent location = entity.getComponent(LocationComponent.class);
location.setWorldPosition(newPos);
location.setWorldRotation(state.getRotation());
entity.saveComponent(location);
}
use of org.terasology.logic.location.LocationComponent 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.logic.location.LocationComponent 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()));
}
}
}
Aggregations