add ability to move from room to room.
This commit is contained in:
78
Scene.ts
78
Scene.ts
@ -3,14 +3,55 @@
|
||||
*/
|
||||
|
||||
import type { Exit, StoryScene, Item, SceneProperties } from "./types.ts";
|
||||
import Vessel from "./Vessel.ts";
|
||||
import Entity from "./Entity.ts";
|
||||
|
||||
// The Scene is a type of "Vessel" which is a generic object that can have effects
|
||||
// The Scene is a type of "Entity" which is a generic object that can have effects
|
||||
// and items.
|
||||
export default class Scene extends Vessel<SceneProperties> {
|
||||
constructor(gameData: StoryScene<SceneProperties>) {
|
||||
// TODO: [] is a placeholder for scene effects.
|
||||
super(gameData.properties, gameData.conditions);
|
||||
export default class Scene extends Entity<SceneProperties> {
|
||||
protected _map: StoryScene<SceneProperties>[];
|
||||
protected _identifier: string;
|
||||
|
||||
constructor(identifier: string, map: StoryScene<SceneProperties>[]) {
|
||||
const scene = Scene.getScene(identifier, map);
|
||||
|
||||
if (!scene) {
|
||||
throw new Error("cannot find scene!");
|
||||
}
|
||||
|
||||
const { properties, conditions } = scene;
|
||||
|
||||
super(properties, conditions);
|
||||
|
||||
this._identifier = identifier;
|
||||
this._map = map;
|
||||
}
|
||||
|
||||
changeScene(identifier: string) {
|
||||
const scene = Scene.getScene(identifier, this._map);
|
||||
|
||||
if (scene) {
|
||||
const { conditions, properties } = scene;
|
||||
|
||||
this.properties = properties;
|
||||
this.conditions = conditions;
|
||||
}
|
||||
}
|
||||
|
||||
// get removes an items from the scene and returns it
|
||||
get(target: string): Item | null {
|
||||
const { items = [] } = this._properties;
|
||||
const idx = items.findIndex(({ name }) => name === target);
|
||||
|
||||
// if we found an index for the given item
|
||||
if (idx >= 0) {
|
||||
const item = items[idx];
|
||||
items.splice(idx, 1);
|
||||
|
||||
return item;
|
||||
}
|
||||
|
||||
// if an item wasn't found and returned, return null
|
||||
return null;
|
||||
}
|
||||
|
||||
// look returns a string that describes the scene and the items in it
|
||||
@ -41,23 +82,6 @@ export default class Scene extends Vessel<SceneProperties> {
|
||||
return fullDescription;
|
||||
}
|
||||
|
||||
// get removes an items from the scene and returns it
|
||||
get(target: string): Item | null {
|
||||
const { items = [] } = this._properties;
|
||||
const idx = items.findIndex(({ name }) => name === target);
|
||||
|
||||
// if we found an index for the given item
|
||||
if (idx >= 0) {
|
||||
const item = items[idx];
|
||||
items.splice(idx, 1);
|
||||
|
||||
return item;
|
||||
}
|
||||
|
||||
// if an item wasn't found and returned, return null
|
||||
return null;
|
||||
}
|
||||
|
||||
// Turn exits into a string description with an "and" separating the last two items
|
||||
private static describeExits(exits: Exit[]) {
|
||||
const exitDescriptions = exits.map(({ description }) => description);
|
||||
@ -74,4 +98,12 @@ export default class Scene extends Vessel<SceneProperties> {
|
||||
return `${restExits.join(", ")} and ${lastExit}`;
|
||||
}
|
||||
}
|
||||
|
||||
// This needs to be static so that we can use it in the constructor
|
||||
private static getScene(
|
||||
identifier: string,
|
||||
map: StoryScene<SceneProperties>[]
|
||||
): StoryScene<SceneProperties> | null {
|
||||
return map.find((scene) => scene.identifier === identifier) || null;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user