forked from Bitcoindefi/OpenAO
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrowLayerContainers.ts
More file actions
139 lines (114 loc) · 3.83 KB
/
Copy pathrowLayerContainers.ts
File metadata and controls
139 lines (114 loc) · 3.83 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import { Container } from "pixi.js";
import { TILE_SIZE } from "../../../lib/viewport";
import type { Character, Engine } from "../engine/Engine";
export type MapRowLayerKind = "object" | "character" | "above";
const MAP_ROW_LAYER_ORDER: MapRowLayerKind[] = ["object", "character", "above"];
function getMapRowLayerKey(row: number, kind: MapRowLayerKind): string {
return `${row}:${kind}`;
}
function clampRow(engine: Engine, row: number): number {
return Math.max(1, Math.min(engine.mapDimensions.height, Math.round(row)));
}
export function createMapRowLayerContainers(engine: Engine): void {
if (!engine.mapContainer) {
return;
}
engine.mapRowLayerContainers.clear();
engine.groundLayerContainer = new Container();
engine.belowLayerContainer = new Container();
engine.mapContainer.addChild(engine.groundLayerContainer);
engine.mapContainer.addChild(engine.belowLayerContainer);
for (let row = 1; row <= engine.mapDimensions.height; row++) {
for (const kind of MAP_ROW_LAYER_ORDER) {
const container = new Container();
engine.mapContainer.addChild(container);
engine.mapRowLayerContainers.set(
getMapRowLayerKey(row, kind),
container,
);
}
}
}
export function createRoofRowContainers(engine: Engine): void {
if (!engine.roofContainer) {
return;
}
engine.roofRowContainers.clear();
for (let row = 1; row <= engine.mapDimensions.height; row++) {
const container = new Container();
engine.roofContainer.addChild(container);
engine.roofRowContainers.set(row, container);
}
}
export function createEntityFXRowContainers(engine: Engine): void {
if (!engine.entityFXOverlayContainer) {
return;
}
engine.entityFXRowContainers.clear();
for (let row = 1; row <= engine.mapDimensions.height; row++) {
const container = new Container();
engine.entityFXOverlayContainer.addChild(container);
engine.entityFXRowContainers.set(row, container);
}
}
export function getMapRowLayerContainer(
engine: Engine,
row: number,
kind: MapRowLayerKind,
): Container | null {
return (
engine.mapRowLayerContainers.get(
getMapRowLayerKey(clampRow(engine, row), kind),
) ?? null
);
}
export function getRoofRowContainer(
engine: Engine,
row: number,
): Container | null {
return engine.roofRowContainers.get(clampRow(engine, row)) ?? null;
}
export function getEntityFXRowContainer(
engine: Engine,
row: number,
): Container | null {
return engine.entityFXRowContainers.get(clampRow(engine, row)) ?? null;
}
export function getWorldRowFromWorldY(engine: Engine, worldY: number): number {
return clampRow(engine, Math.floor(worldY / TILE_SIZE) + 1);
}
export function getCharacterBaseRenderRow(character: Character): number {
return Math.max(1, character.pos.y - character.addtoUserPos.y);
}
export function syncCharacterContainerToRenderRow(
engine: Engine,
character: Character | null | undefined,
container: Container | null | undefined,
): void {
if (!character || !container) {
return;
}
const targetContainer = getMapRowLayerContainer(
engine,
getCharacterBaseRenderRow(character),
"character",
);
if (!targetContainer || container.parent === targetContainer) {
return;
}
targetContainer.addChild(container);
}
export function syncDisplayObjectToEntityFXRow(
engine: Engine,
worldY: number,
displayObject: any,
): void {
const targetContainer = getEntityFXRowContainer(
engine,
getWorldRowFromWorldY(engine, worldY),
);
if (!targetContainer || displayObject.parent === targetContainer) {
return;
}
targetContainer.addChild(displayObject as Container);
}