import { Action } from "../Actions" ; import { ActionsOrderer , UnderEachTeam , TeamId } from "../Teams" ; import { Actions } from "./Actions" ; import { Animator , AnimatorSettings } from "./Animator" ; import { Introductions } from "./Introductions" ; import { Queue } from "./Queue" ; /**
 * Root animator for a battle.
 */ export class Main extends Animator { /**
     * Animator for teams' actions.
     */ private readonly actions = new Actions ( this ) ; /**
     * Animator for teams introducing themselves.
     */ private readonly introductions = new Introductions ( this ) ; /**
     * Orders teams' chosen actions.
     */ private readonly actionsOrderer : ActionsOrderer ; param settings param actionsOrderer public constructor ( settings , actionsOrderer ) { super ( settings ) ; this . actionsOrderer = actionsOrderer ; } /**
     * Starts the battle's gameplay.
     */ public run ( ) { const queue = new Queue ( ) ; queue . add ( ( next ) : void => { this . animations . start ( next ) ; } ) ; queue . add ( ( next ) : void => { this . introductions . run ( next ) ; } ) ; queue . run ( ( ) : void => { this . waitForActions ( ) ; } ) ; } /**
     * Waits for actions from each team's selector.
     */ private waitForActions ( ) { const actions = { } ; let completed = 0 ; const onChoice = ( ) : void => { completed += 1 ; if ( completed === 2 ) { this . executeActions ( actions as UnderEachTeam < Action > ) ; } } ; this . battleInfo . teams . opponent . selector . nextAction ( this . battleInfo , TeamId . opponent , ( action ) : void => { actions [ TeamId [ TeamId . opponent ] ] = action ; onChoice ( ) ; } ) ; this . battleInfo . teams . player . selector . nextAction ( this . battleInfo , TeamId . player , ( action ) : void => { actions [ TeamId [ TeamId . player ] ] = action ; onChoice ( ) ; } ) ; } param actions private executeActions ( actions ) { const queue = new Queue ( ) ; for ( const action of this . actionsOrderer ( actions ) ) { queue . add ( ( next ) : void => { this . actions . run ( action , next ) ; } ) ; } queue . run ( ( ) : void => { this . waitForActions ( ) ; } ) ; } }