-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added streamer checkpoint and callbacks
- Loading branch information
Showing
6 changed files
with
190 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./baseCheckpoint"; | ||
export * from "./checkPointEvent"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from "./mapIcon"; | ||
export * from "./pickup"; | ||
export * from "./checkpoint"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters