mc-beta-server/server/entities/EntityLiving.ts

84 lines
2.1 KiB
TypeScript
Raw Permalink Normal View History

2023-11-09 21:59:45 +00:00
import { IReader, IWriter } from "bufferstuff";
import Block from "../blocks/Block";
import Entity from "./Entity";
import EntityStatus from "../enums/EntityStatus";
import IEntity from "./IEntity";
import PacketEntityStatus from "../packets/EntityStatus";
import World from "../World";
export default class EntityLiving extends Entity {
2023-08-20 01:17:27 +01:00
public timeInWater:number;
public headHeight:number;
public lastHealth:number;
2023-04-09 04:19:10 +01:00
public constructor(world:World, isPlayer:boolean = false) {
super(world, isPlayer);
2023-04-09 04:19:10 +01:00
2023-11-05 13:30:17 +00:00
this.timeInWater = 0;
2023-08-20 01:17:27 +01:00
this.headHeight = 1.62;
this.lastHealth = this.health;
2023-08-20 01:17:27 +01:00
}
2023-11-09 21:59:45 +00:00
public fromSave(reader:IReader) {
super.fromSave(reader);
this.timeInWater = reader.readShort();
}
public toSave(writer:IWriter) {
super.toSave(writer);
writer.writeShort(this.timeInWater == Number.MIN_SAFE_INTEGER ? 0 : this.timeInWater);
}
2023-08-20 01:17:27 +01:00
damageFrom(damage:number, entity?:IEntity) {
2023-12-18 21:49:23 +00:00
super.damageFrom(damage, entity);
2023-08-20 01:17:27 +01:00
if (this.health <= 0) {
2023-11-09 21:59:45 +00:00
this.isDead = true;
2023-08-20 01:17:27 +01:00
}
2023-11-02 08:31:43 +00:00
// Send Damage Animation packet or death packet
2023-11-09 21:59:45 +00:00
if (this.isDead) {
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
}
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-11-09 21:59:45 +00:00
if (!this.isPlayer && !this.motion.isZero) {
this.moveEntity(this.motion.x, this.motion.y, this.motion.z);
}
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
}
}