387 lines
15 KiB
TypeScript
387 lines
15 KiB
TypeScript
|
import GameModel from "./core/GameModel";
|
||
|
import GameUtil from "./core/GameUtil";
|
||
|
import ItemUtil from "./core/ItemUtil";
|
||
|
import FGUtil from "./gear_2.3.4/fgui/FGUtil";
|
||
|
import SKDataUtil from "./gear_2.3.4/util/SKDataUtil";
|
||
|
import SKUIUtil from "./gear_2.3.4/util/SKUIUtil";
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
const { ccclass, property } = cc._decorator;
|
||
|
@ccclass
|
||
|
export default class Shop extends cc.Component {
|
||
|
/**
|
||
|
* 單例實例
|
||
|
*/
|
||
|
public static Instance: Shop = null;
|
||
|
|
||
|
/**
|
||
|
* 主面板
|
||
|
*/
|
||
|
mainPanel: fgui.GComponent = null;
|
||
|
|
||
|
shopList: any = [];
|
||
|
bagList: any = [];
|
||
|
calcBox: fgui.GComponent = null;
|
||
|
/**
|
||
|
* 選擇的商品信息
|
||
|
*/
|
||
|
selectedGoodItemsInfo: any = null;
|
||
|
selectedId: number = -1;
|
||
|
selectedMax: number = 1;
|
||
|
onLoad() {
|
||
|
if (Shop.Instance === null) {
|
||
|
Shop.Instance = this;
|
||
|
} else {
|
||
|
this.destroy();
|
||
|
return;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
openShopMainPanel(page: number = 0) {
|
||
|
if (!SKUIUtil.isFGUIValid(this.mainPanel)) {
|
||
|
this.mainPanel = FGUtil.create("main_ui", "guo_shop_panel");
|
||
|
FGUtil.root().addChild(this.mainPanel);
|
||
|
this.mainPanel.makeFullScreen();
|
||
|
}
|
||
|
|
||
|
FGUtil.getComponent(this.mainPanel, "mask").onClick(this.closeAll, this);
|
||
|
FGUtil.getButton(this.mainPanel, "alert/n4").onClick(this.closeAll, this);
|
||
|
FGUtil.getButton(this.mainPanel, "alert/n49").onClick(this.goBuyPage, this);
|
||
|
FGUtil.getButton(this.mainPanel, "alert/n50").onClick(this.goSalePage, this);
|
||
|
FGUtil.getButton(this.mainPanel, "alert/n50").onClick(this.goSalePage, this);
|
||
|
FGUtil.getButton(this.mainPanel, "alert/buy_box/n6").onClick(this.showCalcPad, this);
|
||
|
FGUtil.getButton(this.mainPanel, "alert/sale_box/n7").onClick(this.showCalcPad, this);
|
||
|
|
||
|
FGUtil.getButton(this.mainPanel, "alert/buy_box/add").onClick(this.addGoods, this);
|
||
|
FGUtil.getButton(this.mainPanel, "alert/buy_box/reduce").onClick(this.reduceGoods, this);
|
||
|
FGUtil.getButton(this.mainPanel, "alert/sale_box/add").onClick(this.addGoods, this);
|
||
|
FGUtil.getButton(this.mainPanel, "alert/sale_box/reduce").onClick(this.reduceGoods, this);
|
||
|
|
||
|
|
||
|
FGUtil.getButton(this.mainPanel, "alert/buy_box/buyBtn").onClick(this.buyGuoItem, this);
|
||
|
FGUtil.getButton(this.mainPanel, "alert/sale_box/buyBtn").onClick(this.saleBagItem, this);
|
||
|
|
||
|
|
||
|
|
||
|
// 頂部金幣組件動畫
|
||
|
var trans: fgui.Transition = this.mainPanel.getTransition("drop");
|
||
|
trans.play();
|
||
|
|
||
|
this.refreshTopMoneyByType();
|
||
|
|
||
|
|
||
|
|
||
|
var shopList = FGUtil.getList(this.mainPanel, "alert/buy_box/list");
|
||
|
shopList.itemRenderer = this.initBuyItem.bind(this);
|
||
|
shopList.setVirtual();
|
||
|
|
||
|
var saleList = FGUtil.getList(this.mainPanel, "alert/sale_box/n0");
|
||
|
saleList.itemRenderer = this.initSaleItem.bind(this);
|
||
|
saleList.setVirtual();
|
||
|
|
||
|
if (page == 0)
|
||
|
this.goBuyPage();
|
||
|
else
|
||
|
this.goSalePage();
|
||
|
}
|
||
|
|
||
|
buyGuoItem() {
|
||
|
var num = parseInt(FGUtil.getTextField(this.calcBox, "num").text);
|
||
|
var price = parseInt(FGUtil.getButton(this.calcBox, "buyBtn").title);
|
||
|
|
||
|
GameModel.send("c2s_buymall_items", {
|
||
|
type: 4,
|
||
|
mallid: this.selectedGoodItemsInfo.id,
|
||
|
mallnum: num,
|
||
|
mallprices: price,
|
||
|
})
|
||
|
}
|
||
|
saleBagItem() {
|
||
|
var num = parseInt(FGUtil.getTextField(this.calcBox, "num").text);
|
||
|
var price = parseInt(FGUtil.getButton(this.calcBox, "buyBtn").title);
|
||
|
|
||
|
GameModel.send("c2s_recycle_item", {
|
||
|
roleid: GameModel.player.roleid,
|
||
|
itemid: this.selectedGoodItemsInfo.itemid,
|
||
|
count: num,
|
||
|
guoScore: price,
|
||
|
})
|
||
|
}
|
||
|
showCalcPad() {
|
||
|
if (!SKUIUtil.isFGUIValid(this.mainPanel)) return;
|
||
|
FGUtil.getComponent(this.calcBox, "calc").visible = true;
|
||
|
FGUtil.getTextField(this.calcBox, "num").text = "";
|
||
|
var realPirce = this.selectedGoodItemsInfo.discountPrice ? this.selectedGoodItemsInfo.discountPrice : this.selectedGoodItemsInfo.price
|
||
|
FGUtil.getButton(this.calcBox, "buyBtn").title = realPirce.toString();
|
||
|
}
|
||
|
|
||
|
goBuyPage() {
|
||
|
if (!SKUIUtil.isFGUIValid(this.mainPanel)) return;
|
||
|
FGUtil.getControl(this.mainPanel, "alert/page").selectedIndex = 0;
|
||
|
FGUtil.getControl(this.mainPanel, "alert/buy_box/choose").selectedIndex = 0;
|
||
|
this.calcBox = FGUtil.getComponent(this.mainPanel, "alert/buy_box");
|
||
|
let calcPad = FGUtil.getComponent(this.calcBox, "calc")
|
||
|
this.initCalcPad(calcPad);
|
||
|
GameModel.send("c2s_integral_mall", {
|
||
|
roleid: GameModel.player.roleid
|
||
|
})
|
||
|
}
|
||
|
|
||
|
goSalePage() {
|
||
|
if (!SKUIUtil.isFGUIValid(this.mainPanel)) return;
|
||
|
FGUtil.getControl(this.mainPanel, "alert/page").selectedIndex = 1;
|
||
|
FGUtil.getControl(this.mainPanel, "alert/sale_box/choose").selectedIndex = 0;
|
||
|
this.calcBox = FGUtil.getComponent(this.mainPanel, "alert/sale_box");
|
||
|
let calcPad = FGUtil.getComponent(this.calcBox, "calc")
|
||
|
this.initCalcPad(calcPad);
|
||
|
this.initSaleData();
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
* 初始化項目
|
||
|
*/
|
||
|
initBuyItem(idx, obj: fairygui.GObject) {
|
||
|
var item = obj.asCom;
|
||
|
var info = this.shopList[idx];
|
||
|
var itemInfo = ItemUtil.getItemData(info.itemid);
|
||
|
|
||
|
FGUtil.getLoader(item, "icon").url = `ui://main_ui/${itemInfo.icon}`;
|
||
|
FGUtil.getLoader(item, "goldIcon").url = `ui://main_ui/ui_common_score_guo_shi`;
|
||
|
FGUtil.getTextField(item, "name").text = itemInfo.name;
|
||
|
FGUtil.getTextField(item, "price").text = `${info.price}`;
|
||
|
|
||
|
item.node["idx"] = idx;
|
||
|
item.onClick(this.clickShopItem, this);
|
||
|
}
|
||
|
|
||
|
clickShopItem(e: Event) {
|
||
|
var id = 0;
|
||
|
if (SKDataUtil.hasProperty(e.target, "idx"))
|
||
|
id = e.target["idx"];
|
||
|
var info = this.shopList[id];
|
||
|
this.selectedGoodItemsInfo = info;
|
||
|
var itemInfo = ItemUtil.getItemData(info.itemid)
|
||
|
FGUtil.getTextField(this.calcBox, "num").text = "1";
|
||
|
var realPirce = info.discountPrice ? info.discountPrice : info.price;
|
||
|
FGUtil.getButton(this.calcBox, "buyBtn").title = realPirce.toString();
|
||
|
FGUtil.getTextField(this.calcBox, "name").text = itemInfo.name;
|
||
|
FGUtil.getTextField(this.calcBox, "des").text = itemInfo.description;
|
||
|
FGUtil.getControl(this.mainPanel, "alert/buy_box/choose").selectedIndex = 1;
|
||
|
this.selectedMax = 99999;
|
||
|
}
|
||
|
|
||
|
initSaleItem(idx, obj: fairygui.GObject) {
|
||
|
var item = obj.asCom;
|
||
|
var info = this.bagList[idx];
|
||
|
if (info.type == 0) {
|
||
|
// 道具
|
||
|
let data = ItemUtil.getItemData(info.itemid);
|
||
|
if (data == null) {
|
||
|
FGUtil.getLoader(item, "icon").texture = ItemUtil.getItemIcon("1000");
|
||
|
return;
|
||
|
}
|
||
|
FGUtil.getLoader(item, "icon").url = `ui://main_ui/${data.icon}`;
|
||
|
FGUtil.getTextField(item, "title").text = SKDataUtil.transform(info.count);
|
||
|
}
|
||
|
// 是否選中
|
||
|
FGUtil.getControl(item, "selected").selectedIndex = this.selectedId == idx ? 1 : 0;
|
||
|
item.node["idx"] = idx;
|
||
|
item.onClick(this.clickSaleItem, this);
|
||
|
}
|
||
|
clickSaleItem(e: Event) {
|
||
|
var id = 0;
|
||
|
if (SKDataUtil.hasProperty(e.target, "idx"))
|
||
|
id = e.target["idx"];
|
||
|
var info = this.bagList[id];
|
||
|
info.price = 50;
|
||
|
this.selectedGoodItemsInfo = info;
|
||
|
this.selectedId = id;
|
||
|
this.selectedMax = info.count > 99999 ? 99999 : info.count;
|
||
|
FGUtil.getTextField(this.calcBox, "num").text = `${this.selectedMax}`
|
||
|
FGUtil.getButton(this.calcBox, "buyBtn").title = `${this.selectedMax * 50}`;
|
||
|
|
||
|
var goodsList = FGUtil.getList(this.calcBox, "n0");
|
||
|
goodsList.refreshVirtualList();
|
||
|
FGUtil.getControl(this.mainPanel, "alert/sale_box/choose").selectedIndex = 1;
|
||
|
|
||
|
if (info.type == 0) {
|
||
|
// 道具
|
||
|
let data = ItemUtil.getItemData(info.itemid);
|
||
|
if (data == null) {
|
||
|
FGUtil.getLoader(this.calcBox, "n5/icon").texture = ItemUtil.getItemIcon("1000");
|
||
|
return;
|
||
|
}
|
||
|
FGUtil.getLoader(this.calcBox, "n5/icon").url = `ui://main_ui/${data.icon}`;
|
||
|
var typearr = ['物品', '五行天書', '材料', '藥品', '經驗', '銀兩', '仙玉', '神兵', '仙器', '召喚獸', '技能書', '綁定仙玉', '元氣丹', '寶箱', `鑰匙`, `變身卡`, "特效"];
|
||
|
FGUtil.getTextField(this.calcBox, "n3").text = typearr[data.type];
|
||
|
FGUtil.getTextField(this.calcBox, "n4").text = data.name;
|
||
|
FGUtil.getRichTextField(this.calcBox, "n6").text = data.description;
|
||
|
}
|
||
|
|
||
|
}
|
||
|
/**
|
||
|
* 初始化數據
|
||
|
* @param data
|
||
|
*/
|
||
|
initShopData(data) {
|
||
|
if (!SKUIUtil.isFGUIValid(this.mainPanel)) return;
|
||
|
this.shopList = []
|
||
|
for (let key in data) {
|
||
|
if (data[key].item_kind == 1)
|
||
|
this.shopList.push(data[key])
|
||
|
}
|
||
|
|
||
|
var list = FGUtil.getList(this.mainPanel, "alert/buy_box/list");
|
||
|
list.numItems = this.shopList.length;
|
||
|
}
|
||
|
|
||
|
initSaleData() {
|
||
|
var list = [];
|
||
|
// this.arrangeEquipData(list);
|
||
|
this.arrangeItemData(list);
|
||
|
this.bagList = list;
|
||
|
|
||
|
var salelist = FGUtil.getList(this.mainPanel, "alert/sale_box/n0");
|
||
|
salelist.numItems = this.bagList.length;
|
||
|
}
|
||
|
arrangeEquipData(list: any = []) {
|
||
|
var equipData = GameModel.equipData;
|
||
|
for (const equip of equipData.list) {
|
||
|
if (equipData.info[equip]) {
|
||
|
let info = {
|
||
|
itemid: equip,
|
||
|
info: GameModel.equipData.info[equip],
|
||
|
type: 1
|
||
|
};
|
||
|
list.push(info);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
arrangeItemData(list: any = []) {
|
||
|
var itemList = GameModel.player.itemList;
|
||
|
for (var itemId in itemList) {
|
||
|
var count = SKDataUtil.valueForKey(GameModel.player.itemList, itemId);
|
||
|
if (count != null && count > 0) {
|
||
|
// 跳過不存在道具
|
||
|
if (!ItemUtil.getItemData(itemId)) continue;
|
||
|
// 跳過變身卡和變身卡升級道具
|
||
|
// if (ItemUtil.getItemData(itemId).type == 15) continue;
|
||
|
// if (parseInt(itemId) >= 1003010 && parseInt(itemId) <= 1003100) continue;
|
||
|
// // 跳過天策符
|
||
|
// if (parseInt(itemId) >= 100320 && parseInt(itemId) <= 100323) continue;
|
||
|
|
||
|
let info = {
|
||
|
itemid: itemId,
|
||
|
count: count,
|
||
|
type: 0
|
||
|
};
|
||
|
list.push(info);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
refreshTopMoneyByType() {
|
||
|
if (!SKUIUtil.isFGUIValid(this.mainPanel)) return;
|
||
|
var topMoney = FGUtil.getComponent(this.mainPanel, "top_money");
|
||
|
FGUtil.getControl(topMoney, "shopOther").selectedIndex = 1;
|
||
|
var otherItem = FGUtil.getButton(topMoney, "shopOther");
|
||
|
FGUtil.getLoader(otherItem, "icon").url = "ui://main_ui/ui_common_score_guo_shi";
|
||
|
var num = GameModel.player.gameData.guoscore ? GameModel.player.gameData.guoscore : 0;
|
||
|
otherItem.title = `[color=${GameUtil.getNumColorRich(num)}]${num}[/color]`;
|
||
|
}
|
||
|
|
||
|
closeAll() {
|
||
|
this.closeView(this.mainPanel)
|
||
|
}
|
||
|
|
||
|
closeView(view) {
|
||
|
if (view) {
|
||
|
FGUtil.dispose(view);
|
||
|
view = null;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
initCalcPad(pad) {
|
||
|
for (let i = 0; i < 10; i++) {
|
||
|
FGUtil.getButton(pad, `n${i}`).clearClick();
|
||
|
FGUtil.getButton(pad, `n${i}`).onClick(() => {
|
||
|
// 在原基礎數字末尾添加數字
|
||
|
var numStr = FGUtil.getTextField(this.calcBox, "num").text;
|
||
|
numStr += i;
|
||
|
var num = parseInt(numStr);
|
||
|
if (num >= this.selectedMax)
|
||
|
num = this.selectedMax
|
||
|
FGUtil.getTextField(this.calcBox, "num").text = num.toString();
|
||
|
var realPirce = this.selectedGoodItemsInfo.discountPrice ? this.selectedGoodItemsInfo.discountPrice : this.selectedGoodItemsInfo.price
|
||
|
FGUtil.getButton(this.calcBox, "buyBtn").title = (num * realPirce).toString();
|
||
|
})
|
||
|
}
|
||
|
|
||
|
FGUtil.getButton(pad, `n10`).clearClick();
|
||
|
FGUtil.getButton(pad, `n10`).onClick(() => {
|
||
|
FGUtil.getTextField(this.calcBox, "num").text = "";
|
||
|
var realPirce = this.selectedGoodItemsInfo.discountPrice ? this.selectedGoodItemsInfo.discountPrice : this.selectedGoodItemsInfo.price
|
||
|
FGUtil.getButton(this.calcBox, "buyBtn").title = (realPirce).toString();
|
||
|
})
|
||
|
FGUtil.getButton(pad, `n12`).clearClick();
|
||
|
FGUtil.getButton(pad, `n12`).onClick(() => {
|
||
|
FGUtil.getComponent(this.calcBox, "calc").visible = false;
|
||
|
var numStr = FGUtil.getTextField(this.calcBox, "num").text;
|
||
|
if (numStr == "") {
|
||
|
FGUtil.getTextField(this.calcBox, "num").text = "1";
|
||
|
var realPirce = this.selectedGoodItemsInfo.discountPrice ? this.selectedGoodItemsInfo.discountPrice : this.selectedGoodItemsInfo.price
|
||
|
FGUtil.getButton(this.calcBox, "buyBtn").title = (realPirce).toString();
|
||
|
}
|
||
|
})
|
||
|
FGUtil.getButton(pad, `mask`).clearClick();
|
||
|
FGUtil.getButton(pad, `mask`).onClick(() => {
|
||
|
FGUtil.getComponent(this.calcBox, "calc").visible = false;
|
||
|
var numStr = FGUtil.getTextField(this.calcBox, "num").text;
|
||
|
if (numStr == "") {
|
||
|
FGUtil.getTextField(this.calcBox, "num").text = "1";
|
||
|
var realPirce = this.selectedGoodItemsInfo.discountPrice ? this.selectedGoodItemsInfo.discountPrice : this.selectedGoodItemsInfo.price
|
||
|
FGUtil.getButton(this.calcBox, "buyBtn").title = (realPirce).toString();
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 減少商品數量
|
||
|
*/
|
||
|
reduceGoods() {
|
||
|
if (!SKUIUtil.isFGUIValid(this.calcBox)) return;
|
||
|
var num = parseInt(FGUtil.getTextField(this.calcBox, "num").text);
|
||
|
if (num <= 1)
|
||
|
return;
|
||
|
num--;
|
||
|
FGUtil.getTextField(this.calcBox, "num").text = num.toString();
|
||
|
var realPirce = this.selectedGoodItemsInfo.discountPrice ? this.selectedGoodItemsInfo.discountPrice : this.selectedGoodItemsInfo.price
|
||
|
FGUtil.getButton(this.calcBox, "buyBtn").title = (num * realPirce).toString();
|
||
|
}
|
||
|
/**
|
||
|
* 增加商品數量
|
||
|
*/
|
||
|
addGoods() {
|
||
|
if (!SKUIUtil.isFGUIValid(this.calcBox)) return;
|
||
|
var num = parseInt(FGUtil.getTextField(this.calcBox, "num").text);
|
||
|
if (num >= this.selectedMax)
|
||
|
return;
|
||
|
num++;
|
||
|
FGUtil.getTextField(this.calcBox, "num").text = num.toString();
|
||
|
var realPirce = this.selectedGoodItemsInfo.discountPrice ? this.selectedGoodItemsInfo.discountPrice : this.selectedGoodItemsInfo.price
|
||
|
FGUtil.getButton(this.calcBox, "buyBtn").title = (num * realPirce).toString();
|
||
|
}
|
||
|
|
||
|
|
||
|
refreshSaleView() {
|
||
|
if (!SKUIUtil.isFGUIValid(this.mainPanel)) return;
|
||
|
var page = FGUtil.getControl(this.mainPanel, "alert/page").selectedIndex;
|
||
|
if (page == 0) return;
|
||
|
this.goSalePage()
|
||
|
}
|
||
|
}
|