textadv
Version:
Text Adventures generator from Markdown files
72 lines • 2.1 kB
JavaScript
export var Type;
(function (Type) {
Type["project"] = "project";
Type["location"] = "location";
Type["object"] = "object";
})(Type = Type || (Type = {}));
export class Project {
constructor() {
this.type = Type.project;
this.id = '';
this.name = '';
this.onInput = [];
this.intro = [];
this.children = [];
this.initialRoomIndex = 0; // TODO: find a way to express "initialRoomIndex" on the markdown
}
static fromJSON(json) {
const project = new Project();
Object.assign(project, json);
return project;
}
addChild(type, id) {
let child = this.findChildById(id);
if (child && child.type !== type) {
throw new Error(`Duplicate node ${id} with different type`);
}
if (!child) {
child = {
type,
id,
name: id,
onInput: [],
intro: [],
};
this.children.push(child);
}
return child;
}
findChildById(id) {
for (const child of this.children) {
if (child.id === id) {
return child;
}
}
}
getChildren() {
return this.children
.map((child, index) => (Object.assign(Object.assign({}, child), { index })))
.reduce((prev, cur) => {
prev[cur.id] = cur;
return prev;
}, {});
}
getChildrenByType(type) {
return this.children
.map((child, index) => (Object.assign(Object.assign({}, child), { index })))
.filter((child) => child.type === type)
.reduce((prev, cur) => {
prev[cur.id] = cur;
return prev;
}, {});
}
getChild(index) {
return this.children[index];
}
getChildById(id) {
return this.children
.map((child, index) => (Object.assign(Object.assign({}, child), { index })))
.find((child) => child.id === id);
}
}
//# sourceMappingURL=types.js.map