206 lines
8.1 KiB
TypeScript
206 lines
8.1 KiB
TypeScript
import {Gamesystem} from "./gamesystems/Gamesystem";
|
|
import {ScriptAccount} from "./scriptAccounts/ScriptAccount";
|
|
import {Transition} from "./gamesystems/transitions/Transition";
|
|
import {State} from "./gamesystems/states/State";
|
|
import {ProductGamesystem} from "./gamesystems/ProductGamesystem";
|
|
import {SimpleGamesystem} from "./gamesystems/SimpleGamesystem";
|
|
import {Character} from "./characters/Character";
|
|
import {ModelComponentType} from "./ModelComponentType";
|
|
import {TemplateType} from "./templates/TemplateType";
|
|
import {SimpleTemplateGamesystem} from "./templates/simpleGamesystem/SimpleTemplateGamesystem";
|
|
import {ProductTemplateSystem} from "./templates/productGamesystem/ProductTemplateSystem";
|
|
import {ProductTemplateCreator} from "./templates/productGamesystem/ProductTemplateCreator";
|
|
import {CharacterRelation} from "./characters/CharacterRelation";
|
|
|
|
export class GameModel {
|
|
gameModelName: string
|
|
|
|
gamesystems: Gamesystem<any, any>[] = [];
|
|
scriptAccounts: ScriptAccount[] = [];
|
|
characters: Character[] = []
|
|
|
|
constructor(gameModelName: string) {
|
|
this.gameModelName = gameModelName;
|
|
}
|
|
|
|
addGamesystem(gamesystem: Gamesystem<any, any>) {
|
|
if(this.findGamesystem(gamesystem.componentName) == undefined) {
|
|
this.gamesystems.push(gamesystem);
|
|
}
|
|
|
|
}
|
|
|
|
removeGamesystem(gamesystem : Gamesystem<any, any>) {
|
|
if(gamesystem.parentGamesystem == undefined) {
|
|
this.gamesystems = this.gamesystems.filter(g => g !== gamesystem);
|
|
} else {
|
|
(gamesystem.parentGamesystem as ProductGamesystem).removeChildGamesystem(gamesystem);
|
|
}
|
|
}
|
|
|
|
createScriptAccount(scriptAccountName: string) {
|
|
if(scriptAccountName != undefined && scriptAccountName.length > 0) {
|
|
const scriptAccount = new ScriptAccount(scriptAccountName, "");
|
|
const searchedScriptAccount = this.scriptAccounts.find(s => s.componentName === scriptAccount.componentName);
|
|
if(searchedScriptAccount == undefined) {
|
|
this.scriptAccounts.push(scriptAccount);
|
|
return scriptAccount;
|
|
}
|
|
}
|
|
return undefined;
|
|
}
|
|
|
|
createSimpleGamesystem(gamesystemName: string, templateType: TemplateType | undefined, pushToTop: boolean = true) {
|
|
if(gamesystemName != undefined && this.findGamesystem(gamesystemName) == undefined) {
|
|
let simpleGamesystem: SimpleGamesystem
|
|
|
|
if(templateType == undefined) {
|
|
simpleGamesystem = new SimpleGamesystem(gamesystemName, "")
|
|
} else {
|
|
simpleGamesystem = new SimpleTemplateGamesystem(gamesystemName, "", templateType)
|
|
}
|
|
|
|
if(pushToTop) {
|
|
this.gamesystems.push(simpleGamesystem)
|
|
}
|
|
return simpleGamesystem;
|
|
}
|
|
return undefined;
|
|
}
|
|
|
|
createSimpleGamesystemWithParent(gamesystemName: string, parentGamesystemName: string, templateType: TemplateType | undefined) {
|
|
const simpleGamesystem = this.createSimpleGamesystem(gamesystemName, templateType, false)!;
|
|
|
|
const parentGamesystem = this.findGamesystem(parentGamesystemName)
|
|
if(parentGamesystem == undefined) {return undefined}
|
|
|
|
let parentProductGamesystem: ProductGamesystem;
|
|
if(parentGamesystem instanceof SimpleTemplateGamesystem) {
|
|
console.log("Simple Template")
|
|
parentProductGamesystem = ProductTemplateCreator.constructTemplateFromSimpleGamesystem(parentGamesystem, this, templateType!)
|
|
} else if(parentGamesystem instanceof SimpleGamesystem) {
|
|
if(simpleGamesystem instanceof SimpleTemplateGamesystem) {
|
|
parentProductGamesystem = ProductTemplateCreator.constructTemplateFromSimpleGamesystem(parentGamesystem, this, templateType!)
|
|
} else {
|
|
console.log("Test")
|
|
parentProductGamesystem = ProductGamesystem.constructFromSimpleGamesystem(parentGamesystem, this);
|
|
console.log(this.gamesystems)
|
|
}
|
|
} else {
|
|
if(simpleGamesystem instanceof SimpleTemplateGamesystem) {
|
|
parentProductGamesystem = ProductTemplateCreator.convertProductToTemplate(parentGamesystem as ProductGamesystem, this, templateType!)
|
|
} else {
|
|
parentProductGamesystem = parentGamesystem as ProductGamesystem
|
|
}
|
|
|
|
}
|
|
|
|
parentProductGamesystem.addChildGamesystem(simpleGamesystem);
|
|
simpleGamesystem.parentGamesystem = parentProductGamesystem;
|
|
|
|
return simpleGamesystem;
|
|
}
|
|
|
|
|
|
|
|
createGamesystem(gamesystemName: string, parentGamesystemName: string | undefined, templateType: TemplateType | undefined) {
|
|
if(gamesystemName != undefined && this.findGamesystem(gamesystemName) == undefined) {
|
|
const simpleGamesystem = new SimpleGamesystem(gamesystemName, "");
|
|
|
|
if(parentGamesystemName != undefined) {
|
|
const parentGamesystem = this.findGamesystem(parentGamesystemName);
|
|
if(parentGamesystem instanceof SimpleGamesystem) {
|
|
const parentProductGamesystem = ProductGamesystem.constructFromSimpleGamesystem(parentGamesystem, this);
|
|
parentProductGamesystem.addChildGamesystem(simpleGamesystem);
|
|
simpleGamesystem.parentGamesystem = parentProductGamesystem;
|
|
} else {
|
|
const productParentGamesystem = parentGamesystem as ProductGamesystem;
|
|
productParentGamesystem.addChildGamesystem(simpleGamesystem);
|
|
simpleGamesystem.parentGamesystem = productParentGamesystem;
|
|
}
|
|
} else {
|
|
this.gamesystems.push(simpleGamesystem);
|
|
}
|
|
return simpleGamesystem;
|
|
}
|
|
}
|
|
|
|
createCharacter(characterName: string) {
|
|
const searchedCharacter = this.characters.find(character => character.componentName === characterName);
|
|
if(searchedCharacter == undefined) {
|
|
const character = new Character(characterName, "");
|
|
this.createCharacterRelation(character)
|
|
this.characters.push(character)
|
|
return character
|
|
}
|
|
return undefined
|
|
}
|
|
|
|
private createCharacterRelation(addedCharacter: Character) {
|
|
this.characters.forEach(character => {
|
|
const characterRelation = new CharacterRelation(character, addedCharacter);
|
|
character.addCharacterRelation(characterRelation);
|
|
addedCharacter.addCharacterRelation(characterRelation);
|
|
})
|
|
}
|
|
|
|
removeScriptAccount(scriptAccount: ScriptAccount) {
|
|
if(scriptAccount != undefined) {
|
|
this.scriptAccounts = this.scriptAccounts.filter(s => s != scriptAccount);
|
|
}
|
|
}
|
|
|
|
removeCharacter(character: Character) {
|
|
if(character != undefined) {
|
|
this.characters = this.characters.filter(c => c.componentName !== character.componentName)
|
|
}
|
|
}
|
|
|
|
findGamesystem(gamesystemName: string) {
|
|
const gamesystemQueue : Gamesystem<State<any>, Transition<any>>[] = [];
|
|
this.gamesystems.forEach(gamesystem => gamesystemQueue.push(gamesystem));
|
|
|
|
while(gamesystemQueue.length > 0) {
|
|
const currentGamesystem = gamesystemQueue.shift()!;
|
|
if(currentGamesystem.componentName == gamesystemName) {
|
|
return currentGamesystem;
|
|
} else if(currentGamesystem instanceof ProductGamesystem) {
|
|
const currentProductGamesystem = currentGamesystem as ProductGamesystem;
|
|
currentProductGamesystem.innerGamesystems.forEach(gamesystem => gamesystemQueue.push(gamesystem));
|
|
}
|
|
}
|
|
}
|
|
|
|
addScriptAccount(scriptAccount: ScriptAccount) {
|
|
this.scriptAccounts.push(scriptAccount);
|
|
}
|
|
|
|
generateProductSystemContents() {
|
|
this.gamesystems.forEach(gamesystem => {
|
|
if(gamesystem instanceof ProductGamesystem) {
|
|
gamesystem.generateFromChildsystems();
|
|
}
|
|
})
|
|
}
|
|
|
|
getTemplateSystems(templateType: TemplateType) {
|
|
const requestedTemplates: Gamesystem<any, any>[] = []
|
|
const gamesystemQueue: Gamesystem<any, any>[] = this.gamesystems.concat();
|
|
while(gamesystemQueue.length > 0) {
|
|
const currentGamesystem = gamesystemQueue.shift()!;
|
|
|
|
if(currentGamesystem instanceof SimpleTemplateGamesystem && currentGamesystem.templateType === templateType) {
|
|
requestedTemplates.push(currentGamesystem)
|
|
} else if(currentGamesystem instanceof ProductTemplateSystem && currentGamesystem.templateType === templateType) {
|
|
requestedTemplates.push(currentGamesystem)
|
|
}
|
|
|
|
if(currentGamesystem instanceof ProductGamesystem) {
|
|
currentGamesystem.innerGamesystems.forEach(innerGamesystem => gamesystemQueue.push(innerGamesystem))
|
|
}
|
|
}
|
|
|
|
return requestedTemplates;
|
|
}
|
|
}
|