-
Notifications
You must be signed in to change notification settings - Fork 45
/
tile.js
272 lines (241 loc) · 10 KB
/
tile.js
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
let TILE_COUNTER = 1;
AFRAME.registerComponent("tile", {
schema: {
tileSize: { type: "number", default: 160 },
sidewalkHeight: { type: "number", default: 0.2 },
buildingTextures: { type: "array", default: [] },
buildingColors: { type: "array", default: [] },
groundTextures: { type: "array", default: [] },
groundColors: { type: "array", default: [] },
roadTextures: { type: "array", default: [] },
roadColors: { type: "array", default: [] }
},
init: function() {
this.randState = TILE_COUNTER++;
this.addGround1();
let buildings = [this.addBuildings1.bind(this), this.addBuildings2.bind(this)];
buildings[this.nextRandElement(buildings)]();
},
nextRand: function() {
this.randState = (1103515245 * this.randState + 12345) % 0x80000000;
return Math.abs(this.randState / (0x80000000 - 1));
},
nextRandElement: function(arr) {
return Math.floor(this.nextRand() * arr.length);
},
addGround1: function() {
let plane = document.createElement("a-plane");
plane.setAttribute("position", { x: 0, y: -1 * this.data.sidewalkHeight, z: 0 });
plane.setAttribute("width", this.data.tileSize);
plane.setAttribute("height", this.data.tileSize);
plane.setAttribute("rotation", { x: -90, y: 0, z: 0 });
plane.setAttribute("material", {
src: this.data.roadTextures[0],
repeat: { x: 60, y: 60 },
shader: "flat"
});
this.el.appendChild(plane);
// sidewalk
plane = document.createElement("a-box");
plane.setAttribute("position", { x: 0, y: (-1 * this.data.sidewalkHeight) / 2, z: 0 });
plane.setAttribute("width", this.data.tileSize - 11);
plane.setAttribute("depth", this.data.tileSize - 11);
plane.setAttribute("height", this.data.sidewalkHeight);
plane.setAttribute("material", {
src: this.data.groundTextures[0],
repeat: { x: 44, y: 55 }
});
this.el.appendChild(plane);
},
addBuilding: function(width, depth, height, x, z) {
let texture = this.nextRandElement(this.data.buildingTextures);
let box = document.createElement("a-box");
box.setAttribute("position", { x: x, y: height / 2, z: z });
box.setAttribute("width", width);
box.setAttribute("depth", depth);
box.setAttribute("height", height);
box.setAttribute("class", "collidable");
box.setAttribute("material", {
src: this.data.buildingTextures[texture],
repeat: { x: width / 10, y: height / 10 }
});
this.el.appendChild(box);
let roof = document.createElement("a-plane");
roof.setAttribute("position", { x: x, y: height + 0.1, z: z });
roof.setAttribute("width", width - 0.2);
roof.setAttribute("height", depth - 0.2);
roof.setAttribute("rotation", { x: -90, y: 0, z: 0 });
roof.setAttribute("color", this.data.buildingColors[texture]);
this.el.appendChild(roof);
},
addBuildings1: function() {
this.addBuilding(90, 90, 50 + 150 * this.nextRand(), 20 * this.nextRand() - 10, 20 * this.nextRand() - 10);
},
addBuildings2: function() {
this.addBuilding(50, 50, 40 + 100 * this.nextRand(), -35 - this.nextRand() * 8, -35 - this.nextRand() * 8);
this.addBuilding(50, 50, 60 + 90 * this.nextRand(), 35 + this.nextRand() * 8, -35 - this.nextRand() * 8);
this.addBuilding(50, 50, 40 + 180 * this.nextRand(), -35 - this.nextRand() * 8, 35 + this.nextRand() * 8);
this.addBuilding(50, 50, 50 + 70 * this.nextRand(), 35 + this.nextRand() * 8, 35 + this.nextRand() * 8);
}
/*
addGroundDebug: function() {
let plane = document.createElement("a-plane");
plane.setAttribute("position", { x: 0, y: 0, z: 0 });
plane.setAttribute("width", 150);
plane.setAttribute("height", 150);
plane.setAttribute("rotation", { x: -90, y: 0, z: 0 });
plane.setAttribute("color", "#" + Math.round(0xffffff * this.nextRand()).toString(16));
this.el.appendChild(plane);
},
*/
/*
addBuilding: function(width, depth, height, x, z) {
let texture = this.nextRandElement(this.data.buildingTextures);
let bottomHeight = 0.3;
// four sides
let plane = document.createElement("a-plane");
plane.setAttribute("position", { x: x, y: bottomHeight + height / 2, z: z + depth / 2 });
plane.setAttribute("width", width);
plane.setAttribute("height", height);
plane.setAttribute("rotation", { x: 0, y: 0, z: 0 });
plane.setAttribute("class", "collidable");
plane.setAttribute("material", {
src: this.data.buildingTextures[texture],
repeat: { x: width / 10, y: height / 10 }
});
this.el.appendChild(plane);
plane = document.createElement("a-plane");
plane.setAttribute("position", { x: x, y: bottomHeight + height / 2, z: z - depth / 2 });
plane.setAttribute("width", width);
plane.setAttribute("height", height);
plane.setAttribute("rotation", { x: 0, y: 180, z: 0 });
plane.setAttribute("class", "collidable");
plane.setAttribute("material", {
src: this.data.buildingTextures[texture],
repeat: { x: width / 10, y: height / 10 }
});
this.el.appendChild(plane);
plane = document.createElement("a-plane");
plane.setAttribute("position", { x: x + width / 2, y: bottomHeight + height / 2, z: z });
plane.setAttribute("width", depth);
plane.setAttribute("height", height);
plane.setAttribute("rotation", { x: 0, y: 90, z: 0 });
plane.setAttribute("class", "collidable");
plane.setAttribute("material", {
src: this.data.buildingTextures[texture],
repeat: { x: depth / 10, y: height / 10 }
});
this.el.appendChild(plane);
plane = document.createElement("a-plane");
plane.setAttribute("position", { x: x - width / 2, y: bottomHeight + height / 2, z: z });
plane.setAttribute("width", depth);
plane.setAttribute("height", height);
plane.setAttribute("rotation", { x: 0, y: 270, z: 0 });
plane.setAttribute("class", "collidable");
plane.setAttribute("material", {
src: this.data.buildingTextures[texture],
repeat: { x: depth / 10, y: height / 10 }
});
this.el.appendChild(plane);
// roof
plane = document.createElement("a-plane");
plane.setAttribute("position", { x: x, y: bottomHeight + height, z: z });
plane.setAttribute("width", width);
plane.setAttribute("height", depth);
plane.setAttribute("rotation", { x: -90, y: 0, z: 0 });
plane.setAttribute("class", "collidable");
plane.setAttribute("color", this.data.buildingColors[texture]);
plane.setAttribute("material", {
shader: "flat"
});
this.el.appendChild(plane);
// bottom sides
plane = document.createElement("a-plane");
plane.setAttribute("position", { x: x, y: bottomHeight / 2, z: z + depth / 2 });
plane.setAttribute("width", width);
plane.setAttribute("height", bottomHeight);
plane.setAttribute("rotation", { x: 0, y: 0, z: 0 });
plane.setAttribute("color", this.data.buildingColors[texture]);
plane.setAttribute("material", {
shader: "flat"
});
this.el.appendChild(plane);
plane = document.createElement("a-plane");
plane.setAttribute("position", { x: x, y: bottomHeight / 2, z: z - depth / 2 });
plane.setAttribute("width", width);
plane.setAttribute("height", bottomHeight);
plane.setAttribute("rotation", { x: 0, y: 180, z: 0 });
plane.setAttribute("color", this.data.buildingColors[texture]);
this.el.appendChild(plane);
plane = document.createElement("a-plane");
plane.setAttribute("position", { x: x + width / 2, y: bottomHeight / 2, z: z });
plane.setAttribute("width", depth);
plane.setAttribute("height", bottomHeight);
plane.setAttribute("rotation", { x: 0, y: 90, z: 0 });
plane.setAttribute("color", this.data.buildingColors[texture]);
this.el.appendChild(plane);
plane = document.createElement("a-plane");
plane.setAttribute("position", { x: x - width / 2, y: bottomHeight / 2, z: z });
plane.setAttribute("width", depth);
plane.setAttribute("height", bottomHeight);
plane.setAttribute("rotation", { x: 0, y: 270, z: 0 });
plane.setAttribute("color", this.data.buildingColors[texture]);
this.el.appendChild(plane);
},
*/
/*
addGround1: function() {
let plane = document.createElement("a-plane");
plane.setAttribute("position", { x: 0, y: 0, z: 73.25 });
plane.setAttribute("width", 150);
plane.setAttribute("height", 3.5);
plane.setAttribute("rotation", { x: -90, y: 0, z: 0 });
plane.setAttribute("color", "#333");
plane.setAttribute("material", {
shader: "flat"
});
this.el.appendChild(plane);
plane = document.createElement("a-plane");
plane.setAttribute("position", { x: 0, y: 0, z: -73.25 });
plane.setAttribute("width", 150);
plane.setAttribute("height", 3.5);
plane.setAttribute("rotation", { x: -90, y: 0, z: 0 });
plane.setAttribute("color", "#333");
plane.setAttribute("material", {
shader: "flat"
});
this.el.appendChild(plane);
plane = document.createElement("a-plane");
plane.setAttribute("position", { x: 73.25, y: 0, z: 0 });
plane.setAttribute("width", 3.5);
plane.setAttribute("height", 143);
plane.setAttribute("rotation", { x: -90, y: 0, z: 0 });
plane.setAttribute("color", "#333");
plane.setAttribute("material", {
shader: "flat"
});
this.el.appendChild(plane);
plane = document.createElement("a-plane");
plane.setAttribute("position", { x: -73.25, y: 0, z: 0 });
plane.setAttribute("width", 3.5);
plane.setAttribute("height", 143);
plane.setAttribute("rotation", { x: -90, y: 0, z: 0 });
plane.setAttribute("color", "#333");
plane.setAttribute("material", {
shader: "flat"
});
this.el.appendChild(plane);
plane = document.createElement("a-plane");
plane.setAttribute("position", { x: 0, y: 0, z: 0 });
plane.setAttribute("width", 143);
plane.setAttribute("height", 143);
plane.setAttribute("rotation", { x: -90, y: 0, z: 0 });
plane.setAttribute("material", {
src: this.data.groundTextures[0],
repeat: { x: 40, y: 50 },
shader: "flat"
});
this.el.appendChild(plane);
},
*/
});