121 lines
4.9 KiB
TypeScript
121 lines
4.9 KiB
TypeScript
// curios: accessory slots, in 2 columns left of the player's inventory. each player's curios are a
|
|
// container of their own, and fall out when they die like the rest of what they carry. items with the
|
|
// curios:attribute_modifiers component change the wearer's attributes, like curios' attribute modifiers
|
|
import type { AttributeModifier, Container, Player, ServerContext } from "bworld/server";
|
|
import { active_slots, fits, SLOTS } from "./slots.ts";
|
|
|
|
// where the left column of slots goes, from the inventory screen's left edge. the panel around them ends just left of it
|
|
const COLUMNS_X = -45;
|
|
|
|
// curios:attribute_modifiers' params, a list of these
|
|
interface WornModifier {
|
|
attribute: string;
|
|
amount: number;
|
|
operation: AttributeModifier["operation"];
|
|
}
|
|
|
|
export function setup(ctx: ServerContext) {
|
|
const active = active_slots(ctx.tags);
|
|
|
|
ctx.components.register_item<WornModifier[]>("curios:attribute_modifiers", {
|
|
get_lore: (_item, modifiers) => ["When worn:", ...modifiers.map(describe)].join("\n"),
|
|
});
|
|
const modifiers_of = (id: string) =>
|
|
(ctx.items.components(id)["curios:attribute_modifiers"] ?? []) as WornModifier[];
|
|
|
|
// the modifiers each online player's curios gave them, by player id, to take them off again
|
|
const applied = new Map<string, { attribute: string; id: string }[]>();
|
|
const stop_watching = new Map<string, () => void>();
|
|
|
|
// by player name, like the players themselves are saved. undefined until they first join with curios
|
|
function saved_curios(player: Player): Container | undefined {
|
|
const id = ctx.storage.get<Record<string, string>>("containers")?.[player.name];
|
|
return id ? ctx.containers.get(id) : undefined;
|
|
}
|
|
|
|
function curios_of(player: Player): Container {
|
|
const existing = saved_curios(player);
|
|
if (existing) return existing;
|
|
const container = ctx.containers.create(SLOTS.length);
|
|
const ids = ctx.storage.get<Record<string, string>>("containers") ?? {};
|
|
ctx.storage.set("containers", { ...ids, [player.name]: container.id });
|
|
watch(player, container);
|
|
return container;
|
|
}
|
|
|
|
// keeps what their curios give them up to date
|
|
function watch(player: Player, curios: Container) {
|
|
stop_watching.get(player.id)?.();
|
|
stop_watching.set(player.id, curios.on_change(() => update_modifiers(player, curios)));
|
|
update_modifiers(player, curios);
|
|
}
|
|
|
|
function update_modifiers(player: Player, curios: Container) {
|
|
for (const { attribute, id } of applied.get(player.id) ?? []) player.remove_modifier(attribute, id);
|
|
const now: { attribute: string; id: string }[] = [];
|
|
for (const [index, slot] of SLOTS.entries()) {
|
|
const item = curios.get(index);
|
|
if (!item || !active.includes(slot)) continue;
|
|
for (const [i, { attribute, amount, operation }] of modifiers_of(item.id).entries()) {
|
|
const id = `curios:${slot}/${i}`;
|
|
player.add_modifier(attribute, { id, amount, operation });
|
|
now.push({ attribute, id });
|
|
}
|
|
}
|
|
applied.set(player.id, now);
|
|
}
|
|
|
|
ctx.events.after.player_join.subscribe(({ player }) => {
|
|
const curios = saved_curios(player);
|
|
if (curios) watch(player, curios);
|
|
});
|
|
ctx.events.after.player_leave.subscribe(({ player }) => {
|
|
stop_watching.get(player.id)?.();
|
|
stop_watching.delete(player.id);
|
|
applied.delete(player.id);
|
|
});
|
|
|
|
// the slot types in 2 columns left of the inventory, always there while it's open. the engine draws their panel
|
|
ctx.menus.extend_player_menu((menu, player) => {
|
|
if (active.length === 0) return;
|
|
const curios = curios_of(player);
|
|
// what's in slot types no item goes in anymore, since the mod that added them was removed, goes back to them
|
|
for (const [index, slot] of SLOTS.entries()) {
|
|
const item = curios.get(index);
|
|
if (item && !active.includes(slot)) {
|
|
player.give_item(item.id, item.count, item.data);
|
|
curios.set(index, undefined);
|
|
}
|
|
}
|
|
for (const [i, slot] of active.entries()) {
|
|
menu.add_slot(curios, SLOTS.indexOf(slot), {
|
|
x: COLUMNS_X + (i % 2) * 18,
|
|
y: 7 + Math.floor(i / 2) * 18,
|
|
filter: (item) => fits(ctx.tags, slot, item.id),
|
|
icon: `curios:empty_${slot}_slot`,
|
|
});
|
|
}
|
|
});
|
|
|
|
ctx.events.after.player_death.subscribe(({ player }) => {
|
|
const curios = saved_curios(player);
|
|
if (!curios) return;
|
|
const { x, y, z } = player.position;
|
|
for (let i = 0; i < curios.size; i++) {
|
|
const item = curios.get(i);
|
|
if (!item) continue;
|
|
const { id, count, data } = item;
|
|
curios.set(i, undefined);
|
|
ctx.world.drop_item(Math.floor(x), Math.floor(Math.max(y, 0)), Math.floor(z), { id, count, data });
|
|
}
|
|
});
|
|
}
|
|
|
|
// like minecraft's attribute tooltips: "+4 Attack Damage", "+30% Attack Speed"
|
|
function describe({ attribute, amount, operation }: WornModifier): string {
|
|
const name = attribute.split(":").at(-1)!.split("_").map((word) => word[0].toUpperCase() + word.slice(1)).join(" ");
|
|
const sign = amount < 0 ? "-" : "+";
|
|
const value = operation === "add_value" ? `${Math.abs(amount)}` : `${Math.round(Math.abs(amount) * 100)}%`;
|
|
return `${sign}${value} ${name}`;
|
|
}
|