Search in sources :

Example 1 with CharacterMoveInputEvent

use of org.terasology.logic.characters.CharacterMoveInputEvent in project Terasology by MovingBlocks.

the class LocalPlayerSystem method processInput.

private void processInput(EntityRef entity, CharacterMovementComponent characterMovementComponent) {
    lookYaw = (lookYaw - lookYawDelta) % 360;
    lookYawDelta = 0f;
    lookPitch = TeraMath.clamp(lookPitch + lookPitchDelta, -89, 89);
    lookPitchDelta = 0f;
    Vector3f relMove = new Vector3f(relativeMovement);
    relMove.y = 0;
    Quat4f viewRotation;
    switch(characterMovementComponent.mode) {
        case CROUCHING:
        case WALKING:
            if (!config.getRendering().isVrSupport()) {
                viewRotation = new Quat4f(TeraMath.DEG_TO_RAD * lookYaw, 0, 0);
                playerCamera.setOrientation(viewRotation);
            }
            playerCamera.getOrientation().rotate(relMove, relMove);
            break;
        case CLIMBING:
            // Rotation is applied in KinematicCharacterMover
            relMove.y += relativeMovement.y;
            break;
        default:
            if (!config.getRendering().isVrSupport()) {
                viewRotation = new Quat4f(TeraMath.DEG_TO_RAD * lookYaw, TeraMath.DEG_TO_RAD * lookPitch, 0);
                playerCamera.setOrientation(viewRotation);
            }
            playerCamera.getOrientation().rotate(relMove, relMove);
            relMove.y += relativeMovement.y;
            break;
    }
    // For some reason, Quat4f.rotate is returning NaN for valid inputs. This prevents those NaNs from causing trouble down the line.
    if (!Float.isNaN(relMove.getX()) && !Float.isNaN(relMove.getY()) && !Float.isNaN(relMove.getZ())) {
        entity.send(new CharacterMoveInputEvent(inputSequenceNumber++, lookPitch, lookYaw, relMove, run, crouch, jump, time.getGameDeltaInMs()));
    }
    jump = false;
}
Also used : Vector3f(org.terasology.math.geom.Vector3f) CharacterMoveInputEvent(org.terasology.logic.characters.CharacterMoveInputEvent) Quat4f(org.terasology.math.geom.Quat4f)

Example 2 with CharacterMoveInputEvent

use of org.terasology.logic.characters.CharacterMoveInputEvent in project Terasology by MovingBlocks.

the class SimpleAISystem method update.

@Override
public void update(float delta) {
    for (EntityRef entity : entityManager.getEntitiesWith(SimpleAIComponent.class, CharacterMovementComponent.class, LocationComponent.class)) {
        LocationComponent location = entity.getComponent(LocationComponent.class);
        Vector3f worldPos = location.getWorldPosition();
        // Skip this AI if not in a loaded chunk
        if (!worldProvider.isBlockRelevant(worldPos)) {
            continue;
        }
        SimpleAIComponent ai = entity.getComponent(SimpleAIComponent.class);
        Vector3f drive = new Vector3f();
        // TODO: shouldn't use local player, need some way to find nearest player
        if (localPlayer != null) {
            Vector3f dist = new Vector3f(worldPos);
            dist.sub(localPlayer.getPosition());
            double distanceToPlayer = dist.lengthSquared();
            if (distanceToPlayer > 6 && distanceToPlayer < 16) {
                // Head to player
                ai.movementTarget.set(localPlayer.getPosition());
                ai.followingPlayer = true;
                entity.saveComponent(ai);
            } else {
                // Random walk
                if (time.getGameTimeInMs() - ai.lastChangeOfDirectionAt > 12000 || ai.followingPlayer) {
                    ai.movementTarget.set(worldPos.x + random.nextFloat(-500.0f, 500.0f), worldPos.y, worldPos.z + random.nextFloat(-500.0f, 500.0f));
                    ai.lastChangeOfDirectionAt = time.getGameTimeInMs();
                    ai.followingPlayer = false;
                    entity.saveComponent(ai);
                }
            }
            Vector3f targetDirection = new Vector3f();
            targetDirection.sub(ai.movementTarget, worldPos);
            targetDirection.normalize();
            drive.set(targetDirection);
            float yaw = (float) Math.atan2(targetDirection.x, targetDirection.z);
            location.getLocalRotation().set(new Vector3f(0, 1, 0), yaw);
            entity.saveComponent(location);
        }
        entity.send(new CharacterMoveInputEvent(0, 0, 0, drive, false, false, time.getGameDeltaInMs()));
    }
}
Also used : Vector3f(org.terasology.math.geom.Vector3f) CharacterMoveInputEvent(org.terasology.logic.characters.CharacterMoveInputEvent) EntityRef(org.terasology.entitySystem.entity.EntityRef) LocationComponent(org.terasology.logic.location.LocationComponent)

Example 3 with CharacterMoveInputEvent

use of org.terasology.logic.characters.CharacterMoveInputEvent in project Terasology by MovingBlocks.

the class HierarchicalAISystem method loop.

/**
 * main loop of hierarchical system
 *
 * @param entity
 * @param location
 * @param worldPos
 */
private void loop(EntityRef entity, LocationComponent location, Vector3f worldPos) {
    HierarchicalAIComponent ai = entity.getComponent(HierarchicalAIComponent.class);
    long tempTime = time.getGameTimeInMs();
    // TODO remove next
    long lastAttack = 0;
    // skip update if set to skip them
    if (tempTime - ai.lastProgressedUpdateAt < ai.updateFrequency) {
        ai.lastProgressedUpdateAt = time.getGameTimeInMs();
        return;
    }
    long directionChangeTime = ai.moveUpdateTime;
    long moveChangeTime = ai.moveUpdateTime;
    long idleChangeTime = ai.idlingUpdateTime;
    long dangerChangeTime = ai.dangerUpdateTime;
    // get movement
    Vector3f drive = new Vector3f();
    // player
    if (localPlayer != null) {
        Vector3f dist = new Vector3f(worldPos);
        dist.sub(localPlayer.getPosition());
        double distanceToPlayer = dist.lengthSquared();
        ai.inDanger = false;
        if (ai.dieIfPlayerFar && distanceToPlayer > ai.dieDistance) {
            entity.destroy();
        }
        // update
        if (tempTime - ai.lastChangeOfDangerAt > dangerChangeTime) {
            dangerChangeTime = (long) (ai.dangerUpdateTime * random.nextDouble() * ai.hectic);
            if (ai.hunter) {
                if (distanceToPlayer > ai.playerdistance && distanceToPlayer < ai.playerSense) {
                    // Head to player
                    Vector3f tempTarget = localPlayer.getPosition();
                    if (ai.forgiving != 0) {
                        ai.movementTarget.set(new Vector3f(tempTarget.x + random.nextFloat(-ai.forgiving, ai.forgiving), tempTarget.y + random.nextFloat(-ai.forgiving, ai.forgiving), tempTarget.z + random.nextFloat(-ai.forgiving, ai.forgiving)));
                    } else {
                        ai.movementTarget.set(tempTarget);
                    }
                    ai.inDanger = true;
                    entity.saveComponent(ai);
                // System.out.print("\nhunting palyer\n");
                }
            }
            // run opposite direction
            if (ai.wild) {
                if (distanceToPlayer > ai.panicDistance && distanceToPlayer < ai.runDistance) {
                    runAway(entity, ai);
                }
            }
            ai.lastChangeOfDangerAt = time.getGameTimeInMs();
        }
    }
    if (!ai.inDanger) {
        // if anything edible is in front
        if (foodInFront()) {
            return;
        }
        // what AI does when nothing better to do
        if (idling) {
            // time to stop idling
            if (tempTime - ai.lastChangeOfidlingtAt > idleChangeTime) {
                idleChangeTime = (long) (ai.idlingUpdateTime * random.nextDouble() * ai.hectic);
                idling = false;
                // mark idling state changed
                ai.lastChangeOfidlingtAt = time.getGameTimeInMs();
            }
            entity.saveComponent(location);
            ai.lastProgressedUpdateAt = time.getGameTimeInMs();
            return;
        }
        // check if it is time to idle again
        if (tempTime - ai.lastChangeOfMovementAt > moveChangeTime) {
            // update time
            moveChangeTime = (long) (ai.moveUpdateTime * random.nextDouble() * ai.hectic);
            idling = true;
            entity.saveComponent(location);
            // mark start idling
            ai.lastChangeOfMovementAt = time.getGameTimeInMs();
            ai.lastProgressedUpdateAt = time.getGameTimeInMs();
            return;
        }
        // check if time to change direction
        if (tempTime - ai.lastChangeOfDirectionAt > directionChangeTime) {
            directionChangeTime = (long) (ai.moveUpdateTime * random.nextDouble() * ai.straightLined);
            randomWalk(worldPos, ai);
            entity.saveComponent(ai);
        // System.out.print("direction changed\n");
        }
    }
    Vector3f targetDirection = new Vector3f();
    targetDirection.sub(ai.movementTarget, worldPos);
    targetDirection.normalize();
    drive.set(targetDirection);
    float yaw = (float) Math.atan2(targetDirection.x, targetDirection.z);
    entity.send(new CharacterMoveInputEvent(0, 0, yaw, drive, false, false, time.getGameDeltaInMs()));
    entity.saveComponent(location);
    // System.out.print("\Destination set: " + targetDirection.x + ":" +targetDirection.z + "\n");
    // System.out.print("\nI am: " + worldPos.x + ":" + worldPos.z + "\n");
    ai.lastProgressedUpdateAt = time.getGameTimeInMs();
}
Also used : Vector3f(org.terasology.math.geom.Vector3f) CharacterMoveInputEvent(org.terasology.logic.characters.CharacterMoveInputEvent)

Aggregations

CharacterMoveInputEvent (org.terasology.logic.characters.CharacterMoveInputEvent)3 Vector3f (org.terasology.math.geom.Vector3f)3 EntityRef (org.terasology.entitySystem.entity.EntityRef)1 LocationComponent (org.terasology.logic.location.LocationComponent)1 Quat4f (org.terasology.math.geom.Quat4f)1