From 3ea5e47c578236119473db6db55f0c4f38e6035e Mon Sep 17 00:00:00 2001 From: Holly Date: Wed, 6 Dec 2023 00:04:57 +0000 Subject: [PATCH] Allow for entities to be killed. --- server/MPClient.ts | 8 ++++---- server/World.ts | 4 ++++ server/entities/Entity.ts | 14 ++++++++------ server/entities/EntityItem.ts | 2 +- server/entities/IEntity.ts | 1 + 5 files changed, 18 insertions(+), 11 deletions(-) diff --git a/server/MPClient.ts b/server/MPClient.ts index 91957d9..4c7537b 100644 --- a/server/MPClient.ts +++ b/server/MPClient.ts @@ -178,14 +178,14 @@ export class MPClient { private breakBlock(brokenBlockId:number, x:number, y:number, z:number) { const metadata = this.entity.world.getBlockMetadata(this.diggingAt.x, this.diggingAt.y, this.diggingAt.z); this.entity.world.setBlockWithNotify(this.diggingAt.x, this.diggingAt.y, this.diggingAt.z, 0); - this.inventory.addItemStack(new ItemStack(Block.blockBehaviours[brokenBlockId].droppedItem(brokenBlockId), 1, metadata)); - this.send(new PacketWindowItems(0, this.inventory.getInventorySize(), this.inventory.constructInventoryPayload()).writeData()); - /*const itemId = Block.blockBehaviours[brokenBlockId].droppedItem(brokenBlockId); + //this.inventory.addItemStack(new ItemStack(Block.blockBehaviours[brokenBlockId].droppedItem(brokenBlockId), 1, metadata)); + //this.send(new PacketWindowItems(0, this.inventory.getInventorySize(), this.inventory.constructInventoryPayload()).writeData()); + const itemId = Block.blockBehaviours[brokenBlockId].droppedItem(brokenBlockId); if (itemId !== -1) { const itemEntity = new EntityItem(this.entity.world, new ItemStack(itemId, 1, metadata)); itemEntity.position.set(x + 0.5, y + 0.5, z + 0.5); this.entity.world.addEntity(itemEntity); - }*/ + } } // TODO: Cap how far away a player is able to break blocks diff --git a/server/World.ts b/server/World.ts index 2f2a9b5..eade78f 100644 --- a/server/World.ts +++ b/server/World.ts @@ -287,6 +287,10 @@ export class World { entity.justUnloaded = new Array(); } } + + if (entity.markedForDisposal) { + this.removeEntity(entity); + } }) } } \ No newline at end of file diff --git a/server/entities/Entity.ts b/server/entities/Entity.ts index f086301..672bb86 100644 --- a/server/entities/Entity.ts +++ b/server/entities/Entity.ts @@ -58,6 +58,8 @@ export class Entity implements IEntity { public readonly isPlayer:boolean; private queuedChunkUpdate:boolean; + public markedForDisposal:boolean = false; + public constructor(world:World, isPlayer:boolean = false) { this.entityId = Entity.nextEntityId++; @@ -217,6 +219,11 @@ export class Entity implements IEntity { // TODO: Entity falling mount transfer } + kill() { + this.health = 0; + this.markedForDisposal = true; + } + updateFalling(distance:number) { if (this.onGround) { if (this.fallDistance > 0) { @@ -241,11 +248,7 @@ export class Entity implements IEntity { // TODO: Handle X and Z collisions. if (this.entityAABB.intersects(blockBoundingBox)) { const inersectionY = this.entityAABB.intersectionY(blockBoundingBox); - this.position.add( - 0, - inersectionY, - 0 - ); + this.position.add(0, inersectionY, 0); this.motion.y = 0; this.onGround = true; } @@ -267,7 +270,6 @@ export class Entity implements IEntity { if (!this.isDead && this.health <= 0) { this.isDead = true; - } if (this.wasHurt) { diff --git a/server/entities/EntityItem.ts b/server/entities/EntityItem.ts index f2cf04b..3ee2d9e 100644 --- a/server/entities/EntityItem.ts +++ b/server/entities/EntityItem.ts @@ -46,7 +46,7 @@ export class EntityItem extends Entity { this.age++; if (this.age >= 6000) { - // TODO: Kill entity + this.kill(); } } } \ No newline at end of file diff --git a/server/entities/IEntity.ts b/server/entities/IEntity.ts index e0224db..e668c87 100644 --- a/server/entities/IEntity.ts +++ b/server/entities/IEntity.ts @@ -7,6 +7,7 @@ export interface IEntity { lastPosition:Vec3, crouching:boolean, isDead:boolean, + markedForDisposal:boolean, updateMetadata:() => void, distanceTo:(entity:IEntity) => number, onTick:() => void