Skip to content

Commit

Permalink
feat: added some vehicle functions
Browse files Browse the repository at this point in the history
  • Loading branch information
dockfries committed Sep 9, 2022
1 parent f9d7c3b commit 9a90359
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 9 deletions.
4 changes: 4 additions & 0 deletions src/controllers/player/basePlayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ import {
CreateExplosionForPlayer,
GetMaxPlayers,
IsPlayerConnected,
DisableRemoteVehicleCollisions,
} from "@/wrapper/functions";
import logger from "@/logger";
import { BaseGameMode } from "../gamemode";
Expand Down Expand Up @@ -575,4 +576,7 @@ export abstract class BasePlayer {
public static isConnected<P extends BasePlayer>(player: P) {
return IsPlayerConnected(player.id);
}
public disableRemoteVehicleCollisions(disable: boolean) {
return DisableRemoteVehicleCollisions(this.id, disable);
}
}
119 changes: 115 additions & 4 deletions src/controllers/vehicle/baseVehicle.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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);
Expand All @@ -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<P extends BasePlayer>(player: P): boolean {
if (this.id === -1) return false;
return IsPlayerInVehicle(player.id, this.id);
}
public putPlayerIn<P extends BasePlayer>(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);
}
}
12 changes: 7 additions & 5 deletions src/wrapper/functions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2333,7 +2333,7 @@ export const SetPlayerSpecialAction = (

export const DisableRemoteVehicleCollisions = (
playerid: number,
disable: number
disable: boolean
): number => {
return samp.callNative(
"DisableRemoteVehicleCollisions",
Expand Down Expand Up @@ -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 => {
Expand Down Expand Up @@ -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");
}
Expand Down Expand Up @@ -3096,7 +3098,7 @@ export const RepairVehicle = (vehicleid: number): number => {
return samp.callNative("RepairVehicle", "i", vehicleid);
};

export const GetVehicleVelocity = (vehicleid: number): Array<any> => {
export const GetVehicleVelocity = (vehicleid: number): Array<number> => {
return samp.callNative("GetVehicleVelocity", "iFFF", vehicleid);
};

Expand Down

0 comments on commit 9a90359

Please sign in to comment.