Skip to content

Commit

Permalink
fix(world): fixed chunk unloading bug
Browse files Browse the repository at this point in the history
  • Loading branch information
PMK744 committed Sep 22, 2024
1 parent 03a55ba commit f55e0cd
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions packages/world/src/components/player/chunk-rendering.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,15 @@ class PlayerChunkRenderingComponent extends PlayerComponent {
* @returns The distance between the player and the chunk.
*/
public distance(hash: bigint): number {
// Convert the chunk hash to a position
// Convert the chunk hash to a chunk position
const { x: cx, z: cz } = ChunkCoords.unhash(hash);

// Get the player's position
const { x: px, z: pz } = this.player.position.floor();
// Get the player's chunk position
const px = this.player.position.x >> 4;
const pz = this.player.position.z >> 4;

// Calculate the distance between the player and the chunk
return Math.max(Math.abs(cx - (px >> 4)), Math.abs(cz - (pz >> 4)));
// Calculate the Euclidean distance between the player and the chunk
return Math.hypot(cx - px, cz - pz);
}

/**
Expand Down Expand Up @@ -206,7 +207,13 @@ class PlayerChunkRenderingComponent extends PlayerComponent {
const distance = this.distance(hash);

// Check if the chunk is outside of the player's view distance
if (distance > this.viewDistance) this.clear(ChunkCoords.unhash(hash));
if (distance > this.viewDistance + 0.5) {
// Get the chunk position
const { x, z } = ChunkCoords.unhash(hash);

// Clear the chunk from the player's view
this.clear({ x, z });
}
}

// Create a new NetworkChunkPublisherUpdatePacket
Expand Down

0 comments on commit f55e0cd

Please sign in to comment.