forked from robterrell/cocos2d-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathActionManager.js
207 lines (168 loc) · 5.57 KB
/
ActionManager.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
'use strict'
var util = require('util'),
Timer = require('./Scheduler').Timer,
Scheduler = require('./Scheduler').Scheduler
/**
* @class
* A singleton that manages all the actions. Normally you
* won't need to use this singleton directly. 99% of the cases you will use the
* cocos.nodes.Node interface, which uses this singleton. But there are some cases where
* you might need to use this singleton. Examples:
*
* * When you want to run an action where the target is different from a cocos.nodes.Node
* * When you want to pause / resume the actions
*
* @memberOf cocos
* @singleton
*/
function ActionManager () {
ActionManager.superclass.constructor.call(this)
Scheduler.sharedScheduler.scheduleUpdate({target: this, priority: 0, paused: false})
this.targets = []
}
ActionManager.inherit(Object, /** @lends cocos.ActionManager# */ {
targets: null,
currentTarget: null,
currentTargetSalvaged: null,
/**
* Adds an action with a target. If the target is already present, then the
* action will be added to the existing target. If the target is not
* present, a new instance of this target will be created either paused or
* paused, and the action will be added to the newly created target. When
* the target is paused, the queued actions won't be 'ticked'.
*
* @opt {cocos.nodes.Node} target Node to run the action on
*/
addAction: function (opts) {
var targetID = opts.target.id
var element = this.targets[targetID]
if (!element) {
element = this.targets[targetID] = {
paused: false,
target: opts.target,
actions: []
}
}
element.actions.push(opts.action)
opts.action.startWithTarget(opts.target)
},
/**
* Remove an action
*
* @param {cocos.actions.Action} action Action to remove
*/
removeAction: function (action) {
var targetID = action.originalTarget.id,
element = this.targets[targetID]
if (!element) {
return
}
var actionIndex = element.actions.indexOf(action)
if (actionIndex == -1) {
return
}
if (this.currentTarget == element) {
element.currentActionSalvaged = true
}
element.actions[actionIndex] = null
element.actions.splice(actionIndex, 1); // Delete array item
if (element.actions.length === 0) {
if (this.currentTarget == element) {
this.currentTargetSalvaged = true
}
}
},
/**
* Fetch an action belonging to a cocos.nodes.Node
*
* @returns {cocos.actions.Action}
*
* @opts {cocos.nodes.Node} target Target of the action
* @opts {String} tag Tag of the action
*/
getActionFromTarget: function(opts) {
var tag = opts.tag,
targetID = opts.target.id
var element = this.targets[targetID]
if (!element) {
return null
}
for (var i = 0; i < element.actions.length; i++ ) {
if (element.actions[i] &&
(element.actions[i].tag === tag)) {
return element.actions[i]
}
}
// Not found
return null
},
/**
* Remove all actions for a cocos.nodes.Node
*
* @param {cocos.nodes.Node} target Node to remove all actions for
*/
removeAllActionsFromTarget: function (target) {
var targetID = target.id
var element = this.targets[targetID]
if (!element) {
return
}
// Delete everything in array but don't replace it incase something else has a reference
element.actions.splice(0, element.actions.length)
},
/**
* @private
*/
update: function (dt) {
var self = this
util.each(this.targets, function (currentTarget, i) {
if (!currentTarget) {
return
}
self.currentTarget = currentTarget
if (!currentTarget.paused) {
util.each(currentTarget.actions, function (currentAction, j) {
if (!currentAction) {
return
}
currentTarget.currentAction = currentAction
currentTarget.currentActionSalvaged = false
currentTarget.currentAction.step(dt)
if (currentTarget.currentAction.isDone) {
currentTarget.currentAction.stop()
var a = currentTarget.currentAction
currentTarget.currentAction = null
self.removeAction(a)
}
currentTarget.currentAction = null
})
}
if (self.currentTargetSalvaged && currentTarget.actions.length === 0) {
self.targets[i] = null
delete self.targets[i]
}
})
},
pauseTarget: function (target) {
},
resumeTarget: function (target) {
// TODO
}
})
Object.defineProperty(ActionManager, 'sharedManager', {
/**
* A shared singleton instance of cocos.ActionManager
*
* @memberOf cocos.ActionManager
* @getter {cocos.ActionManager} sharedManager
*/
get: function () {
if (!ActionManager._instance) {
ActionManager._instance = new this()
}
return ActionManager._instance
}
, enumerable: true
})
exports.ActionManager = ActionManager
// vim:et:st=4:fdm=marker:fdl=0:fdc=1