BlueGit

blue-star cellular-evolution

Files

cellular-evolution / src / gemSpawner.js

gemSpawner.js

8a7938e1 History
gemSpawner.js 1.4 KiB Download Raw
1/**
2 * Gem Spawner spawns gems in the world
3 */
4var gemSpawner =
5{
6 start: function()
7 {
8 gemSpawner.currentActive = 0;
9
10 this.maxGems = 7;
11
12 for (var i = 0; i < this.maxGems; i++)
13 {
14 this.spawnGem(i * 200, i == this.maxGems - 1);
15 }
16
17 this.spawnInUpdate = false;
18
19 this.allGems = [];
20 },
21
22 spawnGem: function(delay, enableSpawningAfter)
23 {
24 delay = delay || 0;
25
26 if (gemSpawner.currentActive < this.maxGems)
27 {
28 gemSpawner.currentActive++;
29
30 timingUtil.delay(delay, function()
31 {
32 var x = 100 + Math.random() * (GameWindow.width - 200);
33 var y = 100 + Math.random() * (GameWindow.height - 200);
34
35 this.allGems.push(GameObject.create(gem, x, y, s_gem, 0));
36
37 this.allGems = this.allGems.filter(function(item)
38 {
39 return !item.destroyed;
40 });
41
42 if (enableSpawningAfter)
43 {
44 this.spawnInUpdate = true;
45 }
46 }, this);
47 }
48 },
49
50 onDestroy: function()
51 {
52 for (var i = 0; i < this.allGems.length; i++)
53 {
54 this.allGems[i].destroy();
55 }
56 },
57
58 update: function(deltaTime)
59 {
60 if (this.spawnInUpdate)
61 {
62 this.spawnGem(500 + (Math.random() * 500));
63 }
64 }
65}