127 lines
10 KiB
JavaScript
127 lines
10 KiB
JavaScript
// Learn cc.Class:
|
|
// - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/class.html
|
|
// - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/class.html
|
|
// Learn Attribute:
|
|
// - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
|
|
// - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/reference/attributes.html
|
|
// Learn life-cycle callbacks:
|
|
// - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
|
|
// - [English] https://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html
|
|
var jellyAnimation = [[0, 0, 1, 1, 0, 0], [0, -.2, .990325927734375, 1.0163421630859375, 0, 0], [0, -.85, .9597015380859375, 1.0679931640625, 0, 0], [0, -2, .90777587890625, 1.1556243896484375, 0, 0], [0, -3.45, .839111328125, 1.271484375, 0, 0], [0, -5.05, .7647552490234375, 1.3969268798828125, 0, 0], [0, -6.5, .6983184814453125, 1.5090484619140625, 0, 0], [0, -7.55, .6492462158203125, 1.591827392578125, 0, 0], [0, -8.15, .6207122802734375, 1.639984130859375, 0, 0], [0, -8.25, .611724853515625, 1.6551513671875, 0, 0], [], [0, 0, 1, 1, 0, 0], [4.6, 4.65, 1.26666259765625, .8162329058533069, 10.772172752407087, 0], [9.2, 9.25, 1.5333251953125, .6334487873339344, 21.620468223663323, 0], [1.4, 5.8, 1.357635498046875, .7792788568603103, 4.05452351570284, 0], [-6.45, 2.35, 1.1819305419921875, .925467115184136, -13.191458606259076, 0], [-3.2, -.95, 1.01348876953125, 1.0567754423261255, -6.526565175237877, 0], [0, -4.25, .8450469970703125, 1.1888885498046875, 0, 0], [3.1, -2.7, .916717529296875, 1.1285485952784342, 4.535063497453464, 0], [6.15, -1.2, .9883880615234375, 1.0687622833051502, 9.273376151585296, 0], [4.15, .75, 1.0811309814453125, .9769953841175838, 6.047881985238391, 0], [2.05, 2.75, 1.17388916015625, .8857241411063542, 3.0237858101727397, 0], [0, 4.65, 1.26666259765625, .7944488525390625, 0, 0], [-1.05, 3.6, 1.2183380126953125, .8434575932408014, -2.2819930520332434, 0], [-2.1, 2.5, 1.1699981689453125, .8924740830067877, -4.7535541622246456, 0], [-3.15, 1.45, 1.1216888427734375, .9418679675355531, -7.136990075539856, 0], [-2.15, .4, 1.0753631591796875, .9839297937141035, -4.753827686686492, 0], [-1.05, -.6, 1.029022216796875, 1.0264039447029814, -2.2833587267059414, 0], [0, -1.6, .982696533203125, 1.0688934326171875, 0, 0], [.35, -1.4, .98492431640625, 1.0602445053925167, .76441515641352, 0], [.65, -1.15, .9871673583984375, 1.0516431573527776, 1.527337074008301, 0], [1, -1, .9893951416015625, 1.0431331771009533, 2.416159750495339, 0], [.8, -.8, .99151611328125, 1.0343828507167612, 1.7963521419677733, 0], [.6, -.6, .994110107421875, 1.0238889971616911, 1.274073376721887, 0], [.3, -.3, .996612548828125, 1.013729485673602, .7555049507628127, 0], [.15, -.15, .9985198974609375, 1.005977596469548, .27288878198426403, 0], [.05, -.05, .9996490478515625, 1.0014191343430145, .020952607033734694, 0], [0, 0, 1, 1, 0, 0], [], [0, 0, 1, 1, 0, 0], [-3.1, 4.7, 1.2722930908203125, .8187573190593543, -12.296938381755353, 0], [-6.05, 9.25, 1.5333251953125, .6460869700694191, -24.292097544945918, 0], [-3.4, 7.5, 1.4423065185546875, .7204993841632751, -13.813460697163393, 0], [4.45, 2.35, 1.1819305419921875, .9355071077774614, 15.599698062593149, 0], [1.1, -2.6, .9299468994140625, 1.124781958325034, 3.7958751937056974, 0], [0, -4.25, .8450469970703125, 1.1888885498046875, 0, 0], [-1.55, -3.5, .8802642822265625, 1.1655771151376606, -3.769569469169909, 0], [-6.65, -1.2, .9883880615234375, 1.0949985387914987, -15.574157460853087, 0], [-3.2, 1.9, 1.131103515625, .9404279496175882, -7.525715889629254, 0], [-.8, 4, 1.23388671875, .8297270091149772, -1.771513169863411, 0], [0, 4.65, 1.26666259765625, .7944488525390625, 0, 0], [.3, 4.3, 1.2503662109375, .8108758237352302, .7504296864670579, 0], [1.15, 3.15, 1.1992034912109375, .8624714277595207, 3.0282379744694623, 0], [2.45, 1.45, 1.1216888427734375, .9409700566331797, 6.686171094964038, 0], [1.15, -.2, 1.0460205078125, 1.0104446895887182, 3.012378226202557, 0], [.25, -1.25, .9978179931640625, 1.0549051829487366, .5569361237827479, 0], [0, -1.6, .982696533203125, 1.0688934326171875, 0, 0], [-.1, -1.55, .9834136962890625, 1.066086335325182, -.056584761061770905, 0], [-.4, -1.35, .9857177734375, 1.0571861051189553, -.7997087181308729, 0], [-.85, -1, .9893951416015625, 1.0429154773788731, -2.1138051796834247, 0], [-.6, -.75, .9922332763671875, 1.0313194037738669, -1.512497983808231, 0], [-.45, -.55, .99493408203125, 1.0204045992999402, -1.0024859970574198, 0], [-.25, -.25, .9971923828125, 1.011363047612536, -.5160785678648097, 0], [-.1, -.1, .9987640380859375, 1.0049139140583043, -.06176924245222892, 0], [0, -.05, .9997100830078125, 1.0011749644321115, -.015718287616209636, 0], [0, 0, 1, 1, 0, 0], [], [0, 0, 1, 1, 0, 0], [0, 2.55, 1.126678466796875, .8877716064453125, 0, 0], [0, 8.3, 1.4143829345703125, .6329193115234375, 0, 0], [0, 10.65, 1.5333251953125, .527557373046875, 0, 0], [0, 9.25, 1.4581298828125, .5888671875, 0, 0], [0, 4.95, 1.2271728515625, .77716064453125, 0, 0], [0, -.65, .9223480224609375, 1.0257110595703125, 0, 0], [0, -4.7, .70281982421875, 1.2047119140625, 0, 0], [0, -5.9, .632843017578125, 1.2617645263671875, 0, 0], [0, -5.55, .6529541015625, 1.2469329833984375, 0, 0], [0, -4.45, .7166595458984375, 1.1999664306640625, 0, 0], [0, -2.75, .822998046875, 1.1215667724609375, 0, 0], [0, -.55, .9564361572265625, 1.0231781005859375, 0, 0], [0, 1.65, 1.087371826171875, .9266510009765625, 0, 0], [0, 3.3, 1.1884765625, .8520965576171875, 0, 0], [0, 4.3, 1.24798583984375, .8082275390625, 0, 0], [0, 4.65, 1.26666259765625, .7944488525390625, 0, 0], [0, 4.45, 1.2573394775390625, .803497314453125, 0, 0], [0, 3.8, 1.2279052734375, .8321685791015625, 0, 0], [0, 2.7, 1.177947998046875, .88079833984375, 0, 0], [0, 1.25, 1.1118927001953125, .9450836181640625, 0, 0], [0, -.35, 1.040374755859375, 1.0146942138671875, 0, 0], [0, -1.75, .9764556884765625, 1.0768890380859375, 0, 0], [0, -2.8, .92926025390625, 1.122833251953125, 0, 0], [0, -3.35, .9018096923828125, 1.1495513916015625, 0, 0], [0, -3.6, .893157958984375, 1.157958984375, 0, 0], [0, -3.55, .894927978515625, 1.1553497314453125, 0, 0], [0, -3.35, .9004974365234375, 1.147125244140625, 0, 0], [0, -3.05, .910064697265625, 1.13299560546875, 0, 0], [0, -2.6, .9233245849609375, 1.11334228515625, 0, 0], [0, -2.05, .9392852783203125, 1.089752197265625, 0, 0], [0, -1.5, .956085205078125, 1.0649566650390625, 0, 0], [0, -.95, .971588134765625, 1.0419921875, 0, 0], [0, -.6, .984222412109375, 1.0233154296875, 0, 0], [0, -.25, .993194580078125, 1.01007080078125, 0, 0], [0, -.05, .9983673095703125, 1.002410888671875, 0, 0], [0, 0, 1, 1, 0, 0], []];
|
|
var animationLen = jellyAnimation.length;
|
|
|
|
cc.Class({
|
|
extends: cc.Component,
|
|
|
|
properties: {
|
|
// foo: {
|
|
// // ATTRIBUTES:
|
|
// default: null, // The default value will be used only when the component attaching
|
|
// // to a node for the first time
|
|
// type: cc.SpriteFrame, // optional, default is typeof default
|
|
// serializable: true, // optional, default is true
|
|
// },
|
|
// bar: {
|
|
// get () {
|
|
// return this._bar;
|
|
// },
|
|
// set (value) {
|
|
// this._bar = value;
|
|
// }
|
|
// },
|
|
animationPower: 0.8,
|
|
animationSpeed: 0.8,
|
|
cdtime:2,
|
|
cdtimerand:2,
|
|
needtouch:false,
|
|
needauto:true,
|
|
|
|
doleft:true,
|
|
doright:true,
|
|
doshake:true
|
|
},
|
|
|
|
// LIFE-CYCLE CALLBACKS:
|
|
|
|
// onLoad () {},
|
|
|
|
onLoad () {
|
|
this.currentFrame = 0
|
|
this.waittime = 0
|
|
this.frameTime = 1/30
|
|
this.aniarr = []
|
|
if(this.doleft){
|
|
this.aniarr.push(39)
|
|
}
|
|
if(this.doright){
|
|
this.aniarr.push(11)
|
|
}
|
|
if(this.doshake){
|
|
this.aniarr.push(67)
|
|
}
|
|
if(this.needtouch){
|
|
this.node.on(cc.Node.EventType.TOUCH_START, this._touchStartEvent, this);
|
|
}
|
|
this.setwait()
|
|
},
|
|
setwait(){
|
|
this.waittime = this.cdtime+Math.random()*this.cdtimerand
|
|
},
|
|
_touchStartEvent(){
|
|
this.waittime = 0
|
|
var n = Math.floor(Math.random()*this.aniarr.length);
|
|
this.setAnimation(this.aniarr[n])
|
|
},
|
|
setAnimation(idx){
|
|
this.isPlaying = true
|
|
this.currentFrame = idx;
|
|
},
|
|
update (dt) {
|
|
if(this.needauto&&this.waittime>0){
|
|
this.waittime-=dt
|
|
if(this.waittime<=0){
|
|
this._touchStartEvent()
|
|
}
|
|
return
|
|
}
|
|
if(!this.isPlaying){
|
|
return
|
|
}
|
|
var t = dt * this.animationSpeed
|
|
while(t>0){
|
|
if(t>this.frameTime){
|
|
this.currentFrame+=1
|
|
}else{
|
|
this.currentFrame+= t / this.frameTime
|
|
}
|
|
t-=this.frameTime
|
|
this.setFrame(~~this.currentFrame, t <= this.frameTime)
|
|
}
|
|
},
|
|
setFrame(cfram,t,n){
|
|
|
|
var i = cfram < 0 || cfram >= animationLen ? null : jellyAnimation[cfram];
|
|
var s = cfram + 1 < 0 || cfram + 1 >= animationLen ? null : jellyAnimation[cfram + 1];
|
|
var o = i == null || i.length <= 0;
|
|
var u = !o && s != null && s.length > 0;
|
|
var a = this.currentFrame - cfram;
|
|
var f = this.animationPower;
|
|
if (o) {
|
|
this.node.scaleX = 1;
|
|
this.node.scaleY = 1;
|
|
this.node.skewX = 0;
|
|
this.node.skewY = 0
|
|
this.isPlaying = false;
|
|
this.currentFrame = 0
|
|
this.setwait()
|
|
} else if (t) {
|
|
var l = !u;
|
|
this.node.scaleX = 1 * (l ? 1 + (i[2] - 1) * f : 1 + (i[2] + a * (s[2] - i[2]) - 1) * f);
|
|
this.node.scaleY = 1 * (l ? 1 + (i[3] - 1) * f : 1 + (i[3] + a * (s[3] - i[3]) - 1) * f);
|
|
this.node.skewX = l ? i[4] * f : f * (i[4] + a * (s[4] - i[4]));
|
|
this.node.skewY = l ? i[5] * f : f * (i[5] + a * (s[5] - i[5]))
|
|
}
|
|
|
|
}
|
|
});
|