Refactor parser for some added flexibility.
This commit is contained in:
34
terms/Term.ts
Normal file
34
terms/Term.ts
Normal file
@ -0,0 +1,34 @@
|
||||
import Player from "../Player.ts";
|
||||
import Scene from "../Scene.ts";
|
||||
|
||||
export type ActionFn = (
|
||||
player: Player,
|
||||
scene: Scene,
|
||||
target?: string,
|
||||
object?: string
|
||||
) => string;
|
||||
|
||||
export interface Constant {
|
||||
constant: string;
|
||||
category: Category;
|
||||
}
|
||||
|
||||
export type Category =
|
||||
| "action"
|
||||
| "direction"
|
||||
| "compound"
|
||||
| "position"
|
||||
| "interaction";
|
||||
|
||||
export interface Term {
|
||||
precedesCategories: Category[];
|
||||
precedesConstants: Constant[];
|
||||
category: Category;
|
||||
constant: string;
|
||||
canPrecedeVariable: boolean;
|
||||
}
|
||||
|
||||
export interface Action extends Term {
|
||||
action: ActionFn;
|
||||
category: "action";
|
||||
}
|
28
terms/actions.ts
Normal file
28
terms/actions.ts
Normal file
@ -0,0 +1,28 @@
|
||||
import Scene from "../Scene.ts";
|
||||
import Player from "../Player.ts";
|
||||
|
||||
export function look(_player: Player, scene: Scene): string {
|
||||
return scene.look();
|
||||
}
|
||||
|
||||
export function pickUpItem(
|
||||
player: Player,
|
||||
scene: Scene,
|
||||
target?: string
|
||||
): string {
|
||||
if (!target) {
|
||||
return "What do you want me to get?";
|
||||
}
|
||||
|
||||
const item = scene.get(target);
|
||||
|
||||
if (item !== null) {
|
||||
player.put(item);
|
||||
}
|
||||
|
||||
return "Taken.";
|
||||
}
|
||||
|
||||
export function checkInventory(player: Player) {
|
||||
return player.look();
|
||||
}
|
35
terms/terms.ts
Normal file
35
terms/terms.ts
Normal file
@ -0,0 +1,35 @@
|
||||
import * as actions from "./actions.ts";
|
||||
import { Action } from "./Term.ts";
|
||||
|
||||
const inventory: Action = {
|
||||
action: actions.checkInventory,
|
||||
canPrecedeVariable: false,
|
||||
category: "action",
|
||||
constant: "inventory",
|
||||
precedesCategories: [],
|
||||
precedesConstants: [],
|
||||
};
|
||||
|
||||
const look: Action = {
|
||||
action: actions.look,
|
||||
canPrecedeVariable: false,
|
||||
category: "action",
|
||||
constant: "look",
|
||||
precedesCategories: [],
|
||||
precedesConstants: [],
|
||||
};
|
||||
|
||||
const take: Action = {
|
||||
action: actions.pickUpItem,
|
||||
category: "action",
|
||||
canPrecedeVariable: true,
|
||||
constant: "take",
|
||||
precedesCategories: [],
|
||||
precedesConstants: [],
|
||||
};
|
||||
|
||||
export const terms = {
|
||||
[inventory.constant]: inventory,
|
||||
[look.constant]: look,
|
||||
[take.constant]: take,
|
||||
};
|
Reference in New Issue
Block a user