ConceptCreator/src/app/game-model/fs/parser/SimpleGamesystemParser.ts
Sebastian Böckelmann 3b0d4e0194
All checks were successful
E2E Testing / test (push) Successful in 1m34s
Load Gamesystems from Filesystem
2024-02-17 14:32:19 +01:00

48 lines
1.9 KiB
TypeScript

import {SimpleGamesystem} from "../../gamesystems/SimpleGamesystem";
import {SimpleState} from "../../gamesystems/SimpleState";
import {SimpleTransition} from "../../gamesystems/SimpleTransition";
export class SimpleGamesystemParser {
static parseSimpleGamesystem(jsonObject: any) : SimpleGamesystem {
const gamesystemName = jsonObject.componentName;
const gamesystemDescription = jsonObject.componentDescription;
const simpleStates = SimpleGamesystemParser.parseSimpleStates(jsonObject)
const simpleTransitions = SimpleGamesystemParser.parseSimpleTransitions(jsonObject, simpleStates);
const gamesystem = new SimpleGamesystem(gamesystemName, gamesystemDescription);
gamesystem.states = simpleStates;
gamesystem.transitions = simpleTransitions;
return gamesystem;
}
static parseSimpleStates(jsonObject: any): SimpleState[] {
const states: SimpleState[] = [];
for(let i=0; i<jsonObject.states.length; i++) {
const state = new SimpleState("", "");
Object.assign(state, jsonObject.states[i]);
states.push(state);
}
return states;
}
static parseSimpleTransitions(jsonObject: any, states: SimpleState[]): SimpleTransition[] {
const transitions: SimpleTransition[] = [];
for(let i=0; i<jsonObject.transitions.length; i++) {
const startingStateLabel = jsonObject.transitions[i].startingState;
const endingStateLabel = jsonObject.transitions[i].endingState;
const startingState = states.find(state => state.stateLabel === startingStateLabel);
const endingState = states.find(state => state.stateLabel === endingStateLabel);
if(startingState != undefined && endingState != undefined) {
transitions.push(new SimpleTransition(startingState, endingState));
} else {
console.error("Starting or Ending State are not defined!", startingState, endingState)
}
}
return transitions;
}
}