307 lines
11 KiB
TypeScript
307 lines
11 KiB
TypeScript
import GameModel from "../core/GameModel";
|
|
import ItemUtil from "../core/ItemUtil";
|
|
import FGUtil from "../gear_2.3.4/fgui/FGUtil";
|
|
import SKDataUtil from "../gear_2.3.4/util/SKDataUtil";
|
|
import SKTimeUtil from "../gear_2.3.4/util/SKTimeUtil";
|
|
|
|
export default class Lottery {
|
|
static shared: Lottery = new Lottery();
|
|
main: fgui.GComponent;
|
|
oneBtn: fgui.GButton;
|
|
tenBtn: fgui.GButton;
|
|
isDug: boolean = false;
|
|
boxId: number = 0;
|
|
timer: number;
|
|
count: number;
|
|
rewardList: any[] = [];
|
|
result: fgui.GComponent;
|
|
itemId: number = 0;
|
|
show(itemId: number = 0) {
|
|
this.itemId = itemId;
|
|
if (this.itemId == 50004)
|
|
this.loadNewTreasure()
|
|
else
|
|
this.loadUI();
|
|
}
|
|
|
|
hide() {
|
|
FGUtil.dispose(this.main);
|
|
this.main = null;
|
|
}
|
|
|
|
loadNewTreasure() {
|
|
this.itemId = 50004
|
|
FGUtil.dispose(this.main);
|
|
this.main = fgui.UIPackage.createObject("main_ui", "treasure_panel").asCom;
|
|
FGUtil.root().addChild(this.main);
|
|
FGUtil.fitScreen(this.main);
|
|
this.oneBtn = FGUtil.getButton(this.main, `alert/one_btn`);
|
|
this.tenBtn = FGUtil.getButton(this.main, `alert/ten_btn`);
|
|
this.result = FGUtil.getComponent(this.main, "reward_main");
|
|
this.oneBtn.onClick(this.clickOne, this);
|
|
this.tenBtn.onClick(this.clickTen, this);
|
|
FGUtil.getButton(this.main, "alert/close").onClick(this.hide, this);
|
|
FGUtil.getComponent(this.result, "mask").onClick(this.clickMask, this);
|
|
FGUtil.getButton(this.main, "alert/again").onClick(this.clickOne, this);
|
|
|
|
this.isDug = false;
|
|
this.clickOne(null)
|
|
}
|
|
|
|
loadUI() {
|
|
FGUtil.dispose(this.main);
|
|
this.main = fgui.UIPackage.createObject("main_ui", "lottery_main").asCom;
|
|
if (this.itemId == 50023)
|
|
this.initZhuXianTip();
|
|
FGUtil.root().addChild(this.main);
|
|
FGUtil.fitScreen(this.main);
|
|
this.oneBtn = FGUtil.getButton(this.main, `alert/high/one_btn`);
|
|
this.tenBtn = FGUtil.getButton(this.main, `alert/high/ten_btn`);
|
|
this.result = FGUtil.getComponent(this.main, "reward_main");
|
|
this.initEvent();
|
|
for (let i = 1; i <= 15; i++) {
|
|
let itemBtn = FGUtil.getButton(this.main, `alert/high/item_${i}`);
|
|
let dugBG = FGUtil.getImage(itemBtn, "dug_bg");
|
|
dugBG.visible = true;
|
|
}
|
|
this.refreshCount();
|
|
this.isDug = false;
|
|
this.rewardList = [];
|
|
}
|
|
|
|
initZhuXianTip() {
|
|
// FGUtil.getTextField(this.main, "alert/title_bg/title").text = "誅仙閣";
|
|
FGUtil.getTextField(this.main, "alert/high/item_name").text = "山河社稷圖";
|
|
FGUtil.getLoader(this.main, "alert/high/use_item/icon").url = `ui://main_ui/7000`;
|
|
}
|
|
|
|
initEvent() {
|
|
FGUtil.getButton(this.main, "alert/close_btn").onClick(this.hide, this);
|
|
this.oneBtn.onClick(this.clickOne, this);
|
|
this.tenBtn.onClick(this.clickTen, this);
|
|
FGUtil.getComponent(this.result, "mask").onClick(this.clickMask, this);
|
|
}
|
|
|
|
clickOne(event: fgui.Event) {
|
|
if (this.isDug) {
|
|
return;
|
|
}
|
|
var againBtn = FGUtil.getButton(this.main, "alert/again");
|
|
if (againBtn)
|
|
againBtn.visible = false
|
|
this.isDug = true;
|
|
this.count = 1;
|
|
this.oneBtn.enabled = false;
|
|
this.tenBtn.enabled = false;
|
|
GameModel.send('c2s_ask_lottery_info', {
|
|
itemId: this.itemId
|
|
});
|
|
}
|
|
|
|
clickTen(event: fgui.Event) {
|
|
if (this.isDug) {
|
|
return;
|
|
}
|
|
this.isDug = true;
|
|
this.count = 10;
|
|
this.oneBtn.enabled = false;
|
|
this.tenBtn.enabled = false;
|
|
for (let i = 1; i <= 10; i++) {
|
|
let itemBtn = FGUtil.getButton(this.result, `alert/ten/item_${i}`);
|
|
ItemUtil.clearItemBtn(itemBtn);
|
|
}
|
|
GameModel.send('c2s_ask_lottery_info', {
|
|
itemId: this.itemId
|
|
});
|
|
}
|
|
|
|
refresh(json: string) {
|
|
if (this.main == null) {
|
|
return;
|
|
}
|
|
let data = SKDataUtil.jsonBy(json);
|
|
|
|
if (data == null) {
|
|
this.isDug = false;
|
|
if (!this.result.visible) {
|
|
this.rewardList = [];
|
|
this.hideAllDug();
|
|
this.oneBtn.enabled = true;
|
|
this.tenBtn.enabled = true;
|
|
}
|
|
return;
|
|
}
|
|
this.boxId = data.nBoxID;
|
|
let mapValue = data.mapValue;
|
|
let size = SKDataUtil.getLength(mapValue);
|
|
if (size != 15) {
|
|
return;
|
|
}
|
|
let index = 1;
|
|
for (let key in mapValue) {
|
|
let item = mapValue[key];
|
|
let itemData = SKDataUtil.clone(ItemUtil.getItemData(item.strItem));
|
|
itemData.count = item.nNum;
|
|
if (this.itemId == 50004) {
|
|
let itemBtn = FGUtil.getButton(this.main, `alert/box/n${index - 1}`);
|
|
itemBtn.data = itemData;
|
|
FGUtil.getLoader(itemBtn, "icon").url = `ui://main_ui/${itemData.icon}`;
|
|
FGUtil.getTextField(itemBtn, "name").text = itemData.name;
|
|
FGUtil.getTextField(itemBtn, "num").text = SKDataUtil.transform(itemData.count);
|
|
FGUtil.getControl(itemBtn, "choose").selectedIndex = 0
|
|
FGUtil.getControl(itemBtn, "color").selectedIndex = Math.random() < 0.5 ? 0 : 1
|
|
} else {
|
|
let itemBtn = FGUtil.getButton(this.main, `alert/high/item_${index}`);
|
|
ItemUtil.showItem(itemBtn, itemData);
|
|
}
|
|
index++;
|
|
}
|
|
this.refreshCount();
|
|
this.timer = SKTimeUtil.delay(() => {
|
|
this.start();
|
|
}, 150);
|
|
}
|
|
|
|
start() {
|
|
GameModel.send("c2s_lottery_go", { nID: this.boxId });
|
|
}
|
|
|
|
dugEnd(data: any) {
|
|
if (this.main == null) {
|
|
return;
|
|
}
|
|
|
|
let index = data.nSelect;
|
|
// 單抽十連抽模式
|
|
// if (this.itemId == 50004) {
|
|
// let itemBtn = FGUtil.getButton(this.main, `alert/box/n${index}`);
|
|
// FGUtil.getControl(itemBtn, "choose").selectedIndex = 1
|
|
// this.rewardList.push(itemBtn.data);
|
|
// let self = this;
|
|
// this.timer = SKTimeUtil.delay(() => {
|
|
// self.checkEnd();
|
|
// }, 150);
|
|
// }
|
|
if (this.itemId == 50004) {
|
|
let itemBtn = FGUtil.getButton(this.main, `alert/box/n${index}`);
|
|
FGUtil.getControl(itemBtn, "choose").selectedIndex = 1
|
|
var data = itemBtn.data;
|
|
this.dropItemToBag(data.id)
|
|
this.timer = SKTimeUtil.delay(() => {
|
|
this.isDug = false
|
|
FGUtil.getButton(this.main, `alert/again`).visible = true;
|
|
}, 100);
|
|
}
|
|
else {
|
|
let itemBtn = FGUtil.getButton(this.main, `alert/high/item_${index + 1}`);
|
|
let effect = FGUtil.getAnim(itemBtn, `effect`);
|
|
effect.visible = true;
|
|
effect.playing = true;
|
|
this.rewardList.push(itemBtn.data);
|
|
let self = this;
|
|
this.timer = SKTimeUtil.delay(() => {
|
|
self.checkEnd();
|
|
}, 150);
|
|
}
|
|
}
|
|
|
|
checkEnd() {
|
|
let control = FGUtil.getControl(this.result, "alert/tab")
|
|
control.selectedIndex = (this.count == 1 ? 0 : 1);
|
|
if (this.count == 1) {
|
|
let itemBtn = FGUtil.getButton(this.result, "alert/item");
|
|
let itemData = this.rewardList[0];
|
|
ItemUtil.showItem(itemBtn, itemData, true);
|
|
this.isDug = false;
|
|
} else {
|
|
let next = this.rewardList.length - 1;
|
|
let itemBtn = FGUtil.getButton(this.result, `alert/ten/item_${next + 1}`);
|
|
let itemData = this.rewardList[next];
|
|
ItemUtil.showItem(itemBtn, itemData, true);
|
|
if (this.rewardList.length < this.count) {
|
|
GameModel.send('c2s_ask_lottery_info', {
|
|
itemId: this.itemId
|
|
});
|
|
} else {
|
|
this.isDug = false;
|
|
}
|
|
}
|
|
this.result.visible = true;
|
|
}
|
|
|
|
clickMask() {
|
|
if (this.isDug) {
|
|
return;
|
|
}
|
|
this.rewardList = [];
|
|
this.result.visible = false;
|
|
this.oneBtn.enabled = true;
|
|
this.tenBtn.enabled = true;
|
|
this.hideAllDug();
|
|
this.refreshCount();
|
|
}
|
|
|
|
hideAllDug() {
|
|
if (this.itemId == 50004) {
|
|
for (let i = 0; i < 15; i++) {
|
|
let itemBtn = FGUtil.getComponent(this.main, `alert/box/n${i}`);
|
|
FGUtil.getLoader(itemBtn, "icon").url = "";
|
|
FGUtil.getTextField(itemBtn, "name").text = "";
|
|
FGUtil.getTextField(itemBtn, "num").text = "";
|
|
FGUtil.getControl(itemBtn, "choose").selectedIndex = 0;
|
|
}
|
|
return
|
|
}
|
|
for (let i = 1; i <= 15; i++) {
|
|
let itemBtn = FGUtil.getButton(this.main, `alert/high/item_${i}`);
|
|
let dugBG = FGUtil.getImage(itemBtn, "dug_bg");
|
|
dugBG.visible = true;
|
|
let effect = FGUtil.getAnim(itemBtn, `effect`);
|
|
effect.visible = false;
|
|
effect.playing = false;
|
|
itemBtn.touchable = true;
|
|
}
|
|
}
|
|
|
|
refreshCount() {
|
|
if (this.itemId == 50004) {
|
|
let itemCount = FGUtil.getTextField(this.main, "alert/item_count");
|
|
let count = ItemUtil.getBagItemCount(this.itemId);
|
|
itemCount.text = `${count}`;
|
|
this.oneBtn.enabled = (count > 0);
|
|
this.tenBtn.enabled = (count > 9);
|
|
return
|
|
}
|
|
let itemCount = FGUtil.getTextField(this.main, "alert/high/item_count");
|
|
let count = ItemUtil.getBagItemCount(this.itemId);
|
|
itemCount.text = `${count}`;
|
|
this.oneBtn.enabled = (count > 0);
|
|
this.tenBtn.enabled = (count > 9);
|
|
}
|
|
|
|
dropItemToBag(id) {
|
|
var t = cc.tween;
|
|
var itemNode = new cc.Node("dropItem");
|
|
itemNode.addComponent(cc.Sprite).spriteFrame;
|
|
var data = ItemUtil.getItemData(id);
|
|
itemNode.getComponent(cc.Sprite).spriteFrame = ItemUtil.getItemIconBy(data);
|
|
itemNode.getComponent(cc.Sprite).sizeMode = cc.Sprite.SizeMode.CUSTOM
|
|
itemNode.width = 50;
|
|
itemNode.height = 50;
|
|
itemNode.setPosition(this.main.node.width / 2 + 120, -this.main.node.height + 80)
|
|
itemNode.opacity = 0;
|
|
itemNode.parent = this.main.node;
|
|
|
|
t(itemNode)
|
|
.parallel(
|
|
t().by(0.4, { y: 80 }),
|
|
t().to(0.2, { opacity: 255 })
|
|
)
|
|
.delay(0.15)
|
|
.call(() => {
|
|
let bezerby = cc.bezierBy(0.5, [cc.v2(88, 10), cc.v2(130, 80), cc.v2(155, -350)]);
|
|
itemNode.runAction(cc.sequence(cc.spawn(bezerby, cc.rotateBy(0.3, 90)), cc.removeSelf()));
|
|
})
|
|
.start()
|
|
}
|
|
} |