Store each entities current chunk inside of their object

This commit is contained in:
Holly Stubbs 2023-08-20 02:10:49 +01:00
parent 41e1124c04
commit 5c478f461e
Signed by: tgpholly
GPG key ID: B8583C4B7D18119E
3 changed files with 24 additions and 1 deletions

View file

@ -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;

View file

@ -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) {

View file

@ -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;
}