This repository has been archived by the owner on Sep 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
registry.ts
249 lines (204 loc) · 7.03 KB
/
registry.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
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
/*
*
* Main registry stuff, adds blocks/models to noa engine.
*
*/
import * as BABYLON from '@babylonjs/core/Legacy/legacy';
import * as MATERIALS from '@babylonjs/materials';
import type { Engine } from 'noa-engine';
import { gameSettings } from '../../values';
import { getAsset } from '../helpers/assets';
/*
* Clean registry copies. Used for lookups
*/
export let blocks = {};
export let items = {};
export let blockIDs = {};
export let blockIDmap = {};
export function registerBlocks(noa: Engine, blockList) {
const scene = noa.rendering.getScene();
// Saves blocks for later lookups
if (gameSettings.debugSettings.printRegistryToConsole) {
console.log('Blocks', blockList);
}
blocks = blockList;
// Create lookup tables
const list = Object.values(blockList);
list.forEach((x: any) => {
blockIDs[x.id] = x.rawid;
blockIDmap[x.rawid] = x.id;
});
// Temponary
noa.registry.registerMaterial('water', [0.5, 0.5, 0.8, 0.7], null, true);
noa.registry.registerMaterial('barrier', [0.0, 0.0, 0.0, 0.0], null, true);
const entries = Object.values(blockList);
// Create blocks from registry
entries.forEach(function (item: any) {
try {
createBlock(item.rawid, item.id, item.type, item.texture, item.options, item.hardness, item.miningtime, item.tool);
} catch (e) {
console.log(e);
}
});
function createBlock(id, name, type, texture, options, hardness, miningtime, tool) {
if (type == 0) {
let mat: any;
let txtTransparent = options.opaque == false ? true : false;
let color: number[] = options.color != undefined ? Object.values(options.color) : [0, 0, 0];
if (texture == undefined) texture = [''];
if (texture.length == 1 && options.material == undefined) {
noa.registry.registerMaterial(name, color, texture[0] != '' ? getAsset(texture[0], 'texture') : null, txtTransparent);
mat = name;
} else if (options.material == undefined) {
mat = new Array();
for (let x = 0; x < texture.length; x++) {
noa.registry.registerMaterial(name + x, color, texture[x] != '' ? getAsset(texture[x], 'texture') : null, txtTransparent);
mat.push(name + x);
}
} else {
mat = options.material;
}
const finOpts = options;
finOpts.material = mat;
noa.registry.registerBlock(id, finOpts);
} else if (type == 1) {
const mesh = makePlantSpriteMesh(noa, scene, texture[0], name);
const finOpts = options;
finOpts.blockMesh = mesh;
noa.registry.registerBlock(id, finOpts);
} else if (type == 2) {
const mesh = makeCactusMesh(noa, scene, [texture[0], texture[1]], name);
const finOpts = options;
finOpts.blockMesh = mesh;
noa.registry.registerBlock(id, finOpts);
} else if (type == 4) {
let matl;
if (texture.length == 1 && options.material == undefined) {
const mat = noa.rendering.makeStandardMaterial(name);
let tex: any;
tex = new BABYLON.Texture(getAsset(texture[0], 'texture'), scene, true, true, BABYLON.Texture.NEAREST_SAMPLINGMODE);
mat.diffuseTexture = tex;
mat.opacityTexture = mat.diffuseTexture;
mat.backFaceCulling = true;
noa.registry.registerMaterial(name, null, null, false, mat);
matl = name;
} else if (options.material == undefined) {
matl = new Array();
for (let x = 0; x < texture.length; x++) {
const mat = noa.rendering.makeStandardMaterial(name);
let tex: any;
tex = new BABYLON.Texture(getAsset(texture[0], 'texture'), scene, true, true, BABYLON.Texture.NEAREST_SAMPLINGMODE);
mat.diffuseTexture = tex;
mat.opacityTexture = mat.diffuseTexture;
mat.backFaceCulling = true;
noa.registry.registerMaterial(name + x, null, null, false, mat);
mat.push(name + x);
}
}
const finOpts = options;
finOpts.material = matl;
//finOpts.blockMesh = mesh;
noa.registry.registerBlock(id, finOpts);
}
}
}
/*
* Saves for lookup. In future there will be more stuff
*/
export function registerItems(noa, itemList) {
items = itemList;
if (gameSettings.debugSettings.printRegistryToConsole) {
console.log('Items', {...itemList});
}
}
/*
* Creates X shaped mesh
*/
function makePlantSpriteMesh(noa, scene, url, name) {
const matname = name || 'mat';
let tex = new BABYLON.Texture(getAsset(url, 'texture'), scene, true, true, BABYLON.Texture.NEAREST_SAMPLINGMODE);
tex.hasAlpha = true;
const mesh = BABYLON.Mesh.CreatePlane('sprite-' + matname, 1, scene);
const mat = noa.rendering.makeStandardMaterial(matname);
mat.backFaceCulling = false;
mat.diffuseTexture = tex;
mat.diffuseTexture.vOffset = 0.99;
mesh.material = mat;
mesh.rotation.y += 0.81;
const offset = BABYLON.Matrix.Translation(0, 0.5, 0);
mesh.bakeTransformIntoVertices(offset);
const clone = mesh.clone();
clone.rotation.y += 1.62;
return BABYLON.Mesh.MergeMeshes([mesh, clone], true);
}
/*
* Creates cactus mesh. It's little broken rn. Todo: replace it with custom model support
*/
function makeCactusMesh(noa, scene: BABYLON.Scene, url: string[], name: string) {
const mesh = {};
let matname = name || 'mat';
const mat: BABYLON.StandardMaterial = noa.rendering.makeStandardMaterial(matname);
const mat2: BABYLON.StandardMaterial = noa.rendering.makeStandardMaterial(matname);
mat.backFaceCulling = false;
mat2.backFaceCulling = false;
const top = getAsset(url[0], 'texture');
const side = getAsset(url[1], 'texture');
mat.diffuseTexture = new BABYLON.Texture(side, scene, true, true, BABYLON.Texture.NEAREST_LINEAR); //new BABYLON.CubeTexture(side, scene, [], false, [side, top, side, side, top, side]);
mat.diffuseTexture.hasAlpha = true;
mat2.diffuseTexture = new BABYLON.Texture(top, scene, true, true, BABYLON.Texture.NEAREST_LINEAR); //new BABYLON.CubeTexture(side, scene, [], false, [side, top, side, side, top, side]);
mat2.diffuseTexture.hasAlpha = true;
for (let x = 0; x < 6; x++) {
mesh[x] = BABYLON.Mesh.CreatePlane('sprite-' + matname, 1, scene);
mesh[x].material = mat;
let offset: any;
switch (x) {
case 0:
offset = BABYLON.Matrix.Translation(0, 0.5, 0.435);
mesh[x].rotation.y = 1.57;
break;
case 1:
offset = BABYLON.Matrix.Translation(0, 0.5, -0.435);
mesh[x].rotation.y = 1.57;
break;
case 2:
offset = BABYLON.Matrix.Translation(0, 0.5, 0.435);
break;
case 3:
offset = BABYLON.Matrix.Translation(0, 0.5, -0.435);
break;
case 4:
offset = BABYLON.Matrix.Translation(0, 0, -1);
mesh[x].rotation.x = 1.57;
break;
case 5:
offset = BABYLON.Matrix.Translation(0, 0, 0);
mesh[x].rotation.x = 1.57;
break;
}
mesh[x].bakeTransformIntoVertices(offset);
}
const newmesh = BABYLON.Mesh.MergeMeshes(Object.values(mesh), true, true, undefined, true, false);
for (let x = 0; x < 6; x++) {
switch (x) {
case 0:
newmesh.subMeshes[x].materialIndex = 1;
break;
case 1:
newmesh.subMeshes[x].materialIndex = 1;
break;
case 2:
newmesh.subMeshes[x].materialIndex = 0;
break;
case 3:
newmesh.subMeshes[x].materialIndex = 0;
break;
case 4:
newmesh.subMeshes[x].materialIndex = 0;
break;
case 5:
newmesh.subMeshes[x].materialIndex = 0;
break;
}
}
return newmesh;
}