From 9a9035956c242d59e1ba6e8a7b96b35d551e13e6 Mon Sep 17 00:00:00 2001 From: yucarl77 Date: Fri, 9 Sep 2022 16:39:40 +0800 Subject: [PATCH] feat: added some vehicle functions --- src/controllers/player/basePlayer.ts | 4 + src/controllers/vehicle/baseVehicle.ts | 119 ++++++++++++++++++++++++- src/wrapper/functions/index.ts | 12 +-- 3 files changed, 126 insertions(+), 9 deletions(-) diff --git a/src/controllers/player/basePlayer.ts b/src/controllers/player/basePlayer.ts index 33e1c6e..eb34c13 100644 --- a/src/controllers/player/basePlayer.ts +++ b/src/controllers/player/basePlayer.ts @@ -90,6 +90,7 @@ import { CreateExplosionForPlayer, GetMaxPlayers, IsPlayerConnected, + DisableRemoteVehicleCollisions, } from "@/wrapper/functions"; import logger from "@/logger"; import { BaseGameMode } from "../gamemode"; @@ -575,4 +576,7 @@ export abstract class BasePlayer { public static isConnected

(player: P) { return IsPlayerConnected(player.id); } + public disableRemoteVehicleCollisions(disable: boolean) { + return DisableRemoteVehicleCollisions(this.id, disable); + } } diff --git a/src/controllers/vehicle/baseVehicle.ts b/src/controllers/vehicle/baseVehicle.ts index f614594..277fa96 100644 --- a/src/controllers/vehicle/baseVehicle.ts +++ b/src/controllers/vehicle/baseVehicle.ts @@ -1,14 +1,34 @@ -import { LimitsEnum } from "@/enums"; +import { CarModTypeEnum, LimitsEnum } from "@/enums"; import logger from "@/logger"; +import { basePos } from "@/types"; import { IsValidVehComponent } from "@/utils/vehicleUtils"; import { AddVehicleComponent, + ChangeVehicleColor, CreateVehicle, DestroyVehicle, + GetVehicleComponentInSlot, + GetVehicleComponentType, + GetVehicleHealth, + GetVehiclePoolSize, + GetVehiclePos, + GetVehicleVelocity, GetVehicleVirtualWorld, + GetVehicleZAngle, + IsPlayerInVehicle, LinkVehicleToInterior, + PutPlayerInVehicle, + RemoveVehicleComponent, + RepairVehicle, + SetVehicleAngularVelocity, + SetVehicleHealth, + SetVehicleNumberPlate, + SetVehiclePos, + SetVehicleVelocity, SetVehicleVirtualWorld, + SetVehicleZAngle, } from "@/wrapper/functions"; +import { BasePlayer } from "../player"; import { vehicleBus, vehicleHooks } from "./vehicleBus"; export interface IVehicle { @@ -75,16 +95,33 @@ export abstract class BaseVehicle { vehicleBus.emit(vehicleHooks.created, this); this._id = -1; } - public addComponent(componentid: number): number | undefined { - if (this.id === -1) return; + public addComponent(componentid: number): number { + if (this.id === -1) return 0; if (!IsValidVehComponent(this.info.vehicletype, componentid)) { logger.warn( `[BaseVehicle]: Invalid component id ${componentid} attempted to attach to the vehicle ${this}` ); - return; + return 0; } return AddVehicleComponent(this.id, componentid); } + public removeComponent(componentid: number): number { + if ( + this.getComponentInSlot(BaseVehicle.getComponentType(componentid)) === 0 + ) { + logger.warn( + `[BaseVehicle]: component id ${componentid} does not exist on this vehicle` + ); + return 0; + } + return RemoveVehicleComponent(this.id, componentid); + } + public getComponentInSlot(slot: CarModTypeEnum) { + return GetVehicleComponentInSlot(this.id, slot); + } + public static getComponentType(component: number) { + return GetVehicleComponentType(component); + } public linkToInterior(interiorId: number): number { if (this.id === -1) return 0; return LinkVehicleToInterior(this.id, interiorId); @@ -97,4 +134,78 @@ export abstract class BaseVehicle { if (this.id === -1) return 0; return GetVehicleVirtualWorld(this.id); } + public repair(): number { + if (this.id === -1) return 0; + return RepairVehicle(this.id); + } + public setPos(x: number, y: number, z: number): number { + if (this.id === -1) return 0; + return SetVehiclePos(this.id, x, y, z); + } + public getPos(): void | basePos { + if (this.id === -1) return; + return GetVehiclePos(this.id); + } + public getHealth(): number { + if (this.id === -1) return 0; + return GetVehicleHealth(this.id); + } + public setHealth(health: number): number { + if (this.id === -1) return 0; + return SetVehicleHealth(this.id, health); + } + public isPlayerIn

(player: P): boolean { + if (this.id === -1) return false; + return IsPlayerInVehicle(player.id, this.id); + } + public putPlayerIn

(player: P, seatid: number): number { + if (this.id === -1) return 0; + if (seatid < 0) return 0; + if (seatid > 4) { + logger.warn( + "[BaseVehicle]: If the seat is invalid or is taken, will cause a crash when they EXIT the vehicle." + ); + } + return PutPlayerInVehicle(player.id, this.id, seatid); + } + public getZAngle(): number { + if (this.id === -1) return 0; + return GetVehicleZAngle(this.id); + } + public setZAngle(z_angle: number): number { + if (this.id === -1) return 0; + return SetVehicleZAngle(this.id, z_angle); + } + public setNumberPlate(numberplate: string): number { + if (this.id === -1) return 0; + if (numberplate.length < 1 || numberplate.length > 32) { + logger.error( + "[BaseVehicle]: The length of the number plate ranges from 32 characters" + ); + return 0; + } + return SetVehicleNumberPlate(this.id, numberplate); + } + public static getPoolSize(): number { + return GetVehiclePoolSize(); + } + public changeColor(color1: number, color2: number): number { + if (this.id === -1) return 0; + this.info.color1 = color1; + this.info.color2 = color2; + return ChangeVehicleColor(this.id, color1, color2); + } + public setVelocity(X: number, Y: number, Z: number): number { + if (this.id === -1) return 0; + return SetVehicleVelocity(this.id, X, Y, Z); + } + public getVelocity(): void | basePos { + if (this.id === -1) return; + const [x, y, z] = GetVehicleVelocity(this.id); + return { x, y, z }; + } + public setAngularVelocity(X: number, Y: number, Z: number): number { + if (this.id === -1) return 0; + return SetVehicleAngularVelocity(this.id, X, Y, Z); + } } diff --git a/src/wrapper/functions/index.ts b/src/wrapper/functions/index.ts index ca21876..16e832d 100644 --- a/src/wrapper/functions/index.ts +++ b/src/wrapper/functions/index.ts @@ -2333,7 +2333,7 @@ export const SetPlayerSpecialAction = ( export const DisableRemoteVehicleCollisions = ( playerid: number, - disable: number + disable: boolean ): number => { return samp.callNative( "DisableRemoteVehicleCollisions", @@ -2631,8 +2631,10 @@ export const IsPlayerConnected = (playerid: number): boolean => { export const IsPlayerInVehicle = ( playerid: number, vehicleid: number -): number => { - return samp.callNative("IsPlayerInVehicle", "ii", playerid, vehicleid); +): boolean => { + return Boolean( + samp.callNative("IsPlayerInVehicle", "ii", playerid, vehicleid) + ); }; export const IsPlayerInAnyVehicle = (playerid: number): boolean => { @@ -2809,7 +2811,7 @@ export const IsVehicleStreamedIn = ( }; export const GetVehiclePos = (vehicleid: number) => { - const values = samp.callNative("GetVehiclePos", "iFFF", vehicleid); + const values: number[] = samp.callNative("GetVehiclePos", "iFFF", vehicleid); if (values.length < 3) { throw new Error("VehicleID " + vehicleid + " not found"); } @@ -3096,7 +3098,7 @@ export const RepairVehicle = (vehicleid: number): number => { return samp.callNative("RepairVehicle", "i", vehicleid); }; -export const GetVehicleVelocity = (vehicleid: number): Array => { +export const GetVehicleVelocity = (vehicleid: number): Array => { return samp.callNative("GetVehicleVelocity", "iFFF", vehicleid); };