1 /**************************************************************************** 2 Copyright (c) 2010-2012 cocos2d-x.org 3 4 http://www.cocos2d-x.org 5 6 Permission is hereby granted, free of charge, to any person obtaining a copy 7 of this software and associated documentation files (the "Software"), to deal 8 in the Software without restriction, including without limitation the rights 9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 copies of the Software, and to permit persons to whom the Software is 11 furnished to do so, subject to the following conditions: 12 13 The above copyright notice and this permission notice shall be included in 14 all copies or substantial portions of the Software. 15 16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 THE SOFTWARE. 23 ****************************************************************************/ 24 25 26 //animation type 27 /** 28 * the animation just have one frame 29 * @constant 30 * @type {number} 31 */ 32 CC_ANIMATION_TYPE_SINGLE_FRAME = -4; 33 /** 34 * the animation isn't loop 35 * @constant 36 * @type {number} 37 */ 38 CC_ANIMATION_TYPE_NO_LOOP = -3; 39 /** 40 * the animation to loop from front 41 * @constant 42 * @type {number} 43 */ 44 CC_ANIMATION_TYPE_TO_LOOP_FRONT = -2; 45 /** 46 * the animation to loop from back 47 * @constant 48 * @type {number} 49 */ 50 CC_ANIMATION_TYPE_TO_LOOP_BACK = -1; 51 /** 52 * the animation loop from front 53 * @constant 54 * @type {number} 55 */ 56 CC_ANIMATION_TYPE_LOOP_FRONT = 0; 57 /** 58 * the animation loop from back 59 * @constant 60 * @type {number} 61 */ 62 CC_ANIMATION_TYPE_LOOP_BACK = 1; 63 /** 64 * the animation max 65 * @constant 66 * @type {number} 67 */ 68 CC_ANIMATION_TYPE_MAX = 2; 69 70 /** 71 * Base class for ccs.ProcessBase objects. 72 * @class 73 * @extends ccs.Class 74 */ 75 ccs.ProcessBase = ccs.Class.extend(/** @lends ccs.ProcessBase# */{ 76 _processScale:1, 77 _isComplete:true, 78 _isPause:true, 79 _isPlaying:false, 80 _currentPercent:0.0, 81 _rawDuration:0, 82 _loopType:0, 83 _tweenEasing:0, 84 _animationInternal:null, 85 _currentFrame:0, 86 _durationTween:0, 87 _nextFrameIndex:0, 88 _curFrameIndex:null, 89 _isLoopBack:false, 90 ctor:function () { 91 this._processScale = 1; 92 this._isComplete = true; 93 this._isPause = true; 94 this._isPlaying = false; 95 this._currentFrame = 0; 96 this._currentPercent = 0.0; 97 this._durationTween = 0; 98 this._rawDuration = 0; 99 this._loopType = CC_ANIMATION_TYPE_LOOP_BACK; 100 this._tweenEasing = ccs.TweenType.linear; 101 this._animationInternal = 1/60; 102 this._curFrameIndex = 0; 103 this._durationTween = 0; 104 this._isLoopBack = false; 105 }, 106 107 /** 108 * Pause the Process 109 */ 110 pause:function () { 111 this._isPause = true; 112 this._isPlaying = false; 113 }, 114 115 /** 116 * Resume the Process 117 */ 118 resume:function () { 119 this._isPause = false; 120 this._isPlaying = true; 121 }, 122 123 /** 124 * Stop the Process 125 */ 126 stop:function () { 127 this._isComplete = true; 128 this._isPlaying = false; 129 }, 130 131 /** 132 * Play the Process 133 * @param {Number} durationTo 134 * @param {ccs.TweenType} tweenEasing 135 */ 136 play:function (durationTo, tweenEasing) { 137 this._isComplete = false; 138 this._isPause = false; 139 this._isPlaying = true; 140 this._currentFrame = 0; 141 this._nextFrameIndex = durationTo; 142 this._tweenEasing = tweenEasing; 143 }, 144 145 update:function (dt) { 146 if (this._isComplete || this._isPause) { 147 return false; 148 } 149 if (this._rawDuration <= 0) { 150 return false; 151 } 152 var locNextFrameIndex = this._nextFrameIndex; 153 var locCurrentFrame = this._currentFrame; 154 if (locNextFrameIndex <= 0) { 155 this._currentPercent = 1; 156 locCurrentFrame = 0; 157 }else{ 158 /* 159 * update currentFrame, every update add the frame passed. 160 * dt/this._animationInternal determine it is not a frame animation. If frame speed changed, it will not make our 161 * animation speed slower or quicker. 162 */ 163 locCurrentFrame += this._processScale * (dt / this._animationInternal); 164 165 this._currentPercent = locCurrentFrame / locNextFrameIndex; 166 167 /* 168 * if currentFrame is bigger or equal than this._nextFrameIndex, then reduce it util currentFrame is 169 * smaller than this._nextFrameIndex 170 */ 171 locCurrentFrame = ccs.fmodf(locCurrentFrame, locNextFrameIndex); 172 } 173 this._currentFrame = locCurrentFrame 174 this.updateHandler(); 175 return true; 176 }, 177 178 /** 179 * update will call this handler, you can handle your logic here 180 */ 181 updateHandler:function () { 182 //override 183 }, 184 185 /** 186 * goto frame 187 * @param {Number} frameIndex 188 */ 189 gotoFrame:function (frameIndex) { 190 var locLoopType = this._loopType; 191 if (locLoopType == CC_ANIMATION_TYPE_NO_LOOP) { 192 locLoopType = CC_ANIMATION_TYPE_MAX; 193 } 194 else if (locLoopType == CC_ANIMATION_TYPE_TO_LOOP_FRONT) { 195 locLoopType = CC_ANIMATION_TYPE_LOOP_FRONT; 196 } 197 this._loopType = locLoopType; 198 this._curFrameIndex = frameIndex; 199 this._nextFrameIndex = this._durationTween; 200 }, 201 202 /** 203 * get currentFrameIndex 204 * @return {Number} 205 */ 206 getCurrentFrameIndex:function () { 207 this._curFrameIndex = (this._rawDuration-1) * this._currentPercent; 208 return this._curFrameIndex; 209 }, 210 211 /** 212 * whether the animation is pause 213 * @returns {boolean} 214 */ 215 isPause:function () { 216 return this._isPause; 217 }, 218 219 /** 220 * whether the animation is complete 221 * @returns {boolean} 222 */ 223 isComplete:function () { 224 return this._isComplete; 225 }, 226 227 /** 228 * current percent getter 229 * @returns {number} 230 */ 231 getCurrentPercent:function () { 232 return this._currentPercent; 233 }, 234 235 /** 236 * rawDuration getter 237 * @returns {number} 238 */ 239 getRawDuration:function () { 240 return this._rawDuration; 241 }, 242 243 /** 244 * loop type getter 245 * @returns {number} 246 */ 247 getLoop:function () { 248 return this._loopType; 249 }, 250 251 /** 252 * tween easing getter 253 * @returns {number} 254 */ 255 getTweenEasing:function () { 256 return this._tweenEasing; 257 }, 258 259 /** 260 * animationInternal getter 261 * @returns {number} 262 */ 263 getAnimationInternal:function () { 264 return this._animationInternal; 265 }, 266 267 /** 268 * animationInternal setter 269 * @param animationInternal 270 */ 271 setAnimationInternal:function(animationInternal){ 272 this._animationInternal = animationInternal; 273 }, 274 275 /** 276 * process scale getter 277 * @returns {number} 278 */ 279 getProcessScale:function () { 280 return this._processScale; 281 }, 282 283 /** 284 * process scale setter 285 * @param processScale 286 */ 287 setProcessScale:function (processScale) { 288 this._processScale = processScale; 289 }, 290 291 /** 292 * whether the animation is playing 293 * @returns {boolean} 294 */ 295 isPlaying:function () { 296 return this._isPlaying; 297 } 298 }); 299