消消方块阵换皮表情
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

180 lines
4.0 KiB

1 week ago
//*********************
// create by 流云
// time: 2025.4.10
// desc: 答题模块
//*********************
import { game } from "cc";
/**
*
*/
export class AnswerQuestions {
/**
*
*/
static readonly EVENT_SELECT = "AnswerQuestions.EVENT_SELECT";
/**
*
*/
static readonly EVENT_END = "AnswerQuestions.EVENT_END";
/**
*
*/
Questions: Question[] = [];
/**
*
*/
Curent: Question = null;
/**
*
*/
private _autoNext: boolean = false;
/**
*
*/
init(Questions: Question[], autoNext: boolean = false) {
this.Questions = Questions;
this._autoNext = autoNext;
this.Curent = null;
}
/**
*
*/
next() {
let index = this.Questions.indexOf(this.Curent);
if (index < this.Questions.length - 1) {
this.Curent = this.Questions[index + 1];
} else {
this.Curent = null;
game.emit(AnswerQuestions.EVENT_END);
}
}
/**
*
* @param questionStr
* @param selectItemStr
*/
select(questionStr: string, selectItemStr: string) {
let question = this.Questions.find((item) => { return item.Question == questionStr });
if (question && question.Options.indexOf(selectItemStr) >= 0) {
// 如果是单选,则清空已选
if (question.IsRadio) {
question.SelectOptions = [];
}
let index = question.SelectOptions.indexOf(selectItemStr);
if (index >= 0) {
question.SelectOptions.splice(index, 1);
} else if (question.SelectOptions.length < question.Answer.length) {
question.SelectOptions.push(selectItemStr);
}
game.emit(AnswerQuestions.EVENT_SELECT, question);
if (question.IsFinishSelect && this._autoNext) {
this.next();
}
}
}
/**
*
* @returns
*/
checkAllFinish() {
return this.Questions.every((item) => { return item.IsFinishSelect });
}
/**
*
* @returns
*/
checkAllRight() {
return this.Questions.every((item) => { return item.IsRight });
}
/**
*
* @returns
*/
getFinshCount() {
return this.Questions.filter((item) => { return item.IsFinishSelect }).length;
}
/**
*
* @returns
*/
getTotalCount() {
return this.Questions.length;
}
/**
*
* @returns
*/
getRightCount() {
return this.Questions.filter((item) => { return item.IsRight }).length;
}
/**
*
*/
reset(question: string) {
let q = this.Questions.find((item) => { return item.Question == question });
if (q) {
q.SelectOptions = [];
}
}
/**
*
*/
resetAll() {
this.Questions.forEach((item) => { item.SelectOptions = [] });
}
}
/**
*
*/
export class Question {
/**
*
*/
GroupName: string = "";
/**
*
*/
Question: string = "";
/**
*
*/
Answer: string[] = [];
/**
*
*/
Options: string[] = [];
/**
*
*/
SelectOptions: string[] = [];
/**
*
*/
get IsFinishSelect() { return this.SelectOptions.length == this.Answer.length && this.SelectOptions.length > 0 }
/**
*
*/
get IsRadio() { return this.Answer.length == 1 }
/**
*
*/
get IsRight() { return this.IsFinishSelect && this.Answer.every((item) => { return this.SelectOptions.indexOf(item) >= 0 }) }
}