Move effects into the modding api

This commit is contained in:
2026-10-02 11:53:44 -03:00
parent 4309c700d2
commit e5dcb97643
33 changed files with 458 additions and 109 deletions
+17 -58
View File
@@ -1,70 +1,31 @@
// status effects on living entities, like minecraft's MobEffect and MobEffectInstance. an effect is a type plus
// how strong it is and for how long. a weaker one that lasts longer waits hidden under the stronger one, and takes
// over when that runs out
import { ATTRIBUTES, type AttributeType, type ModifierOperation } from "./attributes.ts";
import { DAMAGE, finite } from "./entity.ts";
import type { EffectCategory } from "$/common/mod_data.ts";
import type { AttributeType, ModifierOperation } from "./attributes.ts";
import { finite } from "./entity.ts";
import type { LivingEntity } from "./living_entity.ts";
// a kind of effect: a mod's effect json with the components it lists, see ModRuntime.finish_setup. the base game's
// are in mods/bworld/effects
export interface EffectType {
id: string;
beneficial: boolean;
category: EffectCategory;
// happens once when added instead of lasting, like instant health
instant?: boolean;
instant: boolean;
// while it's on. amount is for level 1 and goes up with each level, like minecraft
modifiers?: { attribute: AttributeType; amount: number; operation: ModifierOperation }[];
// whether on_tick runs this tick, from the ticks left (or its age for infinite ones)
should_tick?(duration: number, amplifier: number): boolean;
// returning false takes the effect off
on_tick?(entity: LivingEntity, amplifier: number): boolean | void;
apply_instant?(entity: LivingEntity, amplifier: number): void;
on_added?(entity: LivingEntity, amplifier: number): void;
on_removed?(entity: LivingEntity, amplifier: number): void;
modifiers: { attribute: AttributeType; amount: number; operation: ModifierOperation }[];
// each tick it's on, and once for instant ones, with ticks undefined. ticks is what's left, or how long it's been
// on for ones that last forever. returning false takes the effect off
on_tick?(entity: LivingEntity, ticks: number | undefined, amplifier: number): boolean | void;
}
// every n ticks, n halving with each level: minecraft's regeneration and poison
const every = (ticks: number) => (duration: number, amplifier: number) => {
const interval = ticks >> amplifier;
return interval > 0 ? duration % interval === 0 : true;
};
const registry = new Map<string, EffectType>();
function effect(id: string, beneficial: boolean, rest: Omit<EffectType, "id" | "beneficial"> = {}): EffectType {
return { id: `bworld:${id}`, beneficial, ...rest };
}
export const EFFECTS = {
speed: effect("speed", true, {
modifiers: [{ attribute: ATTRIBUTES.movement_speed, amount: 0.2, operation: "add_multiplied_total" }],
}),
slowness: effect("slowness", false, {
modifiers: [{ attribute: ATTRIBUTES.movement_speed, amount: -0.15, operation: "add_multiplied_total" }],
}),
regeneration: effect("regeneration", true, {
should_tick: every(50),
on_tick: (entity) => {
if (entity.health < entity.max_health) entity.heal(1);
},
}),
poison: effect("poison", false, {
should_tick: every(25),
on_tick: (entity) => {
// never kills
if (entity.health > 1) entity.hurt(1, DAMAGE.magic);
},
}),
instant_health: effect("instant_health", true, {
instant: true,
apply_instant: (entity, amplifier) => entity.heal(Math.max(4 << amplifier, 0)),
}),
// these do their thing where it happens: LivingEntity's damage and air
resistance: effect("resistance", true),
water_breathing: effect("water_breathing", true),
} satisfies Record<string, EffectType>;
const registry = new Map<string, EffectType>(Object.values(EFFECTS).map((type) => [type.id, type]));
// adds an effect type, or replaces one with the same id
export function register_effect(type: EffectType) {
registry.set(type.id, type);
// every effect mods define, replacing the ones before
export function set_effect_types(types: EffectType[]) {
registry.clear();
for (const type of types) registry.set(type.id, type);
}
export function effect_type(id: string): EffectType | undefined {
@@ -181,9 +142,7 @@ export class EffectInstance {
tick(entity: LivingEntity): "ended" | "changed" | undefined {
if (!this.has_time_left) return "ended";
const count = this.infinite ? entity.age : this.duration;
if (this.type.should_tick?.(count, this.amplifier) && this.type.on_tick?.(entity, this.amplifier) === false) {
return "ended";
}
if (this.type.on_tick?.(entity, count, this.amplifier) === false) return "ended";
this.#count_down();
if (this.duration === 0 && this.hidden) {
this.#take_details(this.hidden);