Search in sources :

Example 26 with EntityRef

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

the class PhysicsSystem method update.

@Override
public void update(float delta) {
    PerformanceMonitor.startActivity("Physics Renderer");
    physics.update(time.getGameDelta());
    PerformanceMonitor.endActivity();
    // Update the velocity from physics engine bodies to Components:
    Iterator<EntityRef> iter = physics.physicsEntitiesIterator();
    while (iter.hasNext()) {
        EntityRef entity = iter.next();
        RigidBodyComponent comp = entity.getComponent(RigidBodyComponent.class);
        RigidBody body = physics.getRigidBody(entity);
        if (body.isActive()) {
            body.getLinearVelocity(comp.velocity);
            body.getAngularVelocity(comp.angularVelocity);
            Vector3f vLocation = Vector3f.zero();
            body.getLocation(vLocation);
            Vector3f vDirection = new Vector3f(comp.velocity);
            float fDistanceThisFrame = vDirection.length();
            vDirection.normalize();
            fDistanceThisFrame = fDistanceThisFrame * delta;
            while (true) {
                HitResult hitInfo = physics.rayTrace(vLocation, vDirection, fDistanceThisFrame + 0.5f, DEFAULT_COLLISION_GROUP);
                if (hitInfo.isHit()) {
                    Block hitBlock = worldProvider.getBlock(hitInfo.getBlockPosition());
                    if (hitBlock != null) {
                        Vector3f vTravelledDistance = vLocation.sub(hitInfo.getHitPoint());
                        float fTravelledDistance = vTravelledDistance.length();
                        if (fTravelledDistance > fDistanceThisFrame) {
                            break;
                        }
                        if (hitBlock.isPenetrable()) {
                            if (!hitInfo.getEntity().hasComponent(BlockComponent.class)) {
                                entity.send(new EntityImpactEvent(hitInfo.getHitPoint(), hitInfo.getHitNormal(), comp.velocity, fDistanceThisFrame, hitInfo.getEntity()));
                                break;
                            }
                            // decrease the remaining distance to check if we hit a block
                            fDistanceThisFrame = fDistanceThisFrame - fTravelledDistance;
                            vLocation = hitInfo.getHitPoint();
                        } else {
                            entity.send(new BlockImpactEvent(hitInfo.getHitPoint(), hitInfo.getHitNormal(), comp.velocity, fDistanceThisFrame, hitInfo.getEntity()));
                            break;
                        }
                    } else {
                        break;
                    }
                } else {
                    break;
                }
            }
        }
    }
    if (networkSystem.getMode().isServer() && time.getGameTimeInMs() - TIME_BETWEEN_NETSYNCS > lastNetsync) {
        sendSyncMessages();
        lastNetsync = time.getGameTimeInMs();
    }
    List<CollisionPair> collisionPairs = physics.getCollisionPairs();
    for (CollisionPair pair : collisionPairs) {
        if (pair.b.exists()) {
            short bCollisionGroup = getCollisionGroupFlag(pair.b);
            short aCollidesWith = getCollidesWithGroupFlag(pair.a);
            if ((bCollisionGroup & aCollidesWith) != 0 || (pair.b.hasComponent(BlockComponent.class) && !pair.a.hasComponent(BlockComponent.class))) {
                pair.a.send(new CollideEvent(pair.b, pair.pointA, pair.pointB, pair.distance, pair.normal));
            }
        }
        if (pair.a.exists()) {
            short aCollisionGroup = getCollisionGroupFlag(pair.a);
            short bCollidesWith = getCollidesWithGroupFlag(pair.b);
            if ((aCollisionGroup & bCollidesWith) != 0 || (pair.a.hasComponent(BlockComponent.class) && !pair.b.hasComponent(BlockComponent.class))) {
                pair.b.send(new CollideEvent(pair.a, pair.pointB, pair.pointA, pair.distance, new Vector3f(pair.normal).invert()));
            }
        }
    }
}
Also used : RigidBodyComponent(org.terasology.physics.components.RigidBodyComponent) HitResult(org.terasology.physics.HitResult) BlockComponent(org.terasology.world.block.BlockComponent) CollideEvent(org.terasology.physics.events.CollideEvent) Vector3f(org.terasology.math.geom.Vector3f) BlockImpactEvent(org.terasology.physics.events.BlockImpactEvent) Block(org.terasology.world.block.Block) OnChangedBlock(org.terasology.world.OnChangedBlock) EntityImpactEvent(org.terasology.physics.events.EntityImpactEvent) EntityRef(org.terasology.entitySystem.entity.EntityRef)

Example 27 with EntityRef

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

the class MovementDebugCommands method teleportMeToPlayer.

@Command(shortDescription = "Teleport to player", runOnServer = true, requiredPermission = PermissionManager.CHEAT_PERMISSION)
public String teleportMeToPlayer(@Sender EntityRef sender, @CommandParam("username") String username) {
    for (EntityRef clientEntity : entityManager.getEntitiesWith(ClientComponent.class)) {
        EntityRef clientInfo = clientEntity.getComponent(ClientComponent.class).clientInfo;
        DisplayNameComponent name = clientInfo.getComponent(DisplayNameComponent.class);
        if (username.equalsIgnoreCase(name.name)) {
            LocationComponent locationComponent = clientEntity.getComponent(LocationComponent.class);
            if (locationComponent != null) {
                Vector3f vLocation = locationComponent.getWorldPosition();
                ClientComponent clientComp = sender.getComponent(ClientComponent.class);
                if (clientComp != null) {
                    clientComp.character.send(new CharacterTeleportEvent(vLocation));
                    return "Teleporting you to " + username + " at " + vLocation.x + " " + vLocation.y + " " + vLocation.z;
                }
            }
        }
    }
    throw new IllegalArgumentException("No such user '" + username + "'");
}
Also used : DisplayNameComponent(org.terasology.logic.common.DisplayNameComponent) CharacterTeleportEvent(org.terasology.logic.characters.CharacterTeleportEvent) Vector3f(org.terasology.math.geom.Vector3f) EntityRef(org.terasology.entitySystem.entity.EntityRef) ClientComponent(org.terasology.network.ClientComponent) LocationComponent(org.terasology.logic.location.LocationComponent) Command(org.terasology.logic.console.commandSystem.annotations.Command)

Example 28 with EntityRef

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

the class MovementDebugCommands method teleportPlayerToMe.

@Command(shortDescription = "Teleport player to you", runOnServer = true, requiredPermission = PermissionManager.USER_MANAGEMENT_PERMISSION)
public String teleportPlayerToMe(@Sender EntityRef sender, @CommandParam("username") String username) {
    for (EntityRef clientEntity : entityManager.getEntitiesWith(ClientComponent.class)) {
        EntityRef clientInfo = clientEntity.getComponent(ClientComponent.class).clientInfo;
        DisplayNameComponent name = clientInfo.getComponent(DisplayNameComponent.class);
        if (username.equalsIgnoreCase(name.name)) {
            LocationComponent locationComponent = sender.getComponent(LocationComponent.class);
            if (locationComponent != null) {
                Vector3f vLocation = locationComponent.getWorldPosition();
                ClientComponent clientComp = clientEntity.getComponent(ClientComponent.class);
                if (clientComp != null) {
                    clientComp.character.send(new CharacterTeleportEvent(vLocation));
                    return "Teleporting " + username + " to you at " + vLocation.x + " " + vLocation.y + " " + vLocation.z;
                }
            }
        }
    }
    throw new IllegalArgumentException("No such user '" + username + "'");
}
Also used : DisplayNameComponent(org.terasology.logic.common.DisplayNameComponent) CharacterTeleportEvent(org.terasology.logic.characters.CharacterTeleportEvent) Vector3f(org.terasology.math.geom.Vector3f) EntityRef(org.terasology.entitySystem.entity.EntityRef) ClientComponent(org.terasology.network.ClientComponent) LocationComponent(org.terasology.logic.location.LocationComponent) Command(org.terasology.logic.console.commandSystem.annotations.Command)

Example 29 with EntityRef

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

the class PlayerNameTagSystem method onCharacterActivation.

/**
 * Listening for {@link org.terasology.logic.players.event.OnPlayerSpawnedEvent} does not work, as it is an
 * authority event that does not get processed at clients. That is why we listen for the activation.
 */
@ReceiveEvent(components = CharacterComponent.class)
public void onCharacterActivation(OnActivatedComponent event, EntityRef characterEntity, CharacterComponent characterComponent) {
    EntityRef ownerEntity = networkSystem.getOwnerEntity(characterEntity);
    if (ownerEntity == null) {
        // NPC
        return;
    }
    ClientComponent clientComponent = ownerEntity.getComponent(ClientComponent.class);
    if (clientComponent == null) {
        logger.warn("Can't create player based name tag for character as owner has no client component");
        return;
    }
    if (clientComponent.local) {
        // the character belongs to the local player and does not need a name tag
        return;
    }
    EntityRef clientInfoEntity = clientComponent.clientInfo;
    DisplayNameComponent displayNameComponent = clientInfoEntity.getComponent(DisplayNameComponent.class);
    if (displayNameComponent == null) {
        logger.error("Can't create player based name tag for character as client info has no DisplayNameComponent");
        return;
    }
    String name = displayNameComponent.name;
    float yOffset = characterComponent.nameTagOffset;
    Color color = Color.WHITE;
    ColorComponent colorComponent = clientInfoEntity.getComponent(ColorComponent.class);
    if (colorComponent != null) {
        color = colorComponent.color;
    }
    NameTagComponent nameTagComponent = characterEntity.getComponent(NameTagComponent.class);
    boolean newComponent = nameTagComponent == null;
    if (nameTagComponent == null) {
        nameTagComponent = new NameTagComponent();
    }
    nameTagComponent.text = name;
    nameTagComponent.textColor = color;
    nameTagComponent.yOffset = yOffset;
    if (newComponent) {
        characterEntity.addComponent(nameTagComponent);
    } else {
        characterEntity.saveComponent(nameTagComponent);
    }
}
Also used : DisplayNameComponent(org.terasology.logic.common.DisplayNameComponent) ColorComponent(org.terasology.network.ColorComponent) Color(org.terasology.rendering.nui.Color) EntityRef(org.terasology.entitySystem.entity.EntityRef) ClientComponent(org.terasology.network.ClientComponent) ReceiveEvent(org.terasology.entitySystem.event.ReceiveEvent)

Example 30 with EntityRef

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

the class PlayerNameTagSystem method onDisplayNameChange.

@ReceiveEvent
public void onDisplayNameChange(OnChangedComponent event, EntityRef clientInfoEntity, DisplayNameComponent displayNameComponent) {
    ClientInfoComponent clientInfoComp = clientInfoEntity.getComponent(ClientInfoComponent.class);
    if (clientInfoComp == null) {
        // not a client info object
        return;
    }
    EntityRef clientEntity = clientInfoComp.client;
    if (!clientEntity.exists()) {
        // offline players aren't visible: nothing to do
        return;
    }
    ClientComponent clientComponent = clientEntity.getComponent(ClientComponent.class);
    if (clientComponent == null) {
        logger.warn("Can't update name tag as client entity lacks ClietnComponent");
        return;
    }
    EntityRef characterEntity = clientComponent.character;
    if (characterEntity == null || !characterEntity.exists()) {
        // player has no character, nothing to do
        return;
    }
    NameTagComponent nameTagComponent = characterEntity.getComponent(NameTagComponent.class);
    if (nameTagComponent == null) {
        // local players don't have a name tag
        return;
    }
    nameTagComponent.text = displayNameComponent.name;
    characterEntity.saveComponent(nameTagComponent);
}
Also used : ClientInfoComponent(org.terasology.network.ClientInfoComponent) EntityRef(org.terasology.entitySystem.entity.EntityRef) ClientComponent(org.terasology.network.ClientComponent) ReceiveEvent(org.terasology.entitySystem.event.ReceiveEvent)

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