|
1 | | - |
2 | 1 | var Class = require('../../utils/Class'); |
3 | | -var Components = require('./animation/'); |
4 | 2 |
|
5 | 3 | // Game Object Animation Controller |
6 | 4 |
|
@@ -79,32 +77,350 @@ var Animation = new Class({ |
79 | 77 | this._updateParams = []; |
80 | 78 | }, |
81 | 79 |
|
| 80 | + // Gets or sets the amount of time in seconds between repeats. |
| 81 | + // For example, if repeat is 2 and repeatDelay is 1, the animation will play initially, |
| 82 | + // then wait for 1 second before it repeats, then play again, then wait 1 second again |
| 83 | + // before doing its final repeat. |
82 | 84 |
|
83 | | - destroy: function () |
| 85 | + delay: function (value) |
| 86 | + { |
| 87 | + if (value === undefined) |
| 88 | + { |
| 89 | + return this._delay; |
| 90 | + } |
| 91 | + else |
| 92 | + { |
| 93 | + this._delay = value; |
| 94 | + |
| 95 | + return this; |
| 96 | + } |
| 97 | + }, |
| 98 | + |
| 99 | + delayedPlay: function (delay, key, startFrame) |
| 100 | + { |
| 101 | + this.play(key, true, startFrame); |
| 102 | + |
| 103 | + this.nextTick += (delay * 1000); |
| 104 | + |
| 105 | + return this; |
| 106 | + }, |
| 107 | + |
| 108 | + getCurrentKey: function () |
| 109 | + { |
| 110 | + if (this.currentAnim) |
| 111 | + { |
| 112 | + return this.currentAnim.key; |
| 113 | + } |
| 114 | + }, |
| 115 | + |
| 116 | + load: function (key, startFrame) |
| 117 | + { |
| 118 | + if (startFrame === undefined) { startFrame = 0; } |
| 119 | + |
| 120 | + if (this.isPlaying) |
| 121 | + { |
| 122 | + this.stop(); |
| 123 | + } |
| 124 | + |
| 125 | + // Load the new animation in |
| 126 | + this.animationManager.load(this, key, startFrame); |
| 127 | + |
| 128 | + return this; |
| 129 | + }, |
| 130 | + |
| 131 | + pause: function (atFrame) |
| 132 | + { |
| 133 | + if (!this._paused) |
| 134 | + { |
| 135 | + this._paused = true; |
| 136 | + this._wasPlaying = this.isPlaying; |
| 137 | + this.isPlaying = false; |
| 138 | + } |
| 139 | + |
| 140 | + if (atFrame !== undefined) |
| 141 | + { |
| 142 | + this.updateFrame(atFrame); |
| 143 | + } |
| 144 | + |
| 145 | + return this; |
| 146 | + }, |
| 147 | + |
| 148 | + paused: function (value) |
| 149 | + { |
| 150 | + if (value !== undefined) |
| 151 | + { |
| 152 | + // Setter |
| 153 | + if (value) |
| 154 | + { |
| 155 | + return this.pause(); |
| 156 | + } |
| 157 | + else |
| 158 | + { |
| 159 | + return this.resume(); |
| 160 | + } |
| 161 | + } |
| 162 | + else |
| 163 | + { |
| 164 | + return this._paused; |
| 165 | + } |
| 166 | + }, |
| 167 | + |
| 168 | + play: function (key, ignoreIfPlaying, startFrame) |
| 169 | + { |
| 170 | + if (ignoreIfPlaying === undefined) { ignoreIfPlaying = false; } |
| 171 | + if (startFrame === undefined) { startFrame = 0; } |
| 172 | + |
| 173 | + if (ignoreIfPlaying && this.isPlaying && this.currentAnim.key === key) |
| 174 | + { |
| 175 | + return this; |
| 176 | + } |
| 177 | + |
| 178 | + this.load(key, startFrame); |
| 179 | + |
| 180 | + var anim = this.currentAnim; |
| 181 | + |
| 182 | + // Should give us 9,007,199,254,740,991 safe repeats |
| 183 | + this.repeatCounter = (this._repeat === -1) ? Number.MAX_SAFE_INTEGER : this._repeat; |
| 184 | + |
| 185 | + anim.getFirstTick(this); |
| 186 | + |
| 187 | + this.forward = true; |
| 188 | + this.isPlaying = true; |
| 189 | + this.pendingRepeat = false; |
| 190 | + |
| 191 | + if (anim.showOnStart) |
| 192 | + { |
| 193 | + this.parent.visible = true; |
| 194 | + } |
| 195 | + |
| 196 | + if (anim.onStart) |
| 197 | + { |
| 198 | + anim.onStart.apply(anim.callbackScope, this._callbackArgs.concat(anim.onStartParams)); |
| 199 | + } |
| 200 | + |
| 201 | + return this; |
| 202 | + }, |
| 203 | + |
| 204 | + // Value between 0 and 1. How far this animation is through, ignoring repeats and yoyos. |
| 205 | + // If the animation has a non-zero repeat defined, progress and totalProgress will be different |
| 206 | + // because progress doesn't include any repeats or repeatDelays whereas totalProgress does. |
| 207 | + progress: function (value) |
| 208 | + { |
| 209 | + if (value === undefined) |
| 210 | + { |
| 211 | + var p = this.currentFrame.progress; |
| 212 | + |
| 213 | + if (!this.forward) |
| 214 | + { |
| 215 | + p = 1 - p; |
| 216 | + } |
| 217 | + |
| 218 | + return p; |
| 219 | + } |
| 220 | + else |
| 221 | + { |
| 222 | + // TODO: Set progress |
| 223 | + |
| 224 | + return this; |
| 225 | + } |
| 226 | + }, |
| 227 | + |
| 228 | + remove: function (event) |
| 229 | + { |
| 230 | + if (event === undefined) { event = this.currentAnim; } |
| 231 | + |
| 232 | + if (this.isPlaying && event.key === this.currentAnim.key) |
| 233 | + { |
| 234 | + this.stop(); |
| 235 | + |
| 236 | + var sprite = this.parent; |
| 237 | + var frame = this.currentAnim.frames[0]; |
| 238 | + |
| 239 | + this.currentFrame = frame; |
| 240 | + |
| 241 | + sprite.texture = frame.frame.texture; |
| 242 | + sprite.frame = frame.frame; |
| 243 | + } |
| 244 | + }, |
| 245 | + |
| 246 | + // Gets or sets the number of times that the animation should repeat |
| 247 | + // after its first iteration. For example, if repeat is 1, the animation will |
| 248 | + // play a total of twice (the initial play plus 1 repeat). |
| 249 | + // To repeat indefinitely, use -1. repeat should always be an integer. |
| 250 | + |
| 251 | + repeat: function (value) |
| 252 | + { |
| 253 | + if (value === undefined) |
| 254 | + { |
| 255 | + return this._repeat; |
| 256 | + } |
| 257 | + else |
| 258 | + { |
| 259 | + this._repeat = value; |
| 260 | + this.repeatCounter = 0; |
| 261 | + |
| 262 | + return this; |
| 263 | + } |
| 264 | + }, |
| 265 | + |
| 266 | + // Gets or sets the amount of time in seconds between repeats. |
| 267 | + // For example, if repeat is 2 and repeatDelay is 1, the animation will play initially, |
| 268 | + // then wait for 1 second before it repeats, then play again, then wait 1 second again |
| 269 | + // before doing its final repeat. |
| 270 | + |
| 271 | + repeatDelay: function (value) |
| 272 | + { |
| 273 | + if (value === undefined) |
| 274 | + { |
| 275 | + return this._repeatDelay; |
| 276 | + } |
| 277 | + else |
| 278 | + { |
| 279 | + this._repeatDelay = value; |
| 280 | + |
| 281 | + return this; |
| 282 | + } |
| 283 | + }, |
| 284 | + |
| 285 | + restart: function (includeDelay) |
| 286 | + { |
| 287 | + if (includeDelay === undefined) { includeDelay = false; } |
| 288 | + |
| 289 | + this.currentAnim.getFirstTick(this, includeDelay); |
| 290 | + |
| 291 | + this.forward = true; |
| 292 | + this.isPlaying = true; |
| 293 | + this.pendingRepeat = false; |
| 294 | + this._paused = false; |
| 295 | + |
| 296 | + // Set frame |
| 297 | + this.updateFrame(this.currentAnim.frames[0]); |
| 298 | + |
| 299 | + return this; |
| 300 | + }, |
| 301 | + |
| 302 | + resume: function (fromFrame) |
84 | 303 | { |
| 304 | + if (this._paused) |
| 305 | + { |
| 306 | + this._paused = false; |
| 307 | + this.isPlaying = this._wasPlaying; |
| 308 | + } |
| 309 | + |
| 310 | + if (fromFrame !== undefined) |
| 311 | + { |
| 312 | + this.updateFrame(fromFrame); |
| 313 | + } |
| 314 | + |
| 315 | + return this; |
| 316 | + }, |
85 | 317 |
|
| 318 | + stop: function (dispatchCallbacks) |
| 319 | + { |
| 320 | + if (dispatchCallbacks === undefined) { dispatchCallbacks = false; } |
| 321 | + |
| 322 | + this.isPlaying = false; |
| 323 | + |
| 324 | + var anim = this.currentAnim; |
| 325 | + |
| 326 | + if (dispatchCallbacks && anim.onComplete) |
| 327 | + { |
| 328 | + anim.onComplete.apply(anim.callbackScope, this._callbackArgs.concat(anim.onCompleteParams)); |
| 329 | + } |
| 330 | + |
| 331 | + return this; |
| 332 | + }, |
| 333 | + |
| 334 | + timeScale: function (value) |
| 335 | + { |
| 336 | + if (value === undefined) |
| 337 | + { |
| 338 | + return this._timeScale; |
| 339 | + } |
| 340 | + else |
| 341 | + { |
| 342 | + this._timeScale = value; |
| 343 | + |
| 344 | + return this; |
| 345 | + } |
86 | 346 | }, |
87 | 347 |
|
88 | | - delay: Components.Delay, |
89 | | - delayedPlay: Components.DelayedPlay, |
90 | | - getCurrentKey: Components.GetCurrentKey, |
91 | | - load: Components.Load, |
92 | | - pause: Components.Pause, |
93 | | - paused: Components.Paused, |
94 | | - play: Components.Play, |
95 | | - progress: Components.Progress, |
96 | | - remove: Components.Remove, |
97 | | - repeat: Components.Repeat, |
98 | | - repeatDelay: Components.RepeatDelay, |
99 | | - restart: Components.Restart, |
100 | | - resume: Components.Resume, |
101 | | - stop: Components.Stop, |
102 | | - timeScale: Components.TimeScale, |
103 | | - totalFrames: Components.TotalFrames, |
104 | | - totalProgress: Components.TotalProgress, |
105 | | - update: Components.Update, |
106 | | - updateFrame: Components.UpdateFrame, |
107 | | - yoyo: Components.Yoyo |
| 348 | + totalFrames: function () |
| 349 | + { |
| 350 | + return this.currentAnim.frames.length; |
| 351 | + }, |
| 352 | + |
| 353 | + // Value between 0 and 1. How far this animation is through, including things like delays |
| 354 | + // repeats, custom frame durations, etc. If the animation is set to repeat -1 it can never |
| 355 | + // have a duration, therefore this will return -1. |
| 356 | + totalProgres: function () |
| 357 | + { |
| 358 | + // TODO |
| 359 | + }, |
| 360 | + |
| 361 | + update: function (timestamp, delta) |
| 362 | + { |
| 363 | + if (!this.isPlaying || this.currentAnim.paused) |
| 364 | + { |
| 365 | + return; |
| 366 | + } |
| 367 | + |
| 368 | + this.accumulator += delta * this._timeScale; |
| 369 | + |
| 370 | + if (this.accumulator >= this.nextTick) |
| 371 | + { |
| 372 | + this.currentAnim.setFrame(this); |
| 373 | + } |
| 374 | + }, |
| 375 | + |
| 376 | + updateFrame: function (animationFrame) |
| 377 | + { |
| 378 | + var sprite = this.parent; |
| 379 | + |
| 380 | + this.currentFrame = animationFrame; |
| 381 | + |
| 382 | + sprite.texture = animationFrame.frame.texture; |
| 383 | + sprite.frame = animationFrame.frame; |
| 384 | + |
| 385 | + if (this.isPlaying) |
| 386 | + { |
| 387 | + if (animationFrame.setAlpha) |
| 388 | + { |
| 389 | + sprite.alpha = animationFrame.alpha; |
| 390 | + } |
| 391 | + |
| 392 | + var anim = this.currentAnim; |
| 393 | + |
| 394 | + if (anim.onUpdate) |
| 395 | + { |
| 396 | + anim.onUpdate.apply(anim.callbackScope, this._updateParams); |
| 397 | + } |
| 398 | + |
| 399 | + if (animationFrame.onUpdate) |
| 400 | + { |
| 401 | + animationFrame.onUpdate(sprite, animationFrame); |
| 402 | + } |
| 403 | + } |
| 404 | + }, |
| 405 | + |
| 406 | + yoyo: function (value) |
| 407 | + { |
| 408 | + if (value === undefined) |
| 409 | + { |
| 410 | + return this._yoyo; |
| 411 | + } |
| 412 | + else |
| 413 | + { |
| 414 | + this._yoyo = value; |
| 415 | + |
| 416 | + return this; |
| 417 | + } |
| 418 | + }, |
| 419 | + |
| 420 | + destroy: function () |
| 421 | + { |
| 422 | + // TODO |
| 423 | + } |
108 | 424 |
|
109 | 425 | }); |
110 | 426 |
|
|
0 commit comments