Skip to content

Commit

Permalink
Add a dropdown field to select the entity type that is being added. (#…
Browse files Browse the repository at this point in the history
…1648)

* Add a combobox to add armor stands, lecterns, books and beacon beams.

* Refactor to make entity types more generic.
  • Loading branch information
leMaik authored Oct 27, 2023
1 parent 26cad1e commit 3b7bd9b
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 27 deletions.
5 changes: 5 additions & 0 deletions chunky/src/java/se/llbit/chunky/renderer/scene/Scene.java
Original file line number Diff line number Diff line change
Expand Up @@ -2750,6 +2750,11 @@ public MinecraftProfile getPlayerProfile(PlayerEntity player) {
return entities.getAssociatedProfile(player);
}

public void addActor(Entity entity) {
entities.addActor(entity);
rebuildActorBvh();
}

public void removeEntity(Entity entity) {
entities.removeEntity(entity);
rebuildActorBvh();
Expand Down
74 changes: 49 additions & 25 deletions chunky/src/java/se/llbit/chunky/ui/render/tabs/EntitiesTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
Expand All @@ -53,7 +52,6 @@
import se.llbit.chunky.ui.IntegerAdjuster;
import se.llbit.chunky.ui.IntegerTextField;
import se.llbit.chunky.ui.controller.RenderControlsFxController;
import se.llbit.chunky.ui.elements.TextFieldLabelWrapper;
import se.llbit.chunky.ui.render.RenderControlsTab;
import se.llbit.chunky.world.material.BeaconBeamMaterial;
import se.llbit.fx.LuxColorPicker;
Expand All @@ -63,14 +61,44 @@
import se.llbit.log.Log;
import se.llbit.math.ColorUtil;
import se.llbit.math.Vector3;
import se.llbit.nbt.CompoundTag;
import se.llbit.util.mojangapi.MinecraftProfile;
import se.llbit.util.mojangapi.MinecraftSkin;
import se.llbit.util.mojangapi.MojangApi;

public class EntitiesTab extends ScrollPane implements RenderControlsTab, Initializable {
private static final Map<String, EntitiesTab.EntityType<?>> entityTypes = new HashMap<>();

static {
entityTypes.put("Player", (position, scene) -> {
Collection<Entity> entities = scene.getActors();
Set<String> ids = new HashSet<>();
for (Entity entity : entities) {
if (entity instanceof PlayerEntity) {
ids.add(((PlayerEntity) entity).uuid);
}
}
// Pick a new UUID for the new entity.
long id = System.currentTimeMillis();
while (ids.contains(String.format("%016X%016X", 0, id))) {
id += 1;
}
PlayerEntity player = new PlayerEntity(String.format("%016X%016X", 0, id), position);
player.randomPoseAndLook();
return player;
});
entityTypes.put("Armor stand", (position, scene) -> new ArmorStand(position, new CompoundTag()));
entityTypes.put("Lectern", (position, scene) -> new Lectern(position, "north", true));
entityTypes.put("Book", (position, scene) -> new Book(position, Math.PI - Math.PI / 16, Math.toRadians(30), Math.toRadians(180 - 30)));
entityTypes.put("Beacon beam", (position, scene) -> new BeaconBeam(position));
}

private Scene scene;

public interface EntityType<T extends Entity> {
T createInstance(Vector3 position, Scene scene);
}

public static class EntityData {

public final Entity entity;
Expand Down Expand Up @@ -134,6 +162,7 @@ public String getKind() {
@FXML private DoubleTextField posY;
@FXML private DoubleTextField posZ;
@FXML private VBox controls;
@FXML private ComboBox<String> entityType;

public EntitiesTab() throws IOException {
FXMLLoader loader = new FXMLLoader(getClass().getResource("EntitiesTab.fxml"));
Expand Down Expand Up @@ -566,34 +595,29 @@ else if(entity instanceof BeaconBeam) {

@Override
public void initialize(URL location, ResourceBundle resources) {
add.setTooltip(new Tooltip("Add a player at the target position."));
entityType.getItems().addAll(entityTypes.keySet());
entityType.setValue("Player");
add.setTooltip(new Tooltip("Add an entity at the target position."));
add.setOnAction(e -> {
Collection<Entity> entities = scene.getActors();
Set<String> ids = new HashSet<>();
for (Entity entity : entities) {
if (entity instanceof PlayerEntity) {
ids.add(((PlayerEntity) entity).uuid);
}
}
// Pick a new UUID for the new entity.
long id = System.currentTimeMillis();
while (ids.contains(String.format("%016X%016X", 0, id))) {
id += 1;
}
Vector3 position = scene.getTargetPosition();
if (position == null) {
position = new Vector3(scene.camera().getPosition());
}
PlayerEntity player = new PlayerEntity(String.format("%016X%016X", 0, id), position);
withEntity(selected -> {
if (selected instanceof PlayerEntity) {
player.skin = ((PlayerEntity) selected).skin;
player.model = ((PlayerEntity) selected).model;
}
});
player.randomPoseAndLook();
scene.addPlayer(player);
EntityData data = new EntityData(player, scene);

Entity entity = entityTypes.get(entityType.getValue()).createInstance(position, scene);
if (entity instanceof PlayerEntity) {
PlayerEntity player = (PlayerEntity) entity;
withEntity(selected -> {
if (selected instanceof PlayerEntity) {
player.skin = ((PlayerEntity) selected).skin;
player.model = ((PlayerEntity) selected).model;
}
});
scene.addPlayer(player);
} else {
scene.addActor(entity);
}
EntityData data = new EntityData(entity, scene);
entityTable.getItems().add(data);
entityTable.getSelectionModel().select(data);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.control.ComboBox?>

<fx:root type="javafx.scene.control.ScrollPane" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1">
<VBox spacing="10.0">
<padding>
Expand All @@ -27,8 +29,9 @@
</columns>
</TableView>
<HBox spacing="10.0">
<Button fx:id="delete" mnemonicParsing="false" text="-" />
<Button fx:id="add" mnemonicParsing="false" text="+" />
<ComboBox fx:id="entityType" prefWidth="150.0" />
<Button fx:id="add" mnemonicParsing="false" text="Add entity" />
<Button fx:id="delete" mnemonicParsing="false" text="Remove entity" />
</HBox>
<HBox spacing="10.0">
<Button fx:id="cameraToEntity" mnemonicParsing="false" text="Camera to entity" />
Expand Down

0 comments on commit 3b7bd9b

Please sign in to comment.