From 5c478f461e697822685e81b0c9e2247bb51444ab Mon Sep 17 00:00:00 2001 From: Holly Date: Sun, 20 Aug 2023 02:10:49 +0100 Subject: [PATCH] Store each entities current chunk inside of their object --- server/World.ts | 8 ++++++++ server/entities/Entity.ts | 14 ++++++++++++++ server/entities/EntityLiving.ts | 3 ++- 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/server/World.ts b/server/World.ts index 55ef271..ce8a13f 100644 --- a/server/World.ts +++ b/server/World.ts @@ -121,6 +121,10 @@ export class World { return this.getChunk(chunkX, chunkZ).getBlockId(x & 0xf, y, z & 0xf); } + public getChunkBlockId(chunk:Chunk, x:number, y:number, z:number) { + return chunk.getBlockId(x & 0xf, y, z & 0xf); + } + public getBlockMetadata(x:number, y:number, z:number) { const chunkX = x >> 4, chunkZ = z >> 4; @@ -128,6 +132,10 @@ export class World { return this.getChunk(chunkX, chunkZ).getBlockMetadata(x & 0xf, y, z & 0xf); } + public getChunkBlockMetadata(chunk:Chunk, x:number, y:number, z:number) { + return chunk.getBlockMetadata(x & 0xf, y, z & 0xf); + } + public setBlock(blockId:number, x:number, y:number, z:number, doBlockUpdate?:boolean) { const chunkX = x >> 4, chunkZ = z >> 4; diff --git a/server/entities/Entity.ts b/server/entities/Entity.ts index e2d54e0..bd49b09 100644 --- a/server/entities/Entity.ts +++ b/server/entities/Entity.ts @@ -1,3 +1,4 @@ +import { Chunk } from "../Chunk"; import { MetadataEntry, MetadataWriter } from "../MetadataWriter"; import { World } from "../World"; import { MetadataFieldType } from "../enums/MetadataFieldType"; @@ -21,6 +22,8 @@ export class Entity implements IEntity { public fire:number; + public chunk:Chunk; + public crouching:boolean; private lastCrouchState:boolean; private lastFireState:boolean; @@ -34,6 +37,8 @@ export class Entity implements IEntity { this.x = this.y = this.z = this.lastX = this.lastY = this.lastZ = 0; this.crouching = this.lastCrouchState = this.lastFireState = false; + this.chunk = world.getChunk(this.x >> 4, this.z >> 4); + this.health = 20; } @@ -84,8 +89,17 @@ export class Entity implements IEntity { } } + 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); + } + } + onTick() { this.updateMetadata(); + this.updateEntityChunk(); if (this.fire > 0) { if (this.fire % 20 === 0) { diff --git a/server/entities/EntityLiving.ts b/server/entities/EntityLiving.ts index bd0fbe2..bfa028d 100644 --- a/server/entities/EntityLiving.ts +++ b/server/entities/EntityLiving.ts @@ -48,7 +48,7 @@ export class EntityLiving extends Entity { } isInWater() { - return this.world.getBlockId(this.x, this.y + this.headHeight, this.z) === Block.waterStill.blockId; + return this.world.getChunkBlockId(this.chunk, this.x, this.y + this.headHeight, this.z) === Block.waterStill.blockId; } private constrainRot(rot:number) { @@ -101,6 +101,7 @@ export class EntityLiving extends Entity { // Drowning if (this.isInWater()) { + console.log("in water!"); if (this.timeInWater == Number.MIN_SAFE_INTEGER) { this.timeInWater = 320; }