forked from excaliburjs/excalibird
-
Notifications
You must be signed in to change notification settings - Fork 0
/
towerDispatcher.js
98 lines (75 loc) · 2.14 KB
/
towerDispatcher.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
// Responsible for generating the level as time increases, instantiating and cleaning up "pipes"
var removeItem = function(array, item){
var index = -1;
if((index = array.indexOf(item)) > -1){
array.splice(index, 1);
}
}
var TowerDispatcher = function(engine, bird, stats){
var me = this;
me.engine = engine;
me.bird = bird;
me.stats = stats;
me.towers = [];
me.triggers = []
/*me.timer = new ex.Timer(function(){
me.generateNewTower();
}, Config.TowerTimer, true);
me.engine.add(me.timer);*/
return me;
}
TowerDispatcher.prototype.generateNewTower = function(){
var dispatch = this;
var height = this.engine.getHeight();
var topTowerY = ex.Util.randomInRange(Config.TowerGap* gameScale.y, height);
var bottomTowerY = topTowerY - Config.TowerGap*gameScale.y* 2;
var top = new Tower(engine.getWidth() - 1, topTowerY, true);
var bottom = new Tower(engine.getWidth() - 1, bottomTowerY, false);
var scoreTrigger = new ex.Trigger(top.x + top.getWidth(), bottomTowerY, 20, Config.TowerGap*gameScale.y* 2, function(){
dispatch.stats.score++;
dispatch.stats.text = "Score: " + dispatch.stats.score;
Resource.ScoreSound.play();
this.kill();
}, false);
scoreTrigger.anchor = new ex.Point(0, 0);
scoreTrigger.target = this.bird;
scoreTrigger.dx = Config.LevelSpeed;
this.towers.push(top);
this.towers.push(bottom);
this.triggers.push(scoreTrigger);
top.on('exitviewport', function(){
this.kill();
removeItem(dispatch.towers, top);
});
bottom.on('exitviewport', function(){
this.kill();
removeItem(dispatch.towers, bottom);
});
this.engine.add(top);
this.engine.add(bottom);
this.engine.add(scoreTrigger);
}
TowerDispatcher.prototype.start = function(){
var me = this;
me.timer = new ex.Timer(function(){
me.generateNewTower();
}, Config.TowerTimer * gameScale.x, true);
me.engine.add(me.timer);
}
TowerDispatcher.prototype.stop = function(){
this.towers.forEach(function(t){
t.dx = 0;
});
this.triggers.forEach(function(t){
t.dx = 0;
});
this.timer.cancel();
}
TowerDispatcher.prototype.clear = function(){
this.towers.forEach(function(t){
t.kill();
});
this.triggers.forEach(function(t){
t.kill();
});
}