2023-11-02 08:31:43 +00:00
|
|
|
import { Rotation } from "../Rotation";
|
|
|
|
import { Vec3 } from "../Vec3";
|
2023-04-09 04:19:10 +01:00
|
|
|
import { World } from "../World";
|
2023-08-20 01:17:27 +01:00
|
|
|
import { Block } from "../blocks/Block";
|
2023-11-05 00:55:23 +00:00
|
|
|
import { EntityStatus } from "../enums/EntityStatus";
|
2023-08-20 01:17:27 +01:00
|
|
|
import { PacketAnimation } from "../packets/Animation";
|
2023-04-10 14:42:14 +01:00
|
|
|
import { PacketEntityLook } from "../packets/EntityLook";
|
|
|
|
import { PacketEntityLookRelativeMove } from "../packets/EntityLookRelativeMove";
|
|
|
|
import { PacketEntityRelativeMove } from "../packets/EntityRelativeMove";
|
2023-08-20 01:17:27 +01:00
|
|
|
import { PacketEntityStatus } from "../packets/EntityStatus";
|
2023-04-10 14:42:14 +01:00
|
|
|
import { PacketEntityTeleport } from "../packets/EntityTeleport";
|
2023-04-09 04:19:10 +01:00
|
|
|
import { Entity } from "./Entity";
|
2023-08-20 01:17:27 +01:00
|
|
|
import { IEntity } from "./IEntity";
|
2023-04-09 04:19:10 +01:00
|
|
|
|
|
|
|
export class EntityLiving extends Entity {
|
2023-04-13 23:52:13 +01:00
|
|
|
public fallDistance:number;
|
2023-08-20 01:17:27 +01:00
|
|
|
public timeInWater:number;
|
|
|
|
public headHeight:number;
|
2023-11-05 00:55:23 +00:00
|
|
|
public lastHealth:number;
|
2023-04-09 04:19:10 +01:00
|
|
|
|
|
|
|
public constructor(world:World) {
|
|
|
|
super(world);
|
|
|
|
|
2023-11-05 13:30:17 +00:00
|
|
|
this.timeInWater = 0;
|
2023-08-20 01:17:27 +01:00
|
|
|
this.headHeight = 1.62;
|
2023-11-05 00:55:23 +00:00
|
|
|
|
2023-11-05 13:30:17 +00:00
|
|
|
this.fallDistance = Number.MIN_VALUE;
|
|
|
|
|
2023-11-05 00:55:23 +00:00
|
|
|
this.lastHealth = this.health;
|
2023-08-20 01:17:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
damageFrom(damage:number, entity?:IEntity) {
|
|
|
|
if (this.health <= 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
super.damageFrom(damage, entity);
|
2023-11-02 08:31:43 +00:00
|
|
|
|
2023-11-05 00:55:23 +00:00
|
|
|
// Send Damage Animation packet or death packet
|
|
|
|
if (this.health === 0) {
|
|
|
|
this.sendToAllNearby(new PacketEntityStatus(this.entityId, EntityStatus.Dead).writeData());
|
|
|
|
} else {
|
|
|
|
this.sendToAllNearby(new PacketEntityStatus(this.entityId, EntityStatus.Hurt).writeData());
|
|
|
|
}
|
2023-08-20 01:17:27 +01:00
|
|
|
}
|
|
|
|
|
2023-11-05 13:30:17 +00:00
|
|
|
isInWater(fromHead:boolean) {
|
|
|
|
return this.world.getChunkBlockId(this.chunk, this.position.x, this.position.y + (fromHead ? this.headHeight : 0), this.position.z) === Block.waterStill.blockId;
|
2023-04-10 14:42:14 +01:00
|
|
|
}
|
|
|
|
|
2023-11-05 00:55:23 +00:00
|
|
|
fall(distance:number) {
|
|
|
|
const adjustedFallDistance = Math.ceil(distance - 3);
|
|
|
|
if (adjustedFallDistance > 0) {
|
|
|
|
this.damageFrom(adjustedFallDistance);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-09 04:19:10 +01:00
|
|
|
onTick() {
|
|
|
|
super.onTick();
|
2023-04-13 23:52:13 +01:00
|
|
|
|
2023-08-20 01:17:27 +01:00
|
|
|
// Drowning
|
2023-11-05 13:30:17 +00:00
|
|
|
if (this.isInWater(true)) {
|
2023-08-20 01:17:27 +01:00
|
|
|
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;
|
|
|
|
}
|
2023-11-05 13:30:17 +00:00
|
|
|
|
|
|
|
if (this.isInWater(false)) {
|
|
|
|
this.fallDistance = 0;
|
|
|
|
}
|
2023-04-09 04:19:10 +01:00
|
|
|
}
|
|
|
|
}
|