var Utils = require("Utils") var gameConfig = require("gameConfig") var dropItemConfig = gameConfig.dropItemConfig var dropFrame = {} var dreadyFrame = {} var Main = require("Main") var commonsacle = 0.4 var gunscale = 1 var dcolorarr = [ "#00FF29", "#00D6FF", "#EB00FF", "#FFA300", "#FF0000", ] var dropManage = function() { this.itempool = []; this.itemarr = []; this.carpool = []; this.itemNode = null; this.setFrame = function(dir, frameName, sprite) { if (dropFrame[frameName]) { sprite.sizeMode = 2 sprite.spriteFrame = dropFrame[frameName]; } else { if (dreadyFrame[frameName] == undefined) { dreadyFrame[frameName] = [] cc.loader.loadRes(dir + frameName, cc.SpriteFrame, function(err, res) { if (!err && sprite.isValid) { dropFrame[frameName] = res for (var i = dreadyFrame[frameName].length - 1; i >= 0; i--) { try { dreadyFrame[frameName][i].spriteFrame = res dreadyFrame[frameName][i].sizeMode = 2 } catch (e) {} } dreadyFrame[frameName] = null } }); } if (dreadyFrame[frameName]) { dreadyFrame[frameName].push(sprite) } } } this.reset = function() { this.itempool.length = 0; this.itemarr.length = 0; this.itemNode = null; } this.clean = function() { for (var i = 0; i < this.itempool.length; i++) { this.itempool[i].icon = null } for (var i = 0; i < this.itemarr.length; i++) { this.itemarr[i].icon = null } this.reset() } this.createItem = function(id, pos, data2) { var dt = dropItemConfig[id] if (!dt) { console.log(id) } if (dt.equip_type == 9) { // var tank // if (this.carpool.length > 0) { // tank = this.carpool.pop(); // }else{ // tank = cc.instantiate(cc.gameMgr.pbtank) // tank.width = 70 // tank.height = 70 // tank.istank = true // // tank.rotation = -45 // } // this.itemarr.push(tank) // tank.position = pos // tank.needDelete=false // cc.gameMgr.ndplayer.addChild(tank) // tank.dbid = id // tank.lifeTime = 99999 // tank.opacity = 255 // tank.zIndex = -tank.y // //tank.getComponent("horsectrl").initdata(id,true) // return tank var tank = new cc.Node() tank.position = pos tank.needDelete = false cc.gameMgr.ndplayer.addChild(tank) tank.dbid = id tank.lifeTime = 99999 tank.opacity = 255 tank.zIndex = -tank.y /cc.zscale tank.istank = true tank.resname = dt.model tank.width = 70 tank.height = 70 this.itemarr.push(tank) var cb = function(nd) { if (tank.isValid) { tank.addChild(nd) tank.model = nd } } cc.gameMgr.effectManager.createprefab(dt.model, "prefabs/gameprefab/", cb) return tank } var item if (this.itempool.length > 0) { item = this.itempool.pop(); } else { item = new cc.Node(); item.width = 32; item.height = 32; item.icon = new cc.Node(); let sp1 = item.icon.addComponent(cc.Sprite); item.icon.width = 24; item.icon.height = 24; item.icon.scale = 0.4 item.istank = false let sp2 = item.addComponent(cc.Sprite); //this.setFrame("icons/","loot-circle-blue",sp2); // item.rotation = -45 item.scaleY = 1 // 1/cc.gameMgr.shijiao item.addChild(item.icon) //sp1.sizeMode = 0; sp2.sizeMode = 0; sp1.sizeMode = 2 sp1.trim = false this.spani = item.icon.addComponent(cc.Animation) item.icon.getComponent(cc.Animation).addClip(cc.battleCache.drop_ani, "drop_ani"); item.icon.getComponent(cc.Animation).addClip(cc.battleCache.drop_ani2, "drop_ani2"); // this.spani.play("drop_ani") } // if (dt.quality != 999) { // this.spani.play("drop_ani") // } else { // this.spani.stop() // } if (item.gunpb) { cc.gameMgr.effectManager.huishouprefab(item.gunpb.pname, item.gunpb) item.gunpb = null } item.opacity = 255 item.lifeTime = 99999 this.itemarr.push(item) item.stopAllActions() if (!data2.show_anim) { item.position = pos } else { item.position = cc.v2(data2.born_pos.x, data2.born_pos.y) item.runAction(cc.moveTo(0.3, pos)); } item.zIndex = -pos.y /cc.zscale item.needDelete = false this.itemNode.addChild(item) item.dbid = id // item.data = dt; // item.data2 = data2; let sp1 = item.icon.getComponent(cc.Sprite); var resname = dt.model item.icon.scale = commonsacle // item.zIndex = 0 var dirpath = "icons/" var isava = false if (dt.equip_type == 10 && dt.equip_subtype == 12) { dirpath = "avatar/cloth/" item.icon.scale = 0.3 * cc.hdscale // item.zIndex = 1 isava = true } else if (dt.equip_type == 10 && dt.equip_subtype == 11) { dirpath = "avatar/hat/" item.icon.scale = 0.3 * cc.hdscale // item.zIndex = 1 isava = true } if (dt.quality != 999) { this.setFrame("icons/", "loot-circle-blue", item.getComponent(cc.Sprite)); } else { item.getComponent(cc.Sprite).spriteFrame = null } if (dt.quality) { if(dt.quality!=999){ item.color = new cc.color().fromHEX(dcolorarr[Number(dt.quality) - 1]) this.spani.play("drop_ani") }else{ this.spani.play("drop_ani2") } } else { item.color = new cc.color().fromHEX(dcolorarr[0]) this.spani.play("drop_ani") } if (dt.equip_type == 1) { var lv = data2.item_level if (!lv) { lv = 1 } sp1.spriteFrame = null var cb = function(nd) { if (item.gunpb) { if (cc.gameMgr && cc.gameMgr.effectManager) { cc.gameMgr.effectManager.huishouprefab(item.gunpb.pname, item.gunpb) } item.gunpb = null } nd.scale = gunscale nd.pname = dt.world_img item.gunpb = nd item.icon.addChild(nd) var cmp = nd.getComponent(cc.Animation) if (cmp) { cmp.play(nd.pname + "_" + lv) } } cc.gameMgr.effectManager.createprefab(dt.world_img, "prefabs/gunprefab/", cb) } else { this.setFrame(dirpath, resname, sp1); } return item } this.update = function(playerarr, dt) { var px = cc.gameMgr.watchPlayer.x var py = cc.gameMgr.watchPlayer.y var vw = cc.winSize.width / 2 / cc.gameMgr.viewScale +100 var vh = cc.winSize.height / 2 / cc.gameMgr.viewScale +100 var dxx = 0 var dyy = 0 // var vw = cc.winSize.width/1.5/cc.gameMgr.viewScale // var vh = cc.winSize.height/1.5/cc.gameMgr.viewScale for (var i = this.itemarr.length - 1; i >= 0; i--) { var item = this.itemarr[i]; item.lifeTime -= dt if (item.lifeTime < 0) { cc.gameMgr.objMap[item.objuuid] = null; item.needDelete = true } if (item.needDelete) { if (!item.istank) { item.removeFromParent(false); this.itempool.push(item) } else { if (item.model) { cc.gameMgr.effectManager.huishouprefab(item.resname, item.model) } item.destroy() // this.carpool.push(item) } this.itemarr.splice(i, 1); continue; } dxx = item.x - px dyy = item.y - py if (Math.abs(dxx) > vw || Math.abs(dyy) > vh ) { item.opacity = 0 } else { item.opacity = 255 } if(!cc.gameMgr.nditem.active){ item.opacity = 0 } } } this.getNearDrop = function(horsemode) { var playerarr = cc.gameMgr.playerarr; var outarr = [] for (var i = this.itemarr.length - 1; i >= 0; i--) { var item = this.itemarr[i]; if (item.opacity == 255) { outarr.push(item) } } if (!horsemode) { for (var i = cc.gameMgr.cararr.length - 1; i >= 0; i--) { if (cc.gameMgr.cararr[i].pctrl.canzuo()) { outarr.push(cc.gameMgr.cararr[i]) } } } if (outarr.length == 0) { return null } outarr = outarr.sort((a, b) => { var disa = Math.pow(cc.gameMgr.watchPlayer.x - a.x, 2) + Math.pow(cc.gameMgr.watchPlayer.y - a.y, 2) var disb = Math.pow(cc.gameMgr.watchPlayer.x - b.x, 2) + Math.pow(cc.gameMgr.watchPlayer.y - b.y, 2) return disa - disb; }); if (Utils.hitTestRectangle(outarr[0], cc.gameMgr.watchPlayer)) { return outarr[0] } else { return null } } } module.exports = dropManage