From 45f155f62b76facf3db25eee907ed5eee54ab7e6 Mon Sep 17 00:00:00 2001 From: Luke <39926192+LukeWasTakenn@users.noreply.github.com> Date: Fri, 14 Jul 2023 00:57:01 +0200 Subject: [PATCH] perf(package): only run points loop when there's at least one point --- package/client/resource/points/index.ts | 115 +++++++++++++----------- 1 file changed, 62 insertions(+), 53 deletions(-) diff --git a/package/client/resource/points/index.ts b/package/client/resource/points/index.ts index 8c4d675..c418817 100644 --- a/package/client/resource/points/index.ts +++ b/package/client/resource/points/index.ts @@ -6,6 +6,7 @@ let nearbyPoints: Point[] = []; let nearbyCount: number = 0; let closestPoint: Point | undefined; let tick: number | undefined; +let pointsInterval: CitizenTimer; interface LibPoint { coords: number[]; @@ -37,6 +38,9 @@ export class Point { this.nearby = point.nearby; this.args = point.args; points.push(this); + if (points.length === 1) { + startPointsInterval(); + } } remove = () => { @@ -51,71 +55,76 @@ export class Point { } } points = points.filter((point) => point.id !== this.id); + if (points.length === 0) { + clearInterval(pointsInterval); + } }; } -setInterval(() => { - if (points.length < 1) return; +const startPointsInterval = () => { + pointsInterval = setInterval(() => { + if (points.length < 1) return; - if (nearbyCount !== 0) { - nearbyPoints = []; - nearbyCount = 0; - } + if (nearbyCount !== 0) { + nearbyPoints = []; + nearbyCount = 0; + } - const coords = Vector3.fromArray(GetEntityCoords(cache.ped, false)); + const coords = Vector3.fromArray(GetEntityCoords(cache.ped, false)); - if (closestPoint && coords.distance(closestPoint.coords) > closestPoint.distance) { - closestPoint = undefined; - } + if (closestPoint && coords.distance(closestPoint.coords) > closestPoint.distance) { + closestPoint = undefined; + } - for (let i = 0; i < points.length; i++) { - const point = points[i]; - const distance = coords.distance(point.coords); + for (let i = 0; i < points.length; i++) { + const point = points[i]; + const distance = coords.distance(point.coords); - if (distance <= point.distance) { - point.currentDistance = distance; + if (distance <= point.distance) { + point.currentDistance = distance; - if (closestPoint && closestPoint.currentDistance) { - if (distance < closestPoint.currentDistance) { - closestPoint.isClosest = false; + if (closestPoint && closestPoint.currentDistance) { + if (distance < closestPoint.currentDistance) { + closestPoint.isClosest = false; + point.isClosest = true; + closestPoint = point; + } + } else if (distance < point.distance) { point.isClosest = true; closestPoint = point; } - } else if (distance < point.distance) { - point.isClosest = true; - closestPoint = point; - } - if (point.nearby) { - nearbyCount++; - nearbyPoints[nearbyCount - 1] = point; - } - - if (point.onEnter && !point.inside) { - point.inside = true; - point.onEnter(); - } - } else if (point.currentDistance) { - if (point.onExit) point.onExit(); - point.inside = false; - point.currentDistance = undefined; - } - } - - if (!tick) { - if (nearbyCount !== 0) { - tick = setTick(() => { - for (let i = 0; i < nearbyCount; i++) { - const point = nearbyPoints[i]; - - if (point && point.nearby) { - point.nearby(); - } + if (point.nearby) { + nearbyCount++; + nearbyPoints[nearbyCount - 1] = point; } - }); + + if (point.onEnter && !point.inside) { + point.inside = true; + point.onEnter(); + } + } else if (point.currentDistance) { + if (point.onExit) point.onExit(); + point.inside = false; + point.currentDistance = undefined; + } } - } else if (nearbyCount === 0) { - clearTick(tick); - tick = undefined; - } -}, 300); + + if (!tick) { + if (nearbyCount !== 0) { + tick = setTick(() => { + for (let i = 0; i < nearbyCount; i++) { + const point = nearbyPoints[i]; + + if (point && point.nearby) { + point.nearby(); + } + } + }); + } + } else if (nearbyCount === 0) { + clearTick(tick); + tick = undefined; + } + }, 300); +};