Last active
December 20, 2015 14:49
-
-
Save 6174/6149621 to your computer and use it in GitHub Desktop.
极其简陋的 js 游戏基础frame -----_-----
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
* @class Animation | |
*/ | |
KISSY.add('fp-dsf/CatGameAnim', function(S, Event, util, Ticker, Tween) { | |
var nullf = function() {}; | |
//--get delta data of two attributes | |
function getAttrDelta(a, b){ | |
if(!b) b = 0; | |
return Number(a) - Number(b); | |
} | |
//--return easing function | |
function getEasingFunc(easingStr){ | |
var reg = /[|:-]/g; | |
if(Tween[easingStr]) return Tween[easingStr]; | |
var a = easingStr.split(reg); | |
if(Tween[a[0]][a[1]]) return Tween[a[0]][a[1]]; | |
return Tween['linear']; | |
} | |
/** | |
* @Class Anim | |
*/ | |
var Anim = util.Klass(null,{ | |
__construct: function (target, duration, props, easing) { | |
var self = this; | |
util.mix(self, S.EventTarget); | |
self.target = target; | |
self.originStyle = util.mix({}, target.style); | |
self.props = props; | |
self.canRun = false; | |
self.started = false; | |
self.stoped = false; | |
self.startTime = 0; | |
//get delta props | |
self.deltaProps = {}; | |
for (attr in props){ | |
if (props.hasOwnProperty(attr)){ | |
self.deltaProps[attr] = getAttrDelta(props[attr] - self.originStyle[attr]) | |
} | |
} | |
self.duration = duration || 0; | |
self.easing = getEasingFunc(easing); | |
}, | |
step: function() { | |
var self = this; | |
var style = self.target.style; | |
var originStyle = self.originStyle; | |
var deltaProps = self.deltaProps; | |
var easing = self.easing; | |
var duration = self.duration; | |
var currentTime = (+new Date() - self.startTime) / 1000; | |
if(self.canRun) { | |
if(currentTime >= duration){ | |
self._finish(); | |
return; | |
} | |
for(attr in deltaProps){ | |
style[attr] = easing(currentTime, originStyle[attr], deltaProps[attr], duration); | |
} | |
self.fire('update'); | |
} | |
}, | |
_finish: function() { | |
var self = this; | |
self.canRun = false; | |
self.started = false; | |
self.stoped = true; | |
self.fire('end'); | |
Ticker.detach('tick', self._proxiedFunc); | |
}, | |
run: function() { | |
var self = this; | |
self.canRun = true; | |
self.started = true; | |
self.startTime = +new Date(); | |
self._proxiedFunc = util.proxy(self, self.step); | |
Ticker.on('tick', self._proxiedFunc); | |
return self; | |
} | |
}); | |
return Anim; | |
}, { | |
requires: [ | |
'event', 'fp-dsf/CatGameUtil', 'fp-dsf/CatGameTicker', 'fp-dsf/Tween' | |
] | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @Module CatGameAssetsManager | |
* | |
* @Fan-outs: Event, DOM | |
*/ | |
KISSY.add('fp-dsf/CatGameAssetsManager', function(S, util) { | |
var CatGameAssetsManager = {}; | |
var imageList = [ | |
{ | |
id: "icons", src: "img/icons-high.png" | |
}]; | |
//extend --object attributes | |
CatGameAssetsManager.loadCatGameImage = function(conf) { | |
var self = this; | |
self.imageCache = loadImage(imageList, conf); | |
} | |
CatGameAssetsManager.getImg = function(id){ | |
var self = this; | |
if(self.imageCache[id]) return self.imageCache[id]; | |
return null; | |
} | |
function loadImage(imgList, conf) { | |
var imgs = {}; | |
var totalCount = imgList.length; | |
var loadedCount = 0; | |
var checkIntervalId = 0; | |
for (var i = 0; i < totalCount; i++) { | |
var img = imgList[i]; | |
var image = imgs[img.id] = new Image(); | |
image.src = img.src; | |
image.onload = function(event) { | |
loadedCount++; | |
if (util.isF(conf.onProgress)) conf.onProgress.call({}, loadedCount, totalCount); | |
} | |
} | |
function check() { | |
if (loadedCount >= totalCount) { | |
if(util.isF(conf.onSuccess)) conf.onSuccess.call({}, loadedCount, totalCount); | |
} else { | |
checkIntervalId = setTimeout(check, 100); | |
} | |
} | |
//set timeout check loaded count | |
check(); | |
//max time for loading | |
setTimeout(function() { | |
clearTimeout(checkIntervalId); | |
if(util.isF(conf.onFail)) conf.onFail.call({}, loadedCount, totalCount); | |
}, conf.maxTime || 1000 * 120); | |
return imgs; | |
} | |
return CatGameAssetsManager; | |
}, { | |
requires: [ | |
'fp-dsf/CatGameUtil' | |
] | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @CatGameFrameManager | |
* sprite frame manager | |
*/ | |
KISSY.add('fp-dsf/CatGameFrameManager', function(S, DOM, Event, util) { | |
var nullf = function() {}; | |
var nulls = ''; | |
/** | |
* @class CatGameFrameManager | |
*/ | |
var CatGameFrameManager = util.Klass({}, { | |
__construct: function(conf){ | |
var self = this; | |
util.mix(self, { | |
//img source with all frames on the img | |
img: null, | |
//frames on the img | |
frames: [], | |
//frame count | |
frameCount: 0, | |
//current frame | |
currentFrame: null, | |
//frame played | |
currentFrameIndex: -1, | |
//frame played timecounter | |
currentFramePlayed: -1, | |
inversion: false | |
}); | |
util.mix(self, conf); | |
self.frames = self.frames || []; | |
self.frameCount = self.frames.length; | |
//set the startframe | |
self._setFrame(0); | |
}, | |
/** | |
* @method setFrame | |
*/ | |
_setFrame: function(index) { | |
var self = this; | |
self.currentFrameIndex = index; | |
self.currentFrame = self.frames[self.currentFrameIndex]; | |
self.currentFramePlayed = 0 | |
}, | |
/** | |
* @method getCurrentFrame | |
*/ | |
getCurrentFrame: function() { | |
var self = this; | |
return self.currentFrame; | |
}, | |
/** | |
* @method update | |
*/ | |
update: function(deltaTime) { | |
var self = this; | |
if (self.currentFramePlayed >= self.currentFrame[4]) { | |
//then play next frame | |
self.currentFrameIndex++; | |
if (self.currentFrameIndex >= self.frameCount) { | |
self.currentFrameIndex = 0; | |
} | |
self._setFrame(self.currentFrameIndex); | |
} else { | |
self.currentFramePlayed += deltaTime; | |
} | |
} | |
}); | |
return CatGameFrameManager; | |
}, { | |
requires: [ | |
'dom', 'event', 'fp-dsf/CatGameUtil' | |
] | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @Module CatGameNode | |
*/ | |
KISSY.add('fp-dsf/CatGameNode', function(S, DOM, Event, util) { | |
//empty function , object, array, string | |
var nullf = function() {}; | |
var nulls = ''; | |
/** | |
* @class CatGameNode composite pattern | |
* just like document div elment | |
* the style has the same interface | |
*/ | |
var CatGameNode = util.Klass(null, { | |
/** | |
* @constructor | |
* @param {object} -- conf | |
* --style | |
*/ | |
__construct: function(conf){ | |
var self = this; | |
// console.log('__construct node'); | |
self._id = util.guid(); | |
self.data = {}; | |
self.nodes = {}; | |
self.style = { | |
transform: null, | |
width: 0, | |
height: 0, | |
top: 0, | |
left: 0, | |
backgroundImage: null, | |
backgroundPosition: [], | |
backgroundClip: [], | |
overflow: 'hidden', | |
display: 'block' | |
} | |
util.mix(self, conf); | |
}, | |
/** | |
* @method toImage | |
* change this node in to an pixels data | |
*/ | |
toImage: function() { | |
}, | |
_render: function(ctx, deltaTime) { | |
var self = this; | |
// util.log("@" + self._id + " is rendring " + deltaTime); | |
if(self.type === 'sprite'){ | |
self.update(deltaTime); | |
self._renderInCanvas(ctx); | |
// util.log('not node container'); | |
return; | |
} | |
// //render all children | |
for (it in self.nodes) | |
if (self.nodes.hasOwnProperty(it)) { | |
self.nodes[it]._render(ctx, deltaTime); | |
} | |
}, | |
/** | |
* @method _renderInCanvas | |
* @param {object} ctx --- canvas ctx | |
* draw current style on the canvas | |
*/ | |
_renderInCanvas: function(ctx) { | |
var self = this; | |
var f = self.style.backgroundClip; | |
var img = self.style.backgroundImage; | |
var x = self.style.left; | |
var y = self.style.top; | |
var scale = self.style.scale; | |
window.ctx = ctx; | |
window.f = f; | |
window.img = img; | |
if (scale) { | |
ctx.drawImage(img, f[0], f[1], f[2], f[3], x - (f[2] / 2) * scale, y - (f[3] / 2) * scale, f[2] * scale, f[3] * this.scale); | |
} else { | |
ctx.drawImage(img, f[0], f[1], f[2], f[3], x - f[2] / 2, y - f[3] / 2, f[2], f[3]); | |
} | |
}, | |
/** | |
* @method _renderInDom | |
*/ | |
_renderInDom: function() { | |
}, | |
/** | |
* @method css | |
*/ | |
css: function(styleObj) { | |
var self = this; | |
util.mix(self.style, styleObj); | |
}, | |
/** | |
* @method translate | |
*/ | |
translate: function(x, y) { | |
var self = this; | |
self.style.left += x; | |
self.style.top += y; | |
return self; | |
}, | |
/** | |
* @method translateTo | |
*/ | |
translateTo: function(x, y) { | |
var self = this; | |
self.style.left = x; | |
self.style.top = y; | |
return self; | |
}, | |
/** | |
* @method isNodeInViewPort | |
*/ | |
_isElInViewPort: function() { | |
var self = this; | |
} | |
}); | |
//---static methods | |
util.mix(CatGameNode, { | |
addChild: function(child, parent) { | |
var cid = child._id; | |
if (!cid) return false; | |
parent.nodes[cid] = child; | |
child.parentNode = parent; | |
return true; | |
}, | |
removeChild: function(child, parent) { | |
var cid = child._id; | |
if (parent[cid]) { | |
delete parent[cid]; | |
delete child.parentNode; | |
return true; | |
} | |
return false; | |
} | |
}); | |
return CatGameNode; | |
}, { | |
requires: [ | |
'dom', 'event', 'fp-dsf/CatGameUtil' | |
] | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @object Anim | |
* | |
* @param {Object} -- currentStyle: the origin style | |
* @param {object} -- duration: duration time | |
* @param {object} -- props: to style object | |
* | |
* @easing{String} -- easing: easing function | |
* @author: xuejia[at]tmall.com | |
* @date: 2013-08-25 | |
*/ | |
KISSY.add("khc/lib/anim", function(S, Event, Ticker) { | |
/** | |
* free的 anim对象的缓存池 | |
*/ | |
var freePool = {}; | |
var freeIdPool = []; | |
/** | |
* 当前激活的anim的引用池 | |
*/ | |
var activePool = {}; | |
//--guid | |
var idCount = 0; | |
/** | |
* @module Anim | |
*/ | |
var Anim = {}; | |
/** | |
* @desc -- 获取一个缓存的动画 | |
*/ | |
Anim._getPooledA = function() { | |
var id = freeIdPool.pop(); | |
var anim = freePool[id]; | |
delete freePool[id]; | |
return anim; | |
} | |
/** | |
* @desc -- 创建新的动画 | |
* -- 如果在缓存池里边有, 那么就使用缓存, 但需要重新设置id | |
*/ | |
Anim.create = function(currentStyle, duration, props, easing) { | |
var self = this; | |
var anim; | |
idCount++; | |
if (freeIdPool.length > 0) { | |
//--获取缓存A | |
anim = self._getPooledA(); | |
//--重新初始化 | |
anim.init(currentStyle, duration, props, easing); | |
} else { | |
//--创建新的A | |
anim = new A(currentStyle, duration, props, easing) | |
} | |
//--生成最新id | |
anim.id = idCount; | |
//--放到激活池中 | |
activePool[idCount] = anim; | |
return anim; | |
}; | |
/** | |
* @desc 释放一个A, 重激活pool里边转换到缓存pool中 | |
*/ | |
Anim.release = function(animId) { | |
var self = this; | |
var anim = activePool[animId]; | |
if(!anim){ | |
return; | |
} | |
//--接触事件绑定 | |
anim.detach(); | |
//--将改id放到缓存池当中 | |
freePool[animId] = anim; | |
freeIdPool.push(animId); | |
//--重激活池塘中删除 | |
delete activePool[animId]; | |
} | |
/** | |
* 通过Anim代理事件注册 | |
*/ | |
Anim.on = function(id, type, callback) { | |
console.log('regist anim ev', id, activePool); | |
if (activePool[id]) { | |
activePool[id].on(type, callback); | |
} | |
} | |
Anim.detach = function(id, type, callback) { | |
if (activePool[id]) { | |
activePool[id].detach(type, callback); | |
} | |
} | |
Anim.cancel = function(id) { | |
if (activePool[id]) { | |
activePool[id].cancel(); | |
Anim.release(id); | |
} | |
} | |
Anim.pause = function(id) { | |
if (activePool[id]) { | |
activePool[id].pause(); | |
} | |
} | |
Anim.resume = function(id) { | |
if (activePool[id]) { | |
activePool[id].resume(); | |
} | |
} | |
/** | |
* @-- Anim到某个属性 | |
*/ | |
Anim.to = function(target, duration, props) { | |
var animProps = props.props || {}; | |
var startProps = props.startProps || {}; | |
var easing = props.easing || "linear"; | |
var callback = props.callback; | |
var updatefunc = props.update; | |
var delay = props.delay || 0; | |
var anim = Anim.create(target, duration, animProps, easing); | |
anim.detach('update'); | |
if (S.isFunction(callback)) { | |
anim.on('end', function() { | |
callback.call(target); | |
}); | |
} | |
if (S.isFunction(updatefunc)) { | |
anim.on('update', function(e){ | |
updatefunc.call(target, e); | |
}); | |
} | |
setTimeout(function() { | |
for (var p in startProps) { | |
target[p] = startProps[p]; | |
} | |
anim.run(); | |
}, delay * 1000); | |
return anim.id; | |
}; | |
/** | |
* @ -- 重某个属性移动过来 | |
*/ | |
Anim.from = function(target, duration, props, easing) { | |
var animProps = props.props; | |
for (var p in animProps) { | |
var temp = target[p]; | |
target[p] = animProps[p]; | |
animProps[p] = temp; | |
} | |
return Anim.to(target, duration, props, easing); | |
}; | |
/** | |
* @Class A | |
* @param {Object} -- currentStyle: the origin style | |
* @param {object} -- duration: duration time | |
* @param {object} -- props: to style object | |
* @easing{String} -- easing: easing function | |
*/ | |
function A(currentStyle, duration, props, easing) { | |
var self = this; | |
self.init(currentStyle, duration, props, easing); | |
} | |
S.augment(A, S.EventTarget, { | |
init: function(currentStyle, duration, props, easing) { | |
var self = this; | |
var target = { | |
style: currentStyle | |
}; | |
self.target = target; | |
self.originStyle = S.mix({}, target.style); | |
self.currentOriginStyle = S.mix({}, target.style); | |
self.props = props; | |
self.canRun = false; | |
self.started = false; | |
self.stoped = false; | |
self.startTime = 0; | |
//get delta props | |
self.deltaProps = {}; | |
for (attr in props) { | |
if (props.hasOwnProperty(attr)) { | |
self.deltaProps[attr] = getAttrDelta(props[attr] - self.currentOriginStyle[attr]) | |
} | |
} | |
self.duration = duration || 0; | |
self.easing = getEasingFunc(easing); | |
}, | |
_setLatestStyle: function() { | |
var self = this; | |
var target = self.target; | |
var props = self.props; | |
self.currentOriginStyle = S.mix({}, target.style); | |
for (attr in props) { | |
if (props.hasOwnProperty(attr)) { | |
self.deltaProps[attr] = getAttrDelta(props[attr] - self.currentOriginStyle[attr]) | |
} | |
} | |
}, | |
step: function() { | |
var self = this; | |
if (self.paused || !self.canRun) return; | |
var style = self.target.style; | |
var currentOriginStyle = self.currentOriginStyle; | |
var deltaProps = self.deltaProps; | |
var easing = self.easing; | |
var duration = self.duration; | |
var currentTime = (+new Date() - self.startTime) / 1000; | |
if (currentTime >= duration) { | |
for (attr in deltaProps) { | |
style[attr] = easing(duration, currentOriginStyle[attr], deltaProps[attr], duration); | |
} | |
self._finish(); | |
return; | |
} | |
for (attr in deltaProps) { | |
style[attr] = easing(currentTime, currentOriginStyle[attr], deltaProps[attr], duration); | |
} | |
self.fire('update', { | |
'props': style, | |
'currentTime': currentTime | |
}); | |
}, | |
_finish: function() { | |
var self = this; | |
self.canRun = false; | |
self.started = false; | |
self.stoped = true; | |
self.fire('end'); | |
Anim.release(self); | |
}, | |
cancel: function() { | |
var self = this; | |
self.canRun = false; | |
self.started = false; | |
self.stoped = true; | |
Anim.release(self); | |
}, | |
run: function() { | |
var self = this; | |
self._setLatestStyle(); | |
self.canRun = true; | |
self.started = true; | |
self.startTime = +new Date(); | |
self._proxiedFunc = S.bind(self.step, self); | |
Ticker.on('tick', self._proxiedFunc); | |
return self; | |
}, | |
pause: function() { | |
var self = this; | |
self.paused = true; | |
self.pausedTime = +new Date(); | |
}, | |
resume: function() { | |
var self = this; | |
self.paused = false; | |
self.startTime += +new Date() - self.pausedTime; | |
} | |
}); | |
//--Tween functions | |
var Tween = { | |
linear: function(t, b, c, d) { | |
return c * t / d + b; | |
}, | |
easeIn: function(t, b, c, d) { | |
return c * (t /= d) * t + b; | |
}, | |
easeOut: function(t, b, c, d) { | |
return -c * (t /= d) * (t - 2) + b; | |
}, | |
easeInOut: function(t, b, c, d) { | |
if ((t /= d / 2) < 1) return c / 2 * t * t + b; | |
return -c / 2 * ((--t) * (t - 2) - 1) + b; | |
}, | |
Bounce: { | |
easeIn: function(t, b, c, d) { | |
return c - Tween.Bounce.easeOut(d - t, 0, c, d) + b; | |
}, | |
easeOut: function(t, b, c, d) { | |
if ((t /= d) < (1 / 2.75)) { | |
return c * (7.5625 * t * t) + b; | |
} else if (t < (2 / 2.75)) { | |
return c * (7.5625 * (t -= (1.5 / 2.75)) * t + .75) + b; | |
} else if (t < (2.5 / 2.75)) { | |
return c * (7.5625 * (t -= (2.25 / 2.75)) * t + .9375) + b; | |
} else { | |
return c * (7.5625 * (t -= (2.625 / 2.75)) * t + .984375) + b; | |
} | |
}, | |
easeInOut: function(t, b, c, d) { | |
if (t < d / 2) return Tween.Bounce.easeIn(t * 2, 0, c, d) * .5 + b; | |
else return Tween.Bounce.easeOut(t * 2 - d, 0, c, d) * .5 + c * .5 + b; | |
} | |
} | |
}; | |
//--get delta data of two attributes | |
function getAttrDelta(a, b) { | |
if (!b) b = 0; | |
return Number(a) - Number(b); | |
} | |
//--return easing function | |
function getEasingFunc(easingStr) { | |
var reg = /[\.|:-]/g; | |
if (Tween[easingStr]) { | |
return Tween[easingStr]; | |
} | |
var a = easingStr.split(reg); | |
if (Tween[a[0]][a[1]]) { | |
return Tween[a[0]][a[1]]; | |
} | |
return Tween['linear']; | |
} | |
return Anim; | |
}, { | |
requires: [ | |
'event', | |
'khc/lib/ticker' | |
] | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
* @class Animation | |
*/ | |
KISSY.add('fp-dsf/CatGameAnim', function(S, Event, util, Ticker, Tween) { | |
var nullf = function() {}; | |
//--get delta data of two attributes | |
function getAttrDelta(a, b) { | |
if (!b) b = 0; | |
return Number(a) - Number(b); | |
} | |
//--return easing function | |
function getEasingFunc(easingStr) { | |
var reg = /[|:-]/g; | |
if (Tween[easingStr]) return Tween[easingStr]; | |
var a = easingStr.split(reg); | |
if (Tween[a[0]][a[1]]) return Tween[a[0]][a[1]]; | |
return Tween['linear']; | |
} | |
/** | |
* @Class Anim | |
*/ | |
var Anim = util.Klass(null, { | |
__construct: function(target, duration, props, easing) { | |
var self = this; | |
util.mix(self, S.EventTarget); | |
self.target = target; | |
self.originStyle = util.mix({}, target.style); | |
self.currentOriginStyle = util.mix({}, target.style); | |
self.props = props; | |
self.canRun = false; | |
self.started = false; | |
self.stoped = false; | |
self.startTime = 0; | |
//get delta props | |
self.deltaProps = {}; | |
for (attr in props) { | |
if (props.hasOwnProperty(attr)) { | |
self.deltaProps[attr] = getAttrDelta(props[attr] - self.currentOriginStyle[attr]) | |
} | |
} | |
self.duration = duration || 0; | |
self.easing = getEasingFunc(easing); | |
}, | |
_setLatestStyle: function(){ | |
var self = this; | |
var target = self.target; | |
var props = self.props; | |
self.currentOriginStyle = util.mix({}, target.style); | |
for (attr in props) { | |
if (props.hasOwnProperty(attr)) { | |
self.deltaProps[attr] = getAttrDelta(props[attr] - self.currentOriginStyle[attr]) | |
} | |
} | |
}, | |
step: function() { | |
var self = this; | |
if (self.paused || !self.canRun) return; | |
var style = self.target.style; | |
var currentOriginStyle = self.currentOriginStyle; | |
var deltaProps = self.deltaProps; | |
var easing = self.easing; | |
var duration = self.duration; | |
var currentTime = (+new Date() - self.startTime) / 1000; | |
if (currentTime >= duration) { | |
self._finish(); | |
return; | |
} | |
for (attr in deltaProps) { | |
style[attr] = easing(currentTime, currentOriginStyle[attr], deltaProps[attr], duration); | |
} | |
self.fire('update'); | |
}, | |
_finish: function() { | |
var self = this; | |
self.canRun = false; | |
self.started = false; | |
self.stoped = true; | |
Ticker.detach('tick', self._proxiedFunc); | |
self.fire('end'); | |
}, | |
run: function() { | |
var self = this; | |
self._setLatestStyle(); | |
self.canRun = true; | |
self.started = true; | |
self.startTime = +new Date(); | |
self._proxiedFunc = util.proxy(self, self.step); | |
Ticker.on('tick', self._proxiedFunc); | |
return self; | |
}, | |
pause: function() { | |
var self = this; | |
self.paused = true; | |
self.pausedTime = +new Date(); | |
}, | |
resume: function() { | |
var self = this; | |
self.paused = false; | |
self.startTime -= +new Date() - self.pausedTime; | |
} | |
}); | |
//static class attributes | |
var AnimationQueue = util.Klass(null, { | |
__construct: function(animArr, repeatTime) { | |
var self = this; | |
util.mix(self, S.EventTarget); | |
self.animArr = animArr; | |
self.repeatTime = repeatTime; | |
self.currentIndex = -1; | |
self._init(); | |
}, | |
run: function() { | |
var self = this; | |
self._runSingleAnim(0); | |
}, | |
_init: function() { | |
var self = this; | |
var animArr = self.animArr; | |
var len = animArr.length; | |
var i; | |
for (i = 0; i < len; i++) { | |
(function(index, anim) { | |
if (index < len - 1) { | |
anim.on('end', function() { | |
console.log('buxing ?') | |
self._runSingleAnim(index + 1) | |
}); | |
} else { | |
anim.on('end', function() { | |
self._finish(); | |
}); | |
} | |
})(i, animArr[i]); | |
} | |
}, | |
_runSingleAnim: function(index) { | |
console.log('_runSingleAnim'); | |
var self = this; | |
var anim = self.animArr[index]; | |
if (anim instanceof Anim) { | |
self.currentIndex = index; | |
anim.run(); | |
} | |
}, | |
_finish: function() { | |
var self = this; | |
var animArr = self.animArr; | |
var len = animArr.len; | |
var i; | |
if (self.repeatTime === 'forever' || self.repeatTime > 0) { | |
util.isN(self.repeatTime) && self.repeatTime--; | |
self.run(); | |
return; | |
} | |
for (i = 0; i < len; i++) { | |
if (i > 0) animArr[i - 1].detach('end'); | |
} | |
self.fire('end'); | |
}, | |
pause: function() { | |
}, | |
resume: function() { | |
} | |
}); | |
//static class methods | |
util.mix(Anim, { | |
/** | |
* @method to | |
*/ | |
to: function(target, duration, props, easing) { | |
return new Anim(target, duration, props, easing); | |
}, | |
/** | |
* @method queue | |
*/ | |
queue: function(arr) { | |
return new AnimationQueue(arr, 1); | |
}, | |
/** | |
* @method repeat | |
*/ | |
repeat: function(arr, repeatTime) { | |
return new AnimationQueue(arr, repeatTime); | |
}, | |
}); | |
return Anim; | |
}, { | |
requires: [ | |
'event', 'fp-dsf/CatGameUtil', 'fp-dsf/CatGameTicker', 'fp-dsf/Tween' | |
] | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @class catGameSprite | |
*/ | |
KISSY.add('fp-dsf/CatGameSprite', function(S, DOM, util, CatGameNode, CatGameFrameManager) { | |
/** | |
* @class catGameSprite | |
*/ | |
var CatGameSprite = util.Klass(CatGameNode,{ | |
__construct: function(conf){ | |
// util.log("__construct sprite"); | |
var self = this; | |
util.mix(self, conf); | |
self.type = 'sprite'; | |
self.frameManager = new CatGameFrameManager({ | |
img: null, | |
frames: [] | |
}); | |
}, | |
update: function(deltaTime) { | |
var self = this; | |
self.frameManager.update(deltaTime); | |
self.style.backgroundClip = self.frameManager.getCurrentFrame(); | |
} | |
}); | |
return CatGameSprite; | |
}, { | |
requires: [ | |
'dom', 'fp-dsf/CatGameUtil', 'fp-dsf/CatGameNode', 'fp-dsf/CatGameFrameManager' | |
] | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @Object Tween | |
*/ | |
KISSY.add('fp-dsf/Tween', function(S) { | |
return Tween = { | |
Linear: function(t, b, c, d) { | |
return c * t / d + b; | |
}, | |
Quad: { | |
easeIn: function(t, b, c, d) { | |
return c * (t /= d) * t + b; | |
}, | |
easeOut: function(t, b, c, d) { | |
return -c * (t /= d) * (t - 2) + b; | |
}, | |
easeInOut: function(t, b, c, d) { | |
if ((t /= d / 2) < 1) return c / 2 * t * t + b; | |
return -c / 2 * ((--t) * (t - 2) - 1) + b; | |
} | |
}, | |
Cubic: { | |
easeIn: function(t, b, c, d) { | |
return c * (t /= d) * t * t + b; | |
}, | |
easeOut: function(t, b, c, d) { | |
return c * ((t = t / d - 1) * t * t + 1) + b; | |
}, | |
easeInOut: function(t, b, c, d) { | |
if ((t /= d / 2) < 1) return c / 2 * t * t * t + b; | |
return c / 2 * ((t -= 2) * t * t + 2) + b; | |
} | |
}, | |
Quart: { | |
easeIn: function(t, b, c, d) { | |
return c * (t /= d) * t * t * t + b; | |
}, | |
easeOut: function(t, b, c, d) { | |
return -c * ((t = t / d - 1) * t * t * t - 1) + b; | |
}, | |
easeInOut: function(t, b, c, d) { | |
if ((t /= d / 2) < 1) return c / 2 * t * t * t * t + b; | |
return -c / 2 * ((t -= 2) * t * t * t - 2) + b; | |
} | |
}, | |
Quint: { | |
easeIn: function(t, b, c, d) { | |
return c * (t /= d) * t * t * t * t + b; | |
}, | |
easeOut: function(t, b, c, d) { | |
return c * ((t = t / d - 1) * t * t * t * t + 1) + b; | |
}, | |
easeInOut: function(t, b, c, d) { | |
if ((t /= d / 2) < 1) return c / 2 * t * t * t * t * t + b; | |
return c / 2 * ((t -= 2) * t * t * t * t + 2) + b; | |
} | |
}, | |
Sine: { | |
easeIn: function(t, b, c, d) { | |
return -c * Math.cos(t / d * (Math.PI / 2)) + c + b; | |
}, | |
easeOut: function(t, b, c, d) { | |
return c * Math.sin(t / d * (Math.PI / 2)) + b; | |
}, | |
easeInOut: function(t, b, c, d) { | |
return -c / 2 * (Math.cos(Math.PI * t / d) - 1) + b; | |
} | |
}, | |
Expo: { | |
easeIn: function(t, b, c, d) { | |
return (t == 0) ? b : c * Math.pow(2, 10 * (t / d - 1)) + b; | |
}, | |
easeOut: function(t, b, c, d) { | |
return (t == d) ? b + c : c * (-Math.pow(2, -10 * t / d) + 1) + b; | |
}, | |
easeInOut: function(t, b, c, d) { | |
if (t == 0) return b; | |
if (t == d) return b + c; | |
if ((t /= d / 2) < 1) return c / 2 * Math.pow(2, 10 * (t - 1)) + b; | |
return c / 2 * (-Math.pow(2, -10 * --t) + 2) + b; | |
} | |
}, | |
Circ: { | |
easeIn: function(t, b, c, d) { | |
return -c * (Math.sqrt(1 - (t /= d) * t) - 1) + b; | |
}, | |
easeOut: function(t, b, c, d) { | |
return c * Math.sqrt(1 - (t = t / d - 1) * t) + b; | |
}, | |
easeInOut: function(t, b, c, d) { | |
if ((t /= d / 2) < 1) return -c / 2 * (Math.sqrt(1 - t * t) - 1) + b; | |
return c / 2 * (Math.sqrt(1 - (t -= 2) * t) + 1) + b; | |
} | |
}, | |
Elastic: { | |
easeIn: function(t, b, c, d, a, p) { | |
if (t == 0) return b; | |
if ((t /= d) == 1) return b + c; | |
if (!p) p = d * .3; | |
if (!a || a < Math.abs(c)) { | |
a = c; | |
var s = p / 4; | |
} else var s = p / (2 * Math.PI) * Math.asin(c / a); | |
return -(a * Math.pow(2, 10 * (t -= 1)) * Math.sin((t * d - s) * (2 * Math.PI) / p)) + b; | |
}, | |
easeOut: function(t, b, c, d, a, p) { | |
if (t == 0) return b; | |
if ((t /= d) == 1) return b + c; | |
if (!p) p = d * .3; | |
if (!a || a < Math.abs(c)) { | |
a = c; | |
var s = p / 4; | |
} else var s = p / (2 * Math.PI) * Math.asin(c / a); | |
return (a * Math.pow(2, -10 * t) * Math.sin((t * d - s) * (2 * Math.PI) / p) + c + b); | |
}, | |
easeInOut: function(t, b, c, d, a, p) { | |
if (t == 0) return b; | |
if ((t /= d / 2) == 2) return b + c; | |
if (!p) p = d * (.3 * 1.5); | |
if (!a || a < Math.abs(c)) { | |
a = c; | |
var s = p / 4; | |
} else var s = p / (2 * Math.PI) * Math.asin(c / a); | |
if (t < 1) return -.5 * (a * Math.pow(2, 10 * (t -= 1)) * Math.sin((t * d - s) * (2 * Math.PI) / p)) + b; | |
return a * Math.pow(2, -10 * (t -= 1)) * Math.sin((t * d - s) * (2 * Math.PI) / p) * .5 + c + b; | |
} | |
}, | |
Back: { | |
easeIn: function(t, b, c, d, s) { | |
if (s == undefined) s = 1.70158; | |
return c * (t /= d) * t * ((s + 1) * t - s) + b; | |
}, | |
easeOut: function(t, b, c, d, s) { | |
if (s == undefined) s = 1.70158; | |
return c * ((t = t / d - 1) * t * ((s + 1) * t + s) + 1) + b; | |
}, | |
easeInOut: function(t, b, c, d, s) { | |
if (s == undefined) s = 1.70158; | |
if ((t /= d / 2) < 1) return c / 2 * (t * t * (((s *= (1.525)) + 1) * t - s)) + b; | |
return c / 2 * ((t -= 2) * t * (((s *= (1.525)) + 1) * t + s) + 2) + b; | |
} | |
}, | |
Bounce: { | |
easeIn: function(t, b, c, d) { | |
return c - Tween.Bounce.easeOut(d - t, 0, c, d) + b; | |
}, | |
easeOut: function(t, b, c, d) { | |
if ((t /= d) < (1 / 2.75)) { | |
return c * (7.5625 * t * t) + b; | |
} else if (t < (2 / 2.75)) { | |
return c * (7.5625 * (t -= (1.5 / 2.75)) * t + .75) + b; | |
} else if (t < (2.5 / 2.75)) { | |
return c * (7.5625 * (t -= (2.25 / 2.75)) * t + .9375) + b; | |
} else { | |
return c * (7.5625 * (t -= (2.625 / 2.75)) * t + .984375) + b; | |
} | |
}, | |
easeInOut: function(t, b, c, d) { | |
if (t < d / 2) return Tween.Bounce.easeIn(t * 2, 0, c, d) * .5 + b; | |
else return Tween.Bounce.easeOut(t * 2 - d, 0, c, d) * .5 + c * .5 + b; | |
} | |
} | |
}; | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @Object util | |
*/ | |
KISSY.add('fp-dsf/CatGameUtil', function(S, Event, UA) { | |
var util = {}; | |
util.isIE6 = UA.ie === 6; | |
util.log = S.log; | |
util.isSupportCanvas = (function() { | |
if (!document.createElement('canvas').getContext('2d')) { | |
return false; | |
} else { | |
return true; | |
} | |
})(); | |
util.guid = function() { | |
return Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15); | |
} | |
util.EventTartget = S.EventTartget; | |
util.mix = S.mix; | |
var ts = Object.prototype.toString; | |
var U = { | |
isU: function(o) { | |
return o === void 0; | |
}, | |
isS: function(o) { | |
return ts.call(o) === '[object String]'; | |
}, | |
isN: function(o) { | |
return ts.call(o) === '[object Number]'; | |
}, | |
isO: function(o) { | |
return !!o && ts.call(o) === '[object Object]'; | |
}, | |
isF: function(o) { | |
return ts.call(o) === '[object Function]'; | |
}, | |
isA: function(o) { | |
return ts.call(o) === '[object Array]'; | |
}, | |
isE: function(o) { | |
return !!(o && o.nodeType && o.nodeType === 1); | |
}, | |
each: function(arr, fn) { | |
if (!U.isA(arr) || !U.isF(fn)) { | |
return; | |
} | |
var i = 0, | |
l = arr.length; | |
while (i < l) fn.call(null, arr[i++]); | |
}, | |
trim: function(s) { | |
return s.replace(/^\s+/, '').replace(/\s+$/, ''); | |
}, | |
// substitute | |
ss: function(s, o) { | |
return s.replace(/{{([^}{2}]+)}}/g, function(a, b) { | |
return (b && o[b]) || ''; | |
}); | |
} | |
}; | |
var xuejia = { | |
/** | |
* proxy | |
*/ | |
proxy: function(context, func) { | |
return function() { | |
func.apply(context, arguments); | |
} | |
}, | |
/** | |
* function wraper | |
*/ | |
wrap: function(func, wrapper) { | |
var __method = func; | |
return function() { | |
var args = Array.prototype.slice.call(arguments); | |
return wrapper.apply(this, [__method.bind(this)].concat(args)); | |
} | |
/* | |
eg: | |
f=wrap(f, function(org,x,y){ | |
console.log("x:"+x,"y:"+y); | |
var r=org(x,y); | |
console.log("result:"+r); | |
}); | |
*/ | |
} | |
}; | |
if (typeof Object.create !== "function") { | |
Object.create = function(o) { | |
function F() {} | |
F.prototype = o; | |
return new F(); | |
}; | |
} | |
/** | |
*Klass 语法糖 | |
*/ | |
util.Klass = function(Parent, props) { | |
var Child, F, i; | |
Child = function() { | |
var parent = Child.parent; | |
while(parent){ | |
parent.prototype && parent.prototype.hasOwnProperty("__construct") && parent.prototype.__construct.apply(this, arguments); | |
parent = parent.parent; | |
} | |
if (Child.prototype.hasOwnProperty("__construct")) { | |
Child.prototype.__construct.apply(this, arguments); | |
} | |
this.super = Parent.prototype; | |
}; | |
Parent = Parent || Object; | |
F = function() {}; | |
F.prototype = Parent.prototype; | |
Child.prototype = new F(); | |
Child.parent = Parent; | |
Child.super = Parent.prototype; | |
Child.prototype.constructor = Child; | |
for (i in props) { | |
if (props.hasOwnProperty(i)) { | |
Child.prototype[i] = props[i]; | |
} | |
} | |
return Child; | |
} | |
S.mix(util, U); | |
S.mix(util, xuejia); | |
return util; | |
}, { | |
requires: [ | |
'event', 'ua' | |
] | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
买表, 虽然简陋! 我却写了两天!! ==========