-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwall-manager.js
54 lines (46 loc) · 1.64 KB
/
wall-manager.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
class WallManager {
constructor (app, container) {
// Screen rendering references.
this.renderer = app.renderer;
this.container = container;
this.biomes = [];
this.BG = new BiomeGenerator(this.renderer);
// Manager properties
this.updateSpeed = 4; // How fast biomes move to left
// First wall spawns after 1st frame (and slides in from the right)
this.ticks = 1;
this.biomeNumber = 0;
}
runTick() {
// Once ticks are depleted, spawn biom and re-schedule next ticks
// depletion, to spawn the next biom.
this.ticks -= 1;
if (this.ticks == 0) {
let spawnedBiome = Math.random() > 0.5 ? this.spawnBiome("WINTER") : this.spawnBiome("DESERT");
this.ticks = spawnedBiome.nextBiomeSpawn;
}
this.biomes.forEach((biome, index, array) => {
biome.updatePosition(this.updateSpeed);
if (biome.isOffScreen()) {
biome.destructor();
array.splice(0,1);
}
});
}
spawnBiome(type) {
// Second parameter is the scale of the basic biom length
// e.g. 1 for standard width, 2 for two screens biom, ...
let biomLength = Math.floor(Utils.randomNumberFromRange(2,5));
let biome = this.BG.constructBiome(type, biomLength);
this.biomeNumber += 1;
biome.setId(this.biomeNumber);
this.biomes.push(biome);
this.container.addChild(biome.container);
return biome;
}
destructor() {
this.biomes.forEach(element => {
element.destructor();
});
}
}