-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
144 lines (135 loc) · 4.45 KB
/
main.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
import * as TR from 'three';
import { loadBuildings } from './src/loadBuildings';
import { setLight } from './src/light';
import { init } from './src/init';
import { onResize } from './src/onResize';
import { tiles } from './src/tiles';
import { loadRetroForrest } from './src/loadRertoForrest';
import { loadUnits } from './src/loadUnits';
import { loadMechs } from './src/loadMechs';
// initial setup
//const map_size = 195; dota
//const map_size = 480; //wc3
const map_size = 96;
const viewSize = map_size / 2;
const { scene, renderer, camera, controls } = init(TR, viewSize);
setLight(TR, scene);
window.addEventListener('resize', () => onResize());
function animate() {
controls.update();
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
animate();
// level
let blocks = await loadRetroForrest(TR);
const map = Array.from({ length: map_size }, () => Array.from({ length: map_size }, () => ({ tiles: [] })));
createLevel();
function randomFloor() {
const x = ['floor_A', 'floor_B' /*'floor_C'*/];
const r = Math.floor(Math.random() * x.length);
return x[r];
}
export function set(blockName, x = 0, y = 0, rx = 0) {
const b = blocks[blockName].clone();
b.rotation.y = rx;
b.position.set(x, 0, y);
if (blockName.startsWith('water')) {
b.position.set(x, -0.1, y);
}
scene.add(b);
}
export async function createLevel() {
const walls = false;
const s = map_size;
for (let a = 0; a < s; a++) {
set('wall_A', a - s / 2, -s / 2 - 1.5);
if (walls) set('wall_A', a - s / 2, s / 2 + 0.5, Math.PI);
if (walls) set('wall_A', s / 2 + 0.5, a - s / 2, -Math.PI / 2);
set('wall_A', -s / 2 - 1.5, a - s / 2, Math.PI / 2);
for (let b = 0; b < s; b++) {
if (b == a) {
set('waterfloor_A', a - s / 2, b - s / 2);
} else if (b == a - 1) {
set('waterfloorEdge_1vert', a - s / 2, b - s / 2, Math.PI);
} else if (b == a + 1) {
set('waterfloorEdge_1vert', a - s / 2, b - s / 2);
} else if (b == a - 2) {
set('waterfloorEdge_corner', a - s / 2, b - s / 2, Math.PI);
set('wallHole_1wall_B', a - s / 2, b - s / 2);
set('wallHole_1wall_B', a - s / 2, b - s / 2, -Math.PI / 2);
} else if (b == a + 2) {
set('waterfloorEdge_corner', a - s / 2, b - s / 2);
set('wallHole_1wall_B', a - s / 2, b - s / 2, Math.PI);
set('wallHole_1wall_B', a - s / 2, b - s / 2, Math.PI / 2);
} else if (b - a == 3) {
set('floorEdge_corner', a - s / 2, b - s / 2, Math.PI);
} else if (a - b == 3) {
set('floorEdge_corner', a - s / 2, b - s / 2);
} else {
set(randomFloor(), a - s / 2, b - s / 2);
continue;
if (a > b) {
const deco = tiles.filter((t) => t.startsWith('deco_treeDead')).map((a) => a.split('.')[0]);
if (Math.random() < 0.1) set(deco[Math.floor(Math.random() * deco.length)], a - s / 2, b - s / 2, Math.PI);
} else {
const shrooms = tiles.filter((t) => t.startsWith('deco_mushrooms')).map((a) => a.split('.')[0]);
if (Math.random() < 0.04) set(shrooms[Math.floor(Math.random() * shrooms.length)], a - s / 2, b - s / 2, Math.PI);
if (Math.random() < 0.1) set('deco_tree_B', a - s / 2, b - s / 2, Math.PI);
}
}
}
}
set('wall_cornerInCap_AB', -s / 2 - 1.5, -s / 2 - 1.5);
if (walls) set('wall_cornerInCap_AB', s / 2 + 0.5, s / 2 + 0.5, -Math.PI);
if (walls) set('wall_cornerInCap_AB', -s / 2 - 1.5, s / 2 + 0.5, Math.PI / 2);
if (walls) set('wall_cornerInCap_AB', s / 2 + 0.5, -s / 2 - 1.5, -Math.PI / 2);
//set('floor_big', 1, 1);
}
async function placeBuildings() {
let buildings = await loadBuildings(TR);
let x = 0;
let y = 0;
Object.values(buildings).forEach((b) => {
x = x + 5;
if (x % 25 == 0) {
y += 5;
x = 0;
}
b.position.set(x - 30.5, 0, y - 30.5);
scene.add(b);
});
}
//await placeBuildings();
async function placeUnits() {
let units = await loadUnits(TR);
let x = 0;
let y = 0;
Object.values(units).forEach((b) => {
x = x + 1;
if (x % 5 == 0) {
y += 1;
x = 0;
}
b.position.set(x - 25, 0, y - 2);
scene.add(b);
});
}
//await placeUnits();
async function placeMechs() {
let mechs = await loadMechs(TR);
let x = 0;
let y = 0;
Object.values(mechs).forEach((b) => {
console.log(b);
console.log(b.animtions);
x = x + 1;
if (x % 5 == 0) {
y += 1;
x = 0;
}
b.position.set(x - 1, 0, y - 2);
scene.add(b);
});
}
await placeMechs();