feat(package/getNearbyVehicles): Implement getNearbyVehicles (#8)

This commit is contained in:
uShifty
2025-05-04 10:00:08 -06:00
committed by GitHub
parent f59beb2af6
commit 8039539158
3 changed files with 33 additions and 1 deletions

View File

@@ -1,5 +1,7 @@
export * from './resource'; export * from './resource';
export const context: 'server' | 'client' = IsDuplicityVersion() ? 'server' : 'client';
// https://www.raygesualdo.com/posts/flattening-object-keys-with-typescript-types/ // https://www.raygesualdo.com/posts/flattening-object-keys-with-typescript-types/
export type FlattenObjectKeys<T extends Record<string, any>, Key = keyof T> = Key extends string export type FlattenObjectKeys<T extends Record<string, any>, Key = keyof T> = Key extends string
? T[Key] extends Record<string, unknown> ? T[Key] extends Record<string, unknown>

View File

@@ -0,0 +1,29 @@
import { cache } from '../cache/index';
import { context } from 'shared';
import { Vector3 } from "@nativewrappers/client";
interface NearbyVehicle {
vehicle: number;
coords: Vector3;
}
export function getNearbyVehicles(coords: Vector3, maxDistance: number = 2.0, includePlayerVehicle: boolean = false): NearbyVehicle[] {
const vehicles = GetGamePool('CVehicle');
const nearbyVehicles: NearbyVehicle[] = [];
for (const vehicle of vehicles) {
if (context === 'server' || !cache.vehicle || vehicle !== cache.vehicle || includePlayerVehicle) {
const vehicleCoords = Vector3.fromArray(GetEntityCoords(vehicle, true));
const distance = vehicleCoords.distance(coords);
if (distance < maxDistance && NetworkGetEntityIsNetworked(vehicle) ) {
nearbyVehicles.push({
vehicle,
coords: vehicleCoords
});
}
}
}
return nearbyVehicles;
}

View File

@@ -1,3 +1,4 @@
export * from './version'; export * from './version';
export * from './cache'; export * from './cache';
export * from './locale' export * from './locale';
export * from './getNearbyVehicles';