diff options
author | dusoleil <howcansocksbereal@gmail.com> | 2022-09-20 18:52:46 -0400 |
---|---|---|
committer | dusoleil <howcansocksbereal@gmail.com> | 2022-09-20 18:52:46 -0400 |
commit | 743c526b46a1d1c6e0b7ef8b9b0f10b22ee43ab3 (patch) | |
tree | d7e1a2f2c0182664b7fe0e5b40d8b35a7433f8fa /world/monuments/Monument.gd | |
parent | 02cbcb1a7f09c2b4d9ca89bb25cfc0a8a4354b63 (diff) | |
download | game_jam49-743c526b46a1d1c6e0b7ef8b9b0f10b22ee43ab3.tar.gz game_jam49-743c526b46a1d1c6e0b7ef8b9b0f10b22ee43ab3.zip |
Simplify and Fix Monument Code
Simplify the monument chunks to just be a bounding box with the scene origin at the center.
This allows us to automatically calculate the chunks with only the bounds provided.
It also fixes the issues with scaling and rotation.
Diffstat (limited to 'world/monuments/Monument.gd')
-rw-r--r-- | world/monuments/Monument.gd | 44 |
1 files changed, 34 insertions, 10 deletions
diff --git a/world/monuments/Monument.gd b/world/monuments/Monument.gd index 86074f1..e34c2dc 100644 --- a/world/monuments/Monument.gd +++ b/world/monuments/Monument.gd @@ -1,21 +1,45 @@ class_name Monument var xform:Transform = Transform(Basis(),Vector3.ZERO) -var scene:PackedScene = null var chunks = {} -var origin_chunk = Vector2.ZERO +var scene:PackedScene = null var loaded_chunk = null func is_chunk_in_monument(chunk_coords:Vector2): return self.chunks.has(chunk_coords) -func _init(transform,scn,member_chunks, origin): +func _init(transform:Transform,bounds:Vector2,scn:PackedScene): self.xform = transform - self.xform.origin = ChunkLoader.v3_coords(ChunkLoader.chunk_coords(ChunkLoader.v2_coords(self.xform.origin))) + var origin = ChunkLoader.chunk_coords(ChunkLoader.v2_coords(self.xform.origin)) + self.xform.origin.x = origin.x + self.xform.origin.z = origin.y + bounds /= 2.0 + var bounding_box = [] + bounding_box.push_back(ChunkLoader.v2_coords(self.xform.basis * Vector3(-bounds.x,0.0,-bounds.y))) + bounding_box.push_back(ChunkLoader.v2_coords(self.xform.basis * Vector3(bounds.x,0.0,-bounds.y))) + bounding_box.push_back(ChunkLoader.v2_coords(self.xform.basis * Vector3(bounds.x,0.0,bounds.y))) + bounding_box.push_back(ChunkLoader.v2_coords(self.xform.basis * Vector3(-bounds.x,0.0,bounds.y))) + var aabb = bounds + for b in bounding_box: + if b.x > aabb.x: + aabb.x = b.x + if b.y > aabb.y: + aabb.y = b.y + var chunk_size = ChunkLoader.world.chunk_size + aabb = ChunkLoader.chunk_coords(aabb) + for x in range(-aabb.x,aabb.x+chunk_size,chunk_size): + for y in range(-aabb.y,aabb.y+chunk_size,chunk_size): + var test_chunk = Vector2(x,y) + if _point_in_rect(test_chunk,bounding_box): + chunks[test_chunk+origin] = true self.scene = scn - var org = ChunkLoader.chunk_coords(ChunkLoader.v2_coords(self.xform.origin)) - for c in member_chunks.keys(): - var c2 = ChunkLoader.v2_coords(self.xform.basis * ChunkLoader.v3_coords(c)) - self.chunks[ChunkLoader.chunk_coords(c2)+org] = member_chunks[c] - origin = ChunkLoader.v2_coords(self.xform.basis * ChunkLoader.v3_coords(origin)) - self.origin_chunk = ChunkLoader.chunk_coords(origin)+org + +func _point_in_rect(point,rect): + var am = point - rect[0] + var ab = rect[1] - rect[0] + var ad = rect[3] - rect[0] + var am_dot_ab = am.dot(ab) + var ab_dot_ab = ab.dot(ab) + var am_dot_ad = am.dot(ad) + var ad_dot_ad = ad.dot(ad) + return 0 <= am_dot_ab && am_dot_ab <= ab_dot_ab && 0 <= am_dot_ad && am_dot_ad <= ad_dot_ad |