added library for polygons
This commit is contained in:
20
ReallifeGamemode.Client/polygons/events.js
Normal file
20
ReallifeGamemode.Client/polygons/events.js
Normal file
@@ -0,0 +1,20 @@
|
||||
setInterval(() => {
|
||||
const { position, dimension } = mp.players.local;
|
||||
|
||||
mp.polygons.pool.map((polygon) => {
|
||||
|
||||
if (polygon.colliding) {
|
||||
if (!mp.polygons.isPositionWithinPolygon(position, polygon, dimension)) {
|
||||
polygon.colliding = false;
|
||||
mp.events.call('playerLeavePolygon', polygon);
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (mp.polygons.isPositionWithinPolygon(position, polygon, dimension)) {
|
||||
polygon.colliding = true;
|
||||
mp.events.call('playerEnterPolygon', polygon);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
}, 100);
|
||||
53
ReallifeGamemode.Client/polygons/helpers.js
Normal file
53
ReallifeGamemode.Client/polygons/helpers.js
Normal file
@@ -0,0 +1,53 @@
|
||||
const isPointInArea2D = (point, area) => {
|
||||
let x = point[0], y = point[1];
|
||||
|
||||
let inside = false;
|
||||
|
||||
for (let i = 0, j = area.length - 1; i < area.length; j = i++) {
|
||||
let xi = area[i][0], yi = area[i][1];
|
||||
let xj = area[j][0], yj = area[j][1];
|
||||
|
||||
let intersect = ((yi > y) != (yj > y))
|
||||
&& (x < (xj - xi) * (y - yi) / (yj - yi) + xi);
|
||||
|
||||
if (intersect)
|
||||
inside = !inside;
|
||||
}
|
||||
|
||||
return inside;
|
||||
};
|
||||
|
||||
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 getAngleSumBetweenPositionAndVertices = (position, vertices) => {
|
||||
let i;
|
||||
let m1, m2;
|
||||
let anglesum=0, costheta;
|
||||
|
||||
for (i = 0; i < vertices.length; i++) {
|
||||
|
||||
const p1 = new mp.Vector3(vertices[i].x - position.x, vertices[i].y - position.y, vertices[i].z - position.z);
|
||||
const p2 = new mp.Vector3(vertices[(i+1)%vertices.length].x - position.x, vertices[(i+1)%vertices.length].y - position.y, vertices[(i+1)%vertices.length].z - position.z);
|
||||
|
||||
m1 = modulus(p1);
|
||||
m2 = modulus(p2);
|
||||
|
||||
if (m1*m2 <= EPSILON)
|
||||
return(TWOPI);
|
||||
else
|
||||
costheta = (p1.x*p2.x + p1.y*p2.y + p1.z*p2.z) / (m1*m2);
|
||||
|
||||
anglesum += Math.acos(costheta);
|
||||
}
|
||||
return(anglesum);
|
||||
}
|
||||
|
||||
const generateUniquePolygonId = () => {
|
||||
const timestamp = Date.now();
|
||||
return mp.polygons.pool.some(p => p.id === timestamp) ? generateUniquePolygonId() : timestamp;
|
||||
};
|
||||
|
||||
export { isPointInArea2D, generateUniquePolygonId, getAngleSumBetweenPositionAndVertices };
|
||||
27
ReallifeGamemode.Client/polygons/render.js
Normal file
27
ReallifeGamemode.Client/polygons/render.js
Normal file
@@ -0,0 +1,27 @@
|
||||
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);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
Reference in New Issue
Block a user