Move effects out of cubi

This commit is contained in:
2026-11-05 22:37:41 -03:00
parent 0e2d8fdcac
commit 9bc8fd992a
17 changed files with 452 additions and 70 deletions
+6 -2
View File
@@ -15,6 +15,7 @@ import type { Season } from "@/common/worldgen/biomes.ts";
import { type Collisions, type FluidInCell, physics } from "@/common/physics.ts";
import type { EntityData, EntityEvent, EntityInfo } from "@/common/protocol.ts";
import type { ServerPlayer } from "../player.ts";
import type { LivingEntity } from "./living_entity.ts";
// below this, entities have fallen out of the world
export const VOID_Y = -64;
@@ -85,8 +86,11 @@ export interface EntityLevel {
// entities whose feet are within the box around x, y, z
entities_near(x: number, y: number, z: number, radius: number): Entity[];
nearest_player(x: number, y: number, z: number, max_distance: number): ServerPlayer | undefined;
// what ServerPlayer.hurt does: mods get a say, then they're hurt. returns whether they were
hurt_player(player: ServerPlayer, amount: number, source: DamageSource): boolean;
// mods' before.entity_damage, see LivingEntity.hurt: what's left of amount once they had their say, 0 when one
// canceled it
before_hurt(entity: LivingEntity, amount: number, source: DamageSource): number;
// mods' after.entity_damage, it was hurt by amount
after_hurt(entity: LivingEntity, amount: number, source: DamageSource): void;
spawn_item(x: number, y: number, z: number, stack: ItemStack, pickup_delay?: number): Entity;
// an item on the ground was picked up whole by who, clients see it fly to them. it's gone after
take_item(item: Entity, by: string): void;
+9 -3
View File
@@ -15,6 +15,7 @@ import type { ServerWorld } from "../world.ts";
import type { DamageSource, Entity, EntityLevel, SavedEntity } from "./entity.ts";
import { load_entity } from "./entity_types.ts";
import { ItemEntity } from "./item_entity.ts";
import type { LivingEntity } from "./living_entity.ts";
// everything the entities need from the game server
export interface EntityHost {
@@ -22,7 +23,8 @@ export interface EntityHost {
readonly current_tick: number;
players(): ServerPlayer[];
send(player: ServerPlayer, message: ServerMessage): void;
hurt_player(player: ServerPlayer, amount: number, source: DamageSource): boolean;
before_hurt(entity: LivingEntity, amount: number, source: DamageSource): number;
after_hurt(entity: LivingEntity, amount: number, source: DamageSource): void;
on_entity_death?(entity: Entity, source: DamageSource): void;
// what it drops when it dies, from its loot table
entity_loot?(entity: Entity, source: DamageSource): ItemStack[];
@@ -136,8 +138,12 @@ export class EntityManager implements EntityLevel {
return nearest;
}
hurt_player(player: ServerPlayer, amount: number, source: DamageSource) {
return this.#host.hurt_player(player, amount, source);
before_hurt(entity: LivingEntity, amount: number, source: DamageSource) {
return this.#host.before_hurt(entity, amount, source);
}
after_hurt(entity: LivingEntity, amount: number, source: DamageSource) {
this.#host.after_hurt(entity, amount, source);
}
spawn_item(x: number, y: number, z: number, stack: ItemStack, pickup_delay = 0): ItemEntity {
+7 -11
View File
@@ -31,9 +31,6 @@ 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;
@@ -141,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(WATER_BREATHING)) {
if (this.eyes_in_water && !this.can_breathe_underwater) {
this.air -= 1;
if (this.air <= -20) {
this.air = 0;
@@ -270,11 +267,14 @@ export abstract class LivingEntity extends Entity {
// health
// like minecraft's LivingEntity.hurt. returns whether it was hurt
// like minecraft's LivingEntity.hurt. mods can change or cancel it first. returns whether it was hurt
override hurt(amount: number, source: DamageSource): boolean {
if (this.is_invulnerable_to(source) || this.dead || !(amount > 0)) {
return false;
}
amount = this.level.before_hurt(this, amount, source);
if (!(amount > 0)) return false;
const taken = amount;
let fresh = true;
if (this.#hurt_cooldown > HURT_COOLDOWN / 2) {
if (amount <= this.#last_hurt_amount) return false;
@@ -306,24 +306,20 @@ export abstract class LivingEntity extends Entity {
}
}
this.on_hurt(source);
this.level.after_hurt(this, taken, source);
if (this.dead) {
this.die(source);
}
return true;
}
// after resistance
// after mods and the hurt cooldown
actually_hurt(amount: number, source: DamageSource) {
// absolute damage, take it and theres no counterplay
if (source.type === "absolute") {
this.health -= amount;
return;
}
const resistance = this.get_effect(RESISTANCE);
if (resistance && !source.bypasses_invulnerability) {
amount = Math.max(0, amount * (25 - (resistance.amplifier + 1) * 5) / 25);
}
const shielding = this.attribute(
source.type === "physical" ? ATTRIBUTES.physical_shielding : ATTRIBUTES.arcane_shielding,
);