diff --git a/ReallifeGamemode.Client/polygons/events.js b/ReallifeGamemode.Client/polygons/events.ts similarity index 61% rename from ReallifeGamemode.Client/polygons/events.js rename to ReallifeGamemode.Client/polygons/events.ts index 9f5f7584..5df30b37 100644 --- a/ReallifeGamemode.Client/polygons/events.js +++ b/ReallifeGamemode.Client/polygons/events.ts @@ -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); } diff --git a/ReallifeGamemode.Client/polygons/helpers.js b/ReallifeGamemode.Client/polygons/helpers.ts similarity index 76% rename from ReallifeGamemode.Client/polygons/helpers.js rename to ReallifeGamemode.Client/polygons/helpers.ts index 6de4afb8..283ddf4a 100644 --- a/ReallifeGamemode.Client/polygons/helpers.js +++ b/ReallifeGamemode.Client/polygons/helpers.ts @@ -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 timestamp = Date.now(); - return mp.polygons.pool.some(p => p.id === timestamp) ? generateUniquePolygonId() : timestamp; +const generateUniquePolygonId = (): number => { + const timestamp = Date.now(); + return polygons.pool.some(p => p.id === timestamp) ? generateUniquePolygonId() : timestamp; }; export { isPointInArea2D, generateUniquePolygonId, getAngleSumBetweenPositionAndVertices }; \ No newline at end of file diff --git a/ReallifeGamemode.Client/polygons/index.js b/ReallifeGamemode.Client/polygons/index.js deleted file mode 100644 index 5393a075..00000000 --- a/ReallifeGamemode.Client/polygons/index.js +++ /dev/null @@ -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); - } -} - diff --git a/ReallifeGamemode.Client/polygons/index.ts b/ReallifeGamemode.Client/polygons/index.ts new file mode 100644 index 00000000..5417de65 --- /dev/null +++ b/ReallifeGamemode.Client/polygons/index.ts @@ -0,0 +1,71 @@ +const { generateUniquePolygonId, getAngleSumBetweenPositionAndVertices, isPointInArea2D } = require('./helpers'); + +require('./render'); +require('./events'); + +class PolygonManager { + pool: Polygon[] = new Array(); + + 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 }; + + diff --git a/ReallifeGamemode.Client/polygons/render.js b/ReallifeGamemode.Client/polygons/render.js deleted file mode 100644 index 7e9288f1..00000000 --- a/ReallifeGamemode.Client/polygons/render.js +++ /dev/null @@ -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); - }); - - }); - -}); \ No newline at end of file diff --git a/ReallifeGamemode.Client/polygons/render.ts b/ReallifeGamemode.Client/polygons/render.ts new file mode 100644 index 00000000..26edc02f --- /dev/null +++ b/ReallifeGamemode.Client/polygons/render.ts @@ -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); + }); + + }); + +}); \ No newline at end of file