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

314 lines
9.3 KiB
TypeScript
Raw Permalink Normal View History

import { IReader, IWriter } from "bufferstuff";
2023-04-10 21:52:30 +01:00
import { MetadataEntry, MetadataWriter } from "../MetadataWriter";
import AABB from "../AABB";
import Block from "../blocks/Block";
import Chunk from "../Chunk";
import IEntity from "./IEntity";
import MetadataFieldType from "../enums/MetadataFieldType";
import PacketEntityLook from "../packets/EntityLook";
import PacketEntityLookRelativeMove from "../packets/EntityLookRelativeMove";
import PacketEntityMetadata from "../packets/EntityMetadata";
import PacketEntityRelativeMove from "../packets/EntityRelativeMove";
import PacketEntityTeleport from "../packets/EntityTeleport";
import PacketEntityVelocity from "../packets/EntityVelocity";
import Player from "./Player";
import Rotation from "../Rotation";
import Vec2 from "../Vec2";
2023-11-08 15:45:25 +00:00
import Vec3 from "../Vec3";
import World from "../World";
export default class Entity implements IEntity {
2023-04-08 20:52:47 +01:00
public static nextEntityId:number = 0;
public entityId:number;
2023-11-08 15:45:25 +00:00
public entitySize:Vec2;
2023-04-08 20:52:47 +01:00
public world:World;
2023-11-02 08:31:43 +00:00
public position:Vec3;
public lastPosition:Vec3;
public absPosition:Vec3;
public lastAbsPosition:Vec3;
public motion:Vec3;
2023-11-02 08:31:43 +00:00
private moveEntityBlockPosRel:Vec3;
2023-11-02 08:31:43 +00:00
public rotation:Rotation;
public lastRotation:Rotation;
public absRotation:Rotation;
public lastAbsRotation:Rotation;
2023-04-13 23:52:13 +01:00
public health:number;
2023-11-02 08:31:43 +00:00
public wasHurt:boolean;
public isDead:boolean;
2023-04-13 23:52:13 +01:00
2023-04-10 21:52:30 +01:00
public fire:number;
public fallDistance:number;
public onGround:boolean;
2023-04-10 21:52:30 +01:00
public chunk:Chunk;
2023-04-10 21:52:30 +01:00
public crouching:boolean;
private lastCrouchState:boolean;
private lastFireState:boolean;
public entityAABB:AABB;
2023-11-09 21:59:45 +00:00
public readonly isPlayer:boolean;
2023-09-04 23:42:38 +01:00
private queuedChunkUpdate:boolean;
2023-12-06 00:04:57 +00:00
public markedForDisposal:boolean = false;
public constructor(world:World, isPlayer:boolean = false) {
2023-04-08 20:52:47 +01:00
this.entityId = Entity.nextEntityId++;
2023-11-08 15:45:25 +00:00
this.isPlayer = isPlayer;
2023-11-08 15:45:25 +00:00
this.entitySize = new Vec2(0.6, 1.8);
this.entityAABB = new AABB(-this.entitySize.x / 2, 0, -this.entitySize.x / 2, this.entitySize.x / 2, this.entitySize.y, this.entitySize.x / 2);
2023-04-08 20:52:47 +01:00
this.fire = this.fallDistance = 0;
this.onGround = false;
2023-04-10 21:52:30 +01:00
2023-04-08 20:52:47 +01:00
this.world = world;
2023-11-02 08:31:43 +00:00
this.position = new Vec3();
this.lastPosition = new Vec3();
this.absPosition = new Vec3();
this.lastAbsPosition = new Vec3();
this.motion = new Vec3();
2023-11-02 08:31:43 +00:00
this.moveEntityBlockPosRel = new Vec3();
2023-11-02 08:31:43 +00:00
this.rotation = new Rotation();
this.lastRotation = new Rotation();
this.absRotation = new Rotation();
this.lastAbsRotation = new Rotation();
2023-09-04 23:42:38 +01:00
this.crouching = this.lastCrouchState = this.lastFireState = this.queuedChunkUpdate = false;
2023-04-13 23:52:13 +01:00
2023-11-02 08:31:43 +00:00
this.chunk = world.getChunk(this.position.x >> 4, this.position.z >> 4);
2023-04-13 23:52:13 +01:00
this.health = 20;
2023-11-02 08:31:43 +00:00
this.wasHurt = false;
this.isDead = false;
2023-04-10 21:52:30 +01:00
}
2023-11-09 17:04:11 +00:00
public fromSave(reader:IReader) {
this.position.set(reader.readDouble(), reader.readDouble(), reader.readDouble());
this.motion.set(reader.readFloat(), reader.readFloat(), reader.readFloat());
this.rotation.set(reader.readFloat(), reader.readFloat());
this.fire = reader.readShort();
this.fallDistance = reader.readFloat();
this.health = reader.readByte();
}
public toSave(writer:IWriter) {
writer.writeDouble(this.position.x).writeDouble(this.position.y).writeDouble(this.position.z) // Position
2023-11-09 21:59:45 +00:00
.writeFloat(this.motion.x).writeFloat(this.motion.y).writeFloat(this.motion.z) // Motion
2023-11-09 17:04:11 +00:00
.writeFloat(this.rotation.x).writeFloat(this.rotation.y) // Rotation
.writeShort(this.fire)
.writeFloat(this.fallDistance)
.writeByte(this.health);
}
async collidesWithPlayer(aabb:AABB) {
2023-12-18 01:23:52 +00:00
let collidedWith:Player | undefined;
await this.world.players.forEach(player => {
if (this.entityAABB.intersects(player.entityAABB) && collidedWith == undefined) {
2023-12-18 01:23:52 +00:00
collidedWith = player;
}
});
return collidedWith;
}
2023-08-20 01:17:27 +01:00
sendToNearby(buffer:Buffer) {
this.world.sendToNearbyClients(this, buffer);
}
sendToAllNearby(buffer:Buffer) {
this.world.sendToNearbyAllNearbyClients(this, buffer);
2023-08-20 01:17:27 +01:00
}
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?
2023-11-02 08:31:43 +00:00
metadata.addMetadataEntry(0, new MetadataEntry(MetadataFieldType.Byte, Number(this.fire > 0) + Number(this.crouching) * 2));
2023-04-10 21:52:30 +01:00
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) {
2023-11-02 08:31:43 +00:00
const dX = entity.position.x - this.position.x,
dY = entity.position.y - this.position.y,
dZ = entity.position.z - this.position.z;
2023-04-10 14:42:14 +01:00
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;
} else {
this.health -= damage;
2023-04-13 23:52:13 +01:00
}
2023-11-02 08:31:43 +00:00
this.wasHurt = true;
2023-04-13 23:52:13 +01:00
}
updateEntityChunk() {
2023-11-02 08:31:43 +00:00
const bitX = this.position.x >> 4;
const bitZ = this.position.z >> 4;
if (bitX != this.lastPosition.x >> 4 || bitZ != this.lastPosition.z >> 4 || this.queuedChunkUpdate) {
2023-09-04 23:42:38 +01:00
if (this.world.chunkExists(bitX, bitZ)) {
this.chunk = this.world.getChunk(bitX, bitZ);
this.queuedChunkUpdate = false;
} else {
this.queuedChunkUpdate = true;
}
}
}
2023-11-02 08:31:43 +00:00
private constrainRot(rot:number) {
return Math.min(Math.max(rot, -128), 127);
}
private sendPositionUpdate() {
this.absPosition.set(Math.floor(this.position.x * 32), Math.floor(this.position.y * 32), Math.floor(this.position.z * 32));
const yaw = this.rotation.yaw / 256 * 180;
const pitch = this.rotation.pitch / 256 * 180;
2023-11-02 08:31:43 +00:00
this.absRotation.set(
Math.floor(yaw - Math.floor((yaw + 128) / 256) * 256),
Math.floor(pitch - Math.floor((pitch + 128) / 256) * 256)
2023-11-02 08:31:43 +00:00
);
const diffX = this.absPosition.x - this.lastAbsPosition.x;
const diffY = this.absPosition.y - this.lastAbsPosition.y;
const diffZ = this.absPosition.z - this.lastAbsPosition.z;
const diffYaw = this.absRotation.yaw - this.lastAbsRotation.yaw;
const diffPitch = this.absRotation.pitch - this.lastAbsRotation.pitch;
const doRelativeMove = Math.abs(diffX) >= 4 || Math.abs(diffY) >= 4 || Math.abs(diffZ) >= 4;
const doLook = Math.abs(diffYaw) >= 4 || Math.abs(diffPitch) >= 4;
if (Math.abs(diffX) > 128 || Math.abs(diffY) > 128 || Math.abs(diffZ) > 128) {
this.sendToNearby(new PacketEntityTeleport(this.entityId, this.absPosition.x, this.absPosition.y, this.absPosition.z, this.absRotation.yaw, this.absRotation.pitch).writeData());
2023-11-02 08:31:43 +00:00
} else if (doRelativeMove && doLook) {
this.sendToNearby(new PacketEntityLookRelativeMove(this.entityId, diffX, diffY, diffZ, this.absRotation.yaw, this.absRotation.pitch).writeData());
2023-11-02 08:31:43 +00:00
} else if (doRelativeMove) {
this.sendToNearby(new PacketEntityRelativeMove(this.entityId, diffX, diffY, diffZ).writeData());
2023-11-02 08:31:43 +00:00
} else if (doLook) {
this.sendToNearby(new PacketEntityLook(this.entityId, this.absRotation.yaw, this.absRotation.pitch).writeData());
2023-11-02 08:31:43 +00:00
}
2023-11-05 10:58:47 +00:00
if (!this.motion.isZero) {
this.sendToNearby(new PacketEntityVelocity(this.entityId, this.motion.x, this.motion.y, this.motion.z).writeData());
}
2023-11-02 08:31:43 +00:00
if (doRelativeMove) {
this.lastAbsPosition.set(this.absPosition);
}
if (doLook) {
this.lastAbsRotation.set(this.absRotation);
}
}
fall(distance:number) {
// TODO: Entity falling mount transfer
}
2023-12-06 00:04:57 +00:00
kill() {
this.health = 0;
this.markedForDisposal = true;
}
updateFalling(distance:number) {
if (this.onGround) {
2023-11-05 13:30:17 +00:00
if (this.fallDistance > 0) {
this.fall(this.fallDistance);
this.fallDistance = 0;
}
} else if (distance < 0) {
this.fallDistance -= distance;
}
}
2024-07-09 21:52:13 +01:00
private getBlockAABBFor(x:number, y:number, z:number) {
const blockId = this.chunk.getBlockId(x, y, z);
const blockEntityIsTouching = blockId > 0 ? Block.blocks[blockId] : null;
if (blockEntityIsTouching != null) {
return blockEntityIsTouching.getBoundingBox(Math.floor(this.position.x), Math.floor(this.position.y), Math.floor(this.position.z));
}
return null;
}
moveEntity(motionX:number, motionY:number, motionZ:number) {
this.position.add(motionX, motionY, motionZ);
this.entityAABB.move(this.position);
2024-07-09 21:52:13 +01:00
let blockAABB = this.getBlockAABBFor(Math.floor(this.position.x) & 0xf, Math.floor(this.position.y), Math.floor(this.position.z) & 0xf);
2024-07-09 21:52:13 +01:00
if (blockAABB !== null) {
this.moveEntityBlockPosRel.set(this.position);
this.moveEntityBlockPosRel.sub(Math.floor(this.position.x), Math.floor(this.position.y), Math.floor(this.position.z));
2023-11-09 21:59:45 +00:00
// TODO: Handle X and Z collisions.
2024-07-09 21:52:13 +01:00
if (this.entityAABB.intersects(blockAABB)) {
const intersectionY = this.entityAABB.intersectionY(blockAABB);
if (this.moveEntityBlockPosRel.y > 0.5) {
2024-07-09 21:52:13 +01:00
motionY = intersectionY;
} else {
2024-07-09 21:52:13 +01:00
motionY = -intersectionY;
}
}
2024-07-09 21:52:13 +01:00
this.position.add(0, motionY, 0);
this.motion.y = 0;
this.onGround = true;
}
}
2023-04-09 04:19:10 +01:00
onTick() {
2023-04-10 21:52:30 +01:00
this.updateMetadata();
this.updateEntityChunk();
this.updateFalling(this.motion.y);
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-11-02 08:31:43 +00:00
if (!this.isDead && this.health <= 0) {
this.isDead = true;
}
if (this.wasHurt) {
this.wasHurt = false;
}
this.sendPositionUpdate();
this.lastPosition.set(this.position);
2023-04-09 04:19:10 +01:00
}
2023-04-08 20:52:47 +01:00
}