-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathGenericLayout.tsx
151 lines (134 loc) · 4.06 KB
/
GenericLayout.tsx
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
140
141
142
143
144
145
146
147
148
149
150
151
import type { Dimension } from "@/features/render/lib/schema";
import { PixiComponent, applyDefaultProps } from "@pixi/react";
import { CompositeTilemap } from "@pixi/tilemap";
export const tileSizeBase: Dimension = {
w: 32,
h: 32,
};
export const tileScale = 2;
export const tileSize: Dimension = {
w: tileSizeBase.w * tileScale,
h: tileSizeBase.h * tileScale,
};
// TODO: Add more tileset variations!
export const GenericTileSets = ["clubhouse1", "beach1"] as const;
export type GenericTileSet = (typeof GenericTileSets)[number];
interface Props {
tileSet: GenericTileSet;
roomSizeTiles: Dimension;
}
const setupInstance = (instance: CompositeTilemap, props: Props) => {
instance.clear();
instance.scale.set(tileScale);
const { tileSet, roomSizeTiles } = props;
// Iterate over main + surrounding area
for (let x = -20; x < roomSizeTiles.w + 20; x++) {
for (let y = -20; y < roomSizeTiles.h + 20; y++) {
// Draw background tiles
if (tileSet.startsWith("beach")) {
instance.tile(`${tileSet}_floor_walkable_1_1`, x * tileSizeBase.w, y * tileSizeBase.h);
} else {
instance.tile(`${tileSet}_floor_walkable_1_1`, x * tileSizeBase.w, y * tileSizeBase.h);
}
}
}
const floorOffset = (x: number, y: number) => {
if (x === 0) {
if (y === 0) {
return { x: 4, y: 1 };
}
if (y === roomSizeTiles.h - 1) {
return { x: 4, y: 0 };
}
return { x: 2, y: 1 };
}
if (x === roomSizeTiles.w - 1) {
if (y === 0) {
return { x: 3, y: 1 };
}
if (y === roomSizeTiles.h - 1) {
return { x: 3, y: 0 };
}
return { x: 0, y: 1 };
}
if (y === 0) {
return { x: 1, y: 2 };
}
if (y === roomSizeTiles.h - 1) {
return { x: 1, y: 0 };
}
}
// Iterate over main area
for (let x = 0; x < roomSizeTiles.w; x++) {
for (let y = 0; y < roomSizeTiles.h; y++) {
// Draw regular tile
const offset = floorOffset(x, y);
if (offset === undefined) {
if (tileSet.startsWith("beach")) {
instance.tile(
`${tileSet}_floor_default_${(x + 2) % 3}_${(y + 2) % 3}`,
x * tileSizeBase.w,
y * tileSizeBase.h,
);
} else {
instance.tile(
`${tileSet}_floor_walkable_4_3`,
x * tileSizeBase.w,
y * tileSizeBase.h,
);
}
} else {
instance.tile(
`${tileSet}_floor_walkable_${offset.x}_${offset.y}`,
x * tileSizeBase.w,
y * tileSizeBase.h,
);
}
}
}
// Draw couches
const couchAbsoluteOffset = { x: 2, y: 0 };
const couchRelativeOffset = { x: 5, y: 0 };
for (let n = 0; n < 3; n++) {
const totalOffset = {
x: couchAbsoluteOffset.x + couchRelativeOffset.x * n,
y: couchAbsoluteOffset.y + couchRelativeOffset.y * n,
};
if (totalOffset.x >= roomSizeTiles.w - 2) break;
instance.tile(
`${tileSet}_couch_${n}`,
totalOffset.x * tileSizeBase.w,
totalOffset.y * tileSizeBase.h,
);
}
// Draw rugs
const rugAbsoluteOffset = { x: 2, y: 5 };
const rugRelativeOffset = { x: 6, y: 0 };
for (let n = 0; n < 3; n++) {
const totalOffset = {
x: rugAbsoluteOffset.x + rugRelativeOffset.x * n,
y: rugAbsoluteOffset.y + rugRelativeOffset.y * n,
};
if (totalOffset.x >= roomSizeTiles.w - 3 || totalOffset.y >= roomSizeTiles.h - 3) break;
instance.tile(
`${tileSet}_rug_${n}`,
totalOffset.x * tileSizeBase.w,
totalOffset.y * tileSizeBase.h,
);
}
};
export const GenericLayout = PixiComponent<Props, CompositeTilemap>("GenericLayout", {
create: (props: Props) => {
const instance = new CompositeTilemap();
setupInstance(instance, props);
return instance;
},
// didMount: async (instance) => {},
applyProps: (instance, oldProps: Props, newProps: Props) => {
const { ...oldP } = oldProps;
const { ...newP } = newProps;
setupInstance(instance, newP);
// apply rest props to instance
applyDefaultProps(instance, oldP, newP);
},
});