-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathAreaManager.ts
105 lines (93 loc) · 2.12 KB
/
AreaManager.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import { Area } from './Area';
import { BehaviorManager } from './BehaviorManager';
import { EntityReference } from './EntityReference';
import { IGameState } from './GameState';
import { Room } from './Room';
/**
* Stores references to, and handles distribution of, active areas
* @property {Map<string,Area>} areas
*/
export class AreaManager {
areas: Map<string, Area>;
scripts: BehaviorManager;
private placeholder: null | Area;
constructor() {
this.areas = new Map();
this.scripts = new BehaviorManager();
this.placeholder = null;
}
/**
* @param {string} name
* @return Area
*/
getArea(name: string) {
const area = this.areas.get(name);
if (!area) {
throw new Error(`AreaManager can't find the Area [${name}]`);
}
return area;
}
/**
* @param {string} entityRef
* @return Area
*/
getAreaByReference(entityRef: EntityReference) {
const [name] = entityRef.split(':');
const area = this.getArea(name);
if (!area) {
throw new Error(
`AreaManager did not find Area [${entityRef}] with name [${name}]`
);
}
return area;
}
/**
* @param {Area} area
*/
addArea(area: Area) {
this.areas.set(area.name, area);
}
/**
* @param {Area} area
*/
removeArea(area: Area) {
this.areas.delete(area.name);
}
/**
* Apply `updateTick` to all areas in the game
* @param {GameState} state
* @fires Area#updateTick
*/
tickAll(state: IGameState) {
for (const [name, area] of this.areas) {
/**
* @see Area#update
* @event Area#updateTick
*/
area.emit('updateTick', state);
}
}
/**
* Get the placeholder area used to house players who were loaded into
* an invalid room
*
* @return {Area}
*/
getPlaceholderArea() {
if (this.placeholder) {
return this.placeholder;
}
this.placeholder = new Area(null, 'placeholder', {
title: 'Placeholder',
});
const placeholderRoom = new Room(this.placeholder, {
id: 'placeholder',
title: 'Placeholder',
description:
'You are not in a valid room. Please contact an administrator.',
entityReference: 'placeholder',
});
this.placeholder.addRoom(placeholderRoom);
return this.placeholder;
}
}