Files
bworld/mods/bworld/scripts/effects.ts
T
2026-11-05 22:37:41 -03:00

47 lines
1.9 KiB
TypeScript

// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// what the base game's effects do, see mods/bworld/effects. speed and slowness are only attribute modifiers, and
// resistance has no component, it lowers damage as it comes in
import type { ServerContext } from "cubi/server";
// every n ticks, n halving with each level: minecraft's regeneration and poison
function every(ticks: number, amplifier: number, n: number) {
const interval = n >> amplifier;
return interval > 0 ? ticks % interval === 0 : true;
}
export function register_effects(ctx: ServerContext) {
ctx.components.register_effect<{ every: number }>("bworld:heal_over_time", {
should_tick: (ticks, amplifier, params) => every(ticks, amplifier, params.every),
on_tick(entity) {
if (entity.health < entity.max_health) entity.heal(1);
},
});
ctx.components.register_effect<{ every: number }>("bworld:poison", {
should_tick: (ticks, amplifier, params) => every(ticks, amplifier, params.every),
on_tick(entity) {
// never kills
if (entity.health > 1) entity.damage(1, "magic");
},
});
// effects tick after the entity loses air under water, so it never runs out
ctx.components.register_effect("bworld:refill_air", {
on_tick(entity) {
entity.air = entity.max_air;
},
});
// 20% less damage for each level, unless nothing could stop it, like falling out of the world
ctx.events.before.entity_damage.subscribe((event) => {
const resistance = event.entity.effect("bworld:resistance");
if (!resistance || event.bypasses_invulnerability) return;
event.amount *= Math.max(0, 1 - 0.2 * (resistance.amplifier + 1));
});
// instant: amount for level 1, doubling with each level
ctx.components.register_effect<{ amount: number }>("bworld:heal", {
on_tick(entity, amplifier, params) {
entity.heal(Math.max(params.amount << amplifier, 0));
},
});
}