Skip to content

Commit

Permalink
feat: player weapon functions
Browse files Browse the repository at this point in the history
  • Loading branch information
dockfries committed Sep 12, 2022
1 parent 9b61aa6 commit 28e59e5
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 55 deletions.
35 changes: 35 additions & 0 deletions src/controllers/player/basePlayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,13 @@ import {
GetPlayerCustomSkin,
GetPlayerTargetPlayer,
GetPlayerLastShotVectors,
GetPlayerWeapon,
GetPlayerWeaponData,
GetPlayerWeaponState,
GivePlayerWeapon,
SetPlayerAmmo,
GetPlayerAmmo,
SetPlayerArmedWeapon,
} from "@/wrapper/functions";
import { logger } from "@/logger";
import { BaseGameMode } from "../gamemode";
Expand All @@ -117,7 +124,9 @@ import {
PlayerStateEnum,
SpecialActionsEnum,
SpectateModesEnum,
WeaponEnum,
WeaponSkillsEnum,
WeaponStatesEnum,
} from "@/enums";
import { BaseVehicle } from "../vehicle";
import { basePos } from "@/types";
Expand Down Expand Up @@ -691,4 +700,30 @@ export abstract class BasePlayer {
GetPlayerLastShotVectors(this.id);
return { fOriginX, fOriginY, fOriginZ, fHitPosX, fHitPosY, fHitPosZ };
}
public getWeapon(): WeaponEnum | -1 {
return GetPlayerWeapon(this.id);
}
public getWeaponData(slot: number) {
if (slot < 0 || slot > 12) {
logger.error("[BasePlayer]: weapon slots range from 0 to 12");
return;
}
const [weapons, ammo] = GetPlayerWeaponData(this.id, slot);
return { weapons, ammo };
}
public getWeaponState(): WeaponStatesEnum {
return GetPlayerWeaponState(this.id);
}
public giveWeapon(weaponid: number, ammo: number): number {
return GivePlayerWeapon(this.id, weaponid, ammo);
}
public setAmmo(weaponid: number, ammo: number) {
return SetPlayerAmmo(this.id, weaponid, ammo);
}
public getAmmo(): number {
return GetPlayerAmmo(this.id);
}
public setArmedWeapon(weaponid: number): number {
return SetPlayerArmedWeapon(this.id, weaponid);
}
}
102 changes: 52 additions & 50 deletions src/controllers/player/playerEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import {
OnPlayerClickMap,
OnPlayerClickPlayer,
OnPlayerDeath,
// OnPlayerGiveDamage,
OnPlayerGiveDamage,
OnPlayerKeyStateChange,
OnPlayerRequestSpawn,
OnPlayerSpawn,
OnPlayerStateChange,
OnPlayerStreamIn,
OnPlayerStreamOut,
// OnPlayerTakeDamage,
OnPlayerTakeDamage,
OnPlayerUpdate,
OnEnterExitModShop,
OnPlayerInteriorChange,
Expand All @@ -25,10 +25,12 @@ import { BasePlayer } from "./basePlayer";
import { CmdBus } from "../command";
import { ICmdErr } from "@/interfaces";
import {
BodyPartsEnum,
// BodyPartsEnum,
InvalidEnum,
KeysEnum,
PlayerStateEnum,
WeaponEnum,
// WeaponEnum,
} from "@/enums";
import { throttle } from "lodash";
Expand Down Expand Up @@ -74,13 +76,13 @@ abstract class AbstractPlayerEvent<P extends BasePlayer> {
killer: P | InvalidEnum.PLAYER_ID,
reason: number
): void;
// protected abstract onGiveDamage(
// player: P,
// damage: P,
// amount: number,
// weaponid: WeaponEnum,
// bodypart: BodyPartsEnum
// ): void;
protected abstract onGiveDamage(
player: P,
damage: P,
amount: number,
weaponid: WeaponEnum,
bodypart: BodyPartsEnum
): void;
protected abstract onKeyStateChange(
player: P,
newkeys: KeysEnum,
Expand All @@ -95,13 +97,13 @@ abstract class AbstractPlayerEvent<P extends BasePlayer> {
): void;
protected abstract onStreamIn(player: P, forPlayer: P): void;
protected abstract onStreamOut(player: P, forPlayer: P): void;
// protected abstract onTakeDamage(
// player: P,
// damage: P | InvalidEnum.INVALID_PLAYER_ID,
// amount: number,
// weaponid: WeaponEnum,
// bodypart: BodyPartsEnum
// ): void;
protected abstract onTakeDamage(
player: P,
damage: P | InvalidEnum.PLAYER_ID,
amount: number,
weaponid: WeaponEnum,
bodypart: BodyPartsEnum
): void;
protected abstract onUpdate(player: P): void;
protected abstract onInteriorChange(
player: P,
Expand Down Expand Up @@ -215,21 +217,21 @@ export abstract class BasePlayerEvent<
}
);

// OnPlayerGiveDamage(
// (
// playerid: number,
// damageid: number,
// amount: number,
// weaponid: WeaponsEnum,
// bodypart: BodyPartsEnum
// ): void => {
// const p = this.findPlayerById(playerid);
// if (!p) return;
// const d = this.players.find((p) => p.id === damageid);
// if (!d) return;
// this.onGiveDamage(p, d, amount, weaponid, bodypart);
// }
// );
OnPlayerGiveDamage(
(
playerid: number,
damageid: number,
amount: number,
weaponid: WeaponEnum,
bodypart: BodyPartsEnum
): void => {
const p = this.findPlayerById(playerid);
if (!p) return;
const d = this.findPlayerById(damageid);
if (!d) return;
this.onGiveDamage(p, d, amount, weaponid, bodypart);
}
);

OnPlayerKeyStateChange(
(playerid: number, newkeys: number, oldkeys: number): void => {
Expand Down Expand Up @@ -275,25 +277,25 @@ export abstract class BasePlayerEvent<
this.onStreamOut(p, fp);
});

// OnPlayerTakeDamage(
// (
// playerid: number,
// issuerid: number,
// amount: number,
// weaponid: number,
// bodypart: number
// ): void => {
// const p = this.findPlayerById(playerid);
// if (!p) return;
// if (issuerid === InvalidEnum.INVALID_PLAYER_ID) {
// this.onTakeDamage(p, issuerid, amount, weaponid, bodypart);
// return;
// }
// const i = this.players.find((p) => p.id === issuerid);
// if (!i) return;
// this.onTakeDamage(p, i, amount, weaponid, bodypart);
// }
// );
OnPlayerTakeDamage(
(
playerid: number,
issuerid: number,
amount: number,
weaponid: WeaponEnum,
bodypart: BodyPartsEnum
): void => {
const p = this.findPlayerById(playerid);
if (!p) return;
if (issuerid === InvalidEnum.PLAYER_ID) {
this.onTakeDamage(p, issuerid, amount, weaponid, bodypart);
return;
}
const i = this.findPlayerById(issuerid);
if (!i) return;
this.onTakeDamage(p, i, amount, weaponid, bodypart);
}
);

/** 30 calls per second for a single player means a peak of 30,000 calls for 1000 players.
* If there are 10 player event classes, that means there are 30,0000 calls per second.
Expand Down
11 changes: 6 additions & 5 deletions src/wrapper/functions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
TextDrawAlignEnum,
VehicleModelInfoEnum,
WeaponSkillsEnum,
WeaponStatesEnum,
} from "@/enums";
import { BaseGameMode, I18n } from "@/controllers";

Expand Down Expand Up @@ -1107,7 +1108,7 @@ export const SetObjectRot = (
return samp.callNative("SetObjectRot", "ifff", objectid, RotX, RotY, RotZ);
};

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

Expand Down Expand Up @@ -1621,17 +1622,17 @@ export const GetPlayerArmour = (playerid: number): number => {

export const SetPlayerAmmo = (
playerid: number,
weaponslot: number,
weaponid: number,
ammo: number
): number => {
return samp.callNative("SetPlayerAmmo", "iii", playerid, weaponslot, ammo);
return samp.callNative("SetPlayerAmmo", "iii", playerid, weaponid, ammo);
};

export const GetPlayerAmmo = (playerid: number): number => {
return samp.callNative("GetPlayerAmmo", "i", playerid);
};

export const GetPlayerWeaponState = (playerid: number): number => {
export const GetPlayerWeaponState = (playerid: number): WeaponStatesEnum => {
return samp.callNative("GetPlayerWeaponState", "i", playerid);
};

Expand Down Expand Up @@ -1708,7 +1709,7 @@ export const SetPlayerArmedWeapon = (
export const GetPlayerWeaponData = (
playerid: number,
slot: number
): Array<any> => {
): Array<number> => {
return samp.callNative("GetPlayerWeaponData", "iiII", playerid, slot);
};

Expand Down

0 comments on commit 28e59e5

Please sign in to comment.