197 lines
6.4 KiB
Plaintext
197 lines
6.4 KiB
Plaintext
import { _decorator, EditBox, isValid, Label, Node, utils } from 'cc';
|
||
import { GButton } from '../../Main/Common/GButton';
|
||
import Utils from '../../Main/Common/Utils';
|
||
import { ViewManager } from '../../Main/Manager/ViewManager';
|
||
import { InnerMsgCode } from '../../Main/Config/InnerMsgCode';
|
||
import li_BaseView from '../../Main/Common/li_BaseView';
|
||
import SubManager from '../SubManager';
|
||
import GameManager from '../../Main/Manager/GameManager';
|
||
import { I_LevelConfig } from '../../Main/Config/CommonConfig';
|
||
import HttpUnit from '../../Main/Common/HttpUnit';
|
||
const { ccclass, property } = _decorator;
|
||
|
||
/**对话阶段 */
|
||
const enum TalkStage {
|
||
/**开场白 */
|
||
prologue,
|
||
/**我的对话 */
|
||
myTalk,
|
||
/**NPC对话 */
|
||
npcTalk,
|
||
}
|
||
|
||
//聊天界面
|
||
@ccclass('Talk_UI')
|
||
export class Talk_UI extends li_BaseView {
|
||
|
||
private _nodeTab: any = {};
|
||
private _data;
|
||
|
||
private dazijiInfo = {stage: 0, idx: 0, str: "", tm:0, ts:0.1, lab:null}; //打字机信息 stage:0-未开始,1-正在打字,2-打字结束
|
||
private ebTalk: EditBox;
|
||
private curStage: TalkStage = TalkStage.prologue; //当前对话阶段
|
||
private lvCfg: I_LevelConfig = null; //当前关卡配置
|
||
private lastRandTalkIdx: number = -1; //上一次随机对话的索引
|
||
|
||
//----重写父类接口---------------------------------------
|
||
onLoadCT() {
|
||
this.registerListenner();
|
||
Utils.parseNode(this.node, this._nodeTab)
|
||
this.ebTalk = this._nodeTab.ebTalk.getComponent(EditBox);
|
||
this.ebTalk.node.on('editing-did-began', this.onEditingBegan, this);
|
||
this.ebTalk.node.on('editing-did-ended', this.onEditingEnded, this);
|
||
this.ebTalk.node.on('editing-return', this.onEditingReturn, this);
|
||
}
|
||
openUIDataCT(data: any): void {
|
||
this._data = data
|
||
}
|
||
|
||
|
||
registerListenner() {
|
||
// Utils.addInnerEL(InnerMsgCode.GM_UI_Close, this, this.resiveGMClose)
|
||
}
|
||
|
||
|
||
start() {
|
||
GButton.BandClick(this._nodeTab.btnBack, this.onBackClick, this);
|
||
GButton.BandClick(this._nodeTab.btnSend, this.onSendClick, this);
|
||
GButton.BandClick(this._nodeTab.btnSuiji, this.onSuijiClick, this);
|
||
GButton.BandClick(this._nodeTab.btnRecord, ()=>{
|
||
console.log("聊天记录")
|
||
}, this);
|
||
GButton.BandClick(this._nodeTab.btnTishi, ()=>{
|
||
ViewManager.I.openBundlesView("UI/StartUI/Tips_UI")
|
||
}, this);
|
||
GButton.BandClick(this._nodeTab.btnAD, ()=>{
|
||
console.log("观看广告")
|
||
}, this);
|
||
|
||
this.lvCfg = GameManager.getLevelCfg(GameManager.CurLevelId)
|
||
|
||
//以npc开场白
|
||
this.showNpcPrologue()
|
||
//刷新状态显示
|
||
this.refreshTalkStepData()
|
||
}
|
||
|
||
onDestroy () {
|
||
if (isValid(this.ebTalk)) {
|
||
this.ebTalk.node.off('editing-did-began', this.onEditingBegan, this);
|
||
this.ebTalk.node.off('editing-did-ended', this.onEditingEnded, this);
|
||
this.ebTalk.node.off('editing-return', this.onEditingReturn, this);
|
||
}
|
||
}
|
||
|
||
update(deltaTime: number) {
|
||
|
||
//region 打字机效果逻辑
|
||
if (this.dazijiInfo.stage == 1) {
|
||
this.dazijiInfo.tm += deltaTime
|
||
if (this.dazijiInfo.tm >= this.dazijiInfo.ts) {
|
||
this.dazijiInfo.tm = 0
|
||
this.dazijiInfo.lab.string = this.dazijiInfo.str.substring(0, this.dazijiInfo.idx)
|
||
|
||
this.dazijiInfo.idx += 1
|
||
if (this.dazijiInfo.idx > this.dazijiInfo.str.length) {
|
||
this.dazijiInfo.stage = 2
|
||
}
|
||
}
|
||
} else if (this.dazijiInfo.stage == 2) {
|
||
console.log("文本打字机效果结束")
|
||
this.dazijiInfo.stage = 0
|
||
this.dazijiInfo.lab.string = this.dazijiInfo.str
|
||
if (this.curStage == TalkStage.prologue) {
|
||
this.scheduleOnce(() => {
|
||
this.turnToMyTalk()
|
||
}, 2)
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
//输入框开始编辑事件
|
||
onEditingBegan(param1:any, param2:any) {
|
||
console.log("聊天界面1: onEditingBegan", param1, param2)
|
||
}
|
||
//输入框回车事件
|
||
onEditingReturn(param1:any, param2:any) {
|
||
console.log("聊天界面2: onEditingReturn", param1, param2)
|
||
}
|
||
//输入框结束编辑事件
|
||
onEditingEnded(param1:any, param2:any) {
|
||
console.log("聊天界面3: onEditingEnded", param1, param2)
|
||
}
|
||
|
||
|
||
//显示NPC开场白
|
||
private showNpcPrologue() {
|
||
this.curStage = TalkStage.prologue
|
||
this._nodeTab.NpcTalkNode.active = true
|
||
this._nodeTab.MyTalkNode.active = false
|
||
//名称
|
||
Utils.setString(this._nodeTab.npcName, this.lvCfg.name)
|
||
//开场白
|
||
this.dazijiInfo.stage = 2
|
||
this.dazijiInfo.idx = 0
|
||
this.dazijiInfo.lab = this._nodeTab.npcTalk.getComponent(Label)
|
||
this.dazijiInfo.str = this.lvCfg.prologueMessage
|
||
this.dazijiInfo.tm = this.dazijiInfo.ts
|
||
GameManager.AddTalkRecord({talk:this.lvCfg.prologueMessage, isMe:false})
|
||
}
|
||
|
||
/**切换到玩家说话状态 */
|
||
private turnToMyTalk() {
|
||
if (this.curStage == TalkStage.myTalk) return
|
||
this.curStage = TalkStage.myTalk
|
||
this._nodeTab.NpcTalkNode.active = false
|
||
this._nodeTab.MyTalkNode.active = true
|
||
this.ebTalk.string = ""
|
||
}
|
||
|
||
|
||
/**刷新当前状态显示 */
|
||
private refreshTalkStepData() {
|
||
let stepData = GameManager.CurStepData
|
||
|
||
}
|
||
|
||
|
||
//退出聊天
|
||
onBackClick() {
|
||
this.doClose();
|
||
Utils.sendInnerMsg(InnerMsgCode.UI_Talk_Back, {})
|
||
}
|
||
|
||
//发送消息
|
||
onSendClick() {
|
||
let str = this.ebTalk.string;
|
||
if (str.length == 0) {
|
||
SubManager.ShowPrompt("请先输入内容")
|
||
return
|
||
}
|
||
this.ebTalk.string = ""
|
||
GameManager.AddTalkRecord({talk:str, isMe:true})
|
||
HttpUnit.ins.sendUserTalk({record_id: GameManager.RecordId, user_input: str}, (data) => {
|
||
if (data) {
|
||
|
||
}
|
||
})
|
||
}
|
||
|
||
//随机回复对话
|
||
onSuijiClick() {
|
||
let sjLen = this.lvCfg.randomInputPool.length
|
||
let randIdx = Utils.getRandomInt(0, sjLen-1)
|
||
console.log("随机回复对话:", randIdx, sjLen)
|
||
if (randIdx == this.lastRandTalkIdx) {
|
||
randIdx++
|
||
if (randIdx >= sjLen) randIdx = 0
|
||
}
|
||
this.lastRandTalkIdx = randIdx
|
||
this.ebTalk.string = this.lvCfg.randomInputPool[randIdx]
|
||
}
|
||
}
|
||
|
||
|
||
|