BehaviorTree extends Task<E> { public void step() { if (rootTask.status == Status.RUNNING) { rootTask.run(); } else { rootTask.setControl(this); rootTask.start(); if (rootTask.checkGuard(this)) { rootTask.run(); } else { rootTask.fail(); } } } BehaviorTree();  BehaviorTree(Task<E> rootTask);  BehaviorTree(Task<E> rootTask, E object); @Override E getObject(); void setObject(E object); @Override int getChildCount(); @Override Task<E> getChild(int i); @Override void childRunning(Task<E> runningTask, Task<E> reporter); @Override void childFail(Task<E> runningTask); @Override void childSuccess(Task<E> runningTask); void step(); void notifyChildAdded(Task<E> task, int index); void notifyStatusUpdated(Task<E> task, Status previousStatus); @Override void reset(); @Override void run(); @Override void resetTask(); void addListener(Listener<E> listener); void removeListener(Listener<E> listener); void removeListeners(); public List<Listener<E>> listeners; }
@Test public void testDogBehavior() throws Exception{ Selector<Dog> selector = new Selector<Dog>(); Parallel<Dog> parallel = new Parallel<Dog>(); selector.addChild(parallel); CareTask care = new CareTask(); care.urgentProb = 0.8f; parallel.addChild(care); parallel.addChild(new AlwaysFail<Dog>(new RestTask())); Sequence<Dog> sequence = new Sequence<Dog>(); selector.addChild(sequence); BarkTask bark1 = new BarkTask(); bark1.times = MathUtil.random(1, 5); sequence.addChild(bark1); sequence.addChild(new WalkTask()); sequence.addChild(new BarkTask()); sequence.addChild(new MarkTask()); BehaviorTree<Dog> tree=new BehaviorTree<Dog>(selector, new Dog("Buddy")); int setp=0; while(true) { System.out.println("步骤："+(++setp)); tree.step(); Thread.sleep(1000); } }