2025-04-24 17:03:28 +08:00

155 lines
5.0 KiB
JavaScript

import ItemUtil from "../ts/core/ItemUtil";
import { LongPressSpeedProp } from "../ts/core/EEnum";
let CPubFunction = require('./PubFunction');
let GoodsMgr = require('./GoodsMgr');
cc.Class({
extends: cc.Component,
properties: {
titleLab: cc.Label,
listNode: cc.Node,
itemBtn: cc.Node,
},
onLoad() {
// this.node.on(cc.Node.EventType.TOUCH_START, this.touchBegan.bind(this));
this.itemBtn.active = false;
this.curindex = 0;
this.operateid = 0;
this.operatetype = 0;
this.currentBtn = null;
this.timecnt = 0;
this.maxtiemcnt = 6;
this.listInfo = {};
this.canUse = true;
},
initBtn(btn, data) {
btn.datainfo = data;
btn.on(cc.Node.EventType.TOUCH_START, this.propertyBtnClick.bind(this));
btn.on(cc.Node.EventType.TOUCH_END, this.propertyBtnClick.bind(this));
},
propertyBtnClick(e) {
if (e.type == cc.Node.EventType.TOUCH_START) {
this.maxtiemcnt = LongPressSpeedProp.MAXTIMECNT;
this.timecnt = 0;
this.currentBtn = e.target;
}
else if (e.type == cc.Node.EventType.TOUCH_END) {
this.timecnt = this.maxtiemcnt;
this.update();
this.currentBtn = null;
}
},
update() {
if (this.currentBtn == null) {
return;
}
this.timecnt += LongPressSpeedProp.TIMECNTADD;
if (this.timecnt >= this.maxtiemcnt) {
if (this.maxtiemcnt > LongPressSpeedProp.MINTIMECNT) {
this.maxtiemcnt -= LongPressSpeedProp.TIMECNTSUB;
this.maxtiemcnt = Math.max(LongPressSpeedProp.MINTIMECNT, this.maxtiemcnt)
}
this.timecnt = 0;
if (this.canUse) {
this.canUse = false;
this.itemClicked(null, this.currentBtn.datainfo);
}
}
},
loadList(title, list, operateid = 0, operatetype = 0) {
this.operatetype = operatetype;
this.operateid = operateid;
this.titleLab.string = title;
this.listNode.height -= 100;
for (const itemid of list) {
this.addItem(GoodsMgr.getItem(itemid));
}
},
setUIPos(x, y) {
this.listNode.x = x;
this.listNode.y = y + this.listNode.height;
},
addItem(info) {
if (info == null) {
return;
}
this.listNode.height += 100;
let btn = cc.instantiate(this.itemBtn);
btn.parent = this.listNode;
btn.active = true;
btn.y = -90 - 100 * this.curindex;
btn.x = 0;
if (this.operatetype == 0) {
btn.getComponent(cc.Button).clickEvents.push(CPubFunction.CreateEventHandler(this.node, 'UseItemUI', 'itemClicked', info.id));
}
else {
this.initBtn(btn, info.id);
}
btn.getChildByName('icon').getComponent(cc.Sprite).spriteFrame = ItemUtil.getItemIconBy(info);
btn.getChildByName('name').getComponent(cc.Label).string = info.name;
btn.getChildByName('detail').getComponent(cc.Label).string = info.detailshot;
if (info.id < 90000) {//90000以上為特殊物品
btn.getChildByName('num').getComponent(cc.Label).string = info.count;
if (info.count == 0) {
btn.getChildByName('num').color = cc.Color.RED;
btn.getChildByName('get').active = true;
}
}
else {
btn.getChildByName('num').active = false;
btn.getChildByName('detail').color = cc.Color.GREEN;
}
this.curindex++;
this.listInfo[info.id] = btn;
},
refreshInfo() {
for (const key in this.listInfo) {
if (this.listInfo.hasOwnProperty(key)) {
const btn = this.listInfo[key];
let info = GoodsMgr.getItem(key);
if (key < 90000) {//90000以上為特殊物品
btn.getChildByName('num').getComponent(cc.Label).string = info.count;
if (info.count == 0) {
btn.getChildByName('num').color = cc.Color.RED;
btn.getChildByName('get').active = true;
}
}
}
}
this.canUse = true;
},
refreshInfoNum() {
for (const key in this.listInfo) {
if (this.listInfo.hasOwnProperty(key)) {
const btn = this.listInfo[key];
let info = GoodsMgr.getItem(key);
if (key < 90000) {//90000以上為特殊物品
btn.getChildByName('num').getComponent(cc.Label).string = info.count;
if (info.count == 0) {
btn.getChildByName('num').color = cc.Color.RED;
btn.getChildByName('get').active = true;
}
}
}
}
this.canUse = true;
},
itemClicked(event, data) {
ItemUtil.useItem(data, 1, this.operateid);
},
touchBegan(event) {
this.node.destroy();
},
});