From 90b4015ba1c64efa48d19542802f0a065dc24b7e Mon Sep 17 00:00:00 2001 From: Niklas Kapelle Date: Sun, 20 Oct 2024 22:20:33 +0200 Subject: [PATCH] get chunk from position --- src/lib/BlockPos.hx | 11 +++++++++++ src/lib/WorldPos.hx | 11 +++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/lib/BlockPos.hx b/src/lib/BlockPos.hx index 691852a..c198124 100644 --- a/src/lib/BlockPos.hx +++ b/src/lib/BlockPos.hx @@ -1,5 +1,6 @@ package lib; +import lib.Vec.Vec2; import lib.Vec.Vec3; /** @@ -51,6 +52,16 @@ abstract BlockPos(Vec3) from Vec3 to Vec3 { return 'BlockPos(${this.x}, ${this.y}, ${this.z})'; } + /** + Returns the chunk the position is in. + **/ + public function chunk():Vec2 { + var x = Math.floor(this.x / 16); + var z = Math.floor(this.z / 16); + + return new Vec2(x, z); + } + /** Returns a list of positions neighboring the block. No Diagonal. **/ diff --git a/src/lib/WorldPos.hx b/src/lib/WorldPos.hx index b99fe7a..8f7d9ec 100644 --- a/src/lib/WorldPos.hx +++ b/src/lib/WorldPos.hx @@ -1,5 +1,6 @@ package lib; +import lib.Vec.Vec2; import lib.Vec.Vec3; /** @@ -65,6 +66,16 @@ abstract WorldPos(Vec3) from Vec3 to Vec3 { return 'WorldPos(${this.x}, ${this.y}, ${this.z})'; } + /** + Returns the chunk the position is in. + **/ + public function chunk():Vec2 { + var x = Math.floor(this.x / 16); + var z = Math.floor(this.z / 16); + + return new Vec2(x, z); + } + public function round():WorldPos { return new WorldPos({ x: Math.fround(this.x),