2023-08-20 02:10:49 +01:00
|
|
|
import { Chunk } from "../Chunk";
|
2023-04-10 21:52:30 +01:00
|
|
|
import { MetadataEntry, MetadataWriter } from "../MetadataWriter";
|
2023-04-08 20:52:47 +01:00
|
|
|
import { World } from "../World";
|
2023-04-10 21:52:30 +01:00
|
|
|
import { MetadataFieldType } from "../enums/MetadataFieldType";
|
|
|
|
import { PacketEntityMetadata } from "../packets/EntityMetadata";
|
2023-04-08 20:52:47 +01:00
|
|
|
import { IEntity } from "./IEntity";
|
|
|
|
|
|
|
|
export class Entity implements IEntity {
|
|
|
|
public static nextEntityId:number = 0;
|
|
|
|
|
|
|
|
public entityId:number;
|
|
|
|
|
|
|
|
public world:World;
|
|
|
|
public x:number;
|
|
|
|
public y:number;
|
|
|
|
public z:number;
|
|
|
|
public lastX:number;
|
|
|
|
public lastY:number;
|
|
|
|
public lastZ:number;
|
|
|
|
|
2023-04-13 23:52:13 +01:00
|
|
|
public health:number;
|
|
|
|
|
2023-04-10 21:52:30 +01:00
|
|
|
public fire:number;
|
|
|
|
|
2023-08-20 02:10:49 +01:00
|
|
|
public chunk:Chunk;
|
|
|
|
|
2023-04-10 21:52:30 +01:00
|
|
|
public crouching:boolean;
|
|
|
|
private lastCrouchState:boolean;
|
|
|
|
private lastFireState:boolean;
|
|
|
|
|
2023-04-08 20:52:47 +01:00
|
|
|
public constructor(world:World) {
|
|
|
|
this.entityId = Entity.nextEntityId++;
|
|
|
|
|
2023-04-10 21:52:30 +01:00
|
|
|
this.fire = 0;
|
|
|
|
|
2023-04-08 20:52:47 +01:00
|
|
|
this.world = world;
|
|
|
|
this.x = this.y = this.z = this.lastX = this.lastY = this.lastZ = 0;
|
2023-04-10 21:52:30 +01:00
|
|
|
this.crouching = this.lastCrouchState = this.lastFireState = false;
|
2023-04-13 23:52:13 +01:00
|
|
|
|
2023-08-20 02:10:49 +01:00
|
|
|
this.chunk = world.getChunk(this.x >> 4, this.z >> 4);
|
|
|
|
|
2023-04-13 23:52:13 +01:00
|
|
|
this.health = 20;
|
2023-04-10 21:52:30 +01:00
|
|
|
}
|
|
|
|
|
2023-08-20 01:17:27 +01:00
|
|
|
sendToNearby(buffer:Buffer) {
|
|
|
|
this.world.sendToNearbyClients(this, buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
sendToAllNearby(buffer:Buffer) {
|
|
|
|
this.world.sendToNearbyClients(this, buffer);
|
|
|
|
}
|
|
|
|
|
2023-04-10 21:52:30 +01:00
|
|
|
updateMetadata() {
|
|
|
|
const crouchStateChanged = this.crouching !== this.lastCrouchState;
|
|
|
|
const fireStateChanged = this.fire > 0 !== this.lastFireState;
|
|
|
|
if (crouchStateChanged || fireStateChanged) {
|
|
|
|
const metadata = new MetadataWriter();
|
|
|
|
// Flags:
|
|
|
|
// 1 = On Fire
|
|
|
|
// 2 = Player crouched
|
|
|
|
// 4 = Player on mount?
|
|
|
|
//metadata.addMetadataEntry(0, new MetadataEntry(MetadataFieldType.Byte, 1));
|
|
|
|
if (crouchStateChanged) {
|
|
|
|
metadata.addMetadataEntry(0, new MetadataEntry(MetadataFieldType.Byte, Number(this.fire > 0) + Number(this.crouching) * 2));
|
|
|
|
}
|
|
|
|
|
2023-08-20 01:17:27 +01:00
|
|
|
this.sendToNearby(new PacketEntityMetadata(this.entityId, metadata.writeBuffer()).writeData());
|
2023-04-10 21:52:30 +01:00
|
|
|
|
|
|
|
this.lastCrouchState = this.crouching;
|
|
|
|
this.lastFireState = this.fire > 0;
|
|
|
|
}
|
2023-04-08 20:52:47 +01:00
|
|
|
}
|
2023-04-09 04:19:10 +01:00
|
|
|
|
2023-04-10 14:42:14 +01:00
|
|
|
distanceTo(entity:IEntity) {
|
|
|
|
const dX = entity.x - this.x,
|
|
|
|
dY = entity.y - this.y,
|
|
|
|
dZ = entity.z - this.z;
|
|
|
|
|
|
|
|
return Math.sqrt(Math.pow(dX, 2) + Math.pow(dY, 2) + Math.pow(dZ, 2));
|
|
|
|
}
|
|
|
|
|
2023-04-13 23:52:13 +01:00
|
|
|
damageFrom(damage:number, entity?:IEntity) {
|
2023-08-20 01:17:27 +01:00
|
|
|
if (this.health <= 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-04-13 23:52:13 +01:00
|
|
|
if (entity === undefined) {
|
|
|
|
this.health -= damage;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-20 02:10:49 +01:00
|
|
|
updateEntityChunk() {
|
|
|
|
const bitX = this.x >> 4;
|
|
|
|
const bitZ = this.z >> 4;
|
|
|
|
if (bitX != this.lastX >> 4 || bitZ != this.lastZ >> 4) {
|
|
|
|
this.chunk = this.world.getChunk(bitX, bitZ);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-09 04:19:10 +01:00
|
|
|
onTick() {
|
2023-04-10 21:52:30 +01:00
|
|
|
this.updateMetadata();
|
2023-08-20 02:10:49 +01:00
|
|
|
this.updateEntityChunk();
|
2023-04-10 21:52:30 +01:00
|
|
|
|
|
|
|
if (this.fire > 0) {
|
|
|
|
if (this.fire % 20 === 0) {
|
2023-04-13 23:52:13 +01:00
|
|
|
this.damageFrom(1);
|
2023-04-10 21:52:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
this.fire--;
|
|
|
|
}
|
|
|
|
|
2023-04-09 04:19:10 +01:00
|
|
|
this.lastX = this.x;
|
|
|
|
this.lastY = this.y;
|
|
|
|
this.lastZ = this.z;
|
|
|
|
}
|
2023-04-08 20:52:47 +01:00
|
|
|
}
|