-
Notifications
You must be signed in to change notification settings - Fork 0
/
BombController.js
75 lines (61 loc) · 1.76 KB
/
BombController.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
function BombController() {
this.counter = 10;
}
BombController.prototype = {
constructor: BombController,
setNPCs: function(npcs) {
this._npcs = npcs;
},
tick: function() {
this.counter--;
this.counterLabel.setString('' + this.counter);
if (this.isExploded()) {
this._explode();
}
},
isExploded: function() {
return this.counter <= 0;
},
setGameController: function(controller) {
this._gameCtrl = controller;
},
setContainer: function(container) {
this._container = container;
},
isInContainer: function() {
return !!this._container;
},
getContainer: function() {
return this._container;
},
_explode: function () {
cc.AudioEngine.getInstance().playEffect('bomb');
if (this._container) {
this._container.explode();
return;
}
var explosion = cc.BuilderReader.load('Explosion.ccbi');
explosion.setPosition(this.rootNode.getPosition());
this._gameCtrl.level.addChild(explosion);
this._killNPCS();
this.rootNode.removeFromParent(true);
},
_killNPCS: function() {
this._gameCtrl._npcs.forEach(function(npc) {
if (this._inRadius(npc)) {
npc.controller.kill();
}
}, this);
if (this._inRadius(this._gameCtrl.hero)) {
this._gameCtrl.hitHero();
}
},
_inRadius: function(node) {
var nodePos = node.getPosition(),
selfPos = this.rootNode.getPosition(),
dx = nodePos.x - selfPos.x,
dy = nodePos.y - selfPos.y,
distanceSqr = dx * dx + dy * dy;
return distanceSqr <= 100 * 100;
}
};