Allow for entities to be killed.

This commit is contained in:
Holly Stubbs 2023-12-06 00:04:57 +00:00
parent 91395db919
commit 3ea5e47c57
Signed by: tgpholly
GPG Key ID: B8583C4B7D18119E
5 changed files with 18 additions and 11 deletions

View File

@ -178,14 +178,14 @@ export class MPClient {
private breakBlock(brokenBlockId:number, x:number, y:number, z:number) { 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); 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.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.inventory.addItemStack(new ItemStack(Block.blockBehaviours[brokenBlockId].droppedItem(brokenBlockId), 1, metadata));
this.send(new PacketWindowItems(0, this.inventory.getInventorySize(), this.inventory.constructInventoryPayload()).writeData()); //this.send(new PacketWindowItems(0, this.inventory.getInventorySize(), this.inventory.constructInventoryPayload()).writeData());
/*const itemId = Block.blockBehaviours[brokenBlockId].droppedItem(brokenBlockId); const itemId = Block.blockBehaviours[brokenBlockId].droppedItem(brokenBlockId);
if (itemId !== -1) { if (itemId !== -1) {
const itemEntity = new EntityItem(this.entity.world, new ItemStack(itemId, 1, metadata)); const itemEntity = new EntityItem(this.entity.world, new ItemStack(itemId, 1, metadata));
itemEntity.position.set(x + 0.5, y + 0.5, z + 0.5); itemEntity.position.set(x + 0.5, y + 0.5, z + 0.5);
this.entity.world.addEntity(itemEntity); this.entity.world.addEntity(itemEntity);
}*/ }
} }
// TODO: Cap how far away a player is able to break blocks // TODO: Cap how far away a player is able to break blocks

View File

@ -287,6 +287,10 @@ export class World {
entity.justUnloaded = new Array<number>(); entity.justUnloaded = new Array<number>();
} }
} }
if (entity.markedForDisposal) {
this.removeEntity(entity);
}
}) })
} }
} }

View File

@ -58,6 +58,8 @@ export class Entity implements IEntity {
public readonly isPlayer:boolean; public readonly isPlayer:boolean;
private queuedChunkUpdate:boolean; private queuedChunkUpdate:boolean;
public markedForDisposal:boolean = false;
public constructor(world:World, isPlayer:boolean = false) { public constructor(world:World, isPlayer:boolean = false) {
this.entityId = Entity.nextEntityId++; this.entityId = Entity.nextEntityId++;
@ -217,6 +219,11 @@ export class Entity implements IEntity {
// TODO: Entity falling mount transfer // TODO: Entity falling mount transfer
} }
kill() {
this.health = 0;
this.markedForDisposal = true;
}
updateFalling(distance:number) { updateFalling(distance:number) {
if (this.onGround) { if (this.onGround) {
if (this.fallDistance > 0) { if (this.fallDistance > 0) {
@ -241,11 +248,7 @@ export class Entity implements IEntity {
// TODO: Handle X and Z collisions. // TODO: Handle X and Z collisions.
if (this.entityAABB.intersects(blockBoundingBox)) { if (this.entityAABB.intersects(blockBoundingBox)) {
const inersectionY = this.entityAABB.intersectionY(blockBoundingBox); const inersectionY = this.entityAABB.intersectionY(blockBoundingBox);
this.position.add( this.position.add(0, inersectionY, 0);
0,
inersectionY,
0
);
this.motion.y = 0; this.motion.y = 0;
this.onGround = true; this.onGround = true;
} }
@ -267,7 +270,6 @@ export class Entity implements IEntity {
if (!this.isDead && this.health <= 0) { if (!this.isDead && this.health <= 0) {
this.isDead = true; this.isDead = true;
} }
if (this.wasHurt) { if (this.wasHurt) {

View File

@ -46,7 +46,7 @@ export class EntityItem extends Entity {
this.age++; this.age++;
if (this.age >= 6000) { if (this.age >= 6000) {
// TODO: Kill entity this.kill();
} }
} }
} }

View File

@ -7,6 +7,7 @@ export interface IEntity {
lastPosition:Vec3, lastPosition:Vec3,
crouching:boolean, crouching:boolean,
isDead:boolean, isDead:boolean,
markedForDisposal:boolean,
updateMetadata:() => void, updateMetadata:() => void,
distanceTo:(entity:IEntity) => number, distanceTo:(entity:IEntity) => number,
onTick:() => void onTick:() => void