Files
18xchat/.svn/pristine/b8/b8f0973e4fee9da074dcddf60084ccdacd7ac8c6.svn-base
T
2025-07-17 17:18:21 +08:00

177 lines
5.2 KiB
Plaintext

import { _decorator, Node, v2 } from "cc";
import { I_LevelConfig, I_LevelStepData, I_NpcTalkBack, I_TalkRecordInfo } from "../Config/CommonConfig";
import SocketUnit from "../Common/SocketUnit";
import Utils from "../Common/Utils";
import { InnerMsgCode } from "../Config/InnerMsgCode";
const { ccclass, property } = _decorator;
/**聊天状态 */
export enum E_TalkStatusType {
succ = 0, //成功
talking = 1, //对话中
fail = 2, //失败
noCount = 3, //次数不足
}
//游戏管理器
@ccclass('GameManager')
export default class GameManager {
private static _I: GameManager = null;
public static get I(): GameManager {
if (!GameManager._I) {
GameManager._I = new GameManager();
GameManager._I.init();
}
return GameManager._I;
}
private init(){
}
//长连接相关==============================================================
private _socket: SocketUnit = null;
/**连接长连接 */
public connetSorcket(record_id:string){
this.disconnectSocket();
this._socket = new SocketUnit();
this._socket.connect(`ws://47.119.190.199:20190/game/ai/response/${record_id}`);
this._socket.onDataReceived((data: any) => {
let resData = JSON.parse(data.data)
// console.log("收到长连接消息", resData)
Utils.sendInnerMsg(InnerMsgCode.Data_NpcTalkBack, resData || {})
})
}
/**断开长连接 */
public disconnectSocket(){
if (this._socket){
this._socket.disconnect();
}
this._socket = null;
}
/**获取npc回复 */
public getNpcTalkBack(data: any){
if (this._socket){
let str = JSON.stringify(data)
this._socket.sendData(str);
}
}
//关卡数据相关==============================================================
private static _levelCfgArray: I_LevelConfig[] = [];
private static _levelCfgMap: {[key: string]: I_LevelConfig} = {};
/**设置关卡配置数据 */
public static SetLevelCfg(levelCfg: any[]) {
GameManager._levelCfgArray = levelCfg;
let info:any = null;
for (let i = 0; i < levelCfg.length; i++) {
info = levelCfg[i];
GameManager._levelCfgMap[info.id] = info;
}
}
/**获取关卡配置 */
public static getLevelCfg(levelId: string): I_LevelConfig {
return GameManager._levelCfgMap[levelId];
}
private static _AIVoiceSwt: boolean = false;
/**设置是否显示AI语音 */
public static set AIVoiceSwt(value: boolean) {
GameManager._AIVoiceSwt = value;
}
/**获取是否显示AI语音 */
public static get AIVoiceSwt(): boolean {
return GameManager._AIVoiceSwt;
}
private static _curLevelId: string = "";
/**设置当前关卡ID */
public static set CurLevelId(value: string) {
GameManager._curLevelId = value;
}
/**获取当前关卡ID */
public static get CurLevelId(): string {
return GameManager._curLevelId;
}
private static _recordId: string = "";
/**设置当前关卡流程ID */
public static set RecordId(value: string) {
GameManager._recordId = value;
}
/**获取当前关卡流程ID */
public static get RecordId(): string {
return GameManager._recordId;
}
private static _curLevelData: I_LevelStepData = null;
/**设置当前步骤数据 */
public static set CurLevelData(value: I_LevelStepData) {
GameManager._curLevelData = value;
}
/**获取当前步骤数据 */
public static get CurLevelData(): I_LevelStepData {
return GameManager._curLevelData;
}
private static _curNpcTalkData: I_NpcTalkBack = null;
/**设置当前NPC回复数据 */
public static set CurNpcTalkData(value: I_NpcTalkBack) {
GameManager._curNpcTalkData = value;
}
/**获取当前NPC回复数据 */
public static get CurNpcTalkData(): I_NpcTalkBack {
return GameManager._curNpcTalkData;
}
private static stepScoreList: number[] = [];
/**设置关卡步骤评分 */
public static AddStepScore(score: number) {
GameManager.stepScoreList.push(score);
}
/**清空关卡步骤评分 */
public static ClearStepScore() {
GameManager.stepScoreList = [];
}
/**获取关卡当前评分
* @returns [总分,平均分]
*/
public static getGameScore(): [number,number] {
let totalScore = 0;
let totalStep = 0;
GameManager.stepScoreList.forEach((score) => {
totalScore += score;
totalStep++;
})
let scoreRate = totalStep > 0 ? Math.floor(totalScore / totalStep) : 0;
return [totalScore, scoreRate];
}
private static talkRecordList: I_TalkRecordInfo[] = [];
/**添加对话记录 */
public static AddTalkRecord(record: I_TalkRecordInfo) {
GameManager.talkRecordList.push(record);
}
/**清空对话记录 */
public static ClearTalkRecord() {
GameManager.talkRecordList = [];
}
/**获取对话记录 */
public static get TalkRecordList(): I_TalkRecordInfo[] {
return GameManager.talkRecordList;
}
}