sweet/assets/scriptes/gameEffect.js
2020-08-07 09:55:56 +08:00

311 lines
11 KiB
JavaScript

let Config = require('config');
let Promise = require('ZPromise');
cc.Class({
extends: cc.Component,
properties: {
effectPanel: cc.Node,
starEffectPreb: cc.Prefab,
boxEffectPreb: cc.Prefab,
circleEffectPreb: cc.Prefab,
lineEffectPreb: cc.Prefab,
recordEffectPreb: cc.Prefab,
fireworkPreb: cc.Prefab,
fingerPreb: cc.Prefab,
alertPanel: cc.Node,
scoreEffectPreb: cc.Prefab,
cookieEffectPreb: cc.Prefab,
},
// LIFE-CYCLE CALLBACKS:
onLoad () {
cc.EffectMgr = this;
this.starPool = new cc.NodePool();
let initCount = 5;
// for (let i = 0; i < initCount; ++i) {
// let star = cc.instantiate(this.starEffectPreb);
// this.starPool.put(star);
// }
this.scorePool = [];
for (let i = 0; i < initCount; ++i) {
let scoreNd = cc.instantiate(this.scoreEffectPreb);
scoreNd.active = false;
this.effectPanel.addChild(scoreNd);
this.scorePool.push(scoreNd);
}
this.cookiePool = new cc.NodePool();
// for (let i = 0; i < initCount; ++i) {
// let star = cc.instantiate(this.cookieEffectPreb);
// this.cookiePool.put(star);
// }
this.linePool = new cc.NodePool();
for (let i = 0; i < initCount; ++i) {
this.linePool.put(cc.instantiate(this.lineEffectPreb));
}
},
init(owner) {
this.owner = owner;
},
start () {
cc.loader.loadRes()
},
// 行消除额外的粒子动画
showLineEffect(nd) {
let size = nd.getContentSize();
let position = nd.position;
position.x = position.x + (0.5 - nd.anchorX) * size.width;
position.y = position.y + (0.5 - nd.anchorY) * size.height;
let posW = nd.parent.convertToWorldSpaceAR(position);
let pos = this.effectPanel.convertToNodeSpaceAR(posW);
let effect = null;
if (this.linePool.size() > 0) {
effect = this.linePool.get();
} else {
effect = cc.instantiate(this.lineEffectPreb);
}
this.effectPanel.addChild(effect);
effect.position = pos;
let self = this;
this.scheduleOnce(function () {
effect.parent = null;
self.linePool.put(effect);
}, 1)
},
// 局内道具收集动画
showStarCollect(worldPos, targetNode) {
return new Promise((resolve, reject) => {
this.effectPanel.zIndex = 150;
let star = this.createStar(this.effectPanel);
let sourcePos = this.effectPanel.convertToNodeSpaceAR(worldPos);
// let targetPos = cc.v2(-263, 425);
let targetPosW = targetNode.parent.convertToWorldSpaceAR(targetNode.position);
let targetPos = this.effectPanel.convertToNodeSpaceAR(targetPosW);
let centerPos = cc.v2(targetPos.x + (sourcePos.x - targetPos.x) / 2, targetPos.y + (sourcePos.y - targetPos.y) / 2);
if (sourcePos.x < 0) {
centerPos.x = targetPos.x + (centerPos.x - targetPos.x) / 2;
centerPos.y = centerPos.y - (targetPos.y - centerPos.y) / 2;
} else {
centerPos.x = centerPos.x + (centerPos.x - targetPos.x) / 2;
centerPos.y = centerPos.y + (targetPos.y - centerPos.y) / 2;
}
let linePos = [sourcePos, centerPos, targetPos];
console.log(linePos);
star.position = sourcePos;
star.getComponent(cc.ParticleSystem).resetSystem();
star.active = true;
let actionTime = Config.star_collect_fly_time;
// 道具飞行到目标后的回调
let startCollected = function(obj, pos) {
console.log('star collected');
obj.parent = null;
this.starPool.put(obj);
resolve();
};
let action = cc.sequence(cc.bezierTo(actionTime, linePos), cc.callFunc(startCollected, this, targetPos));
star.runAction(action).easing(cc.easeElasticOut(actionTime));
})
},
// 获取一个道具飞行动画的实例
createStar: function (parentNode) {
let star = null;
if (this.starPool.size() > 0) {
star = this.starPool.get();
} else {
star = cc.instantiate(this.starEffectPreb);
}
parentNode.addChild(star);
// star.parent = parentNode;
return star;
},
// 局内道具收集箱子闪光动画
createBoxEffect: function (pos) {
if (!this.boxEffect) {
this.boxEffect = cc.instantiate(this.boxEffectPreb);
this.effectPanel.addChild(this.boxEffect);
this.boxEffect.position = pos;
}
this.boxEffect.getComponent(cc.ParticleSystem).resetSystem();
this.boxEffect.active = true;
},
// 圆圈提示动画
createCircleEffect: function (node) {
let wpos = node.parent.convertToWorldSpaceAR(node.position);
let myPos = this.effectPanel.convertToNodeSpaceAR(wpos);
if (!this.circleEffect) {
this.circleEffect = cc.instantiate(this.circleEffectPreb);
this.effectPanel.addChild(this.circleEffect);
}
this.circleEffect.getComponent(cc.ParticleSystem).resetSystem();
this.circleEffect.position = myPos;
},
// 隐藏圆圈提示动画
hideCircleEffect: function() {
(this.circleEffect) && (this.circleEffect.active = false);
},
// 显示新记录的效果
createNewRecordEffect: function (node) {
let wpos = node.parent.convertToWorldSpaceAR(node.position);
let myPos = this.effectPanel.convertToNodeSpaceAR(wpos);
if (!this.recordEffect) {
this.recordEffect = cc.instantiate(this.recordEffectPreb);
this.effectPanel.addChild(this.recordEffect);
}
let self = this;
this.recordEffect.getChildByName('effectNode1').getComponent(cc.ParticleSystem).resetSystem();
this.scheduleOnce(function () {
self.recordEffect.getChildByName('effectNode2').active = true;
self.recordEffect.getChildByName('effectNode2').getComponent(cc.ParticleSystem).resetSystem();
}, 0.5);
this.scheduleOnce(function () {
self.recordEffect.getChildByName('effectNode3').active = true;
self.recordEffect.getChildByName('effectNode3').getComponent(cc.ParticleSystem).resetSystem();
}, 1);
this.recordEffect.position = myPos;
this.scheduleOnce(function () {
self.recordEffect.active = false;
}, 2);
},
// 显示烟花效果
showFireworkEffect(node) {
let wpos = node.parent.convertToWorldSpaceAR(node.position);
let myPos = this.effectPanel.convertToNodeSpaceAR(wpos);
if (!this.fireEffect) {
this.fireEffect = cc.instantiate(this.fireworkPreb);
this.effectPanel.addChild(this.fireEffect);
}
let self = this;
self.fireEffect.active = true;
this.fireEffect.getChildByName('effectNode1').getComponent(cc.ParticleSystem).resetSystem();
this.scheduleOnce(function () {
self.fireEffect.getChildByName('effectNode2').active = true;
self.fireEffect.getChildByName('effectNode2').getComponent(cc.ParticleSystem).resetSystem();
}, 1);
this.fireEffect.position = myPos;
this.effectPanel.zIndex = 201;
this.scheduleOnce(function () {
self.fireEffect.active = false;
}, 2);
},
// 显示手指点击效果
showFingerClick(node) {
let wpos = node.parent.convertToWorldSpaceAR(node.position);
let myPos = this.effectPanel.convertToNodeSpaceAR(wpos);
if (!this.fingerEffect) {
this.fingerEffect = cc.instantiate(this.fingerPreb);
this.effectPanel.addChild(this.fingerEffect);
}
this.fingerEffect.active = true;
this.fingerEffect.position = myPos;
},
// 隐藏手指点击效果
hideFingerEffect() {
(this.fingerEffect) && (this.fingerEffect.active = false);
},
// 显示红色警戒蒙板
showAlertPanel() {
this.alertPanel.active = true;
},
// 隐藏红色警戒蒙板
hideAlertPanel() {
this.alertPanel.active = false;
},
// 显示积分收集动画
showScoreCollectEffect(worldPos, targetPosW, score) {
let star = this.createScoreStar();
star.getComponent('scoreEffect').init(this.owner, score);
let sourcePos = this.effectPanel.convertToNodeSpaceAR(worldPos);
let targetPos = this.effectPanel.convertToNodeSpaceAR(targetPosW);
let centerPos = cc.v2(targetPos.x + (sourcePos.x - targetPos.x) / 2, targetPos.y + (sourcePos.y - targetPos.y) / 2);
if (sourcePos.x < 0) {
centerPos.x = targetPos.x + (centerPos.x - targetPos.x) / 2;
centerPos.y = centerPos.y - (targetPos.y - centerPos.y) / 2;
} else {
centerPos.x = centerPos.x + (centerPos.x - targetPos.x) / 2;
centerPos.y = centerPos.y + (targetPos.y - centerPos.y) / 2;
}
let linePos = [sourcePos, centerPos, targetPos];
star.position = sourcePos;
star.getComponent(cc.ParticleSystem).resetSystem();
star.active = true;
let actionTime = Config.score_collect_fly_time;
let action = cc.sequence(cc.bezierTo(actionTime, linePos), cc.callFunc(this.scoreCollected, this, targetPos));
star.runAction(action).easing(cc.easeElasticOut(actionTime));
},
// 显示材料收集动画
showCookieCollectEffect(sourceNd, targetNode, itemId) {
let worldPos = sourceNd.parent.convertToWorldSpaceAR(sourceNd.position);
let star = this.createCookieStar(this.effectPanel);
star.getComponent('scoreEffect').showCookie(itemId);
let sourcePos = this.effectPanel.convertToNodeSpaceAR(worldPos);
let targetPosW = targetNode.parent.convertToWorldSpaceAR(targetNode.position);
let targetPos = this.effectPanel.convertToNodeSpaceAR(targetPosW);
let centerPos = cc.v2(targetPos.x + (sourcePos.x - targetPos.x) / 2, targetPos.y + (sourcePos.y - targetPos.y) / 2);
if (sourcePos.x < 0) {
centerPos.x = targetPos.x + (centerPos.x - targetPos.x) / 2;
centerPos.y = centerPos.y - (targetPos.y - centerPos.y) / 2;
} else {
centerPos.x = centerPos.x + (centerPos.x - targetPos.x) / 2;
centerPos.y = centerPos.y + (targetPos.y - centerPos.y) / 2;
}
let linePos = [sourcePos, centerPos, targetPos];
console.log(linePos);
star.position = sourcePos;
star.getComponent(cc.ParticleSystem).resetSystem();
star.active = true;
let actionTime = Config.score_collect_fly_time;
let action = cc.sequence(cc.bezierTo(actionTime, linePos), cc.callFunc(this.cookieCollected, this, targetPos));
star.runAction(action).easing(cc.easeElasticOut(actionTime));
},
// 获取一个积分飞行动画的实例
createScoreStar: function () {
let star = null;
if (this.scorePool.length > 0) {
star = this.scorePool.shift();
star.active = true;
} else {
star = cc.instantiate(this.scoreEffectPreb);
this.effectPanel.addChild(star);
}
return star;
},
// 获取一个cookie飞行动画的实例
createCookieStar: function (parentNode) {
let star = null;
if (this.cookiePool.size() > 0) {
star = this.cookiePool.get();
} else {
star = cc.instantiate(this.cookieEffectPreb);
}
parentNode.addChild(star);
return star;
},
// 道具飞行到目标后的回调
scoreCollected(obj, pos) {
console.log('score collected');
// obj.parent = null;
obj.active = false;
this.scorePool.push(obj);
},
cookieCollected(obj, pos) {
obj.parent = null;
this.cookiePool.put(obj);
}
});