Search in sources :

Example 1 with BehaviorTree

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);
        }
    }
}
Also used : Actor(org.terasology.logic.behavior.core.Actor) BehaviorTree(org.terasology.logic.behavior.asset.BehaviorTree)

Example 2 with BehaviorTree

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;
}
Also used : BehaviorTreeData(org.terasology.logic.behavior.asset.BehaviorTreeData) BehaviorTree(org.terasology.logic.behavior.asset.BehaviorTree) ResourceUrn(org.terasology.assets.ResourceUrn) Name(org.terasology.naming.Name)

Example 3 with 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();
    }
}
Also used : AssetManager(org.terasology.assets.management.AssetManager) GsonBuilder(com.google.gson.GsonBuilder) UriTypeAdapterFactory(org.terasology.utilities.gson.UriTypeAdapterFactory) JsonReader(com.google.gson.stream.JsonReader) IOException(java.io.IOException) BehaviorTree(org.terasology.logic.behavior.asset.BehaviorTree) ResourceUrn(org.terasology.assets.ResourceUrn) JsonWriter(com.google.gson.stream.JsonWriter)

Example 4 with BehaviorTree

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);
        }
    }
}
Also used : ResourceUrn(org.terasology.assets.ResourceUrn) BehaviorTree(org.terasology.logic.behavior.asset.BehaviorTree)

Example 5 with BehaviorTree

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();
}
Also used : Interpreter(org.terasology.logic.behavior.Interpreter) ReadOnlyBinding(org.terasology.rendering.nui.databinding.ReadOnlyBinding) UIList(org.terasology.rendering.nui.widgets.UIList) BehaviorTree(org.terasology.logic.behavior.asset.BehaviorTree) PropertyLayout(org.terasology.rendering.nui.layouts.PropertyLayout) PropertyProvider(org.terasology.rendering.nui.properties.PropertyProvider) UIDropdown(org.terasology.rendering.nui.widgets.UIDropdown) UIList(org.terasology.rendering.nui.widgets.UIList) List(java.util.List)

Aggregations

BehaviorTree (org.terasology.logic.behavior.asset.BehaviorTree)7 ResourceUrn (org.terasology.assets.ResourceUrn)3 BehaviorTreeData (org.terasology.logic.behavior.asset.BehaviorTreeData)3 IOException (java.io.IOException)2 AssetManager (org.terasology.assets.management.AssetManager)2 Prefab (org.terasology.entitySystem.prefab.Prefab)2 PrefabData (org.terasology.entitySystem.prefab.PrefabData)2 PojoPrefab (org.terasology.entitySystem.prefab.internal.PojoPrefab)2 UIData (org.terasology.rendering.nui.asset.UIData)2 UIElement (org.terasology.rendering.nui.asset.UIElement)2 UISkin (org.terasology.rendering.nui.skin.UISkin)2 UISkinData (org.terasology.rendering.nui.skin.UISkinData)2 DefaultBlockFamilyFactoryRegistry (org.terasology.world.block.family.DefaultBlockFamilyFactoryRegistry)2 BlockFamilyDefinition (org.terasology.world.block.loader.BlockFamilyDefinition)2 BlockFamilyDefinitionData (org.terasology.world.block.loader.BlockFamilyDefinitionData)2 BlockFamilyDefinitionFormat (org.terasology.world.block.loader.BlockFamilyDefinitionFormat)2 BlockShape (org.terasology.world.block.shapes.BlockShape)2 BlockShapeData (org.terasology.world.block.shapes.BlockShapeData)2 BlockSounds (org.terasology.world.block.sounds.BlockSounds)2 BlockSoundsData (org.terasology.world.block.sounds.BlockSoundsData)2