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

204 lines
6.6 KiB
TypeScript

import FGUtil from "../gear_2.3.4/fgui/FGUtil";
import SKUIUtil from "../gear_2.3.4/util/SKUIUtil";
import GameModel from "../core/GameModel";
import MsgAlert from "../game/msg/MsgAlert";
const { ccclass, property } = cc._decorator;
export default class Question extends cc.Component {
/**
* 文曲星單例實例
*/
public static Instance: Question = null;
/**
* 文曲星面板
*/
questionPanel: fgui.GComponent = null;
/**
* 計時函數
*/
timeCall: any = null;
/**
* 倒計時時間
*/
countdownTime: number = 0;
/**
* 需要加載的預製體
*/
prefabObject: any = {};
isOk: boolean = false;
/**
* 特效節點
*/
particleNode: cc.Node = null;
onLoad() {
if (Question.Instance === null) {
Question.Instance = this;
this.loadPrefab();
} else {
this.destroy();
return;
}
}
/**
* 加載預製體
*/
loadPrefab() {
// 加載所需的預製體
var prefabList = [
{ url: "Prefabs/particle", name: "particle" },
]
this.prefabObject = {}
for (let item of prefabList) {
cc.loader.loadRes(item.url, cc.Prefab, (err, prefab) => {
if (err)
console.warn(err);
else {
this.prefabObject[item.name] = prefab;
}
})
}
}
openQuestionPanel(msg: string = "", startTime) {
if (!this.questionPanel || (this.questionPanel && !this.questionPanel.node && !SKUIUtil.isValid(this.questionPanel.node))) {
this.questionPanel = FGUtil.create("main_ui", "question_panel");
FGUtil.root().addChild(this.questionPanel);
this.questionPanel.makeFullScreen();
}
let wqxBtn = FGUtil.getButton(this.questionPanel, "btn");
wqxBtn.clearClick();
wqxBtn.onClick(() => {
var now = new Date()
this.countdownTime = 60 - Math.floor((now.getTime() - startTime.getTime()) / 1000)
// 已經超過答題時間
if (this.countdownTime <= 0) {
this.closeQuestionPanel();
return
}
FGUtil.getControl(this.questionPanel, "show").selectedIndex = 1;
this.questionPanel.opaque = true
let progress = FGUtil.getProgressBar(this.questionPanel, "alert/progress");
progress.value = this.countdownTime / 60 * 100;
FGUtil.getTextField(this.questionPanel, "alert/time").text = this.countdownTime.toString() + "秒";
this.timeCall = function () {
if (this.countdownTime <= 0) {
// 取消這個計時器
this.unschedule(this.timeCall);
}
this.countdownTime--;
let progress = FGUtil.getProgressBar(this.questionPanel, "alert/progress");
progress.value = this.countdownTime / 60 * 100;
FGUtil.getTextField(this.questionPanel, "alert/time").text = this.countdownTime.toString() + "秒";
}
this.schedule(this.timeCall, 1)
if (this.particleNode)
this.particleNode.parent = FGUtil.getComponent(this.questionPanel, "alert/icon").node
}, this)
if (this.prefabObject["particle"]) {
var effNode = cc.instantiate(this.prefabObject["particle"])
this.particleNode = effNode;
effNode.setPosition(-38, 40)
wqxBtn.node.addChild(effNode)
this.moveParticle(effNode.children[0])
this.moveParticle(effNode.children[1])
}
let mask = FGUtil.getComponent(this.questionPanel, "mask");
let close = FGUtil.getComponent(this.questionPanel, "alert/close");
this.pushCloseQuestionPanelEvent(mask);
this.pushCloseQuestionPanelEvent(close);
if (msg == "") return
let eq = JSON.parse(msg);
FGUtil.getTextField(this.questionPanel, "alert/des/des").text = eq.ename;
var chooseArr = ["ea", "eb", "ec", "ed"];
for (let i in chooseArr) {
if (eq[chooseArr[i]] != null) {
let item = FGUtil.getButton(this.questionPanel, `alert/${chooseArr[i]}`);
item.visible = true;
FGUtil.getTextField(item, "des").text = eq[chooseArr[i]];
item.clearClick();
item.onClick(() => {
if (this.isOk) return;
this.isOk = true;
FGUtil.getControl(item, "selected").selectedIndex = 1;
// 對錯
FGUtil.getControl(item, "dc").selectedIndex = eq[chooseArr[i]] == eq["ekeys"] ? 1 : 2;
if (eq[chooseArr[i]] == eq["ekeys"]) {
FGUtil.getControl(item, "dc").selectedIndex = 1;
MsgAlert.addMsg("回答正確")
} else {
FGUtil.getControl(item, "dc").selectedIndex = 2;
MsgAlert.addMsg("回答錯誤")
}
this.scheduleOnce(() => {
GameModel.send("c2c_send_answer", {
roleId: GameModel.player.roleid,
answer: eq[chooseArr[i]]
})
}, 0.8)
}, this)
}
}
}
moveParticle(node) {
cc.tween(node)
.by(0.5, { y: -85 })
.by(0.5, { x: 85 })
.by(0.5, { y: 85 })
.by(0.5, { x: -85 })
.union()
.repeatForever()
.start()
}
/**
* 添加關閉事件
*/
pushCloseQuestionPanelEvent(item: fairygui.GComponent) {
item.clearClick();
item.onClick(() => {
FGUtil.getControl(this.questionPanel, "show").selectedIndex = 0;
this.questionPanel.opaque = false;
this.unscheduleAllCallbacks();
if (this.particleNode)
this.particleNode.parent = FGUtil.getComponent(this.questionPanel, "btn").node
}, this)
}
/**
* 添加關閉事件
*/
pushCloseEvent(item: fairygui.GComponent, target: fairygui.GComponent, call: Function = null) {
item.clearClick();
item.onClick(() => {
call && call()
FGUtil.dispose(target);
}, this)
}
closeQuestionPanel() {
this.unscheduleAllCallbacks();
if (this.questionPanel && this.questionPanel.node) {
this.isOk = false;
this.particleNode = null;
FGUtil.dispose(this.questionPanel)
}
}
}