72 lines
1.9 KiB
TypeScript
72 lines
1.9 KiB
TypeScript
const { generateUniquePolygonId, getAngleSumBetweenPositionAndVertices, isPointInArea2D } = require('./helpers');
|
|
|
|
require('./render');
|
|
require('./events');
|
|
|
|
class PolygonManager {
|
|
pool: Polygon[] = new Array<Polygon>();
|
|
|
|
add(vertices: Vector3Mp[], height: number, visible: boolean, lineColorRGBA: [number, number, number, number], dimension: number = 0) {
|
|
|
|
const polygon: Polygon = {
|
|
id: generateUniquePolygonId(),
|
|
vertices,
|
|
height,
|
|
colliding: false,
|
|
lineColorRGBA: lineColorRGBA,
|
|
dimension: dimension,
|
|
visible: visible
|
|
}
|
|
|
|
this.pool.push(polygon);
|
|
|
|
return polygon;
|
|
}
|
|
|
|
remove(polygon: Polygon) {
|
|
const index = this.pool.findIndex(p => p.id === polygon.id);
|
|
|
|
if (index !== -1)
|
|
this.pool.splice(index, 1);
|
|
}
|
|
|
|
exists(polygon: Polygon) {
|
|
return this.pool.some(p => p.id === polygon.id)
|
|
}
|
|
|
|
isPositionWithinPolygon(position: Vector3Mp, polygon: Polygon, dimension: number) {
|
|
if (dimension && polygon.dimension !== dimension && polygon.dimension !== -1)
|
|
return false;
|
|
|
|
const { vertices } = polygon;
|
|
|
|
const polygonPoints2D = [];
|
|
|
|
for (let i in vertices) {
|
|
if (position.z >= vertices[i].z && position.z <= (vertices[i].z + polygon.height) || getAngleSumBetweenPositionAndVertices(position, vertices) >= 5.8)
|
|
polygonPoints2D.push([vertices[i].x, vertices[i].y]);
|
|
else
|
|
return false;
|
|
}
|
|
|
|
return isPointInArea2D([position.x, position.y], polygonPoints2D);
|
|
}
|
|
}
|
|
|
|
type Polygon = {
|
|
id: number;
|
|
vertices: Vector3Mp[];
|
|
height: number;
|
|
visible: boolean;
|
|
colliding: boolean;
|
|
dimension: number;
|
|
lineColorRGBA: [number, number, number, number];
|
|
};
|
|
|
|
const polygons = new PolygonManager();
|
|
|
|
export default polygons;
|
|
export { Polygon };
|
|
|
|
|