Search in sources :

Example 26 with LocationComponent

use of org.terasology.engine.logic.location.LocationComponent in project Terasology by MovingBlocks.

the class CoreCommands method spawnPrefab.

/**
 * Spawns an instance of a prefab in the world
 *
 * @param sender     Sender of command
 * @param prefabName String containing prefab name
 * @return String containing final message
 */
@Command(shortDescription = "Spawns an instance of a prefab in the world", runOnServer = true, requiredPermission = PermissionManager.CHEAT_PERMISSION)
public String spawnPrefab(@Sender EntityRef sender, @CommandParam("prefabId") String prefabName) {
    ClientComponent clientComponent = sender.getComponent(ClientComponent.class);
    LocationComponent characterLocation = clientComponent.character.getComponent(LocationComponent.class);
    Vector3f spawnPos = characterLocation.getWorldPosition(new Vector3f());
    Vector3f offset = characterLocation.getWorldDirection(new Vector3f());
    offset.mul(2);
    spawnPos.add(offset);
    Vector3f dir = characterLocation.getWorldDirection(new Vector3f());
    dir.y = 0;
    if (dir.lengthSquared() > 0.001f) {
        dir.normalize();
    } else {
        dir.set(Direction.FORWARD.asVector3f());
    }
    return Assets.getPrefab(prefabName).map(prefab -> {
        LocationComponent loc = prefab.getComponent(LocationComponent.class);
        if (loc != null) {
            entityManager.create(prefab, spawnPos);
            return "Done";
        } else {
            return "Prefab cannot be spawned (no location component)";
        }
    }).orElse("Unknown prefab");
}
Also used : DropItemEvent(org.terasology.engine.logic.inventory.events.DropItemEvent) Server(org.terasology.engine.network.Server) Arrays(java.util.Arrays) Component(org.terasology.gestalt.entitysystem.component.Component) In(org.terasology.engine.registry.In) SystemConfig(org.terasology.engine.config.SystemConfig) Sender(org.terasology.engine.logic.console.commandSystem.annotations.Sender) Console(org.terasology.engine.logic.console.Console) SkinSuggester(org.terasology.engine.logic.console.suggesters.SkinSuggester) DisplayDevice(org.terasology.engine.core.subsystem.DisplayDevice) NUIEditorScreen(org.terasology.engine.rendering.nui.editor.layers.NUIEditorScreen) Locale(java.util.Locale) BlockUri(org.terasology.engine.world.block.BlockUri) FontColor(org.terasology.nui.FontColor) Vector3f(org.joml.Vector3f) TranslationProject(org.terasology.engine.i18n.TranslationProject) Path(java.nio.file.Path) StateLoading(org.terasology.engine.core.modes.StateLoading) Command(org.terasology.engine.logic.console.commandSystem.annotations.Command) TranslationSystem(org.terasology.engine.i18n.TranslationSystem) ConsoleCommand(org.terasology.engine.logic.console.commandSystem.ConsoleCommand) CommandParam(org.terasology.engine.logic.console.commandSystem.annotations.CommandParam) ConsoleColors(org.terasology.engine.logic.console.ConsoleColors) GameEngine(org.terasology.engine.core.GameEngine) CommandNameSuggester(org.terasology.engine.logic.console.suggesters.CommandNameSuggester) WaitPopup(org.terasology.engine.rendering.nui.layers.mainMenu.WaitPopup) Set(java.util.Set) StateMainMenu(org.terasology.engine.core.modes.StateMainMenu) NUISkinEditorSystem(org.terasology.engine.rendering.nui.editor.systems.NUISkinEditorSystem) Streams(com.google.common.collect.Streams) BlockFamilyDefinition(org.terasology.engine.world.block.loader.BlockFamilyDefinition) Instant(java.time.Instant) UISkinAsset(org.terasology.nui.skin.UISkinAsset) Collectors(java.util.stream.Collectors) Direction(org.terasology.engine.math.Direction) NUIManager(org.terasology.engine.rendering.nui.NUIManager) List(java.util.List) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) RegisterSystem(org.terasology.engine.entitySystem.systems.RegisterSystem) NUISkinEditorScreen(org.terasology.engine.rendering.nui.editor.layers.NUISkinEditorScreen) ScreenSuggester(org.terasology.engine.logic.console.suggesters.ScreenSuggester) Optional(java.util.Optional) Name(org.terasology.gestalt.naming.Name) TerasologyConstants(org.terasology.engine.core.TerasologyConstants) UIElement(org.terasology.nui.asset.UIElement) ModuleManager(org.terasology.engine.core.module.ModuleManager) EngineEntityManager(org.terasology.engine.entitySystem.entity.internal.EngineEntityManager) NetworkMode(org.terasology.engine.network.NetworkMode) Callable(java.util.concurrent.Callable) BlockItemFactory(org.terasology.engine.world.block.items.BlockItemFactory) ArrayList(java.util.ArrayList) WorldDumper(org.terasology.engine.persistence.WorldDumper) ResourceUrn(org.terasology.gestalt.assets.ResourceUrn) Time(org.terasology.engine.core.Time) StreamSupport(java.util.stream.StreamSupport) AssetManager(org.terasology.gestalt.assets.management.AssetManager) PingService(org.terasology.engine.network.PingService) MessagePopup(org.terasology.engine.rendering.nui.layers.mainMenu.MessagePopup) Assets(org.terasology.engine.utilities.Assets) BlockFamily(org.terasology.engine.world.block.family.BlockFamily) PermissionManager(org.terasology.engine.logic.permission.PermissionManager) NUIEditorSystem(org.terasology.engine.rendering.nui.editor.systems.NUIEditorSystem) LocationComponent(org.terasology.engine.logic.location.LocationComponent) ClientComponent(org.terasology.engine.network.ClientComponent) EntityManager(org.terasology.engine.entitySystem.entity.EntityManager) PrefabManager(org.terasology.engine.entitySystem.prefab.PrefabManager) IOException(java.io.IOException) Prefab(org.terasology.engine.entitySystem.prefab.Prefab) UnknownHostException(java.net.UnknownHostException) BaseComponentSystem(org.terasology.engine.entitySystem.systems.BaseComponentSystem) Ordering(com.google.common.collect.Ordering) BlockManager(org.terasology.engine.world.block.BlockManager) PrefabSerializer(org.terasology.engine.persistence.serializers.PrefabSerializer) JoinStatus(org.terasology.engine.network.JoinStatus) NetworkSystem(org.terasology.engine.network.NetworkSystem) PathManager(org.terasology.engine.core.PathManager) Vector3f(org.joml.Vector3f) ClientComponent(org.terasology.engine.network.ClientComponent) LocationComponent(org.terasology.engine.logic.location.LocationComponent) Command(org.terasology.engine.logic.console.commandSystem.annotations.Command) ConsoleCommand(org.terasology.engine.logic.console.commandSystem.ConsoleCommand)

Example 27 with LocationComponent

use of org.terasology.engine.logic.location.LocationComponent in project Terasology by MovingBlocks.

the class PlayerFactory method newInstance.

/**
 * Creates a new player character entity. The desired spawning location is derived from
 * the {@link LocationComponent} of the controller.
 * @param controller the controlling client entity
 * @return a new player character entity
 */
public EntityRef newInstance(EntityRef controller) {
    EntityBuilder builder = entityManager.newBuilder("engine:player");
    LocationComponent location = controller.getComponent(LocationComponent.class);
    Vector3f spawnPosition = findSpawnPositionFromLocationComponent(location);
    location.setWorldPosition(spawnPosition);
    controller.saveComponent(location);
    logger.debug("Spawing player at: {}", spawnPosition);
    builder.getComponent(LocationComponent.class).setWorldPosition(spawnPosition);
    builder.setOwner(controller);
    CharacterComponent playerComponent = builder.getComponent(CharacterComponent.class);
    playerComponent.controller = controller;
    EntityRef player = builder.build();
    Location.attachChild(player, controller, new Vector3f(), new Quaternionf(0, 0, 0, 1));
    return player;
}
Also used : Vector3f(org.joml.Vector3f) Quaternionf(org.joml.Quaternionf) CharacterComponent(org.terasology.engine.logic.characters.CharacterComponent) EntityBuilder(org.terasology.engine.entitySystem.entity.EntityBuilder) LocationComponent(org.terasology.engine.logic.location.LocationComponent) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef)

Example 28 with LocationComponent

use of org.terasology.engine.logic.location.LocationComponent in project Terasology by MovingBlocks.

the class ThirdPersonRemoteClientSystem method update.

/**
 * Modifies the remote players' held item mount points to show and move their held items at their location. Clean up
 * no longer needed held item entities.
 * <p>
 * TODO: Also responsible for catching characters without current held item entities and then create them. Should be
 * moved elsewhere
 */
@Override
public void update(float delta) {
    // Make a set of all held items that exist so we can review them and later toss any no longer needed
    Set<EntityRef> heldItemsForReview = Sets.newHashSet(entityManager.getEntitiesWith(ItemIsRemotelyHeldComponent.class));
    // critters may also want held items
    for (EntityRef remotePlayer : entityManager.getEntitiesWith(CharacterComponent.class, PlayerCharacterComponent.class)) {
        if (relatesToLocalPlayer(remotePlayer)) {
            continue;
        }
        // Find the associated held item entity for this player, if one exists
        EntityRef currentHeldItem = EntityRef.NULL;
        Iterator<EntityRef> heldItermsIterator = heldItemsForReview.iterator();
        while (heldItermsIterator.hasNext()) {
            EntityRef heldItemCandidate = heldItermsIterator.next();
            ItemIsRemotelyHeldComponent itemIsRemotelyHeldComponent = heldItemCandidate.getComponent(ItemIsRemotelyHeldComponent.class);
            if (itemIsRemotelyHeldComponent.remotePlayer.equals(remotePlayer)) {
                currentHeldItem = heldItemCandidate;
                heldItermsIterator.remove();
                break;
            }
        }
        // selected
        if (currentHeldItem == EntityRef.NULL) {
            if (remotePlayer.hasComponent(CharacterHeldItemComponent.class)) {
                CharacterHeldItemComponent characterHeldItemComponent = remotePlayer.getComponent(CharacterHeldItemComponent.class);
                if (characterHeldItemComponent != null && !characterHeldItemComponent.selectedItem.equals(EntityRef.NULL)) {
                    linkHeldItemLocationForRemotePlayer(remotePlayer.getComponent(CharacterHeldItemComponent.class).selectedItem, remotePlayer);
                }
            }
        }
        // get the remote person mount point
        CharacterHeldItemComponent characterHeldItemComponent = remotePlayer.getComponent(CharacterHeldItemComponent.class);
        RemotePersonHeldItemMountPointComponent mountPointComponent = remotePlayer.getComponent(RemotePersonHeldItemMountPointComponent.class);
        if (characterHeldItemComponent == null || mountPointComponent == null) {
            continue;
        }
        LocationComponent locationComponent = mountPointComponent.mountPointEntity.getComponent(LocationComponent.class);
        if (locationComponent == null) {
            continue;
        }
        long timeElapsedSinceLastUsed = time.getGameTimeInMs() - characterHeldItemComponent.lastItemUsedTime;
        float animateAmount = 0f;
        if (timeElapsedSinceLastUsed < USEANIMATIONLENGTH) {
            // TODO add other easing functions into utilities and use here?
            // half way through the animation will be the maximum extent of rotation and translation
            animateAmount = 1f - Math.abs(((float) timeElapsedSinceLastUsed / (float) USEANIMATIONLENGTH) - 0.5f);
        }
        float addPitch = 15f * animateAmount;
        float addYaw = 10f * animateAmount;
        locationComponent.setLocalRotation(new Quaternionf().rotationYXZ(Math.toRadians(mountPointComponent.rotateDegrees.y + addYaw), Math.toRadians(mountPointComponent.rotateDegrees.x + addPitch), Math.toRadians(mountPointComponent.rotateDegrees.z)));
        Vector3f offset = new Vector3f(0.05f * animateAmount, -0.24f * animateAmount, 0f);
        offset.add(mountPointComponent.translate);
        locationComponent.setLocalPosition(offset);
        mountPointComponent.mountPointEntity.saveComponent(locationComponent);
    }
    heldItemsForReview.forEach(remainingHeldItem -> {
        if (remainingHeldItem.exists()) {
            remainingHeldItem.destroy();
        }
    });
}
Also used : Vector3f(org.joml.Vector3f) Quaternionf(org.joml.Quaternionf) CharacterHeldItemComponent(org.terasology.engine.logic.characters.CharacterHeldItemComponent) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) LocationComponent(org.terasology.engine.logic.location.LocationComponent)

Example 29 with LocationComponent

use of org.terasology.engine.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();
    hasTarget = target.exists();
    if (hasTarget) {
        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(new Vector3f());
                Block block = worldProvider.getBlock(blockPos);
                aabb.set(block.getBounds(blockPos));
            } else {
                MeshComponent mesh = target.getComponent(MeshComponent.class);
                if (mesh != null && mesh.mesh != null) {
                    aabb.set(mesh.mesh.getAABB());
                    aabb.transform(new Matrix4f().translationRotateScale(location.getWorldPosition(new Vector3f()), location.getWorldRotation(new Quaternionf()), location.getWorldScale()));
                }
            }
        }
    }
}
Also used : BlockComponent(org.terasology.engine.world.block.BlockComponent) MeshComponent(org.terasology.engine.rendering.logic.MeshComponent) Matrix4f(org.joml.Matrix4f) BlockRegionComponent(org.terasology.engine.world.block.regions.BlockRegionComponent) Vector3f(org.joml.Vector3f) Block(org.terasology.engine.world.block.Block) Quaternionf(org.joml.Quaternionf) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) LocationComponent(org.terasology.engine.logic.location.LocationComponent) ReceiveEvent(org.terasology.engine.entitySystem.event.ReceiveEvent)

Example 30 with LocationComponent

use of org.terasology.engine.logic.location.LocationComponent in project Terasology by MovingBlocks.

the class MovementDebugCommands method teleportAllPlayersToPlayer.

@Command(shortDescription = "Teleport all users to specified user", runOnServer = true, requiredPermission = PermissionManager.USER_MANAGEMENT_PERMISSION)
public String teleportAllPlayersToPlayer(@CommandParam("username") String username) {
    Vector3f vPlayerLocation = new Vector3f();
    boolean bPlayerLocationWasFound = false;
    EntityRef playerEntity = null;
    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) {
                vPlayerLocation = locationComponent.getWorldPosition(new Vector3f());
                bPlayerLocationWasFound = true;
                playerEntity = clientEntity;
            }
            break;
        }
    }
    if (!bPlayerLocationWasFound) {
        throw new IllegalArgumentException("No such user '" + username + "'");
    }
    MovementMode playerMovementMode = MovementMode.NONE;
    ClientComponent clientInfo = playerEntity.getComponent(ClientComponent.class);
    if (clientInfo != null) {
        CharacterMovementComponent playerMovementComponent = clientInfo.character.getComponent(CharacterMovementComponent.class);
        if (playerMovementComponent != null) {
            playerMovementMode = playerMovementComponent.mode;
        }
    }
    for (EntityRef clientEntity : entityManager.getEntitiesWith(ClientComponent.class)) {
        ClientComponent clientComp = clientEntity.getComponent(ClientComponent.class);
        if (clientComp != null) {
            clientComp.character.send(new CharacterTeleportEvent(vPlayerLocation));
            CharacterMovementComponent characterMovementComponent = clientComp.character.getComponent(CharacterMovementComponent.class);
            if (characterMovementComponent != null && playerMovementMode != MovementMode.NONE && playerMovementMode != characterMovementComponent.mode) {
                clientComp.character.send(new SetMovementModeEvent(playerMovementMode));
            }
        }
    }
    return "All possible players teleported to " + username + " and set to " + playerMovementMode;
}
Also used : DisplayNameComponent(org.terasology.engine.logic.common.DisplayNameComponent) SetMovementModeEvent(org.terasology.engine.logic.characters.events.SetMovementModeEvent) CharacterTeleportEvent(org.terasology.engine.logic.characters.CharacterTeleportEvent) MovementMode(org.terasology.engine.logic.characters.MovementMode) Vector3f(org.joml.Vector3f) CharacterMovementComponent(org.terasology.engine.logic.characters.CharacterMovementComponent) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) ClientComponent(org.terasology.engine.network.ClientComponent) LocationComponent(org.terasology.engine.logic.location.LocationComponent) Command(org.terasology.engine.logic.console.commandSystem.annotations.Command)

Aggregations

LocationComponent (org.terasology.engine.logic.location.LocationComponent)65 Vector3f (org.joml.Vector3f)46 EntityRef (org.terasology.engine.entitySystem.entity.EntityRef)40 Quaternionf (org.joml.Quaternionf)18 ClientComponent (org.terasology.engine.network.ClientComponent)17 ReceiveEvent (org.terasology.engine.entitySystem.event.ReceiveEvent)9 Command (org.terasology.engine.logic.console.commandSystem.annotations.Command)9 Matrix4f (org.joml.Matrix4f)7 Vector3i (org.joml.Vector3i)6 CharacterTeleportEvent (org.terasology.engine.logic.characters.CharacterTeleportEvent)5 BlockFamily (org.terasology.engine.world.block.family.BlockFamily)5 Vector3fc (org.joml.Vector3fc)4 EntityBuilder (org.terasology.engine.entitySystem.entity.EntityBuilder)4 CharacterHeldItemComponent (org.terasology.engine.logic.characters.CharacterHeldItemComponent)4 DisplayNameComponent (org.terasology.engine.logic.common.DisplayNameComponent)4 ConsoleCommand (org.terasology.engine.logic.console.commandSystem.ConsoleCommand)4 DropItemEvent (org.terasology.engine.logic.inventory.events.DropItemEvent)4 Bone (org.terasology.engine.rendering.assets.skeletalmesh.Bone)4 BlockItemFactory (org.terasology.engine.world.block.items.BlockItemFactory)4 Component (org.terasology.gestalt.entitysystem.component.Component)4