1 |
using System; |
2 |
using System.Collections.Generic; |
3 |
|
4 |
namespace Oni.Akira |
5 |
{ |
6 |
internal class Room |
7 |
{ |
8 |
private Polygon floorPolygon; |
9 |
private RoomBspNode bspTree; |
10 |
private BoundingBox boundingBox; |
11 |
private RoomGrid grid; |
12 |
private Plane floorPlane; |
13 |
private float height; |
14 |
private readonly List<RoomAdjacency> adjacencies = new List<RoomAdjacency>(); |
15 |
|
16 |
public BoundingBox BoundingBox |
17 |
{ |
18 |
get { return boundingBox; } |
19 |
set { boundingBox = value; } |
20 |
} |
21 |
|
22 |
public RoomBspNode BspTree |
23 |
{ |
24 |
get { return bspTree; } |
25 |
set { bspTree = value; } |
26 |
} |
27 |
|
28 |
public RoomGrid Grid |
29 |
{ |
30 |
get { return grid; } |
31 |
set { grid = value; } |
32 |
} |
33 |
|
34 |
public Polygon FloorPolygon |
35 |
{ |
36 |
get { return floorPolygon; } |
37 |
set { floorPolygon = value; } |
38 |
} |
39 |
|
40 |
public Plane FloorPlane |
41 |
{ |
42 |
get { return floorPlane; } |
43 |
set { floorPlane = value; } |
44 |
} |
45 |
|
46 |
public bool IsStairs => floorPlane.Normal.Y < 0.999f; |
47 |
|
48 |
public float Height |
49 |
{ |
50 |
get { return height; } |
51 |
set { height = value; } |
52 |
} |
53 |
|
54 |
public List<RoomAdjacency> Ajacencies => adjacencies; |
55 |
|
56 |
public bool Contains(Vector3 point) |
57 |
{ |
58 |
if (!boundingBox.Contains(point)) |
59 |
return false; |
60 |
|
61 |
bool front = false; |
62 |
RoomBspNode node = bspTree; |
63 |
|
64 |
while (node != null) |
65 |
{ |
66 |
front = (node.Plane.DotCoordinate(point) >= MathHelper.Eps); |
67 |
node = front ? node.FrontChild : node.BackChild; |
68 |
} |
69 |
|
70 |
return !front; |
71 |
} |
72 |
|
73 |
public bool Intersect(BoundingBox bbox) |
74 |
{ |
75 |
if (!boundingBox.Intersects(bbox)) |
76 |
return false; |
77 |
|
78 |
bool front = false; |
79 |
RoomBspNode node = bspTree; |
80 |
|
81 |
while (node != null) |
82 |
{ |
83 |
int intersects = node.Plane.Intersects(bbox); |
84 |
|
85 |
if (intersects == 0) |
86 |
return true; |
87 |
|
88 |
front = intersects > 0; |
89 |
node = front ? node.FrontChild : node.BackChild; |
90 |
} |
91 |
|
92 |
return !front; |
93 |
} |
94 |
|
95 |
public List<Vector3[]> GetFloorPolygons() |
96 |
{ |
97 |
var polys = new List<Vector3[]>(); |
98 |
|
99 |
if (floorPolygon != null) |
100 |
{ |
101 |
polys.Add(floorPolygon.Points.ToArray()); |
102 |
return polys; |
103 |
} |
104 |
|
105 |
var min = new Vector2(boundingBox.Min.X, boundingBox.Min.Z); |
106 |
var max = new Vector2(boundingBox.Max.X, boundingBox.Max.Z); |
107 |
|
108 |
var root = new Polygon2(new[] |
109 |
{ |
110 |
new Vector2(min.X, min.Y), |
111 |
new Vector2(max.X, min.Y), |
112 |
new Vector2(max.X, max.Y), |
113 |
new Vector2(min.X, max.Y) |
114 |
}); |
115 |
|
116 |
var cutter = new Polygon2Clipper(bspTree); |
117 |
|
118 |
foreach (Polygon2 polygon in cutter.Clip(root)) |
119 |
{ |
120 |
var points = new Vector3[polygon.Length]; |
121 |
|
122 |
for (int i = 0; i < points.Length; i++) |
123 |
{ |
124 |
var point = polygon[i]; |
125 |
|
126 |
points[i].X = point.X; |
127 |
points[i].Y = (-floorPlane.D - floorPlane.Normal.Z * point.Y - floorPlane.Normal.X * point.X) / floorPlane.Normal.Y; |
128 |
points[i].Z = point.Y; |
129 |
} |
130 |
|
131 |
Array.Reverse(points); |
132 |
|
133 |
polys.Add(points); |
134 |
} |
135 |
|
136 |
return polys; |
137 |
} |
138 |
} |
139 |
} |