-
Notifications
You must be signed in to change notification settings - Fork 0
/
NPCController.js
131 lines (110 loc) · 3.66 KB
/
NPCController.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
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
function NPCController() {
this._toWaitBeforeNextDoor = 0;
this._speed = NPCController.walkSpeed;
}
NPCController.minWalk = 100;
NPCController.maxWalk = 1500;
NPCController.walkSpeed = 100;
NPCController.runSpeed = 200;
NPCController._walkRange = NPCController.maxWalk - NPCController.minWalk;
NPCController.prototype = {
constructor: NPCController,
onDidLoadFromCCB: function () {
this._setDirection(Math.floor(Math.random() * 2) || -1);
this.rootNode.scheduleUpdate();
this.rootNode.update = this.update.bind(this);
this._startWalking();
},
update: function(dt) {
if (this._inDoor) {
return;
}
this._toWaitBeforeNextDoor -= dt;
var dist = this._speed * dt,
position = this.rootNode.getPosition();
position.x += dist * this._direction;
this.rootNode.setPosition(position);
this._toWalkLeft -= dist;
if (this._isOutOfLeftBound()) {
this._setDirection(1);
this._startWalking();
} else if (this._isOutOfRightBound()) {
this._setDirection(1);
this._setDirection(-1);
this._startWalking();
} else if (this._toWalkLeft <= 0) {
this._changeDirection();
}
},
setGameController: function(controller) {
this._gameCtrl = controller;
},
kill: function() {
this._gameCtrl.removeNPC(this.rootNode);
},
probablyMoveToFloor: function(floorY) {
if (this._inDoor || this._lastDoor || this._toWaitBeforeNextDoor > 0) {
return;
}
this._timeFromDoorCheck = 0;
var chance = Math.random();
if (chance < 0.4) {
return;
}
this.rootNode.runAction(cc.Sequence.create([
cc.Hide.create(),
cc.CallFunc.create(function() {
this._inDoor = true;
}, this),
cc.MoveTo.create(0.2, cc.p(this.rootNode.getPosition().x, floorY)),
cc.CallFunc.create(function() {
this._inDoor = false;
this._toWaitBeforeNextDoor = 3;
}, this),
cc.Show.create()
]));
},
runAway: function(pointFrom) {
this.rootNode.animationManager.runAnimationsForSequenceNamed('panic');
if (this.rootNode.getPosition().x > pointFrom.x) {
this._setDirection(1);
} else {
this._setDirection(-1);
}
this._toWalkLeft = 900;
this._speed = NPCController.runSpeed;
},
setLastDoor: function(door) {
this._lastDoor = door;
},
_isOutOfLeftBound: function () {
var x = this.rootNode.getPosition().x,
halfWidth = this.rootNode.getContentSize().width / 2;
return x - halfWidth / 2 < 60;
},
_isOutOfRightBound: function () {
var x = this.rootNode.getPosition().x,
halfWidth = this.rootNode.getContentSize().width / 2;
return x + halfWidth > 840;
},
_changeDirection: function() {
this._setDirection(-this._direction);
this._startWalking();
},
_setDirection: function(direction) {
this._direction = direction;
this.sprite.setFlipX(this._direction === -1);
},
_startWalking: function () {
this._speed = NPCController.walkSpeed;
this.rootNode.animationManager.runAnimationsForSequenceNamed('walk');
this._toWalkLeft = NPCController.minWalk +
Math.floor(Math.random() * NPCController._walkRange);
}
};
Object.defineProperty(NPCController.prototype, 'isNPC', {
writable: false,
configurable: false,
enumerable: true,
value: true
});