Skip to content

Commit

Permalink
optimize engine startup time by removing some js.mixin
Browse files Browse the repository at this point in the history
  • Loading branch information
jareguo committed May 5, 2017
1 parent 6c75eb0 commit 886c8d8
Show file tree
Hide file tree
Showing 24 changed files with 5,072 additions and 5,094 deletions.
265 changes: 132 additions & 133 deletions cocos2d/animation/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ var WrapMode = cc.Enum({
cc.WrapMode = WrapMode;

// For internal
var WrappedInfo = function (info) {
function WrappedInfo (info) {
if (info) {
this.set(info);
return;
Expand All @@ -84,7 +84,7 @@ var WrappedInfo = function (info) {
this.direction = 1;
this.stopped = true;
this.iterations = 0;
};
}

WrappedInfo.prototype.set = function (info) {
for (var k in info) {
Expand All @@ -99,9 +99,9 @@ WrappedInfo.prototype.set = function (info) {
*
* @extends Playable
*/
var AnimationNodeBase = function () {
function AnimationNodeBase () {
Playable.call(this);
};
}
JS.extend(AnimationNodeBase, Playable);

/**
Expand Down Expand Up @@ -257,167 +257,166 @@ function AnimationNode (animator, curves, timingInput) {
}
JS.extend(AnimationNode, AnimationNodeBase);

JS.mixin(AnimationNode.prototype, {
var proto = AnimationNode.prototype;

update: function (delta) {
proto.update = function (delta) {

// calculate delay time
// calculate delay time

if (this._duringDelay) {
this._timeNoScale += delta;
if (this._timeNoScale < this.delay) {
// still waiting
return;
}
else {
this._duringDelay = false;
}
//// start play
// delta -= (this._timeNoScale - this.delay);
}

// make first frame perfect

//var playPerfectFirstFrame = (this.time === 0);
if (this._firstFramePlayed) {
this.time += (delta * this.speed);
if (this._duringDelay) {
this._timeNoScale += delta;
if (this._timeNoScale < this.delay) {
// still waiting
return;
}
else {
this._firstFramePlayed = true;
this._duringDelay = false;
}
//// start play
// delta -= (this._timeNoScale - this.delay);
}

// sample
var info = this.sample();
// make first frame perfect

if (!this._lastWrappedInfo) {
this._lastWrappedInfo = new WrappedInfo(info);
}
//var playPerfectFirstFrame = (this.time === 0);
if (this._firstFramePlayed) {
this.time += (delta * this.speed);
}
else {
this._firstFramePlayed = true;
}

var anotherIteration = (info.iterations | 0) > (this._lastWrappedInfo.iterations | 0);
if (this.repeatCount > 1 && anotherIteration) {
if ((this.wrapMode & WrapModeMask.Reverse) === WrapModeMask.Reverse) {
if (this._lastWrappedInfo.direction < 0) {
this.emit('lastframe', this);
}
}
else {
if (this._lastWrappedInfo.direction > 0) {
this.emit('lastframe', this);
}
// sample
var info = this.sample();

if (!this._lastWrappedInfo) {
this._lastWrappedInfo = new WrappedInfo(info);
}

var anotherIteration = (info.iterations | 0) > (this._lastWrappedInfo.iterations | 0);
if (this.repeatCount > 1 && anotherIteration) {
if ((this.wrapMode & WrapModeMask.Reverse) === WrapModeMask.Reverse) {
if (this._lastWrappedInfo.direction < 0) {
this.emit('lastframe', this);
}
}

if (info.stopped) {
this.stop();
this.emit('finished', this);
else {
if (this._lastWrappedInfo.direction > 0) {
this.emit('lastframe', this);
}
}
}

this._lastWrappedInfo.set(info);
},
if (info.stopped) {
this.stop();
this.emit('finished', this);
}

_needRevers: function (currentIterations) {
var wrapMode = this.wrapMode;
var needRevers = false;
this._lastWrappedInfo.set(info);
};

if ((wrapMode & WrapModeMask.PingPong) === WrapModeMask.PingPong) {
var isEnd = currentIterations - (currentIterations | 0) === 0;
if (isEnd && (currentIterations > 0)) {
currentIterations -= 1;
}
proto._needRevers = function (currentIterations) {
var wrapMode = this.wrapMode;
var needRevers = false;

var isOddIteration = currentIterations & 1;
if (isOddIteration) {
needRevers = !needRevers;
}
if ((wrapMode & WrapModeMask.PingPong) === WrapModeMask.PingPong) {
var isEnd = currentIterations - (currentIterations | 0) === 0;
if (isEnd && (currentIterations > 0)) {
currentIterations -= 1;
}
if ((wrapMode & WrapModeMask.Reverse) === WrapModeMask.Reverse) {

var isOddIteration = currentIterations & 1;
if (isOddIteration) {
needRevers = !needRevers;
}
return needRevers;
},
}
if ((wrapMode & WrapModeMask.Reverse) === WrapModeMask.Reverse) {
needRevers = !needRevers;
}
return needRevers;
};

getWrappedInfo: function (time, info) {
info = info || new WrappedInfo();

var stopped = false;
var duration = this.duration;
var ratio = 0;
var wrapMode = this.wrapMode;
proto.getWrappedInfo = function (time, info) {
info = info || new WrappedInfo();

var currentIterations = Math.abs(time / duration);
if (currentIterations > this.repeatCount) currentIterations = this.repeatCount;
var stopped = false;
var duration = this.duration;
var ratio = 0;
var wrapMode = this.wrapMode;

var needRevers = false;
if (wrapMode & WrapModeMask.ShouldWrap) {
needRevers = this._needRevers(currentIterations);
}
var currentIterations = Math.abs(time / duration);
if (currentIterations > this.repeatCount) currentIterations = this.repeatCount;

var direction = needRevers ? -1 : 1;
if (this.speed < 0) direction *= -1;
var needRevers = false;
if (wrapMode & WrapModeMask.ShouldWrap) {
needRevers = this._needRevers(currentIterations);
}

if (currentIterations >= this.repeatCount) {
stopped = true;
var tempRatio = this.repeatCount - (this.repeatCount | 0);
if (tempRatio === 0) {
tempRatio = 1; // 如果播放过,动画不复位
}
time = tempRatio * duration * (time > 0 ? 1 : -1);
}
var direction = needRevers ? -1 : 1;
if (this.speed < 0) direction *= -1;

if (time > duration) {
var tempTime = time % duration;
time = tempTime === 0 ? duration : tempTime;
}
else if (time < 0) {
time = time % duration;
if (time !== 0 ) time += duration;
if (currentIterations >= this.repeatCount) {
stopped = true;
var tempRatio = this.repeatCount - (this.repeatCount | 0);
if (tempRatio === 0) {
tempRatio = 1; // 如果播放过,动画不复位
}
time = tempRatio * duration * (time > 0 ? 1 : -1);
}

// calculate wrapped time
if (wrapMode & WrapModeMask.ShouldWrap) {
if (needRevers) time = duration - time;
}
if (time > duration) {
var tempTime = time % duration;
time = tempTime === 0 ? duration : tempTime;
}
else if (time < 0) {
time = time % duration;
if (time !== 0 ) time += duration;
}

ratio = time / duration;
// calculate wrapped time
if (wrapMode & WrapModeMask.ShouldWrap) {
if (needRevers) time = duration - time;
}

info.ratio = ratio;
info.time = time;
info.direction = direction;
info.stopped = stopped;
info.iterations = currentIterations;
ratio = time / duration;

return info;
},
info.ratio = ratio;
info.time = time;
info.direction = direction;
info.stopped = stopped;
info.iterations = currentIterations;

sample: function () {
var info = this.getWrappedInfo(this.time, this._wrappedInfo);
var curves = this.curves;
for (var i = 0, len = curves.length; i < len; i++) {
var curve = curves[i];
curve.sample(info.time, info.ratio, this);
}
return info;
};

return info;
},
proto.sample = function () {
var info = this.getWrappedInfo(this.time, this._wrappedInfo);
var curves = this.curves;
for (var i = 0, len = curves.length; i < len; i++) {
var curve = curves[i];
curve.sample(info.time, info.ratio, this);
}

onStop: function () {
this.emit('stop', this);
},
return info;
};

onPlay: function () {
this.emit('play', this);
},
proto.onStop = function () {
this.emit('stop', this);
};

onPause: function () {
this.emit('pause', this);
},
proto.onPlay = function () {
this.emit('play', this);
};

onResume: function () {
this.emit('resume', this);
}
});
proto.onPause = function () {
this.emit('pause', this);
};

proto.onResume = function () {
this.emit('resume', this);
};

cc.defineGetterSetter(AnimationNode.prototype, 'wrapMode',
JS.getset(proto, 'wrapMode',
function () {
return this._wrapMode;
},
Expand All @@ -438,13 +437,13 @@ cc.defineGetterSetter(AnimationNode.prototype, 'wrapMode',
}
);

cc.js.mixin(AnimationNode.prototype, cc.EventTarget.prototype);
cc.js.mixin(proto, cc.EventTarget.prototype);

cc.AnimationNode = AnimationNode;

module.exports = {
WrapModeMask: WrapModeMask,
WrapMode: WrapMode,
AnimationNode: AnimationNode,
WrappedInfo: WrappedInfo
WrapModeMask,
WrapMode,
AnimationNode,
WrappedInfo
};
Loading

0 comments on commit 886c8d8

Please sign in to comment.