Skip to content

Commit

Permalink
refactor(client): use vectors for distance checks
Browse files Browse the repository at this point in the history
  • Loading branch information
thelindat committed Mar 12, 2024
1 parent c2a5624 commit d655d2c
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 31 deletions.
45 changes: 21 additions & 24 deletions client/player/death.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { cache, requestAnimDict, sleep } from '@overextended/ox_lib/client';
import { Vector3, Vector4 } from '@nativewrappers/fivem';
import { OxPlayer } from 'player';
import { DEBUG } from '../../common/config';
import { DEBUG } from 'config';

const hospitals = [
[340.5, -1396.8, 32.5, 60.1],
[-449.3, -340.2, 34.5, 76.2],
[295.6, -583.9, 43.2, 79.5],
[1840.1, 3670.7, 33.9, 207.6],
[1153.2, -1526.4, 34.8, 352.4],
[-244.7, 6328.3, 32.4, 242.1],
new Vector4(340.5, -1396.8, 32.5, 60.1),
new Vector4(-449.3, -340.2, 34.5, 76.2),
new Vector4(295.6, -583.9, 43.2, 79.5),
new Vector4(1840.1, 3670.7, 33.9, 207.6),
new Vector4(1153.2, -1526.4, 34.8, 352.4),
new Vector4(-244.7, 6328.3, 32.4, 242.1),
];

const anims = [
Expand All @@ -33,29 +34,25 @@ async function ClearDeath(tickId: number, bleedOut: boolean) {
clearTick(tickId);

if (bleedOut) {
const coords = GetEntityCoords(cache.ped, true);
const [x, y, z, heading] = hospitals.reduce(
(closest: any, hospital: any) => { //todo
const distance = Math.sqrt(
Math.pow(coords[0] - hospital[0], 2) +
Math.pow(coords[1] - hospital[1], 2) +
Math.pow(coords[2] - hospital[2], 2)
);

if (distance < closest.distance) return { hospital, distance };
return closest;
},
{ hospital: null, distance: 1000 }
).hospital;
const coords = Vector3.fromArray(GetEntityCoords(cache.ped, true));
let distance = 1000;
const hospital = hospitals.reduce((closest, hospital) => {
const hospitalDistance = coords.distance(hospital);

if (hospitalDistance > distance) return closest;

distance = hospitalDistance;
return hospital;
});

DoScreenFadeOut(500);
RequestCollisionAtCoord(x, y, z);
RequestCollisionAtCoord(hospital.x, hospital.y, hospital.z);

while (!IsScreenFadedOut()) await sleep(0);

StopAnimTask(cache.ped, anim[0], anim[1], 8.0);
SetEntityCoordsNoOffset(cache.ped, x, y, z, false, false, false);
SetEntityHeading(cache.ped, heading);
SetEntityCoordsNoOffset(cache.ped, hospital.x, hospital.y, hospital.z, false, false, false);
SetEntityHeading(cache.ped, hospital.w);
SetGameplayCamRelativeHeading(0);

await sleep(500);
Expand Down
11 changes: 4 additions & 7 deletions client/vehicle/index.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
import { cache, onServerCallback, setVehicleProperties, waitFor } from '@overextended/ox_lib/client';
import { Vector3 } from '@nativewrappers/fivem';
import { DEBUG } from '../config';

if (DEBUG) import('./parser');

onServerCallback('ox:getNearbyVehicles', (radius: number) => {
const nearbyEntities: number[] = [];
const playerCoords = GetEntityCoords(cache.ped, true);
const playerCoords = Vector3.fromArray(GetEntityCoords(cache.ped, true));

(GetGamePool('CVehicle') as number[]).forEach((entityId) => {
const coords = GetEntityCoords(entityId, true);
const distance = Math.sqrt(
Math.pow(coords[0] - playerCoords[0], 2) +
Math.pow(coords[1] - playerCoords[1], 2) +
Math.pow(coords[2] - playerCoords[2], 2)
);
const coords = Vector3.fromArray(GetEntityCoords(entityId, true));
const distance = coords.distance(playerCoords);

if (distance <= (radius || 2) && NetworkGetEntityIsNetworked(entityId)) nearbyEntities.push(VehToNet(entityId));
});
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"typescript": "^5.3.3"
},
"dependencies": {
"@nativewrappers/fivem": "^0.0.5",
"@overextended/ox_lib": "^3.16.3",
"mariadb": "^3.2.3"
},
Expand Down
7 changes: 7 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d655d2c

Please sign in to comment.