Search in sources :

Example 1 with BehaviorNode

use of org.terasology.engine.logic.behavior.core.BehaviorNode in project Terasology by MovingBlocks.

the class CountCallsTest method assertBT.

public void assertBT(String tree, List<BehaviorState> result, List<Integer> executed, boolean step) {
    BehaviorNode node = fromJson(tree);
    node.construct(null);
    List<BehaviorState> actualStates = Lists.newArrayList();
    for (int i = 0; i < result.size(); i++) {
        BehaviorState state = node.execute(null);
        actualStates.add(state);
    }
    node.destruct(null);
    assertEquals(result, actualStates);
    assertEquals(executed, executeCalled);
}
Also used : BehaviorState(org.terasology.engine.logic.behavior.core.BehaviorState) BehaviorNode(org.terasology.engine.logic.behavior.core.BehaviorNode)

Example 2 with BehaviorNode

use of org.terasology.engine.logic.behavior.core.BehaviorNode in project Terasology by MovingBlocks.

the class Example method main.

public static void main(String[] args) {
    BehaviorTreeBuilder treeBuilder = new BehaviorTreeBuilder();
    BehaviorNode node = treeBuilder.fromJson("{ sequence:[ success, success, failure ] }");
    logger.info("{}", new DefaultBehaviorTreeRunner(node, null).step());
    treeBuilder.registerAction("print", Print.class);
    node = treeBuilder.fromJson("{ sequence:[ success, { print:{msg:world} } ] }");
    logger.info("{}", new DefaultBehaviorTreeRunner(node, null).step());
    treeBuilder.registerAction("delay", Delay.class);
    Actor actor = new Actor(null);
    actor.setDelta(0.1f);
    node = treeBuilder.fromJson("{ sequence:[ success, { delay:{duration:1}}, { print:{msg:Hello} }, " + "{ delay:{duration:1}}, { print:{msg:World} } ] }");
    DefaultBehaviorTreeRunner tree = new DefaultBehaviorTreeRunner(node, actor);
    for (int i = 0; i < 100; i++) {
        tree.step();
    }
    treeBuilder.registerDecorator("repeat", Repeat.class);
    actor = new Actor(null);
    actor.setDelta(0.1f);
    node = treeBuilder.fromJson("{ sequence:[ " + "{repeat :{ count:5, child:{print:{msg:x}}}}, success, { delay:{duration:1}}, " + "{ print:{msg:Hello} }, { delay:{duration:1}}, { print:{msg:World} } " + "] }");
    tree = new DefaultBehaviorTreeRunner(node, actor);
    for (int i = 0; i < 100; i++) {
        tree.step();
    }
}
Also used : BehaviorTreeBuilder(org.terasology.engine.logic.behavior.core.BehaviorTreeBuilder) Actor(org.terasology.engine.logic.behavior.core.Actor) BehaviorNode(org.terasology.engine.logic.behavior.core.BehaviorNode) Print(org.terasology.engine.logic.behavior.actions.Print)

Example 3 with BehaviorNode

use of org.terasology.engine.logic.behavior.core.BehaviorNode in project Terasology by MovingBlocks.

the class BehaviorTreeFormat method load.

public BehaviorTreeData load(InputStream stream) {
    BehaviorTreeBuilder builder = CoreRegistry.get(BehaviorTreeBuilder.class);
    if (builder == null) {
        builder = new BehaviorTreeBuilder();
        CoreRegistry.put(BehaviorTreeBuilder.class, builder);
    }
    BehaviorNode node = builder.fromJson(stream);
    BehaviorTreeData data = new BehaviorTreeData();
    data.setRoot(node);
    return data;
}
Also used : BehaviorTreeBuilder(org.terasology.engine.logic.behavior.core.BehaviorTreeBuilder) BehaviorNode(org.terasology.engine.logic.behavior.core.BehaviorNode)

Example 4 with BehaviorNode

use of org.terasology.engine.logic.behavior.core.BehaviorNode in project Terasology by MovingBlocks.

the class BehaviorEditor method createNode.

public RenderableNode createNode(BehaviorNodeComponent data) {
    if (tree == null) {
        return null;
    }
    BehaviorNode node = behaviorNodeFactory.createNode(data);
    newNode = createRenderableNode(node);
    behaviorSystem.treeModified(tree);
    return newNode;
}
Also used : BehaviorNode(org.terasology.engine.logic.behavior.core.BehaviorNode)

Example 5 with BehaviorNode

use of org.terasology.engine.logic.behavior.core.BehaviorNode in project Terasology by MovingBlocks.

the class BehaviorEditor method copyNode.

public void copyNode(RenderableNode node) {
    BehaviorTreeBuilder treeBuilder = CoreRegistry.get(BehaviorTreeBuilder.class);
    String json = treeBuilder.toJson(node.getNode());
    BehaviorNode nodeCopy = treeBuilder.fromJson(json);
    List<RenderableNode> renderables = createRenderables(nodeCopy);
    if (renderables.size() > 0) {
        newNode = renderables.get(0);
        Vector2f oldPos = new Vector2f(newNode.getPosition());
        layout(newNode);
        oldPos.sub(newNode.getPosition());
        newNode.move(oldPos);
    }
}
Also used : BehaviorTreeBuilder(org.terasology.engine.logic.behavior.core.BehaviorTreeBuilder) Vector2f(org.joml.Vector2f) BehaviorNode(org.terasology.engine.logic.behavior.core.BehaviorNode)

Aggregations

BehaviorNode (org.terasology.engine.logic.behavior.core.BehaviorNode)9 BehaviorTreeBuilder (org.terasology.engine.logic.behavior.core.BehaviorTreeBuilder)4 Print (org.terasology.engine.logic.behavior.actions.Print)2 Actor (org.terasology.engine.logic.behavior.core.Actor)2 GsonBuilder (com.google.gson.GsonBuilder)1 Vector2f (org.joml.Vector2f)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1 ActionNode (org.terasology.engine.logic.behavior.core.ActionNode)1 BehaviorState (org.terasology.engine.logic.behavior.core.BehaviorState)1 BehaviorTreeRunner (org.terasology.engine.logic.behavior.core.BehaviorTreeRunner)1