-
Notifications
You must be signed in to change notification settings - Fork 0
/
HUDManager.ts
75 lines (64 loc) · 1.97 KB
/
HUDManager.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/**
* Initializes the player local HUDs and passes information to each player about the state of the race
*/
import * as hz from "horizon/core";
import { Pool } from "GameUtils";
import { Events } from "Events";
export class HUDManager extends hz.Component {
static propsDefinition = {};
private HUDPool: Pool<hz.Entity> = new Pool<hz.Entity>();
private playerHUDCtrlMap: Map<number, hz.Entity> = new Map<number, hz.Entity>();
private static s_instance: HUDManager;
public static getInstance(): HUDManager {
return HUDManager.s_instance;
}
constructor() {
super();
if (HUDManager.s_instance === undefined) {
HUDManager.s_instance = this;
}
else {
console.error(`There are two ${this.constructor.name} in the world!`)
return;
}
}
preStart() {
this.connectLocalBroadcastEvent(
Events.onRegisterRaceHUD,
(data) => {
this.HUDPool.addToPool(data.caller);
});
this.connectCodeBlockEvent(
this.entity,
hz.CodeBlockEvents.OnPlayerEnterWorld,
(player: hz.Player) => {
this.handleOnPlayerEnterWorld(player);
}
);
this.connectCodeBlockEvent(
this.entity,
hz.CodeBlockEvents.OnPlayerExitWorld,
(player: hz.Player) => {
this.handleOnPlayerExitWorld(player);
}
);
}
start() { }
private handleOnPlayerExitWorld(player: hz.Player): void {
const playerHC = this.playerHUDCtrlMap.get(player.id);
if (playerHC) {
playerHC.owner.set(this.world.getServerPlayer());
this.HUDPool.addToPool(playerHC);
}
this.playerHUDCtrlMap.delete(player.id);
}
private handleOnPlayerEnterWorld(player: hz.Player): void {
const availableHC = this.HUDPool.getNextAvailable();
if (availableHC) {
console.log(`${this.constructor.name} Attached HUD Local to ${player.name.get()}`);
availableHC.owner.set(player);
this.playerHUDCtrlMap.set(player.id, availableHC);
}
}
}
hz.Component.register(HUDManager);