use of org.terasology.engine.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.
the class AbstractState method createLocalPlayer.
protected static void createLocalPlayer(Context context) {
EngineEntityManager entityManager = context.get(EngineEntityManager.class);
EntityRef localPlayerEntity = entityManager.create(new ClientComponent());
LocalPlayer localPlayer = new LocalPlayer();
localPlayer.setRecordAndReplayClasses(context.get(DirectionAndOriginPosRecorderList.class), context.get(RecordAndReplayCurrentStatus.class));
context.put(LocalPlayer.class, localPlayer);
localPlayer.setClientEntity(localPlayerEntity);
}
use of org.terasology.engine.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.
the class CoreCommands method bowlingPrep.
@Command(shortDescription = "Sets up a typical bowling pin arrangement in front of the player. ", helpText = "Spawns the specific block in a regular bowling pin pattern, Throw something at it!", runOnServer = true, requiredPermission = PermissionManager.CHEAT_PERMISSION)
public String bowlingPrep(@Sender EntityRef sender, @CommandParam("blockName") String blockName) {
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(5);
spawnPos.add(offset);
BlockFamily block = blockManager.getBlockFamily(blockName);
if (block == null) {
return "Sorry, your block is not found";
}
BlockItemFactory blockItemFactory = new BlockItemFactory(entityManager);
Vector3f startPos = new Vector3f(spawnPos);
// delta x is the distance between the pins in the rows.
float deltax = 0.5f;
// delta z is the distance between the rows.
float deltaz = 1.0f;
// the height of the drop (to be modified to keep the bowlingPin upright)
float vectorY = 0.0f;
// rownumber loop is for selecting row
for (int rownumber = 0; rownumber < 4; rownumber++) {
// Spawn starting position for Rownumber
startPos.add(deltax * (4 - rownumber), vectorY, deltaz);
// pinPosx loop is for vectorx position of bowling pin in a particular row
for (int pinPosx = 0; pinPosx <= rownumber; pinPosx++) {
EntityRef blockItem = blockItemFactory.newInstance(block);
blockItem.send(new DropItemEvent(startPos));
if (pinPosx < rownumber) {
// drift of position in vector x coordinate, for the last pin stop drifting
startPos.add(2 * deltax, 0, 0);
}
}
// returns to start position
startPos.add(-deltax * (rownumber + 4), 0, 0);
}
return "prepared 10 " + blockName + " in a bowling pin pattern :)";
}
use of org.terasology.engine.entitySystem.entity.EntityRef 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");
}
use of org.terasology.engine.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.
the class UsernameSuggester method suggest.
@Override
public Set<String> suggest(EntityRef sender, Object... resolvedParameters) {
Set<String> clientNames = Sets.newHashSet();
for (EntityRef clientInfo : entityManager.getEntitiesWith(ClientInfoComponent.class)) {
DisplayNameComponent displayNameComponent = clientInfo.getComponent(DisplayNameComponent.class);
clientNames.add(displayNameComponent.name);
}
return clientNames;
}
use of org.terasology.engine.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.
the class CyclingTabCompletionEngine method findMatches.
private Set<String> findMatches(Name commandName, List<String> commandParameters, ConsoleCommand command, int suggestedIndex) {
if (suggestedIndex <= 0) {
updateCommandNamesIfNecessary();
return StringUtility.wildcardMatch(commandName.toString(), commandNames, true);
} else if (command == null) {
return null;
}
List<String> finishedParameters = Lists.newArrayList();
for (int i = 0; i < suggestedIndex - 1; i++) {
finishedParameters.add(commandParameters.get(i));
}
String currentValue = commandParameters.size() >= suggestedIndex ? commandParameters.get(suggestedIndex - 1) : null;
EntityRef sender = localPlayer.getClientEntity();
try {
return command.suggest(currentValue, finishedParameters, sender);
} catch (CommandSuggestionException e) {
String causeMessage = e.getLocalizedMessage();
if (causeMessage == null) {
Throwable cause = e.getCause();
causeMessage = cause.getLocalizedMessage();
if (causeMessage == null || causeMessage.isEmpty()) {
causeMessage = cause.toString();
if (causeMessage == null || causeMessage.isEmpty()) {
return null;
}
}
}
console.addMessage("Error when suggesting command: " + causeMessage, CoreMessageType.ERROR);
return null;
}
}
Aggregations