443 lines
14 KiB
Plaintext
443 lines
14 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";
|
|
import SubManager from "../../Sub/SubManager";
|
|
import HttpUnit from "../Common/HttpUnit";
|
|
import { SDKManager } from "../Channel/SDKManager";
|
|
|
|
const { ccclass, property } = _decorator;
|
|
|
|
|
|
/**聊天状态 */
|
|
export enum E_TalkStatusType {
|
|
succ = 0, //成功
|
|
talking = 1, //对话中
|
|
fail = 2, //失败
|
|
noCount = 3, //次数不足
|
|
}
|
|
|
|
|
|
/**对话阶段 */
|
|
export enum E_TalkStage {
|
|
/**开场白 */
|
|
prologue,
|
|
/**我的对话 */
|
|
myTalk,
|
|
/**等待NPC回复 */
|
|
waitNpcTalk,
|
|
/**NPC对话 */
|
|
npcTalk,
|
|
/**NPC固定话术 */
|
|
npcFixedTalk,
|
|
/**更新对话状态 */
|
|
upTalkStatus,
|
|
}
|
|
|
|
|
|
//游戏管理器
|
|
@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(){
|
|
|
|
}
|
|
|
|
|
|
//region长连接相关
|
|
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}`);
|
|
let socketUrl = `${HttpUnit.SocketHost}${record_id}`
|
|
this._socket.connect(socketUrl);
|
|
this._socket.onDataReceived((data: any) => {
|
|
let resData = JSON.parse(data.data)
|
|
if (GameManager.curStage != E_TalkStage.waitNpcTalk) return
|
|
if (resData.isFinished) {
|
|
console.log("npc回复:", resData)
|
|
GameManager.CurNpcTalkData = 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);
|
|
}
|
|
}
|
|
|
|
|
|
//region关卡配置
|
|
private static _levelCfgArray: I_LevelConfig[] = [];
|
|
private static _levelCfgMap: {[key: string]: I_LevelConfig} = {};
|
|
/**设置关卡配置数据 */
|
|
public static SetLevelCfg(levelCfg: any[]) {
|
|
GameManager._levelCfgArray = levelCfg;
|
|
let info:any = null;
|
|
let umRecords = [] //友盟解锁关卡记录
|
|
let recordLvs = HttpUnit.GetLevelRecord(); //友盟已记录的关卡
|
|
for (let i = 0; i < levelCfg.length; i++) {
|
|
info = levelCfg[i];
|
|
GameManager._levelCfgMap[info.id] = info;
|
|
//判断是否已记录
|
|
if (info.is_unlock) {
|
|
if (recordLvs.indexOf(info.id) == -1){
|
|
umRecords.push(info.id)
|
|
}
|
|
}
|
|
}
|
|
//友盟记录关卡
|
|
if (umRecords.length > 0 && SDKManager.umaSwt()){
|
|
let umr = umRecords.join(',')
|
|
HttpUnit.ins.sendLevelRecord({level_ids:umr}, (_data)=>{
|
|
console.log('友盟记录关卡',_data)
|
|
let userId = HttpUnit.GetID()
|
|
let curDate = Utils.GetNowFormatDay()
|
|
for (let i = 0; i < umRecords.length; i++) {
|
|
let lvId = umRecords[i]
|
|
let _cfg = GameManager._levelCfgMap[lvId]
|
|
SDKManager.umaEvent(SDKManager.Uma_Event_LvUnlock, {
|
|
lvId: lvId,
|
|
userId: userId,
|
|
time: curDate,
|
|
type: _cfg.unlock_type
|
|
})
|
|
}
|
|
})
|
|
}
|
|
}
|
|
/**设置关卡星级 */
|
|
public static SetLevelStar(levelId: string, star: number) {
|
|
let cfg = GameManager._levelCfgMap[levelId];
|
|
if (cfg) {
|
|
cfg.star = star;
|
|
}
|
|
}
|
|
|
|
/**获取关卡配置 */
|
|
public static getLevelCfg(levelId: string): I_LevelConfig {
|
|
return GameManager._levelCfgMap[levelId];
|
|
}
|
|
/**获取关卡配置列表 */
|
|
public static getLevelCfgList(): I_LevelConfig[] {
|
|
return GameManager._levelCfgArray;
|
|
}
|
|
|
|
/**获取npc表情图片 */
|
|
public static GetNpcEmoPic(emoStr: string): string {
|
|
let path = ""
|
|
let lvCfg = GameManager.getLevelCfg(GameManager.CurLevelId);
|
|
if (emoStr == "害羞") {
|
|
path = `emo_1/${lvCfg.characterShyPortrait}`
|
|
} else if (emoStr == "开心") {
|
|
path = `emo_2/${lvCfg.characterHappyPortrait}`
|
|
} else if (emoStr == "厌恶") {
|
|
path = `emo_3/${lvCfg.characterHatePortrait}`
|
|
} else {
|
|
path = `role/${lvCfg.characterFullLengthPortrait}` //默认
|
|
}
|
|
return path;
|
|
}
|
|
|
|
|
|
//regionAI语音开关
|
|
private static _AIVoiceSwt: boolean = false;
|
|
/**设置是否显示AI语音 */
|
|
public static set AIVoiceSwt(value: boolean) {
|
|
GameManager._AIVoiceSwt = value;
|
|
}
|
|
/**获取是否显示AI语音 */
|
|
public static get AIVoiceSwt(): boolean {
|
|
return GameManager._AIVoiceSwt;
|
|
}
|
|
|
|
|
|
//region当前关卡ID
|
|
private static _curLevelId: string = "";
|
|
/**设置当前关卡ID */
|
|
public static set CurLevelId(value: string) {
|
|
GameManager._curLevelId = value;
|
|
}
|
|
/**获取当前关卡ID */
|
|
public static get CurLevelId(): string {
|
|
return GameManager._curLevelId;
|
|
}
|
|
|
|
|
|
//region流程id
|
|
private static _recordId: string = "";
|
|
/**设置当前关卡流程ID */
|
|
public static set RecordId(value: string) {
|
|
GameManager._recordId = value;
|
|
}
|
|
/**获取当前关卡流程ID */
|
|
public static get RecordId(): string {
|
|
return GameManager._recordId;
|
|
}
|
|
|
|
|
|
//region当前步骤数据
|
|
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;
|
|
}
|
|
|
|
|
|
//region NPC回复数据
|
|
private static _curNpcTalkData: I_NpcTalkBack = null;
|
|
private static _npcTalkDataAry: I_NpcTalkBack[] = [];
|
|
/**设置当前NPC回复数据 */
|
|
public static set CurNpcTalkData(value: I_NpcTalkBack) {
|
|
GameManager._curNpcTalkData = value;
|
|
GameManager._npcTalkDataAry.push(value);
|
|
}
|
|
/**获取当前NPC回复数据 */
|
|
public static get CurNpcTalkData(): I_NpcTalkBack {
|
|
return GameManager._curNpcTalkData;
|
|
}
|
|
/**移除最后一条NPC回复数据 */
|
|
public static removeLastNpcTalkData() {
|
|
if (GameManager._npcTalkDataAry.length > 0) {
|
|
let talkData = GameManager._npcTalkDataAry.pop();
|
|
//心动次数也要减
|
|
if (talkData.judgement == 0) {
|
|
if (GameManager.xindongCishu > 0) {
|
|
GameManager.xindongCishu = GameManager.xindongCishu - 1
|
|
}
|
|
}
|
|
}
|
|
if (GameManager._npcTalkDataAry.length == 0) {
|
|
GameManager._curNpcTalkData = null;
|
|
} else {
|
|
GameManager._curNpcTalkData = GameManager._npcTalkDataAry[GameManager._npcTalkDataAry.length - 1];
|
|
}
|
|
}
|
|
|
|
|
|
//region 当前NPC话术文本
|
|
private static _curNpcFixedTalkText: string = "";
|
|
/**设置当前NPC话术文本 */
|
|
public static set CurNpcFixedTalkText(value: string) {
|
|
GameManager._curNpcFixedTalkText = value;
|
|
}
|
|
/**获取当前NPC话术文本 */
|
|
public static get CurNpcFixedTalkText(): string {
|
|
return GameManager._curNpcFixedTalkText;
|
|
}
|
|
|
|
|
|
//region关卡步骤评分
|
|
private static stepScoreList: number[] = [];
|
|
/**设置关卡步骤评分 */
|
|
public static AddStepScore(score: number) {
|
|
GameManager.stepScoreList.push(score);
|
|
}
|
|
/**移除最后一步骤评分 */
|
|
public static RemoveLastSetpScore() {
|
|
let cellLen = GameManager.stepScoreList.length;
|
|
if (cellLen > 0) {
|
|
GameManager.stepScoreList.pop();
|
|
}
|
|
}
|
|
/**清空关卡步骤评分 */
|
|
public static ClearStepScore() {
|
|
GameManager.stepScoreList = [];
|
|
}
|
|
/**获取最新得分 */
|
|
public static getCurStepScore(): number {
|
|
if (GameManager.stepScoreList.length == 0) return 0;
|
|
return GameManager.stepScoreList[GameManager.stepScoreList.length - 1];
|
|
}
|
|
/**获取关卡当前评分
|
|
* @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];
|
|
}
|
|
|
|
|
|
//region对话记录
|
|
private static talkRecordList: I_TalkRecordInfo[] = [];
|
|
/**添加对话记录 */
|
|
public static AddTalkRecord(record: I_TalkRecordInfo) {
|
|
GameManager.talkRecordList.push(record);
|
|
}
|
|
/**移除最后一条对话记录 */
|
|
public static RemoveLastRoundTalkRecord() {
|
|
let cellLen = GameManager.talkRecordList.length;
|
|
if (cellLen > 1) {
|
|
for (let i=cellLen-1; i>=0; i--) {
|
|
let talkCell = GameManager.talkRecordList.pop();
|
|
if (talkCell.isMe) {
|
|
break
|
|
}
|
|
}
|
|
}
|
|
}
|
|
/**清空对话记录 */
|
|
public static ClearTalkRecord() {
|
|
GameManager.talkRecordList = [];
|
|
}
|
|
/**获取对话记录 */
|
|
public static get TalkRecordList(): I_TalkRecordInfo[] {
|
|
return GameManager.talkRecordList;
|
|
}
|
|
|
|
|
|
//region 当前对话阶段
|
|
private static _curStage: E_TalkStage = E_TalkStage.prologue;
|
|
/**获取当前对话阶段 */
|
|
public static get curStage(): E_TalkStage {
|
|
return GameManager._curStage;
|
|
}
|
|
/**设置当前对话阶段 */
|
|
public static TurnToStage(value: E_TalkStage, force: boolean = false) {
|
|
if (GameManager._curStage == value && !force) return;
|
|
GameManager._curStage = value;
|
|
Utils.sendInnerMsg(InnerMsgCode.UI_TalkStageUp, {})
|
|
}
|
|
|
|
|
|
//region 已用对话轮次
|
|
private static _srouceCnt: number = 0;
|
|
/**获取已用对话轮次 */
|
|
public static get srouceCnt(): number {
|
|
return GameManager._srouceCnt;
|
|
}
|
|
/**设置已用对话轮次 */
|
|
public static set srouceCnt(value: number) {
|
|
GameManager._srouceCnt = value;
|
|
}
|
|
|
|
//region 心动次数
|
|
private static _xindongCishu: number = 0;
|
|
/**获取心动次数 */
|
|
public static get xindongCishu(): number {
|
|
return GameManager._xindongCishu;
|
|
}
|
|
/**设置心动次数 */
|
|
public static set xindongCishu(value: number) {
|
|
GameManager._xindongCishu = value;
|
|
}
|
|
|
|
//region npc固定话术喜欢次数
|
|
private static _npcFixTalkCountXihuan: number = 0;
|
|
/**获取npc固定话术喜欢次数 */
|
|
public static get npcFixTalkCountXihuan(): number {
|
|
return GameManager._npcFixTalkCountXihuan;
|
|
}
|
|
/**设置npc固定话术喜欢次数 */
|
|
public static set npcFixTalkCountXihuan(value: number) {
|
|
GameManager._npcFixTalkCountXihuan = value;
|
|
}
|
|
|
|
//region npc固定话术讨厌次数
|
|
private static _npcFixTalkCountTaoyan: number = 0;
|
|
/**获取npc固定话术讨厌次数 */
|
|
public static get npcFixTalkCountTaoyan(): number {
|
|
return GameManager._npcFixTalkCountTaoyan;
|
|
}
|
|
/**设置npc固定话术讨厌次数 */
|
|
public static set npcFixTalkCountTaoyan(value: number) {
|
|
GameManager._npcFixTalkCountTaoyan = value;
|
|
}
|
|
|
|
|
|
//region 结束关卡
|
|
/**结束关卡 */
|
|
public static EndGame() {
|
|
GameManager.I.disconnectSocket();
|
|
GameManager.ClearStepScore()
|
|
GameManager.ClearTalkRecord()
|
|
GameManager.CurLevelId = ""
|
|
GameManager.RecordId = ""
|
|
GameManager.CurLevelData = null
|
|
GameManager.CurNpcTalkData = null
|
|
GameManager._npcTalkDataAry = []
|
|
GameManager.srouceCnt = 0
|
|
GameManager.xindongCishu = 0
|
|
GameManager.npcFixTalkCountXihuan = 0
|
|
GameManager.npcFixTalkCountTaoyan = 0
|
|
GameManager._curStage = E_TalkStage.prologue
|
|
}
|
|
|
|
|
|
//region 重新开始关卡
|
|
/**重新开始关卡 */
|
|
public static RestartGame() {
|
|
if (GameManager.CurLevelId == "") {
|
|
return
|
|
}
|
|
GameManager._restartGameStep1()
|
|
}
|
|
//重开步骤1 刷新用户数据
|
|
private static _restartGameStep1() {
|
|
HttpUnit.ins.getUserData((userData) => {
|
|
GameManager._restartGameStep2()
|
|
})
|
|
}
|
|
//重开步骤2 刷新关卡数据
|
|
private static _restartGameStep2() {
|
|
HttpUnit.ins.getLevelList((levelList) => {
|
|
if (levelList) {
|
|
GameManager.SetLevelCfg(levelList)
|
|
GameManager._restartGameStep3()
|
|
}
|
|
})
|
|
}
|
|
//重开步骤3 判断条件
|
|
private static _restartGameStep3() {
|
|
if (HttpUnit.GetXiangqinTickets() <= 0) {
|
|
SubManager.ShowPrompt("今日相亲次数已用完");
|
|
return;
|
|
}
|
|
let level_id = GameManager.CurLevelId
|
|
HttpUnit.ins.levelStart({level_id:level_id}, (data) => {
|
|
GameManager.EndGame()
|
|
GameManager.CurLevelId = level_id
|
|
GameManager.CurLevelData = data.level
|
|
GameManager.RecordId = data.record_id
|
|
GameManager.I.connetSorcket(data.record_id) //连接socket
|
|
Utils.sendInnerMsg(InnerMsgCode.UI_ResartGame, {})
|
|
})
|
|
}
|
|
|
|
|
|
static checkLevelRecord() {
|
|
|
|
}
|
|
} |