import BehaviorTreeStatus from "../BehaviorTreeStatus" ; import StateData from "../StateData" ; import BehaviorTreeNodeInterface from "./BehaviorTreeNodeInterface" ; import ParentBehaviorTreeNodeInterface from "./ParentBehaviorTreeNodeInterface" ; property property property export default class ParallelNode implements ParentBehaviorTreeNodeInterface { type { BehaviorTreeNodeInterface [ ] } private children : BehaviorTreeNodeInterface [ ] = [ ] ; public constructor ( public readonly name , public readonly requiredToFail , public readonly requiredToSucceed ) { } public async tick ( state ) { const statuses = await Promise . all ( this . children . map ( ( c ) => this . tickChildren ( state , c ) ) ) ; const succeeded = statuses . filter ( ( x ) => x === BehaviorTreeStatus . Success ) . length ; const failed = statuses . filter ( ( x ) => x === BehaviorTreeStatus . Failure ) . length ; if ( this . requiredToSucceed > 0 && succeeded >= this . requiredToSucceed ) { return BehaviorTreeStatus . Success ; } if ( this . requiredToFail > 0 && failed >= this . requiredToFail ) { return BehaviorTreeStatus . Failure ; } return BehaviorTreeStatus . Running ; } public addChild ( child ) { this . children . push ( child ) ; } private async tickChildren ( state , child ) { try { return await child . tick ( state ) ; } catch ( e ) { return BehaviorTreeStatus . Failure ; } } }