mirror of
https://github.com/CommunityOx/ox_lib.git
synced 2026-08-17 06:56:03 +01:00
feat(package/getNearbyVehicles): Implement getNearbyVehicles (#8)
This commit is contained in:
@@ -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>
|
||||||
|
|||||||
29
package/shared/resource/getNearbyVehicles/index.ts
Normal file
29
package/shared/resource/getNearbyVehicles/index.ts
Normal 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;
|
||||||
|
}
|
||||||
@@ -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';
|
||||||
|
|||||||
Reference in New Issue
Block a user