var Utils = require("Utils") var SMOKETIME = 10 var BREAKTIME = 0.5 var BULLRTTIME = 1 var breadyFrame = {} var parpbs = {} var parpbsIns = {} var BOOMTIME = { 0: 0.95, 1: 1.6, 2: 1.56, 3: 1.64, 4: 1.84, 5: 1.42, 6: 3, 7: 2.47, 8: 1.6, 9: 1.64, 10: 1.64, 11: 1.64, } cc.ParticleSystem.prototype.resetSystem2 = function() { // this._stopped = false; let particles = this._simulator.particles for (let id = 0; id < particles.length; ++id) { particles[id].size = 0; } this._simulator.step(0); this._simulator.reset(); this._activateMaterial(); } var effectManage = function() { // breadyFrame = {} // parpbs = {} // parpbsIns = {} this.boompool = []; this.bulletpool = []; this.smokepool = []; this.breakpool = []; this.bulletparpool = [] this.bulletparArr = [] this.effNode = null this.smokeArr = []; this.boomArr = []; this.breakArr = []; this.bulletEffArr = [] this.shadowarr = [] this.shadowpool = []; this.prefabmap = { } this.createprefab = function(name, dir, cb) { if (!parpbsIns[name]) { parpbsIns[name] = [] } if (parpbsIns[name].length > 0) { var obj = parpbsIns[name].pop(); cb(obj) } else { this.loadprefab(name, dir, cb) } } this.huishouprefab = function(name, nd) { nd.removeFromParent(false) parpbsIns[name].push(nd) } this.createlooteff2 = function(name, ndcankao, rot) { var self = this var cb = function(nd) { let pos1 = ndcankao.parent.convertToWorldSpaceAR(ndcankao); let pos2 = self.effNode.convertToNodeSpaceAR(pos1); nd.rotation = rot nd.position = pos2 nd.lifetime = 8 nd.resname = name self.boomArr.push(nd) self.effNode.addChild(nd) } this.createprefab(name, "prefabs/effprefab/", cb) } this.createlooteff = function(name, cb) { if (!parpbsIns[name]) { parpbsIns[name] = [] } if (parpbsIns[name].length > 0) { var bullet = parpbsIns[name].pop(); cb(bullet) } else { this.loadprefab(name, "prefabs/effprefab/", cb) } } this.huishoulooteff = function(name, nd) { if (!nd) { return } nd.removeFromParent(false) parpbsIns[name].push(nd) } this.loadprefab = function(name, dir, cb) { if (parpbs[name]) { let nd = cc.instantiate(parpbs[name]) nd.parname = name cb(nd); } else { if (breadyFrame[name] == undefined) { breadyFrame[name] = [] cc.loader.loadRes(dir + name, cc.Prefab, function(err, res) { if (!err) { parpbs[name] = res if (cc.gameMgr && cc.gameMgr.isValid) { let tttt = breadyFrame[name] for (let i = tttt.length - 1; i >= 0; i--) { let nd = cc.instantiate(res) nd.parname = name tttt[i](nd) } } breadyFrame[name] = null } }); } breadyFrame[name].push(cb) } }, this.clean = function() { this.reset() } this.reset = function() { this.boompool.length = 0; this.bulletpool.length = 0; this.smokepool.length = 0; this.breakpool.length = 0; this.effNode = null this.smokeArr.length = 0; this.boomArr.length = 0; this.breakArr.length = 0; this.bulletparpool.length = 0; this.bulletparArr.length = 0; this.shadowarr.length = 0; this.shadowpool.length = 0; } this.init = function() { for (var i = 0; i < 30; i++) { var effect = cc.instantiate(cc.gameMgr.pbbullet) effect.getComponent(cc.ParticleSystem).positionType = 1 this.bulletparpool.push(effect) effect.active = false cc.gameMgr.ndtree.addChild(effect) } } this.createboomeff = function(pos, aniname, time) { // var effect = null; var self = this; if (!time) { time = 0.5 } var cb = function(effect) { effect.lifetime = time; effect.position = pos effect.resname = aniname self.boomArr.push(effect) self.effNode.addChild(effect) } this.createprefab(aniname, "prefabs/gameprefab/", cb) // if (this.boompool.length > 0) { // effect = this.boompool.pop(); // } else { // effect = cc.instantiate(cc.gameMgr.pbboom) // } // if(!time){ // time = 0.5 // } // effect.getComponent(cc.Animation).stop() // effect.getComponent(cc.Animation).play(aniname) // effect.lifetime = time; // this.boomArr.push(effect) // effect.position = pos // this.effNode.addChild(effect) } this.playboomsoun = function(pos, soundname) { var px = cc.gameMgr.watchPlayer.x var py = cc.gameMgr.watchPlayer.y var vw = cc.winSize.width / 2 / cc.gameMgr.viewScale var vh = cc.winSize.height / 2 / cc.gameMgr.viewScale if (!(Math.abs(pos.x - px) > vw + pos.width / 2 || Math.abs(pos.y - py > vh + pos.height / 2))) { cc.gameMgr.playSound(soundname) } } this.createBoom = function(pos, aniid) { if (aniid == 999) { return } var aniname = "eff_boom" if (aniid > 0) { aniname = aniname + aniid } this.createboomeff(pos, aniname, BOOMTIME[aniid]) this.playboomsoun(pos, "game_explosion") } this.createBulletP = function(pos) { var effect = null; var self = this; if (this.bulletparpool.length > 0) { effect = this.bulletparpool.pop(); } else { effect = cc.instantiate(cc.gameMgr.pbbullet) effect.getComponent(cc.ParticleSystem).positionType = 1 effect.zIndex = -1 cc.gameMgr.ndtree.addChild(effect) } effect.active = true effect.getComponent(cc.ParticleSystem).resetSystem() effect.lifetime = 0.85; this.bulletparArr.push(effect) effect.x = pos.x effect.y = pos.y } this.createBulletHit = function(pos, aniname) { var self = this; var cb = function(effect) { effect.lifetime = BULLRTTIME; effect.position = pos effect.resname = aniname self.boomArr.push(effect) self.effNode.addChild(effect) } this.createprefab(aniname, "prefabs/gameprefab/", cb) // var effect = null; // var self = this; // if (this.bulletpool.length > 0) { // effect = this.bulletpool.pop(); // } else { // effect = cc.instantiate(cc.gameMgr.pbbullethit) // this.effNode.addChild(effect) // } // this.bulletEffArr.push(effect) // effect.active = true // effect.position = pos // effect.getComponent(cc.Animation).stop() // effect.getComponent(cc.Animation).play(aniname) // effect.lifetime = BULLRTTIME; } this.createSmoke = function(pos, timeadd) { var effect = null; var self = this; if (this.smokepool.length > 0) { effect = this.smokepool.pop(); } else { effect = cc.instantiate(cc.gameMgr.pbsmoke) effect.getComponent(cc.ParticleSystem).positionType = 1 } effect.opacity = 255 effect.lifetime = SMOKETIME; if (timeadd) { effect.lifetime = effect.lifetime + timeadd } this.smokeArr.push(effect) effect.position = pos this.effNode.addChild(effect) var px = cc.gameMgr.watchPlayer.x var py = cc.gameMgr.watchPlayer.y var vw = cc.winSize.width / 2 / cc.gameMgr.viewScale var vh = cc.winSize.height / 2 / cc.gameMgr.viewScale if (!(Math.abs(pos.x - px) > vw + pos.width / 2 || Math.abs(pos.y - py > vh + pos.height / 2))) { cc.gameMgr.playSound("game_explosion_smoke") } } this.createBreak = function(pos, tex) { var effect = null; var self = this; if (this.breakpool.length > 0) { effect = this.breakpool.pop(); } else { effect = cc.instantiate(cc.gameMgr.pbbreak) effect.getComponent(cc.ParticleSystem).positionType = 1 } var par = effect.getComponent(cc.ParticleSystem) par.spriteFrame = null par.spriteFrame = cc.atlMgr.mapthingatlas.getSpriteFrame(tex) effect.getComponent(cc.ParticleSystem).resetSystem() effect.lifetime = BREAKTIME; this.breakArr.push(effect) effect.position = pos this.effNode.addChild(effect) } this.createshadow = function(player) { var effect if (this.shadowpool.length > 0) { effect = this.shadowpool.pop(); // effect.width = effect.height = 100 } else { effect = new cc.Node(); effect.addComponent(cc.Sprite) // effect.rotation = -45 effect.scaleX = player.node.scaleX effect.scaleY = player.node.scaleY } effect.lifetime = 1 effect.getComponent(cc.Sprite).spriteFrame = Utils.rendernode(player.node, 100) effect.x = player.node.x effect.y = player.node.y effect.opacity = 255 effect.zIndex = player.node.zIndex player.node.parent.addChild(effect) this.shadowarr.push(effect) } this.update = function(dt) { for (var i = this.shadowarr.length - 1; i >= 0; i--) { this.shadowarr[i].lifetime -= dt this.shadowarr[i].opacity -= dt * 255 if (this.shadowarr[i].lifetime <= 0) { this.shadowpool.push(this.shadowarr[i]) this.shadowarr[i].removeFromParent(false); this.shadowarr.splice(i, 1); } } for (var i = this.bulletEffArr.length - 1; i >= 0; i--) { this.bulletEffArr[i].lifetime -= dt if (this.bulletEffArr[i].lifetime <= 0) { this.bulletpool.push(this.bulletEffArr[i]) this.bulletEffArr[i].active = false this.bulletEffArr.splice(i, 1); } } for (var i = this.breakArr.length - 1; i >= 0; i--) { this.breakArr[i].lifetime -= dt if (this.breakArr[i].lifetime <= 0) { var par = this.breakArr[i].getComponent(cc.ParticleSystem) par.resetSystem2(); this.breakArr[i].removeFromParent(false); this.breakpool.push(this.breakArr[i]) this.breakArr.splice(i, 1); } } for (var i = this.bulletparArr.length - 1; i >= 0; i--) { this.bulletparArr[i].lifetime -= dt if (this.bulletparArr[i].lifetime <= 0) { //var par = this.bulletparArr[i].getComponent(cc.ParticleSystem) this.bulletparArr[i].active = false this.bulletparpool.push(this.bulletparArr[i]) this.bulletparArr.splice(i, 1); } } for (var i = this.smokeArr.length - 1; i >= 0; i--) { this.smokeArr[i].lifetime -= dt if (this.smokeArr[i].lifetime <= 0) { this.fadesmoke(this.smokeArr[i]) this.smokeArr.splice(i, 1); } } for (var i = this.boomArr.length - 1; i >= 0; i--) { this.boomArr[i].lifetime -= dt if (this.boomArr[i].lifetime <= 0) { this.huishouprefab(this.boomArr[i].resname, this.boomArr[i]) // this.boomArr[i].removeFromParent(false); // this.boompool.push(this.boomArr[i]) this.boomArr.splice(i, 1); } } } this.fadesmoke = function(smoke) { var self = this var cb = cc.callFunc(function() { var par = smoke.getComponent(cc.ParticleSystem) par.resetSystem2(); smoke.removeFromParent(false); self.smokepool.push(smoke) }); smoke.runAction(cc.sequence(cc.fadeOut(2), cb)); } } module.exports = effectManage