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

@ -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;
}
}