Organize a lot of utils

This commit is contained in:
Vortrex
2022-01-09 17:19:53 -06:00
parent 2684ffae7d
commit 5dfe47a3e8
16 changed files with 905 additions and 1069 deletions

View File

@@ -67,4 +67,76 @@ function sendNetworkEventToServer(eventName, ...args) {
triggerNetworkEvent.apply(null, eventName, args);
}
// ===========================================================================
function getElementId(element) {
return element.id;
}
// ===========================================================================
function getClientFromIndex(index) {
let clients = getClients();
for(let i in clients) {
if(clients[i].index == index) {
return clients[i];
}
}
}
// ===========================================================================
function getVehiclesInRange(position, distance) {
return getElementsByType(ELEMENT_VEHICLE).filter(x => x.player && x.position.distance(position) <= distance);
}
// ===========================================================================
function getClientsInRange(position, distance) {
return getPlayersInRange(position, distance);
}
// ===========================================================================
function getCiviliansInRange(position, distance) {
return getElementsByType(ELEMENT_PED).filter(x => !x.isType(ELEMENT_PLAYER) && getElementPosition(x).position.distance(position) <= distance);
}
// ===========================================================================
function getPlayersInRange(position, distance) {
return getClients().filter(x => getPlayerPosition(x).distance(position) <= distance);
}
// ===========================================================================
function getElementsByTypeInRange(elementType, position, distance) {
return getElementsByType(elementType).filter(x => getElementPosition(x).position.distance(position) <= distance);
}
// ===========================================================================
function getClosestCivilian(position) {
return getElementsByType(ELEMENT_PED).reduce((i, j) => ((i.position.distance(position) <= j.position.distance(position)) ? i : j));
}
// ===========================================================================
function is2dPositionOnScreen(pos2d) {
return pos2d.x >= 0 && pos2d.y >= 0 && pos2d.x <= game.width && pos2d.y <= game.height;
}
// ===========================================================================
function getVehiclesInRange(position, range) {
let vehicles = getElementsByType(ELEMENT_VEHICLE);
let inRangeVehicles = [];
for(let i in vehicles) {
if(getDistance(position, vehicles[i].position) <= range) {
inRangeVehicles.push(vehicles[i]);
}
}
return inRangeVehicles;
}
// ===========================================================================