Fix polygon api

This commit is contained in:
hydrant
2021-04-22 17:52:39 +02:00
parent 17c2392a64
commit c276b87977
6 changed files with 114 additions and 85 deletions

View File

@@ -1,16 +1,18 @@
import polygons from ".";
setInterval(() => {
const { position, dimension } = mp.players.local;
mp.polygons.pool.map((polygon) => {
polygons.pool.map((polygon) => {
if (polygon.colliding) {
if (!mp.polygons.isPositionWithinPolygon(position, polygon, dimension)) {
if (!polygons.isPositionWithinPolygon(position, polygon, dimension)) {
polygon.colliding = false;
mp.events.call('playerLeavePolygon', polygon);
}
}
else {
if (mp.polygons.isPositionWithinPolygon(position, polygon, dimension)) {
if (polygons.isPositionWithinPolygon(position, polygon, dimension)) {
polygon.colliding = true;
mp.events.call('playerEnterPolygon', polygon);
}

View File

@@ -1,4 +1,6 @@
const isPointInArea2D = (point, area) => {
import polygons from ".";
const isPointInArea2D = (point, area): boolean => {
let x = point[0], y = point[1];
let inside = false;
@@ -20,9 +22,9 @@ const isPointInArea2D = (point, area) => {
const TWOPI = 6.283185307179586476925287;
const EPSILON = 0.0000001;
const modulus = (p) => Math.sqrt((p.x * p.x) + (p.y * p.y) + (p.z *p.z));
const modulus = (p): number => Math.sqrt((p.x * p.x) + (p.y * p.y) + (p.z *p.z));
const getAngleSumBetweenPositionAndVertices = (position, vertices) => {
const getAngleSumBetweenPositionAndVertices = (position, vertices): number => {
let i;
let m1, m2;
let anglesum=0, costheta;
@@ -42,12 +44,12 @@ const getAngleSumBetweenPositionAndVertices = (position, vertices) => {
anglesum += Math.acos(costheta);
}
return(anglesum);
return (anglesum);
}
const generateUniquePolygonId = () => {
const generateUniquePolygonId = (): number => {
const timestamp = Date.now();
return mp.polygons.pool.some(p => p.id === timestamp) ? generateUniquePolygonId() : timestamp;
return polygons.pool.some(p => p.id === timestamp) ? generateUniquePolygonId() : timestamp;
};
export { isPointInArea2D, generateUniquePolygonId, getAngleSumBetweenPositionAndVertices };

View File

@@ -1,48 +0,0 @@
const { generateUniquePolygonId, getAngleSumBetweenPositionAndVertices, isPointInArea2D } = require('polygons/helpers');
require('polygons/render');
require('polygons/events');
mp.polygons = {
pool: [],
add: (vertices, height, options = { visible: false, lineColorRGBA: [255,255,255,255], dimension: 0 }) => {
const polygon = {
id: generateUniquePolygonId(),
vertices,
height,
...options,
colliding: false
}
mp.polygons.pool.push(polygon);
return polygon;
},
remove: (polygon) => {
const index = mp.polygons.pool.findIndex(p => p.id === polygon.id);
if (index !== -1)
mp.polygons.pool.splice(index, 1);
},
exists: (polygon) => {
return mp.polygons.pool.some(p => p.id === polygon.id)
},
isPositionWithinPolygon: (position, polygon, dimension) => {
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);
}
}

View File

@@ -0,0 +1,71 @@
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 };

View File

@@ -1,27 +0,0 @@
mp.events.add('render', () => {
mp.polygons.pool?.forEach(polygon => {
if (!polygon.visible) return;
const { vertices, height, lineColorRGBA } = polygon;
vertices.forEach((vertex, index) => {
const nextVertex = index === (vertices.length - 1) ? vertices[0] : vertices[index + 1];
// Deepness lower line
mp.game.graphics.drawLine(vertex.x, vertex.y, vertex.z, nextVertex.x, nextVertex.y, nextVertex.z, ...lineColorRGBA);
// Current vertex height line
mp.game.graphics.drawLine(vertex.x, vertex.y, vertex.z, vertex.x, vertex.y, vertex.z + height, ...lineColorRGBA);
// Next vertex height line
mp.game.graphics.drawLine(nextVertex.x, nextVertex.y, nextVertex.z, nextVertex.x, nextVertex.y, nextVertex.z + height, ...lineColorRGBA);
// Deepness higher line
mp.game.graphics.drawLine(vertex.x, vertex.y, vertex.z + height, nextVertex.x, nextVertex.y, nextVertex.z + height, ...lineColorRGBA);
});
});
});

View File

@@ -0,0 +1,29 @@
import polygons from ".";
mp.events.add('render', () => {
polygons.pool.forEach(polygon => {
if (!polygon.visible) return;
const { vertices, height, lineColorRGBA } = polygon;
vertices.forEach((vertex, index) => {
const nextVertex = index === (vertices.length - 1) ? vertices[0] : vertices[index + 1];
// Deepness lower line
mp.game.graphics.drawLine(vertex.x, vertex.y, vertex.z, nextVertex.x, nextVertex.y, nextVertex.z, ...lineColorRGBA);
// Current vertex height line
mp.game.graphics.drawLine(vertex.x, vertex.y, vertex.z, vertex.x, vertex.y, vertex.z + height, ...lineColorRGBA);
// Next vertex height line
mp.game.graphics.drawLine(nextVertex.x, nextVertex.y, nextVertex.z, nextVertex.x, nextVertex.y, nextVertex.z + height, ...lineColorRGBA);
// Deepness higher line
mp.game.graphics.drawLine(vertex.x, vertex.y, vertex.z + height, nextVertex.x, nextVertex.y, nextVertex.z + height, ...lineColorRGBA);
});
});
});