summaryrefslogtreecommitdiffstats
path: root/world/monuments/Monument.gd
blob: e34c2dc5a632456502e23203e0e7101bae6223c6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
class_name Monument

var xform:Transform = Transform(Basis(),Vector3.ZERO)
var chunks = {}
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:Transform,bounds:Vector2,scn:PackedScene):
    self.xform = transform
    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

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