use of org.terasology.logic.behavior.asset.BehaviorTree in project Terasology by MovingBlocks.
the class BehaviorSystem method addEntity.
private void addEntity(EntityRef entityRef, BehaviorComponent behaviorComponent) {
if (behaviorComponent.interpreter == null) {
behaviorComponent.interpreter = new Interpreter(new Actor(entityRef));
BehaviorTree tree = behaviorComponent.tree;
if (tree != null) {
behaviorComponent.interpreter.setTree(tree);
}
}
}
use of org.terasology.logic.behavior.asset.BehaviorTree in project Terasology by MovingBlocks.
the class BehaviorSystem method createTree.
public BehaviorTree createTree(String name, BehaviorNode root) {
BehaviorTreeData data = new BehaviorTreeData();
data.setRoot(root);
BehaviorTree behaviorTree = assetManager.loadAsset(new ResourceUrn(BEHAVIORS, new Name(name.replaceAll("\\W+", ""))), data, BehaviorTree.class);
trees.add(behaviorTree);
save(behaviorTree);
return behaviorTree;
}
use of org.terasology.logic.behavior.asset.BehaviorTree in project Terasology by MovingBlocks.
the class BehaviorTreeBuilder method initGson.
private void initGson() {
if (gson == null) {
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeHierarchyAdapter(BehaviorNode.class, this);
gsonBuilder.registerTypeAdapterFactory(new UriTypeAdapterFactory());
gsonBuilder.registerTypeAdapter(BehaviorTree.class, new TypeAdapter<BehaviorTree>() {
@Override
public void write(JsonWriter out, BehaviorTree value) throws IOException {
if (value != null) {
// TODO doublecheck URN
out.value(value.getUrn().toString());
} else {
out.value("");
}
}
@Override
public BehaviorTree read(JsonReader in) throws IOException {
String uri = in.nextString();
AssetManager assetManager = CoreRegistry.get(AssetManager.class);
return assetManager.getAsset(new ResourceUrn(uri), BehaviorTree.class).orElse(assetManager.getAsset(new ResourceUrn("Behaviors:fallback"), BehaviorTree.class).get());
}
});
gson = gsonBuilder.create();
}
}
use of org.terasology.logic.behavior.asset.BehaviorTree in project Terasology by MovingBlocks.
the class BehaviorSystem method initialise.
@Override
public void initialise() {
List<ResourceUrn> uris = Lists.newArrayList();
uris.addAll(new ArrayList<>(assetManager.getAvailableAssets(StaticSound.class)));
for (ResourceUrn uri : assetManager.getAvailableAssets(BehaviorTree.class)) {
try {
Optional<BehaviorTree> asset = assetManager.getAsset(uri, BehaviorTree.class);
asset.ifPresent(behaviorTree -> trees.add(behaviorTree));
} catch (RuntimeException e) {
logger.info("Failed to load behavior tree asset {}.", uri, e);
}
}
}
use of org.terasology.logic.behavior.asset.BehaviorTree in project Terasology by MovingBlocks.
the class BehaviorEditorScreen method initialise.
@Override
public void initialise() {
entityProperties = find("entity_properties", PropertyLayout.class);
behaviorEditor = find("tree", BehaviorEditor.class);
properties = find("properties", PropertyLayout.class);
selectTree = find("select_tree", UIDropdown.class);
selectEntity = find("select_entity", UIDropdown.class);
palette = find("palette", UIList.class);
behaviorEditor.initialize(context);
behaviorEditor.bindSelection(new Binding<RenderableNode>() {
private PropertyProvider provider = new PropertyProvider();
@Override
public RenderableNode get() {
return selectedNode;
}
@Override
public void set(RenderableNode value) {
onNodeSelected(value, provider);
}
});
Binding<List<BehaviorTree>> treeBinding = new ReadOnlyBinding<List<BehaviorTree>>() {
@Override
public List<BehaviorTree> get() {
return behaviorSystem.getTrees();
}
};
selectTree.bindOptions(treeBinding);
providerFactory.register("behaviorTrees", treeBinding);
selectTree.bindSelection(new Binding<BehaviorTree>() {
@Override
public BehaviorTree get() {
return behaviorEditor.getTree();
}
@Override
public void set(BehaviorTree value) {
onTreeSelected(value);
}
});
selectEntity.bindOptions(new ReadOnlyBinding<List<Interpreter>>() {
@Override
public List<Interpreter> get() {
return behaviorSystem.getInterpreters();
}
});
selectEntity.bindSelection(new Binding<Interpreter>() {
private PropertyProvider provider = new PropertyProvider();
@Override
public Interpreter get() {
return selectedInterpreter;
}
@Override
public void set(Interpreter value) {
onEntitySelected(value, provider);
}
});
palette.bindSelection(new Binding<BehaviorNodeComponent>() {
@Override
public BehaviorNodeComponent get() {
return null;
}
@Override
public void set(BehaviorNodeComponent value) {
onPaletteSelected(value);
}
});
palette.bindList(new ReadOnlyBinding<List<BehaviorNodeComponent>>() {
@Override
public List<BehaviorNodeComponent> get() {
return paletteItems;
}
});
palette.setItemRenderer(new ToStringTextRenderer<BehaviorNodeComponent>() {
@Override
public String getTooltip(BehaviorNodeComponent value) {
return value.description;
}
});
WidgetUtil.trySubscribe(this, "copy", button -> onCopyPressed());
WidgetUtil.trySubscribe(this, "layout", button -> onLayoutPressed());
WidgetUtil.trySubscribe(this, "new", button -> onNewPressed());
WidgetUtil.trySubscribe(this, "assign", button -> onAssignPressed());
WidgetUtil.trySubscribe(this, "remove", button -> onRemovePressed());
WidgetUtil.trySubscribe(this, "debug_run", button -> {
if (selectedInterpreter != null) {
selectedInterpreter.run();
}
});
WidgetUtil.trySubscribe(this, "debug_pause", button -> {
if (selectedInterpreter != null) {
selectedInterpreter.pause();
}
});
WidgetUtil.trySubscribe(this, "debug_reset", button -> {
if (selectedInterpreter != null) {
selectedInterpreter.reset();
}
});
WidgetUtil.trySubscribe(this, "debug_step", button -> {
if (selectedInterpreter != null) {
selectedInterpreter.tick(0.1f);
}
});
paletteItems = findPaletteItems();
}
Aggregations