Describe room exits

This commit is contained in:
2021-05-31 12:12:34 -07:00
parent b134f1849a
commit 8f6d7e4db4
4 changed files with 168 additions and 113 deletions

View File

@ -2,7 +2,7 @@
* Scene.ts contains the Scene class. It represents the state of the current scene. * Scene.ts contains the Scene class. It represents the state of the current scene.
*/ */
import { StoryScene, Item, SceneProperties } from "./types.ts"; import type { Exit, StoryScene, Item, SceneProperties } from "./types.ts";
import Vessel from "./Vessel.ts"; import Vessel from "./Vessel.ts";
// The Scene is a type of "Vessel" which is a generic object that can have effects // The Scene is a type of "Vessel" which is a generic object that can have effects
@ -23,11 +23,16 @@ export default class Scene extends Vessel<SceneProperties> {
// description of the items in the scene // description of the items in the scene
const itemsDescription = super.description(properties.items || []); const itemsDescription = super.description(properties.items || []);
const { description } = properties; // description of the room const { description, exits = [] } = properties; // description of the room
// will be a string that includes both the room and item descriptions // will be a string that includes both the room and item descriptions
let fullDescription = description || "Nothing to see here..."; let fullDescription = description || "Nothing to see here...";
const exitsDescription = Scene.describeExits(exits);
fullDescription +=
exitsDescription && `\n\nThere is ${exitsDescription}.`;
// if there is a description of the items... // if there is a description of the items...
if (itemsDescription) { if (itemsDescription) {
fullDescription += `\n\nThere is ${itemsDescription}`; fullDescription += `\n\nThere is ${itemsDescription}`;
@ -52,4 +57,21 @@ export default class Scene extends Vessel<SceneProperties> {
// if an item wasn't found and returned, return null // if an item wasn't found and returned, return null
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);
if (exits.length === 0) {
return "";
} else if (exits.length === 1) {
return exitDescriptions[0];
} else {
const lastExit = exitDescriptions[exitDescriptions.length - 1];
const restExits = exitDescriptions.slice(
0,
exitDescriptions.length - 1
);
return `${restExits.join(", ")} and ${lastExit}`;
}
}
} }

View File

@ -9,8 +9,9 @@
import { StoryScene, SceneProperties } from "../types.ts"; import { StoryScene, SceneProperties } from "../types.ts";
// hall represents a room const game: { map: StoryScene<SceneProperties>[] } = {
export const hall: StoryScene<SceneProperties> = { map: [
{
// properties represent the base, default state for this room. // properties represent the base, default state for this room.
properties: { properties: {
// This is what the user will see if the look while in the room. // This is what the user will see if the look while in the room.
@ -57,6 +58,19 @@ export const hall: StoryScene<SceneProperties> = {
}, },
}, },
], ],
// exit to the north leads to the Hall Closet
exits: [
{
description: "a closet to the north",
direction: "north",
scene: "Hall Closet",
},
{
description: "a dining room to the south",
direction: "south",
scene: "Dining Room",
},
],
}, },
// conditions are a set of conditions that, when met, will make alterations to the // conditions are a set of conditions that, when met, will make alterations to the
// player or the scene. // player or the scene.
@ -86,4 +100,8 @@ export const hall: StoryScene<SceneProperties> = {
}, },
], ],
}, },
},
],
}; };
export default game;

View File

@ -2,11 +2,11 @@ import Interpreter from "./Interpreter.ts";
import Player from "./Player.ts"; import Player from "./Player.ts";
import Scene from "./Scene.ts"; import Scene from "./Scene.ts";
import User from "./User.ts"; import User from "./User.ts";
import { hall } from "./types.ts"; import game from "./data/rooms.ts";
async function main() { async function main() {
const user = new User(); // for communication with the user const user = new User(); // for communication with the user
const scene = new Scene(hall); // the room that player is in. const scene = new Scene(game.map[0]); // the room that player is in.
const player = new Player(); // the players current state const player = new Player(); // the players current state
let running = true; // running flag for the game loop. Stops on false. let running = true; // running flag for the game loop. Stops on false.
let statement = ""; // holds a statement for the user. let statement = ""; // holds a statement for the user.

View File

@ -1,27 +1,7 @@
/**
* types.ts describes basic types used by the engine.
*/
export * from "./data/rooms.ts";
// ActionArgs is a union of all the different argument types that can be used with an // ActionArgs is a union of all the different argument types that can be used with an
// action // action
export type ActionArgs = ApplyEffectArgs; export type ActionArgs = ApplyEffectArgs;
// ItemActionType is a union of all the types of action that can be used
export type ItemActionType = "applyEffect";
// Args is a base interface for the various argument interfaces
export interface Args {
result: string;
}
// ItemAction represents an action that can be taken by an item
export interface ItemAction {
type: ItemActionType;
args: ActionArgs;
}
// ApplyEffectArgs are the arguments required to apply effects to a player or scene // ApplyEffectArgs are the arguments required to apply effects to a player or scene
export interface ApplyEffectArgs extends Args { export interface ApplyEffectArgs extends Args {
effect: string; effect: string;
@ -36,28 +16,9 @@ export interface ApplyEffectItemAction extends ItemAction {
args: ApplyEffectArgs; args: ApplyEffectArgs;
} }
// Item represents some item either in the scene or in the player's inventory // Args is a base interface for the various argument interfaces
export interface Item { export interface Args {
name: string; result: string;
actions: { [name: string]: ItemAction };
}
// Effect represents some effect that may be applied to a scene or player
export interface Effect<T extends VesselProperties> {
name: string;
properties: T;
source: "player" | "scene"; // where the effect is applied
}
// VesselProperties is a base interface for the properties that a user or scene might have
export interface VesselProperties {
items?: Item[];
}
// SceneProperties are the properties (in addition to the VesselProperties) that are
// needed by the scene
export interface SceneProperties extends VesselProperties {
description?: string;
} }
// Conditions contains story elements that can be conditionally applied to the scene or // Conditions contains story elements that can be conditionally applied to the scene or
@ -66,8 +27,62 @@ export interface Conditions<T extends VesselProperties> {
effects: Effect<T>[]; effects: Effect<T>[];
} }
// Direction is union that contains the different directions a player can go
export type Direction =
| "north"
| "northeast"
| "east"
| "southest"
| "south"
| "southwest"
| "west"
| "northwest"
| "up"
| "down";
// Effect represents some effect that may be applied to a scene or player
export interface Effect<T extends VesselProperties> {
name: string;
properties: T;
source: "player" | "scene"; // where the effect is applied
}
// Exit describes the exits that the user can take to go to another scene
export interface Exit {
description: string;
direction: Direction;
scene: string;
}
// Item represents some item either in the scene or in the player's inventory
export interface Item {
name: string;
actions: { [name: string]: ItemAction };
}
// ItemActionType is a union of all the types of action that can be used
export type ItemActionType = "applyEffect";
// ItemAction represents an action that can be taken by an item
export interface ItemAction {
type: ItemActionType;
args: ActionArgs;
}
// SceneProperties are the properties (in addition to the VesselProperties) that are
// needed by the scene
export interface SceneProperties extends VesselProperties {
description?: string;
exits?: Exit[];
}
// StoryScene holds both the conditions and properties for a scene. // StoryScene holds both the conditions and properties for a scene.
export interface StoryScene<T extends VesselProperties> { export interface StoryScene<T extends VesselProperties> {
conditions: Conditions<T>; conditions: Conditions<T>;
properties: SceneProperties; properties: SceneProperties;
} }
// VesselProperties is a base interface for the properties that a user or scene might have
export interface VesselProperties {
items?: Item[];
}