init
This commit is contained in:
@@ -0,0 +1,567 @@
|
||||
import { _decorator, Node, v2 } from "cc";
|
||||
import { I_LevelConfig, I_LevelStepData, I_NpcTalkBack, I_TalkRecordInfo, I_ZhengCangConfig } 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";
|
||||
import { VideoRoleType } from "../Common/GlobalValue";
|
||||
|
||||
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} = {};
|
||||
private static _zhencangMap: {[key: string]: I_ZhengCangConfig} = {};
|
||||
/**设置关卡配置数据 */
|
||||
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 (info.photoAwards) {
|
||||
for (let j = 0; j < info.photoAwards.length; j++) {
|
||||
let photoAwards = info.photoAwards[j];
|
||||
photoAwards.levelId = info.id;
|
||||
GameManager._zhencangMap[photoAwards.id+""] = photoAwards;
|
||||
}
|
||||
}
|
||||
}
|
||||
//友盟记录关卡
|
||||
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): any {
|
||||
let rolePath = ""
|
||||
let emoName = ""
|
||||
let videoType = VideoRoleType.None
|
||||
let lvCfg = GameManager.getLevelCfg(GameManager.CurLevelId);
|
||||
if (emoStr.includes("害羞")) {
|
||||
rolePath = `emo_1/${lvCfg.characterShyPortrait}`
|
||||
emoName = lvCfg.characterShyPortrait
|
||||
videoType = VideoRoleType.emo
|
||||
} else if (emoStr.includes("开心")) {
|
||||
rolePath = `emo_2/${lvCfg.characterHappyPortrait}`
|
||||
emoName = lvCfg.characterHappyPortrait
|
||||
videoType = VideoRoleType.emo
|
||||
} else if (emoStr.includes("厌恶")) {
|
||||
rolePath = `emo_3/${lvCfg.characterHatePortrait}`
|
||||
emoName = lvCfg.characterHatePortrait
|
||||
videoType = VideoRoleType.emo
|
||||
} else {
|
||||
rolePath = `role/${lvCfg.characterFullLengthPortrait}` //默认
|
||||
emoName = lvCfg.characterFullLengthPortrait
|
||||
videoType = VideoRoleType.Idle
|
||||
}
|
||||
return {rolePath:rolePath, emoName:emoName, videoType:videoType};
|
||||
}
|
||||
|
||||
|
||||
//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 (talkData.aiScore >= 90) {
|
||||
if (GameManager.jidongCishu > 0) {
|
||||
GameManager.jidongCishu = GameManager.jidongCishu - 1
|
||||
}
|
||||
}
|
||||
if (talkData.aiScore >= 100) {
|
||||
if (GameManager.gandongCishu > 0) {
|
||||
GameManager.gandongCishu = GameManager.gandongCishu - 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 心动次数 70分
|
||||
private static _xindongCishu: number = 0;
|
||||
/**获取心动次数 */
|
||||
public static get xindongCishu(): number {
|
||||
return GameManager._xindongCishu;
|
||||
}
|
||||
/**设置心动次数 */
|
||||
public static set xindongCishu(value: number) {
|
||||
GameManager._xindongCishu = value;
|
||||
}
|
||||
|
||||
//region 激动次数 90分
|
||||
private static _jidongCishu: number = 0;
|
||||
/**获取心动次数 */
|
||||
public static get jidongCishu(): number {
|
||||
return GameManager._jidongCishu;
|
||||
}
|
||||
/**设置心动次数 */
|
||||
public static set jidongCishu(value: number) {
|
||||
GameManager._jidongCishu = value;
|
||||
}
|
||||
|
||||
//region 感动次数 100分
|
||||
private static _gandongCishu: number = 0;
|
||||
/**获取心动次数 */
|
||||
public static get gandongCishu(): number {
|
||||
return GameManager._gandongCishu;
|
||||
}
|
||||
/**设置心动次数 */
|
||||
public static set gandongCishu(value: number) {
|
||||
GameManager._gandongCishu = 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.jidongCishu = 0
|
||||
GameManager.gandongCishu = 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, {})
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
//region 可领取的私家珍藏列表
|
||||
private static _zhencangCanget: any[] = []
|
||||
/**可领取的私家珍藏列表 */
|
||||
static get zhencangCanget() {
|
||||
return this._zhencangCanget
|
||||
}
|
||||
/**刷新可领取的私家珍藏列表 */
|
||||
static refreshZhencangCanget() {
|
||||
HttpUnit.ins.getZhencangList({limit:10, page:1, type:0}, (data) => {
|
||||
if (data) {
|
||||
this._zhencangCanget = data.data || []
|
||||
Utils.sendInnerMsg(InnerMsgCode.Data_ZhencangUp, {})
|
||||
}
|
||||
})
|
||||
}
|
||||
/**解锁一个私家珍藏id */
|
||||
static UnlockZhencang(record_id:string, levelId: string) {
|
||||
console.log("解锁私家珍藏:", record_id, levelId)
|
||||
HttpUnit.ins.getZhencangList({limit:999, page:1, type:1}, (data) => {
|
||||
if (data){
|
||||
this._doUnlockZhencang(data.data, record_id, levelId)
|
||||
}
|
||||
})
|
||||
}
|
||||
//解锁私家珍藏 zclist:已解锁列表 record_id:本次聊天id levelId:关卡id
|
||||
private static _doUnlockZhencang(zclist:any, record_id:string, levelId: string) {
|
||||
zclist = zclist || []
|
||||
|
||||
let cangetList = GameManager.zhencangCanget
|
||||
let lvCfg = this.getLevelCfg(levelId)
|
||||
let lockAry:any[] = [] //未解锁的私家珍藏列表
|
||||
for (let i=0; i<lvCfg.photoAwards.length; i++) {
|
||||
let item = lvCfg.photoAwards[i]
|
||||
let ishave = false //是否已解锁
|
||||
//先看可领取列表里有没有
|
||||
if (!ishave) {
|
||||
for (let j=0; j<cangetList.length; j++) {
|
||||
let zc = cangetList[j]
|
||||
if (zc.img == item.id) {
|
||||
ishave = true
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
//再看已解锁列表里有没有
|
||||
if (!ishave) {
|
||||
for (let j=0; j<zclist.length; j++) {
|
||||
let zc = zclist[j]
|
||||
if (zc.img == item.id) {
|
||||
ishave = true
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!ishave) {
|
||||
lockAry.push(item)
|
||||
}
|
||||
}
|
||||
if (lockAry.length > 0) {
|
||||
let randIdx = Utils.getRandomInt(0, lockAry.length-1)
|
||||
let item = lockAry[randIdx]
|
||||
HttpUnit.ins.sendZhencangId({record_id:record_id, level_id:levelId, img:item.id}, (_data) => {
|
||||
if (_data) {
|
||||
console.log("获得珍藏卡:", item)
|
||||
GameManager.refreshZhencangCanget()
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
/**获取珍藏配置信息 */
|
||||
public static getZhencangCfg(zcId: string) {
|
||||
return GameManager._zhencangMap[zcId+""];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user