-
Notifications
You must be signed in to change notification settings - Fork 1
/
room.go
145 lines (134 loc) · 3.22 KB
/
room.go
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package world
type Room struct {
Floor [][]func() ITile
InterestX int
InterestZ int
}
func (r *Room) Width() int {
if r.Height() == 0 {
return 0
}
return len(r.Floor[0])
}
func (r *Room) Height() int {
return len(r.Floor)
}
func (r *Room) GetTile(x, z int) ITile {
if z < 0 || z >= len(r.Floor) {
return nil
}
if x < 0 || x >= len(r.Floor[0]) {
return nil
}
if r.Floor[z][x] == nil {
return nil
}
return r.Floor[z][x]()
}
func (r *Room) CanApply(w *World, x, z int) bool {
for i := 0; i < r.Width(); i++ {
for j := 0; j < r.Height(); j++ {
wI := x+i
wJ := z+j
if w.GetTile(wI, wJ) != w.DefaultTile() {
return false
}
}
}
return true
}
func (r *Room) Apply(w *World, x, z int, y float32) (min, max *Coords) {
w.GrowZ(z+r.Height())
w.GrowZ(z)
w.GrowX(x+r.Width())
w.GrowX(x)
for i := 0; i < r.Width(); i++ {
for j := 0; j < r.Height(); j++ {
wI := x+i
wJ := z+j
occupied := false
if w.GetTile(wI, wJ) != w.DefaultTile() {
occupied = true
}
t := r.GetTile(i, j)
if t != nil {
w.SetTile(wI, wJ, t)
if occupied {
t.SetConnect(false)
}
t.SetY(t.Y()+y)
}
}
}
return &Coords{x, z}, &Coords{x+r.Width()-1, z+r.Height()-1}
}
func (r *Room) FindConnect() []*Coords {
coords := []*Coords{}
for x := 0; x < r.Width(); x++ {
for z := 0; z < r.Height(); z++ {
if t := r.GetTile(x, z); t != nil && t.Connect() {
coords = append(coords, &Coords{X: x, Z: z})
}
}
}
return coords
}
func (r *Room) CanConnect(tileComparator func(ITile, ITile) bool, roomX, roomZ int, w *World, worldX, worldZ int) bool {
if !w.GetTile(worldX, worldZ).Connect() || !r.GetTile(roomX, roomZ).Connect() {
return false
}
anyChange := false
for i := 0; i < r.Width(); i++ {
for j := 0; j < r.Height(); j++ {
wI := worldX+i-roomX
wJ := worldZ+j-roomZ
if (wI != worldX || wJ != worldZ) && w.GetTile(wI, wJ) != w.DefaultTile() && r.GetTile(i, j) != nil && !tileComparator(w.GetTile(wI, wJ), r.GetTile(i, j)) {
return false
}
if w.GetTile(wI, wJ) != w.DefaultTile() && r.GetTile(i, j) != nil {
anyChange = true
}
}
}
if anyChange {
return true
}
return false
}
func (r *Room) Connect(connectorTile func() ITile, roomX, roomZ int, w *World, worldX, worldZ int) (min, max *Coords) {
tile := w.GetTile(worldX, worldZ)
minCoords, maxCoords := r.Apply(w, worldX-roomX, worldZ-roomZ, tile.Y())
if connectorTile != nil && (r.Width() > 1 || r.Height() > 1) {
connector := connectorTile()
connector.SetY(tile.Y())
tile = w.GetTile(worldX, worldZ)
connector.SetCleanable(tile.Cleanable())
w.SetTile(worldX, worldZ, connector)
}
return minCoords, maxCoords
}
func (r *Room) ValidTiles() []*Coords {
validTiles := []*Coords{}
for x := 0; x < r.Width(); x++ {
for z := 0; z < r.Height(); z++ {
valid := true
if x == r.InterestX && z == r.InterestZ {
continue
}
for dx := -1; dx <= 1; dx++ {
for dz := -1; dz <= 1; dz++ {
xC := x+dx
zC := z+dz
tile := r.GetTile(xC, zC)
if tile == nil || !tile.WalkThrough() || tile.FallThrough() {
valid = false
}
}
}
if valid {
validTiles = append(validTiles, &Coords{X: x, Z: z})
}
}
}
return validTiles
}