pubgv3/assets/scripts/game/btnEquip.js
2022-05-26 11:46:36 +08:00

292 lines
7.9 KiB
JavaScript

// Learn cc.Class:
// - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
// - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
// - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
// - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
// - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
// - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html
var gameConfig = require("gameConfig");
cc.Class({
extends: cc.Component,
properties: {
sp_equip: {
default: null,
type: cc.Sprite,
},
nd_chose: {
default: null,
type: cc.Node,
},
sp_quality: {
default: null,
type: cc.Sprite,
},
},
// LIFE-CYCLE CALLBACKS:
// onLoad () {},
onLoad() {
this.dropTime = 1;
this._initTouchEvent();
this.startTime = 0;
this.startUpdate = false;
if (this.sp_quality) {
this.sp_quality.spriteFrame = null;
}
},
setdata(player, idx) {
this.idx = idx;
this.player = player;
this.oldscale = this.node.scale;
if (this.idx > 2) {
if (!cc.pcmode) {
this.node.active = false;
}
this.lb_count = this.node
.getChildByName("lb_count")
.getComponent(cc.Label);
this.lb_count.string = "0";
}
// if(idx==4){
// this.node.active = false;
// }
},
changeguncount() {
if (this.lb_count) {
this.lb_count.string = cc.gameMgr.watchPlayer.pctrl.gunsammo[this.idx];
}
},
onGetIetm(lv) {
if (this.idx > 2) {
this.lb_count.string = cc.gameMgr.watchPlayer.pctrl.gunsammo[this.idx];
this.node.active = true;
}
if (cc.gameMgr.watchPlayer.pctrl.guns[this.idx] == 0) {
if (this.idx > 2 && !cc.pcmode) {
this.node.active = false;
}
if (this.idx == 1) {
cc.gameMgr.uic.lb_bulletCount.node.opacity = 0;
} else if (this.idx == 2) {
cc.gameMgr.uic.lb_bulletCount2.node.opacity = 0;
}
if (this.gunpb) {
cc.gameMgr.effectManager.huishouprefab(this.gunpb.pname, this.gunpb);
this.gunpb = null;
}
this.sp_equip.spriteFrame = null;
if (this.sp_quality) {
this.sp_quality.spriteFrame = null;
}
return;
}
// this.node.active = true
var self = this;
var cdata =
gameConfig.dropItemConfig[cc.gameMgr.watchPlayer.pctrl.guns[this.idx]];
if (this.idx == 1 || this.idx == 2) {
if (this.idx == 1) {
cc.gameMgr.uic.lb_bulletCount.node.opacity = 255;
} else if (this.idx == 2) {
cc.gameMgr.uic.lb_bulletCount2.node.opacity = 255;
}
this.sp_equip.spriteFrame = null;
var cb = function (nd) {
if (self.gunpb) {
if (cc.gameMgr && cc.gameMgr.effectManager) {
cc.gameMgr.effectManager.huishouprefab(
self.gunpb.pname,
self.gunpb
);
}
self.gunpb = null;
}
if (cc.gameMgr.watchPlayer.pctrl.guns[self.idx] == 0) {
return;
}
nd.pname = cdata.world_img;
self.gunpb = nd;
nd.scale = 1;
self.sp_equip.node.addChild(nd);
if (!lv) {
lv = 1;
}
var cmp = nd.getComponent(cc.Animation);
if (cmp) {
cmp.play(nd.pname + "_" + lv);
}
if (cdata.offx) {
nd.x = cdata.offx;
} else {
nd.x = 0;
}
if (cdata.offy) {
nd.y = cdata.offy;
} else {
nd.y = 0;
}
};
cc.gameMgr.effectManager.createprefab(
cdata.world_img,
"prefabs/gunprefab/",
cb
);
this.sp_quality.spriteFrame = null;
cc.loader.loadRes(
"icons/pic_quality" + cdata.quality,
cc.SpriteFrame,
function (err, res) {
if (!err && self.isValid) {
self.sp_quality.spriteFrame = res;
}
}
);
return;
}
var img = cdata.world_img;
cc.loader.loadRes("icons/" + img, cc.SpriteFrame, function (err, res) {
if (!err && self.isValid) {
self.sp_equip.node.position = cc.v2(
self.sp_equip.node.position.x,
self.sp_equip.node.position.y + 40
);
self.sp_equip.spriteFrame = res;
self.sp_equip.node.runAction(
cc.spawn(
cc.fadeIn(0.3),
cc.moveTo(
0.3,
cc.v2(self.sp_equip.node.x, self.sp_equip.node.y - 40)
)
)
);
} else {
}
});
},
_initTouchEvent: function () {
this.node.on(cc.Node.EventType.TOUCH_START, this._touchStartEvent, this);
this.node.on(cc.Node.EventType.TOUCH_END, this._touchEndEvent, this);
this.node.on(cc.Node.EventType.TOUCH_CANCEL, this._touchCanCelEvent, this);
cc.Notifier.on("getGun", this, this.getGun.bind(this));
cc.Notifier.on("switchGun", this, this.switchGun.bind(this));
cc.Notifier.on("changeguncount", this, this.changeguncount.bind(this));
//cc.director.on('boomSwitch', this.boomSwitch,this);
},
getGun(res) {
if (this.idx == res.idx) {
this.onGetIetm(res.lv);
// if(res.idx==4&&this.player.guns[3]==0){
// cc.director.emit('boomSwitch',4,3)
// }
// else if(res.idx==3&&this.player.guns[4]==0){
// cc.director.emit('boomSwitch',3,4)
// }
}
},
switchGun(gun) {
if (this.idx == gun) {
if (this.nd_chose) {
this.nd_chose.active = true;
}
} else {
if (this.nd_chose) {
this.nd_chose.active = false;
}
}
},
onDestroy() {
this.node.off(cc.Node.EventType.TOUCH_START, this._touchStartEvent, this);
this.node.off(cc.Node.EventType.TOUCH_END, this._touchEndEvent, this);
this.node.off(cc.Node.EventType.TOUCH_CANCEL, this._touchCanCelEvent, this);
cc.Notifier.off("getGun", this);
cc.Notifier.off("switchGun", this);
cc.Notifier.off("changeguncount", this);
//cc.director.off('boomSwitch', this.boomSwitch,this);
},
// boomSwitch(idx,idx2){
// if(this.idx==idx){
// this.node.active = true
// }
// else if(this.idx==idx2){
// this.node.active = false
// }
// },
_touchStartEvent() {
if (!this.player || this.player.isdead()) {
return;
}
if (this.player.guns[this.idx]) {
this.startTime = 0;
this.startUpdate = true;
this.node.scale = this.oldscale - 0.1;
}
},
_touchEndEvent() {
if (!this.player || this.player.isdead()) {
return;
}
if (this.startUpdate) {
this.player.switchGunNet(this.idx);
// if(this.idx>2){
// cc.gameMgr.uic.btn_boom.getComponent("btnEquipBoom").setidx( this.idx )
// }
if (this.idx > 2 && !cc.pcmode) {
this.node.parent.active = false;
}
}
this.node.scale = this.oldscale;
this.startUpdate = false;
this.startTime = 0;
},
_touchCanCelEvent() {
if (!this.player || this.player.isdead()) {
return;
}
this.node.scale = this.oldscale;
this.startUpdate = false;
this.startTime = 0;
},
update(dt) {
if (this.idx > 2) {
return;
}
if (this.startUpdate) {
if (this.player.othertime <= 0) {
this.startTime += dt;
}
if (this.startTime > this.dropTime) {
if (!this.player.isdead()) {
this.player.dropItem(this.idx);
}
this.startUpdate = false;
this.node.scale = this.oldscale;
}
}
},
});