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

91 lines
2.5 KiB
TypeScript
Raw Normal View History

2023-11-09 21:59:45 +00:00
import { IReader, IWriter } from "bufferstuff";
2023-11-02 08:31:43 +00:00
import { Rotation } from "../Rotation";
2023-11-08 15:45:25 +00:00
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";
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-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
}
}