Add applyEffect

This commit is contained in:
2021-05-25 18:47:09 -07:00
parent ccd4974266
commit cc306dbf87
9 changed files with 169 additions and 45 deletions

View File

@ -28,7 +28,7 @@ export interface Term {
canPrecedeVariable: boolean;
}
export interface Action extends Term {
export interface ActionTerm extends Term {
action: ActionFn;
category: "action";
}

View File

@ -1,5 +1,10 @@
import Scene from "../Scene.ts";
import Player from "../Player.ts";
import type Scene from "../Scene.ts";
import type Player from "../Player.ts";
import type { ApplyEffectArgs, ItemAction } from "../types.ts";
const ITEM_MISSING = "You don't have that.";
const ITEM_UNUSABLE = "I don't know how to use that.";
const ITEM_ALREADY_USED = "I already did that.";
export function look(player: Player, scene: Scene): string {
return scene.look(player.activeEffects, scene.activeEffects);
@ -23,6 +28,66 @@ export function pickUpItem(
return "Taken.";
}
export function checkInventory(player: Player) {
export function checkInventory(player: Player): string {
return player.look();
}
export function use(player: Player, scene: Scene, target?: string): string {
if (!target) {
return "What do you want to use?";
}
const { inventory } = player;
if (inventory === null) {
return ITEM_MISSING;
}
const item = inventory.find((i) => i.name === target);
if (item === undefined) {
return ITEM_MISSING;
}
const itemAction = item?.actions["use"];
if (itemAction === undefined) {
return ITEM_UNUSABLE;
}
return executeItemAction(player, scene, itemAction);
}
function executeItemAction(
player: Player,
scene: Scene,
{ args, type }: ItemAction
): string {
switch (type) {
case "applyEffect":
return applyEffect(player, scene, args as ApplyEffectArgs);
default:
return ITEM_UNUSABLE;
}
}
function applyEffect(
player: Player,
scene: Scene,
{ applyTo, effect, reverseResult, reversible, result }: ApplyEffectArgs
): string {
const target = applyTo === "player" ? player : scene;
const isApplied = target.hasActiveEffect(effect);
if (!reversible && isApplied) {
return ITEM_ALREADY_USED;
}
if (isApplied) {
target.removeEffect(effect);
return reverseResult || result;
} else {
target.addEffect(effect);
return result;
}
}

View File

@ -1,7 +1,7 @@
import * as actions from "./actions.ts";
import { Action } from "./Term.ts";
import { ActionTerm } from "./Term.ts";
const inventory: Action = {
const inventory: ActionTerm = {
action: actions.checkInventory,
canPrecedeVariable: false,
category: "action",
@ -10,7 +10,7 @@ const inventory: Action = {
precedesConstants: [],
};
const look: Action = {
const look: ActionTerm = {
action: actions.look,
canPrecedeVariable: false,
category: "action",
@ -19,7 +19,7 @@ const look: Action = {
precedesConstants: [],
};
const take: Action = {
const take: ActionTerm = {
action: actions.pickUpItem,
canPrecedeVariable: true,
category: "action",
@ -28,8 +28,18 @@ const take: Action = {
precedesConstants: [],
};
const use: ActionTerm = {
action: actions.use,
canPrecedeVariable: true,
category: "action",
constant: "use",
precedesCategories: [],
precedesConstants: [],
};
export const terms = {
[inventory.constant]: inventory,
[look.constant]: look,
[take.constant]: take,
[use.constant]: use,
};