Search in sources :

Example 1 with Actor

use of org.terasology.engine.logic.behavior.core.Actor 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 2 with Actor

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

the class CollectiveBehaviorSystem method addEntity.

private void addEntity(EntityRef entityRef, CollectiveBehaviorComponent collectiveInterpreter) {
    if (collectiveInterpreter.collectiveInterpreter == null) {
        Set<Actor> newActors = new HashSet<>();
        newActors.add(new Actor(entityRef));
        collectiveInterpreter.collectiveInterpreter = new CollectiveInterpreter(newActors);
        BehaviorTree tree = collectiveInterpreter.tree;
        if (tree != null) {
            collectiveInterpreter.collectiveInterpreter.setTree(tree);
        }
    }
}
Also used : Actor(org.terasology.engine.logic.behavior.core.Actor) BehaviorTree(org.terasology.engine.logic.behavior.asset.BehaviorTree) HashSet(java.util.HashSet)

Example 3 with Actor

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

the class CounterTest method assertRun.

private void assertRun(String tree, int executions, String expectedOutput) {
    Print.output = new StringBuilder();
    BehaviorNode node = treeBuilder.fromJson(tree);
    String json = treeBuilder.toJson(node);
    BehaviorNode n2 = treeBuilder.fromJson(json);
    String json2 = treeBuilder.toJson(n2);
    assertEquals(json, json2);
    Actor actor = new Actor(null);
    actor.setDelta(0.5f);
    BehaviorTreeRunner runner = new DefaultBehaviorTreeRunner(node, actor);
    for (int i = 0; i < executions; i++) {
        runner.step();
    }
    assertEquals(expectedOutput, Print.output.toString());
}
Also used : Actor(org.terasology.engine.logic.behavior.core.Actor) BehaviorNode(org.terasology.engine.logic.behavior.core.BehaviorNode) BehaviorTreeRunner(org.terasology.engine.logic.behavior.core.BehaviorTreeRunner) Print(org.terasology.engine.logic.behavior.actions.Print)

Example 4 with Actor

use of org.terasology.engine.logic.behavior.core.Actor 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.engine.logic.behavior.core.Actor) BehaviorTree(org.terasology.engine.logic.behavior.asset.BehaviorTree)

Aggregations

Actor (org.terasology.engine.logic.behavior.core.Actor)4 Print (org.terasology.engine.logic.behavior.actions.Print)2 BehaviorTree (org.terasology.engine.logic.behavior.asset.BehaviorTree)2 BehaviorNode (org.terasology.engine.logic.behavior.core.BehaviorNode)2 HashSet (java.util.HashSet)1 BehaviorTreeBuilder (org.terasology.engine.logic.behavior.core.BehaviorTreeBuilder)1 BehaviorTreeRunner (org.terasology.engine.logic.behavior.core.BehaviorTreeRunner)1