use of org.terasology.engine.logic.location.LocationComponent in project Terasology by MovingBlocks.
the class EntityMotionState method getWorldTransform.
@Override
public void getWorldTransform(Matrix4f transform) {
LocationComponent loc = entity.getComponent(LocationComponent.class);
transform.translationRotateScale(loc.getWorldPosition(position), loc.getWorldRotation(rot), loc.getWorldScale());
}
use of org.terasology.engine.logic.location.LocationComponent in project Terasology by MovingBlocks.
the class EntityAwareWorldProvider method createBlockEntity.
private EntityRef createBlockEntity(Vector3ic blockPosition, Block block) {
EntityBuilder builder = entityManager.newBuilder(block.getPrefab().orElse(null));
builder.addComponent(new LocationComponent(new Vector3f(blockPosition)));
builder.addComponent(new BlockComponent(block, blockPosition));
boolean isTemporary = isTemporaryBlock(builder, block);
if (!isTemporary && !builder.hasComponent(NetworkComponent.class)) {
builder.addComponent(new NetworkComponent());
}
EntityRef blockEntity;
if (isTemporary) {
blockEntity = builder.buildWithoutLifecycleEvents();
temporaryBlockEntities.add(blockEntity);
} else {
blockEntity = builder.build();
}
blockEntityLookup.put(new Vector3i(blockPosition), blockEntity);
return blockEntity;
}
use of org.terasology.engine.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(new Vector3f());
Vector3f originPos = gazeLocation.getWorldPosition(new Vector3f());
if (recordAndReplayCurrentStatus.getStatus() == RecordAndReplayStatus.RECORDING) {
directionAndOriginPosRecorderList.getAttackEventDirectionAndOriginPosRecorder().add(direction, originPos);
} else if (recordAndReplayCurrentStatus.getStatus() == RecordAndReplayStatus.REPLAYING) {
Vector3f[] data = directionAndOriginPosRecorderList.getAttackEventDirectionAndOriginPosRecorder().poll();
direction = data[0];
originPos = data[1];
}
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.engine.logic.location.LocationComponent in project Terasology by MovingBlocks.
the class CharacterSystem method isPredictionOfEventCorrect.
private boolean isPredictionOfEventCorrect(EntityRef character, ActivationRequest event) {
CharacterComponent characterComponent = character.getComponent(CharacterComponent.class);
EntityRef camera = GazeAuthoritySystem.getGazeEntityForCharacter(character);
LocationComponent location = camera.getComponent(LocationComponent.class);
Vector3f direction = location.getWorldDirection(new Vector3f());
if (!(event.getDirection().equals(direction, 0.0001f))) {
logger.error("Direction at client {} was different than direction at server {}", event.getDirection(), direction);
}
// Assume the exact same value in case there are rounding mistakes:
direction = event.getDirection();
Vector3f originPos = location.getWorldPosition(new Vector3f());
if (!(event.getOrigin().equals(originPos, 0.0001f))) {
String msg = "Player {} seems to have cheated: It stated that it performed an action from {} but the predicted position is {}";
logger.info(msg, getPlayerNameFromCharacter(character), event.getOrigin(), originPos);
return false;
}
if (event.isOwnedEntityUsage()) {
if (!event.getUsedOwnedEntity().exists()) {
String msg = "Denied activation attempt by {} since the used entity does not exist on the authority";
logger.info(msg, getPlayerNameFromCharacter(character));
return false;
}
if (!networkSystem.getOwnerEntity(event.getUsedOwnedEntity()).equals(networkSystem.getOwnerEntity(character))) {
String msg = "Denied activation attempt by {} since it does not own the entity at the authority";
logger.info(msg, getPlayerNameFromCharacter(character));
return false;
}
} else {
// check for cheats so that data can later be trusted:
if (event.getUsedOwnedEntity().exists()) {
String msg = "Denied activation attempt by {} since it is not properly marked as owned entity usage";
logger.info(msg, getPlayerNameFromCharacter(character));
return false;
}
}
if (event.isEventWithTarget()) {
if (!event.getTarget().exists()) {
String msg = "Denied activation attempt by {} since the target does not exist on the authority";
logger.info(msg, getPlayerNameFromCharacter(character));
// can happen if target existed on client
return false;
}
// FIXME This is the same code as in LocalPlayer#activateTargetOrOwnedEntity to derive the actual interaction range from the
// player's character component and the used item's range component...
float interactionRange;
if (event.isOwnedEntityUsage() && event.getUsedOwnedEntity().hasComponent(RangeComponent.class)) {
interactionRange = Math.max(event.getUsedOwnedEntity().getComponent(RangeComponent.class).range, characterComponent.interactionRange);
} else {
interactionRange = characterComponent.interactionRange;
}
HitResult result = physics.rayTrace(originPos, direction, interactionRange, Sets.newHashSet(character), DEFAULTPHYSICSFILTER);
if (!result.isHit()) {
String msg = "Denied activation attempt by {} since at the authority there was nothing to activate at that place";
logger.info(msg, getPlayerNameFromCharacter(character));
return false;
}
EntityRef hitEntity = result.getEntity();
if (!hitEntity.equals(event.getTarget())) {
/**
* Tip for debugging this issue: Obtain the network id of hit entity and search it in both client and
* server entity dump. When certain fields don't get replicated, then wrong entity might get hin in the
* hit test.
*/
String msg = "Denied activation attempt by {} since at the authority another entity would have been activated";
logger.info(msg, getPlayerNameFromCharacter(character));
return false;
}
if (!(event.getHitPosition().equals(result.getHitPoint(), 0.0001f))) {
String msg = "Denied activation attempt by {} since at the authority the object got hit at a differnt position";
logger.info(msg, getPlayerNameFromCharacter(character));
return false;
}
} else {
// In order to trust the data later we need to verify it even if it should be correct if no one cheats:
if (event.getTarget().exists()) {
String msg = "Denied activation attempt by {} since the event was not properly labeled as having a target";
logger.info(msg, getPlayerNameFromCharacter(character));
return false;
}
if (event.getHitPosition() != null) {
String msg = "Denied activation attempt by {} since the event was not properly labeled as having a hit position";
logger.info(msg, getPlayerNameFromCharacter(character));
return false;
}
if (event.getHitNormal() != null) {
String msg = "Denied activation attempt by {} since the event was not properly labeled as having a hit delta";
logger.info(msg, getPlayerNameFromCharacter(character));
return false;
}
}
return true;
}
use of org.terasology.engine.logic.location.LocationComponent in project Terasology by MovingBlocks.
the class KinematicCharacterMover method followToParent.
private void followToParent(final CharacterStateEvent state, EntityRef entity) {
LocationComponent locationComponent = entity.getComponent(LocationComponent.class);
if (!locationComponent.getParent().equals(EntityRef.NULL)) {
Vector3f position = locationComponent.getWorldPosition(new Vector3f());
Vector3f velocity = new Vector3f(position);
velocity.sub(state.getPosition());
state.getVelocity().set(velocity);
state.getPosition().set(position);
}
}
Aggregations