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

62 lines
1.7 KiB
TypeScript
Raw Normal View History

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";
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-09 04:47:23 +01:00
public onGround:boolean;
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-04-09 04:19:10 +01:00
public constructor(world:World) {
super(world);
2023-11-02 08:31:43 +00:00
this.fallDistance = this.timeInWater = 0;
2023-04-13 23:52:13 +01:00
this.onGround = true;
2023-08-20 01:17:27 +01:00
this.headHeight = 1.62;
}
damageFrom(damage:number, entity?:IEntity) {
if (this.health <= 0) {
return;
}
super.damageFrom(damage, entity);
2023-11-02 08:31:43 +00:00
2023-08-20 01:17:27 +01:00
// Send Damage Animation packet
this.sendToAllNearby(new PacketEntityStatus(this.entityId, 2).writeData());
}
isInWater() {
2023-11-02 08:31:43 +00:00
return this.world.getChunkBlockId(this.chunk, this.position.x, this.position.y + this.headHeight, this.position.z) === Block.waterStill.blockId;
2023-04-10 14:42:14 +01:00
}
2023-04-09 04:19:10 +01:00
onTick() {
super.onTick();
2023-04-13 23:52:13 +01:00
if (!this.onGround) {
this.fallDistance
}
2023-08-20 01:17:27 +01:00
// 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;
}
2023-04-09 04:19:10 +01:00
}
}