-
Notifications
You must be signed in to change notification settings - Fork 0
/
level.ts
237 lines (210 loc) · 6.46 KB
/
level.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
class PRNG {
private readonly m: number;
private readonly a: number;
private readonly c: number;
private state: number;
constructor(seed?: number) {
this.m = 0x80000000; // 2**31;
this.a = 1103515245;
this.c = 12345;
this.state = seed !== undefined ? seed : Math.floor(Math.random() * (this.m - 1));
}
nextInt(): number {
this.state = (this.a * this.state + this.c) % this.m;
return this.state;
}
nextFloat(): number {
// returns in range [0, 1)
return this.nextInt() / (this.m - 1);
}
nextRange(start: number, end: number): number {
// returns in range [start, end): including start, excluding end
// can't modulu nextInt because of weak randomness in lower bits
const rangeSize = end - start;
const randomUnder1 = this.nextInt() / this.m;
return start + Math.floor(randomUnder1 * rangeSize);
}
choice<T>(array: Array<T>): T {
return array[this.nextRange(0, array.length)];
}
}
class arrayFuncs {
static forEach = (array: Array<Array<any>>, callback: Function) => {
array.forEach(function (row, y) {
row.forEach(function (element, x) {
callback(element, x, y)
})
})
}
}
function twoDArray(size: [number, number], initializer: any) {
let array: Array<any> = []
if (size[1] > 0 && size[0] > 0) {
let row: Array<any> = []
for (let i = 0; i < size[0]; i++) {
row.push(initializer)
}
for (let i = 0; i < size[1]; i++) {
array.push(row)
}
return array
}
return []
}
class blockType {
name: string;
collisions: boolean;
generatable: boolean;
size: [number, number] = [-1, -1];
imagePath: string | undefined;
constructor(collisions: boolean, name: string, generatable: boolean, imagePath?: string,) {
this.name = name
this.collisions = collisions
this.generatable = generatable
this.imagePath = imagePath
}
}
class block {
static blockTypes = {
air: new blockType(false, "air", false,),
baseplate: new blockType(false, "baseplate", true, "./Assets/baseplate.png"),
stairs: new blockType(false, "stairs", true),
}
constructor(type: string) {
let request: any = block.blockTypes[type as keyof block]
if (!request?.name) { throw ("Unable to create block with nonexistant block type \"" + type + "\"") }
return request
}
}
class map {
prng: PRNG
placeableBlockTypes: Array<blockType>
map: any
constructor(prng: PRNG) {
if (!prng) {
console.error("Missing PRNG");
}
this.prng = prng
this.placeableBlockTypes = map.compilePlaceableBlockTypes()
//@ts-ignore
this.map = map.combineChunks(chunk(), chunk(), chunk(), chunk(), chunk(), chunk(), chunk(), chunk())
return this.map
}
static compilePlaceableBlockTypes() {
return Object.values(block.blockTypes).filter(function (element: blockType) {
if (element.generatable) {
return true
}
})
}
static combineChunks(...chunks: Array<Array<Array<block>>>) {
let combined: Array<Array<block>> = []
chunks[0].forEach(function (yHeight, index) {
combined[index] = yHeight
chunks.forEach((chunk, cIndex) => {
if (cIndex === 0) { return }
combined[index] = combined[index].concat(chunk[index])
})
})
return combined
}
chooseRandomTile(prng: PRNG) {
return new block(this.placeableBlockTypes[prng.nextRange(0, this.placeableBlockTypes.length)].name)
}
}
class baseplate {
constructor() {
let plate = twoDArray([16, 1], new block("baseplate"))
return plate[0]
}
}
function chunk() {
let chunk = twoDArray([16, 100], new block("air"))
chunk.push(new baseplate(), new baseplate(), new baseplate())
return chunk
}
interface asset {
name: string
url: string
bundle?: string
}
interface bundle {
name: string
assets: {
alias: string,
src: string
}[]
}
interface manifest {
bundles: bundle[]
}
function weightedChoose(array: Array<[any, number]>, prng: PRNG) {
let range: any[] = []
array.forEach((value) => {
for (let i = 0; i < value[1]; i++) {
range.push(value[0])
}
})
return prng.choice(range)
}
class level {
levelSeed: number;
prng: PRNG;
map: map
assets: object
constructor(seed?: number) {
this.levelSeed = seed ? seed : Date.now();
this.prng = new PRNG(Number(this.levelSeed))
this.map = new map(this.prng)
this.assets = level.compileAssets(Object.values(block.blockTypes))
}
static compileAssets(blocks: Array<blockType>) {
let blocksWithSprites = blocks.filter(function (value) { return value.imagePath !== undefined })
//@ts-ignore
let assets: asset[] = blocksWithSprites.map((value) => {
return {
name: value.name,
url: value.imagePath,
bundle: "init"
}
})
let manifest: manifest = {
bundles: []
}
let createdBundles = { init: 0 };
//@ts-ignore
manifest.bundles.push({
name: "init",
assets: []
})
assets.forEach(function (asset) {
if (asset.bundle) {
//@ts-ignore
if (createdBundles[asset.bundle] > -1) {
//@ts-ignore
manifest.bundles[createdBundles[asset.bundle]].assets.push({
alias: asset.name,
src: asset.url
})
} else {
manifest.bundles[manifest.bundles.length] = {
name: asset.bundle,
assets: [{
alias: asset.name,
src: asset.url
}]
}
//@ts-ignore
createdBundles[asset.bundle] = manifest.bundles.length - 1
}
} else {
manifest.bundles[0].assets.push({
alias: asset.name,
src: asset.url
})
}
manifest.bundles.push()
})
return manifest
}
}