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
+18 -17
View File
@@ -5,7 +5,7 @@ import { TICKS_PER_SECOND } from "$/common/constants.ts";
import { type ItemData, ItemStack } from "$/common/inventory.ts";
import type { EntityData } from "$/common/protocol.ts";
import { AttributeMap, ATTRIBUTES, type AttributeType } from "./attributes.ts";
import { EffectInstance, EFFECTS, type EffectType, type SavedEffect } from "./effects.ts";
import { EffectInstance, type SavedEffect } from "./effects.ts";
import { DAMAGE, type DamageSource, Entity, finite, type SavedEntity } from "./entity.ts";
export const EQUIPMENT_SLOTS = ["head", "chest", "legs", "feet", "mainhand", "offhand", "body", "saddle"] as const;
@@ -28,6 +28,9 @@ const SWIM_UP = 0.04;
// a jump can't start more often than this while jump is held
const JUMP_DELAY = 10;
const KNOCKBACK = 0.4;
// the base game's effects that work where what they change happens, like minecraft's: in damage and air
const RESISTANCE = "bworld:resistance";
const WATER_BREATHING = "bworld:water_breathing";
export interface SavedLivingEntity extends SavedEntity {
health?: number;
@@ -135,7 +138,7 @@ export abstract class LivingEntity extends Entity {
override base_tick() {
super.base_tick();
if (this.alive) {
if (this.eyes_in_water && !this.can_breathe_underwater && !this.has_effect(EFFECTS.water_breathing)) {
if (this.eyes_in_water && !this.can_breathe_underwater && !this.has_effect(WATER_BREATHING)) {
this.air -= 1;
if (this.air <= -20) {
this.air = 0;
@@ -314,7 +317,7 @@ export abstract class LivingEntity extends Entity {
return;
}
const resistance = this.get_effect(EFFECTS.resistance);
const resistance = this.get_effect(RESISTANCE);
if (resistance && !source.bypasses_invulnerability) {
amount = Math.max(0, amount * (25 - (resistance.amplifier + 1) * 5) / 25);
}
@@ -387,12 +390,12 @@ export abstract class LivingEntity extends Entity {
// effects
has_effect(type: EffectType) {
return this.effects.has(type.id);
has_effect(id: string) {
return this.effects.has(id);
}
get_effect(type: EffectType): EffectInstance | undefined {
return this.effects.get(type.id);
get_effect(id: string): EffectInstance | undefined {
return this.effects.get(id);
}
// whether it's affected by that effect at all, like undead and poison
@@ -404,14 +407,13 @@ export abstract class LivingEntity extends Entity {
add_effect(effect: EffectInstance): boolean {
if (!this.alive || !this.can_be_affected(effect)) return false;
if (effect.type.instant) {
effect.type.apply_instant?.(this, effect.amplifier);
effect.type.on_tick?.(this, undefined, effect.amplifier);
return true;
}
const current = this.effects.get(effect.type.id);
if (!current) {
this.effects.set(effect.type.id, effect);
this.#apply_modifiers(effect);
effect.type.on_added?.(this, effect.amplifier);
return true;
}
const amplifier = current.amplifier;
@@ -420,16 +422,16 @@ export abstract class LivingEntity extends Entity {
return changed;
}
remove_effect(type: EffectType) {
const effect = this.effects.get(type.id);
remove_effect(id: string) {
const effect = this.effects.get(id);
if (!effect) return false;
this.effects.delete(type.id);
this.effects.delete(id);
this.#effect_ended(effect);
return true;
}
clear_effects() {
for (const effect of [...this.effects.values()]) this.remove_effect(effect.type);
for (const effect of [...this.effects.values()]) this.remove_effect(effect.type.id);
}
tick_effects() {
@@ -439,7 +441,7 @@ export abstract class LivingEntity extends Entity {
if (this.effects.get(effect.type.id) !== effect) continue;
const result = effect.tick(this);
if (result === "ended") {
this.remove_effect(effect.type);
this.remove_effect(effect.type.id);
} else if (result === "changed") {
this.#apply_modifiers(effect);
}
@@ -447,7 +449,7 @@ export abstract class LivingEntity extends Entity {
}
#apply_modifiers(effect: EffectInstance) {
for (const { attribute, amount, operation } of effect.type.modifiers ?? []) {
for (const { attribute, amount, operation } of effect.type.modifiers) {
this.attributes.get(attribute)?.add_modifier({
id: effect.type.id,
amount: amount * (effect.amplifier + 1),
@@ -457,10 +459,9 @@ export abstract class LivingEntity extends Entity {
}
#effect_ended(effect: EffectInstance) {
for (const { attribute } of effect.type.modifiers ?? []) {
for (const { attribute } of effect.type.modifiers) {
this.attributes.get(attribute)?.remove_modifier(effect.type.id);
}
effect.type.on_removed?.(this, effect.amplifier);
// back inside what the attributes allow now
this.health = this.#health;
}