Death
This commit is contained in:
@@ -104,6 +104,8 @@ export interface EntityLevel {
|
||||
entity_loot(entity: Entity, source: DamageSource): ItemStack[];
|
||||
// it's standing on the block at x y z after moving, for the block's components, see on_entity_stand
|
||||
stands_on(entity: Entity, x: number, y: number, z: number): void;
|
||||
// it moved, for the components of the blocks its box is in, see on_entity_inside
|
||||
inside_blocks(entity: Entity): void;
|
||||
}
|
||||
|
||||
// an entity as level.json keeps it, see Entity.save. each kind adds its own fields
|
||||
@@ -308,6 +310,7 @@ export abstract class Entity {
|
||||
if (this.on_ground) {
|
||||
this.level.stands_on(this, Math.floor(this.x), Math.floor(this.y - STANDING_DEPTH), Math.floor(this.z));
|
||||
}
|
||||
this.level.inside_blocks(this);
|
||||
this.update_fall_distance(this.y - from_y);
|
||||
return collisions;
|
||||
}
|
||||
@@ -362,6 +365,12 @@ export abstract class Entity {
|
||||
this.fire = 0;
|
||||
}
|
||||
|
||||
// like minecraft's Entity.kill: ones that can die do, even when invulnerable, the rest go away. cause is for events
|
||||
// and the death message
|
||||
kill(_cause: string) {
|
||||
this.remove();
|
||||
}
|
||||
|
||||
// takes it out of the world. the entity manager tells clients and forgets it
|
||||
remove() {
|
||||
if (this.removed) return;
|
||||
|
||||
@@ -30,6 +30,8 @@ export interface EntityHost {
|
||||
entity_loot?(entity: Entity, source: DamageSource): ItemStack[];
|
||||
// see EntityLevel.stands_on
|
||||
stands_on?(entity: Entity, x: number, y: number, z: number): void;
|
||||
// see EntityLevel.inside_blocks
|
||||
inside_blocks?(entity: Entity): void;
|
||||
}
|
||||
|
||||
// what a client was last told about an entity, so only changes get sent
|
||||
@@ -188,6 +190,10 @@ export class EntityManager implements EntityLevel {
|
||||
this.#host.stands_on?.(entity, x, y, z);
|
||||
}
|
||||
|
||||
inside_blocks(entity: Entity) {
|
||||
this.#host.inside_blocks?.(entity);
|
||||
}
|
||||
|
||||
// ticking
|
||||
|
||||
// every entity active says should move, with their passengers. entities removed along the way are forgotten
|
||||
|
||||
@@ -313,6 +313,11 @@ export abstract class LivingEntity extends Entity {
|
||||
return true;
|
||||
}
|
||||
|
||||
// mods can still cancel it, like any damage
|
||||
override kill(cause: string) {
|
||||
this.hurt(Number.MAX_VALUE, { cause, type: "absolute", bypasses_invulnerability: true });
|
||||
}
|
||||
|
||||
// after mods and the hurt cooldown
|
||||
actually_hurt(amount: number, source: DamageSource) {
|
||||
// absolute damage, take it and theres no counterplay
|
||||
|
||||
@@ -274,6 +274,7 @@ export class GameServer {
|
||||
);
|
||||
}
|
||||
},
|
||||
inside_blocks: (entity) => this.#inside_blocks(entity),
|
||||
entity_loot: (entity, source) => {
|
||||
const table = EverythingRegistry.get<EntityJson>("entities", entity.type)?.loot_table;
|
||||
if (!table) return [];
|
||||
@@ -1153,6 +1154,7 @@ export class GameServer {
|
||||
player.sneaking = message.sneaking === true;
|
||||
this.#broadcast(player_move(player), player);
|
||||
this.#update_view(player);
|
||||
this.#inside_blocks(player);
|
||||
break;
|
||||
}
|
||||
case "view_distance":
|
||||
@@ -2197,6 +2199,28 @@ export class GameServer {
|
||||
}
|
||||
}
|
||||
|
||||
// the components of every block an entity's box is in see it, after it moved. players move on their clients, so
|
||||
// theirs is after each move they send
|
||||
#inside_blocks(entity: Entity) {
|
||||
const { x, y, z } = entity;
|
||||
physics.blocks_inside(entity, (bx, by, bz) => {
|
||||
// a block before this one killed it, took it away or moved it somewhere else
|
||||
if (entity.removed || (entity instanceof LivingEntity && entity.dead)) return;
|
||||
if (entity.x !== x || entity.y !== y || entity.z !== z) return;
|
||||
const id = this.world.get_block_id(bx, by, bz);
|
||||
const components = this.mods.components_with(id, "on_entity_inside");
|
||||
if (components.length === 0) return;
|
||||
const block = this.mods.block_ref(bx, by, bz, id);
|
||||
for (const { mod, id: component_id, component, params } of components) {
|
||||
run_guarded(
|
||||
mod,
|
||||
`${component_id} on_entity_inside`,
|
||||
() => component.on_entity_inside!(block, params, entity_ref(entity)),
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// feet on top of something solid, anywhere under the player's box: a thin box just under their feet touches a
|
||||
// block's collision shape. shapes, not whole blocks, so standing on a fence, a slab or a path counts too
|
||||
#on_ground(x: number, y: number, z: number) {
|
||||
|
||||
@@ -1685,6 +1685,9 @@ export function entity_ref(entity: Entity): EntityRef {
|
||||
remove() {
|
||||
entity.remove();
|
||||
},
|
||||
kill(cause = "script") {
|
||||
entity.kill(String(cause));
|
||||
},
|
||||
get_item: () => entity instanceof ItemEntity ? item_copy(entity.item) : undefined,
|
||||
set_item(item) {
|
||||
if (!(entity instanceof ItemEntity)) throw new Error(`${entity.type} isn't an item lying around`);
|
||||
|
||||
Reference in New Issue
Block a user