import {Gamesystem} from "./Gamesystem"; import {ProductState} from "./states/ProductState"; import {ProductTransition} from "./transitions/ProductTransition"; import {State} from "./states/State"; import {Transition} from "./transitions/Transition"; import {SimpleState} from "./states/SimpleState"; import {SimpleGamesystem} from "./SimpleGamesystem"; import {GameModel} from "../GameModel"; import {ScriptAccountCondition} from "./conditions/ScriptAccountCondition"; import {ScriptAccountAction} from "./actions/ScriptAccountAction"; import {ProductSystemGenerator} from "./productSystemGenerator/ProductSystemGenerator"; import {TemplateType} from "../templates/TemplateType"; import {ProductTemplateSystem} from "../templates/productGamesystem/ProductTemplateSystem"; import {IsolatedProductStateGenerator} from "./productSystemGenerator/IsolatedProductStateGenerator"; export class ProductGamesystem extends Gamesystem { innerGamesystems: Gamesystem, Transition>[] = []; static constructFromSimpleGamesystem(simpleGamesystem: SimpleGamesystem, gameModel: GameModel) { const productGamesystem = new ProductGamesystem(simpleGamesystem.componentName, simpleGamesystem.componentDescription); productGamesystem.constructHierarchyFromSimpleGamesystem(simpleGamesystem, gameModel); return productGamesystem; } constructHierarchyFromSimpleGamesystem(simpleGamesystem: SimpleGamesystem, gameModel: GameModel) { const parentGamesystem = simpleGamesystem.parentGamesystem; if(simpleGamesystem.states.length > 0) { simpleGamesystem.componentName += "(Child)"; this.addChildGamesystem(simpleGamesystem); simpleGamesystem.parentGamesystem = this } if(parentGamesystem != undefined) { parentGamesystem.removeChildGamesystem(simpleGamesystem); parentGamesystem.addChildGamesystem(this); this.parentGamesystem = parentGamesystem } else { gameModel.removeGamesystem(simpleGamesystem); gameModel.addGamesystem(this); } } createState(innerStates: State[]): ProductState | undefined { if(innerStates.length == this.innerGamesystems.length && this.findProductStateByInnerStates(innerStates)== undefined) { const productState = new ProductState(innerStates); this.states.push(productState); return productState; } else { return undefined; } } createTransition(startingState: ProductState, endingState: ProductState): ProductTransition | undefined { if(startingState == undefined || endingState == undefined || this.existsTransition(startingState, endingState) || !this.existsState(startingState) || !this.existsState(endingState) || startingState.equals(endingState)) { return undefined; } const productTransition = new ProductTransition(startingState, endingState); this.transitions.push(productTransition); return productTransition; } removeState(state: ProductState): boolean { return false; } removeInnerState(state: SimpleState) { } generateFromChildsystems() { const productGenerator = new ProductSystemGenerator(this); productGenerator.generateFromChildsystems(); if(this.generateIsolatedStates) { const isolatedStatesGenerator = new IsolatedProductStateGenerator(this); isolatedStatesGenerator.generateIsolatedProductStates(); } } addChildGamesystem(gamesystem: Gamesystem, Transition>) { this.innerGamesystems.push(gamesystem); } removeChildGamesystem(gamesystem: Gamesystem, Transition>) { this.innerGamesystems = this.innerGamesystems.filter(childSystem => childSystem.componentName !== gamesystem.componentName); } findProductStateByInnerStates(innerStates: State[]) { return this.states.find(productState => productState.equalInnerStates(innerStates)); } findChildSystemByName(gamesystemName: string) { const gamesystemQueue: Gamesystem, Transition>[] = []; this.innerGamesystems.forEach(gamesystem => gamesystemQueue.push(gamesystem)); while(gamesystemName.length > 0 ){ const currentGamesystem = gamesystemQueue.shift(); if(currentGamesystem!.componentName === gamesystemName) { return currentGamesystem; } } return undefined; } private existsTransition(startingState: ProductState, endingState: ProductState) { return this.transitions.find(transition => transition.startingState.equals(startingState) && transition.endingState.equals(endingState)) != undefined; } private existsState(state: ProductState) { return this.states.find(s => s.equals(state)) != undefined; } }