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

196 lines
5.5 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,
},
},
// LIFE-CYCLE CALLBACKS:
// onLoad () {},
setdata(player,idx){
this.idx = -1
this.player = player
this.oldscale = this.node.scale
this.dropTime = 1;
this._initTouchEvent()
this.startTime = 0;
this.startUpdate = false;
this.lb_count = this.node.getChildByName("lb_count").getComponent(cc.Label)
this.lb_count.string = ""
},
setidx(v){
this.idx = v
this.onGetIetm()
},
onGetIetm(){
if(this.idx==-1||cc.gameMgr.watchPlayer.pctrl.guns[this.idx]==0){
this.sp_equip.spriteFrame = null
this.lb_count.string = ""
return
}
var self = this;
var cdata = gameConfig.dropItemConfig[cc.gameMgr.watchPlayer.pctrl.guns[this.idx]]
var img = cdata.world_img
cc.loader.loadRes("icons/"+img, cc.SpriteFrame, function (err, res) {
if (!err&&self.isValid) {
self.sp_equip.spriteFrame = res;
}
});
// this.changeguncount()
},
_initTouchEvent: function()
{
if(cc.pcmode){
return
}
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(res.idx<=2){
return
}
if(this.idx==-1 && res.idx>2){
this.idx = res.idx
}
if(cc.gameMgr.watchPlayer.pctrl.guns[this.idx]==0){
this.idx = res.idx
}
if(this.idx == res.idx){
this.onGetIetm(res.lv);
}
},
switchGun(gun){
if(gun>2){
this.setidx(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);
},
changeguncount(){
if(this.player.gunsammo[this.idx]){
this.lb_count.string = this.player.gunsammo[this.idx]
}else{
this.lb_count.string = ""
}
},
// 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()||this.idx==-1){
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()||this.idx==-1){
return;
}
if(this.startUpdate){
this.player.switchGunNet(this.idx)
}
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;
}
}
},
});