diff --git a/server/entities/Entity.ts b/server/entities/Entity.ts index 4ec1fc6..e2d54e0 100644 --- a/server/entities/Entity.ts +++ b/server/entities/Entity.ts @@ -37,6 +37,14 @@ export class Entity implements IEntity { this.health = 20; } + sendToNearby(buffer:Buffer) { + this.world.sendToNearbyClients(this, buffer); + } + + sendToAllNearby(buffer:Buffer) { + this.world.sendToNearbyClients(this, buffer); + } + updateMetadata() { const crouchStateChanged = this.crouching !== this.lastCrouchState; const fireStateChanged = this.fire > 0 !== this.lastFireState; @@ -51,7 +59,7 @@ export class Entity implements IEntity { metadata.addMetadataEntry(0, new MetadataEntry(MetadataFieldType.Byte, Number(this.fire > 0) + Number(this.crouching) * 2)); } - this.world.sendToNearbyAllNearbyClients(this, new PacketEntityMetadata(this.entityId, metadata.writeBuffer()).writeData()); + this.sendToNearby(new PacketEntityMetadata(this.entityId, metadata.writeBuffer()).writeData()); this.lastCrouchState = this.crouching; this.lastFireState = this.fire > 0; @@ -67,6 +75,10 @@ export class Entity implements IEntity { } damageFrom(damage:number, entity?:IEntity) { + if (this.health <= 0) { + return; + } + if (entity === undefined) { this.health -= damage; } diff --git a/server/entities/EntityLiving.ts b/server/entities/EntityLiving.ts index 6f6bc50..bd0fbe2 100644 --- a/server/entities/EntityLiving.ts +++ b/server/entities/EntityLiving.ts @@ -1,9 +1,13 @@ import { World } from "../World"; +import { Block } from "../blocks/Block"; +import { PacketAnimation } from "../packets/Animation"; import { PacketEntityLook } from "../packets/EntityLook"; import { PacketEntityLookRelativeMove } from "../packets/EntityLookRelativeMove"; import { PacketEntityRelativeMove } from "../packets/EntityRelativeMove"; +import { PacketEntityStatus } from "../packets/EntityStatus"; import { PacketEntityTeleport } from "../packets/EntityTeleport"; import { Entity } from "./Entity"; +import { IEntity } from "./IEntity"; export class EntityLiving extends Entity { public yaw:number; @@ -12,6 +16,8 @@ export class EntityLiving extends Entity { public lastPitch:number; public onGround:boolean; public fallDistance:number; + public timeInWater:number; + public headHeight:number; public absX:number; public absY:number; @@ -27,8 +33,22 @@ export class EntityLiving extends Entity { public constructor(world:World) { super(world); - this.fallDistance = this.yaw = this.lastYaw = this.pitch = this.lastPitch = this.absX = this.absY = this.absZ = this.absYaw = this.absPitch = this.lastAbsX = this.lastAbsY = this.lastAbsZ = this.lastAbsYaw = this.lastAbsPitch = 0; + this.fallDistance = this.yaw = this.lastYaw = this.pitch = this.lastPitch = this.absX = this.absY = this.absZ = this.absYaw = this.absPitch = this.lastAbsX = this.lastAbsY = this.lastAbsZ = this.lastAbsYaw = this.lastAbsPitch = this.timeInWater = 0; this.onGround = true; + this.headHeight = 1.62; + } + + damageFrom(damage:number, entity?:IEntity) { + if (this.health <= 0) { + return; + } + super.damageFrom(damage, entity); + // Send Damage Animation packet + this.sendToAllNearby(new PacketEntityStatus(this.entityId, 2).writeData()); + } + + isInWater() { + return this.world.getBlockId(this.x, this.y + this.headHeight, this.z) === Block.waterStill.blockId; } private constrainRot(rot:number) { @@ -79,6 +99,19 @@ export class EntityLiving extends Entity { this.fallDistance } + // Drowning + if (this.isInWater()) { + if (this.timeInWater == Number.MIN_SAFE_INTEGER) { + this.timeInWater = 320; + } + if (this.timeInWater <= 0 && this.timeInWater % 20 === 0) { + this.damageFrom(1); + } + this.timeInWater--; + } else { + this.timeInWater = Number.MIN_SAFE_INTEGER; + } + this.lastYaw = this.yaw; this.lastPitch = this.pitch; }