Skip to content

Commit

Permalink
feat: added streamer checkpoint and callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
dockfries committed Sep 16, 2022
1 parent 98829ef commit 5603ebb
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 0 deletions.
129 changes: 129 additions & 0 deletions src/controllers/streamer/checkpoint/baseCheckpoint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import { BasePlayer } from "@/controllers/player";
import { IDynamicCheckPoint } from "@/interfaces";
import { logger } from "@/logger";
import {
CreateDynamicCP,
CreateDynamicCPEx,
DestroyDynamicCP,
GetPlayerVisibleDynamicCP,
IsPlayerInDynamicCP,
IsValidDynamicCP,
StreamerDistances,
TogglePlayerAllDynamicCPs,
TogglePlayerDynamicCP,
} from "omp-wrapper-streamer";
import { checkPointBus, checkPointHooks } from "./checkPointBus";

export class DynamicCheckpoint {
private sourceInfo: IDynamicCheckPoint;
private _id = -1;
public get id(): number {
return this._id;
}
constructor(checkPoint: IDynamicCheckPoint) {
this.sourceInfo = checkPoint;
}
public create(): void | this {
if (this.id !== -1)
return logger.warn(
"[StreamerCheckpoint]: Unable to create checkpoint again"
);
let { streamdistance, worldid, interiorid, playerid, areaid, priority } =
this.sourceInfo;
const { size, x, y, z, extended } = this.sourceInfo;

if (size < 0)
return logger.error("[StreamerCheckpoint]: Invalid checkpoint size");

streamdistance ??= StreamerDistances.CP_SD;
priority ??= 0;

if (extended) {
if (typeof worldid === "number") worldid = [-1];
else worldid ??= [-1];
if (typeof interiorid === "number") interiorid = [-1];
else interiorid ??= [-1];
if (typeof playerid === "number") playerid = [-1];
else playerid ??= [-1];
if (typeof areaid === "number") areaid = [-1];
else areaid ??= [-1];

this._id = CreateDynamicCP(
x,
y,
z,
size,
worldid,
interiorid,
playerid,
streamdistance,
areaid,
priority
);
} else {
if (Array.isArray(worldid)) worldid = -1;
else worldid ??= -1;
if (Array.isArray(interiorid)) interiorid = -1;
else interiorid ??= -1;
if (Array.isArray(playerid)) playerid = -1;
else playerid ??= -1;
if (Array.isArray(areaid)) areaid = -1;
else areaid ??= -1;

this._id = CreateDynamicCPEx(
x,
y,
z,
size,
streamdistance,
worldid,
interiorid,
playerid,
areaid,
priority
);
}

checkPointBus.emit(checkPointHooks.created, this);
return this;
}
public destroy(): void | this {
if (this.id === -1)
return logger.warn(
"[StreamerCheckpoint]: Unable to destroy the checkpoint before create"
);
DestroyDynamicCP(this.id);
checkPointBus.emit(checkPointHooks.destroyed, this);
return this;
}
public static isValid(checkpoint: DynamicCheckpoint): boolean {
return IsValidDynamicCP(checkpoint.id);
}
public togglePlayer<P extends BasePlayer>(
player: P,
toggle: boolean
): void | this {
if (this.id === -1)
return logger.warn(
"[StreamerCheckpoint]: Unable to toggle the player before create"
);
TogglePlayerDynamicCP(player.id, this.id, toggle);
return this;
}
public static togglePlayerAll<P extends BasePlayer>(
player: P,
toggle: boolean
): number {
return TogglePlayerAllDynamicCPs(player.id, toggle);
}
public isPlayerIn<P extends BasePlayer>(player: P): boolean {
if (this.id === -1) return false;
return IsPlayerInDynamicCP(player.id, this.id);
}
public static getPlayerVisible<
P extends BasePlayer,
C extends DynamicCheckpoint
>(player: P, checkpoints: Map<number, C>): C | undefined {
return checkpoints.get(GetPlayerVisibleDynamicCP(player.id));
}
}
8 changes: 8 additions & 0 deletions src/controllers/streamer/checkpoint/checkPointBus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { EventBus } from "@/utils/eventBus";

export enum checkPointHooks {
created = "OnCheckPointCreate",
destroyed = "OnCheckPointDestroy",
}

export const checkPointBus = new EventBus();
46 changes: 46 additions & 0 deletions src/controllers/streamer/checkpoint/checkPointEvent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { BasePlayer } from "@/controllers/player";
import {
OnPlayerEnterDynamicCP,
OnPlayerLeaveDynamicCP,
} from "omp-wrapper-streamer";
import { DynamicCheckpoint } from "./baseCheckpoint";
import { checkPointBus, checkPointHooks } from "./checkPointBus";

export abstract class DynamicCheckPointEvent<
P extends BasePlayer,
C extends DynamicCheckpoint
> {
public readonly checkpoints = new Map<number, C>();
private readonly players;

constructor(playersMap: Map<number, P>) {
this.players = playersMap;
checkPointBus.on(checkPointHooks.created, (checkpoint: C) => {
this.checkpoints.set(checkpoint.id, checkpoint);
});
checkPointBus.on(checkPointHooks.destroyed, (checkpoint: C) => {
this.checkpoints.delete(checkpoint.id);
});
OnPlayerEnterDynamicCP((playerid: number, checkpointid: number): number => {
const cp = this.checkpoints.get(checkpointid);
if (!cp) return 0;
const p = this.players.get(playerid);
if (!p) return 0;
return this.onPlayerEnter(p, cp);
});
OnPlayerLeaveDynamicCP((playerid: number, checkpointid: number): number => {
const cp = this.checkpoints.get(checkpointid);
if (!cp) return 0;
const p = this.players.get(playerid);
if (!p) return 0;
return this.onPlayerLeave(p, cp);
});
}

protected abstract onPlayerEnter(player: P, checkpoint: C): number;
protected abstract onPlayerLeave(player: P, checkpoint: C): number;

public getCheckPointsArr(): Array<C> {
return [...this.checkpoints.values()];
}
}
2 changes: 2 additions & 0 deletions src/controllers/streamer/checkpoint/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./baseCheckpoint";
export * from "./checkPointEvent";
1 change: 1 addition & 0 deletions src/controllers/streamer/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./mapIcon";
export * from "./pickup";
export * from "./checkpoint";
4 changes: 4 additions & 0 deletions src/interfaces/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,7 @@ export interface IDynamicPickup extends IDynamicCommon {
type: number;
modelid: number;
}

export interface IDynamicCheckPoint extends IDynamicCommon {
size: number;
}

0 comments on commit 5603ebb

Please sign in to comment.