Commit 84eed4d9 authored by 李维's avatar 李维

完成AI相关功能

parent 7735f9a1
...@@ -32,7 +32,9 @@ cc.Class({ ...@@ -32,7 +32,9 @@ cc.Class({
// 是否可以转动 // 是否可以转动
isRolling: false, isRolling: false,
// 是否正在闪烁 // 是否正在闪烁
isBlinking: false isBlinking: false,
// 结果状态 转完之后禁止点击
isLock: false,
}, },
// LIFE-CYCLE CALLBACKS: // LIFE-CYCLE CALLBACKS:
...@@ -53,10 +55,10 @@ cc.Class({ ...@@ -53,10 +55,10 @@ cc.Class({
initEventLinister() { initEventLinister() {
// console.log("初始化事件监听"); // console.log("初始化事件监听");
cc.find("dice", this.node).on(cc.Node.EventType.TOUCH_START, () => { cc.find("dice", this.node).on(cc.Node.EventType.TOUCH_START, () => {
if(this.canClick && !this.isRolling) { if(this.canClick && !this.isRolling && !this.isLock) {
this.node.emit("Rolling_Start") this.node.emit("Rolling_Start")
} else { } else {
cc.log(`已屏蔽转盘点击 canClick:${this.canClick} isRolling:${this.isRolling}`) // cc.log(`已屏蔽转盘点击 canClick:${this.canClick} isRolling:${this.isRolling} isLock:${this.isLock}`)
} }
}) })
}, },
...@@ -84,7 +86,8 @@ cc.Class({ ...@@ -84,7 +86,8 @@ cc.Class({
.by(1.5, { angle: 360 }, { easing: "cubicOut" }) .by(1.5, { angle: 360 }, { easing: "cubicOut" })
.call(() => { .call(() => {
this.isRolling = false; this.isRolling = false;
this.node.emit("Rolling_End") this.isLock = true;
this.node.emit("Rolling_End");
resolve(null); resolve(null);
}) })
.start(); .start();
...@@ -101,7 +104,8 @@ cc.Class({ ...@@ -101,7 +104,8 @@ cc.Class({
.by(0.3, { angle: 360 }, { easing: "cubicOut" }) .by(0.3, { angle: 360 }, { easing: "cubicOut" })
.call(() => { .call(() => {
this.isRolling = false; this.isRolling = false;
this.node.emit("Rolling_End") this.isLock = true;
this.node.emit("Rolling_End");
resolve(null); resolve(null);
}) })
.start(); .start();
...@@ -109,6 +113,11 @@ cc.Class({ ...@@ -109,6 +113,11 @@ cc.Class({
}); });
}, },
// 解锁
reset() {
this.isLock = false;
},
// 放大提示 // 放大提示
blink() { blink() {
this.isBlinking = true; this.isBlinking = true;
......
...@@ -25,6 +25,8 @@ const MSG_FINISH = "msg_finish"; ...@@ -25,6 +25,8 @@ const MSG_FINISH = "msg_finish";
const MSG_PLAYER_TO_TREE = "msg_player_to_tree"; const MSG_PLAYER_TO_TREE = "msg_player_to_tree";
const MSG_PLAYER_TO_HOME = "msg_player_to_home"; const MSG_PLAYER_TO_HOME = "msg_player_to_home";
const MSG_PROFIX_END = "__END__"; const MSG_PROFIX_END = "__END__";
const DEBUG = true;
const TOTAL_SCORE = 42; // 总分 - 双方分数之和等于 TOTAL_SCORE
@ccclass @ccclass
export default class SceneComponent extends MyCocosSceneComponent { export default class SceneComponent extends MyCocosSceneComponent {
...@@ -42,7 +44,7 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -42,7 +44,7 @@ export default class SceneComponent extends MyCocosSceneComponent {
playerId; // 当前玩家ID playerId; // 当前玩家ID
isRoomOwner; // 是否为房主 isRoomOwner; // 是否为房主
recordWaitCount; // 录音等待倒计时 recordWaitCount; // 录音等待倒计时
asyncToken: ""; // 同步令牌 消息发送发会把令牌一同传给其他客户端,当客户端执行异步动作完毕以后 会把同步令牌 withAiPlayer=false; // 对手是否AI
addPreloadImage() { addPreloadImage() {
// TODO 根据自己的配置预加载图片资源 // TODO 根据自己的配置预加载图片资源
...@@ -89,6 +91,8 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -89,6 +91,8 @@ export default class SceneComponent extends MyCocosSceneComponent {
isAI: false, isAI: false,
}); });
this.server.startMatch(); this.server.startMatch();
// 设置AI玩家消息处理方法
this.server.onAIEvents = this.onAiEvents.bind(this);
} }
await this.initHeadImgAndName(); await this.initHeadImgAndName();
this.recordWaitCount = 0; this.recordWaitCount = 0;
...@@ -511,10 +515,12 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -511,10 +515,12 @@ export default class SceneComponent extends MyCocosSceneComponent {
this.eventsMap[eventName].pending = true; this.eventsMap[eventName].pending = true;
} }
// 我方执行方法 // 我方 或者 AI方 执行方法
runFunctionMySide(runFunction) { runFunctionMySide(runFunction) {
if(this.side == this.currentGameSide) { if(this.side == this.currentGameSide) {
runFunction && runFunction(); runFunction && runFunction(false);
} else if(this.side != this.currentGameSide && this.withAiPlayer) {
runFunction && runFunction(true);
} }
} }
...@@ -522,6 +528,7 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -522,6 +528,7 @@ export default class SceneComponent extends MyCocosSceneComponent {
turntable: null; turntable: null;
initTurntable() { initTurntable() {
this.turntable = cc.find("Canvas/Turntable").getComponent("Turntable"); this.turntable = cc.find("Canvas/Turntable").getComponent("Turntable");
this.turntable.initTurntable(DEBUG);
cc.find("Canvas/Turntable").on("Rolling_Start", ()=>{ cc.find("Canvas/Turntable").on("Rolling_Start", ()=>{
// 游戏流程1: 玩家点击转盘 // 游戏流程1: 玩家点击转盘
this.turntable.stopBlink(); this.turntable.stopBlink();
...@@ -627,7 +634,6 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -627,7 +634,6 @@ export default class SceneComponent extends MyCocosSceneComponent {
}) })
} }
} else { } else {
if(this.currentGameSide == RED) { if(this.currentGameSide == RED) {
this.playerToHome(this.redPlayerNode, this.redPlayerHomeNode).then(()=>{ this.playerToHome(this.redPlayerNode, this.redPlayerHomeNode).then(()=>{
next() next()
...@@ -641,6 +647,9 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -641,6 +647,9 @@ export default class SceneComponent extends MyCocosSceneComponent {
}, ()=>{ }, ()=>{
this.runFunctionMySide(()=>{ this.runFunctionMySide(()=>{
if((this.scoreBlue + this.scoreRed) >= TOTAL_SCORE) {
this.runEvent(MSG_FINISH, {});
} else {
if(this.currentGameSide == RED) { if(this.currentGameSide == RED) {
this.currentGameSide = BLUE; this.currentGameSide = BLUE;
} else { } else {
...@@ -648,6 +657,7 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -648,6 +657,7 @@ export default class SceneComponent extends MyCocosSceneComponent {
} }
let side = this.currentGameSide; let side = this.currentGameSide;
this.runEvent(MSG_SWITCHSIDE, {side}); this.runEvent(MSG_SWITCHSIDE, {side});
}
}) })
}) })
...@@ -677,7 +687,7 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -677,7 +687,7 @@ export default class SceneComponent extends MyCocosSceneComponent {
} }
}, (param)=>{ }, (param)=>{
// 游戏流程3: 测评 - 对手不会出现测评界面 // 游戏流程3: 测评 - 对手不会出现测评界面
this.runFunctionMySide(()=>{ this.runFunctionMySide((isAi)=>{
console.log("语音评测开始"); console.log("语音评测开始");
setTimeout(() => { setTimeout(() => {
console.log("语音评测结束") console.log("语音评测结束")
...@@ -690,7 +700,7 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -690,7 +700,7 @@ export default class SceneComponent extends MyCocosSceneComponent {
// 玩家回家 // 玩家回家
this.runEvent(MSG_PLAYER_TO_HOME, {success: true, gemIndex: param.index}); this.runEvent(MSG_PLAYER_TO_HOME, {success: true, gemIndex: param.index});
} }
}, 2000); }, DEBUG?10:2000);
}) })
}) })
...@@ -712,6 +722,38 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -712,6 +722,38 @@ export default class SceneComponent extends MyCocosSceneComponent {
}) })
} }
async onAiEvents(event) {
// 当有AI玩家的时候 需要处理这部分逻辑
switch(event.type) {
case MSG_ROLLING:
this,this.scheduleOnce(()=>{
this.eventsMap[MSG_ROLLING + MSG_PROFIX_END].fn(event.param);
}, DEBUG?2:5.5)
break;
case MSG_GO:
this,this.scheduleOnce(()=>{
this.eventsMap[MSG_GO + MSG_PROFIX_END].fn(event.param);
}, 1)
break;
case MSG_PLAYER_TO_HOME:
this,this.scheduleOnce(()=>{
this.eventsMap[MSG_PLAYER_TO_HOME + MSG_PROFIX_END].fn(event.param);
}, 1)
break;
case MSG_SWITCHSIDE:
if(this.side != this.currentGameSide) {
this.runEvent(MSG_PLAYER_TO_TREE,{});
await asyncDelay(1)
const nextGem = this.getNextQuestionIndex();
this.runEvent(MSG_ROLLING, {nextGem: nextGem});
}
break;
}
}
// 获取下一道题的索引 // 获取下一道题的索引
getNextQuestionIndex() { getNextQuestionIndex() {
let nums = [] let nums = []
...@@ -753,10 +795,10 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -753,10 +795,10 @@ export default class SceneComponent extends MyCocosSceneComponent {
let gIndex = -1; let gIndex = -1;
let index = -1; let index = -1;
switch(num) { switch(num) {
case 0: gIndex = Math.floor((Math.random() * gem1.length * 10) / 10); console.log(gIndex); index = gem1[gIndex].__index; break; case 0: gIndex = Math.floor((Math.random() * gem1.length * 10) / 10); index = gem1[gIndex].__index; break;
case 1: gIndex = Math.floor((Math.random() * gem2.length * 10) / 10); console.log(gIndex); index = gem2[gIndex].__index; break; case 1: gIndex = Math.floor((Math.random() * gem2.length * 10) / 10); index = gem2[gIndex].__index; break;
case 2: gIndex = Math.floor((Math.random() * gem3.length * 10) / 10); console.log(gIndex); index = gem3[gIndex].__index; break; case 2: gIndex = Math.floor((Math.random() * gem3.length * 10) / 10); index = gem3[gIndex].__index; break;
default: gIndex = Math.floor((Math.random() * gem1.length * 10) / 10); console.log(gIndex); index = gem1[gIndex].__index; break; default: gIndex = Math.floor((Math.random() * gem1.length * 10) / 10); index = gem1[gIndex].__index; break;
} }
return { return {
score: num, score: num,
...@@ -784,6 +826,9 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -784,6 +826,9 @@ export default class SceneComponent extends MyCocosSceneComponent {
const redName = cc.find("Canvas/playerLand/red/name/label").getComponent(cc.Label); const redName = cc.find("Canvas/playerLand/red/name/label").getComponent(cc.Label);
const blueName = cc.find("Canvas/playerLand/blue/name/label").getComponent(cc.Label); const blueName = cc.find("Canvas/playerLand/blue/name/label").getComponent(cc.Label);
this.allPlayerList.forEach((player) => { this.allPlayerList.forEach((player) => {
if(player.uuid.indexOf("ai_") != -1) {
this.withAiPlayer = true;
}
this.getSpriteFrimeByUrl(player.headUrl, (sf) => { this.getSpriteFrimeByUrl(player.headUrl, (sf) => {
if (player.color == "Red") { if (player.color == "Red") {
red.getComponent(cc.Sprite).spriteFrame = sf; red.getComponent(cc.Sprite).spriteFrame = sf;
...@@ -811,5 +856,7 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -811,5 +856,7 @@ export default class SceneComponent extends MyCocosSceneComponent {
this.turntable.disable(); this.turntable.disable();
this.turntable.stopBlink(); this.turntable.stopBlink();
} }
// 重置转盘
this.turntable.reset();
} }
} }
...@@ -39,6 +39,8 @@ export class GameServer { ...@@ -39,6 +39,8 @@ export class GameServer {
// { from: 24, to: 16 }, // { from: 24, to: 16 },
]; ];
onAIEvents:Function = null;
constructor(playerMaxNumber, networkHelper) { constructor(playerMaxNumber, networkHelper) {
this.playerMaxNumber = playerMaxNumber; this.playerMaxNumber = playerMaxNumber;
this.networkHelper = networkHelper; this.networkHelper = networkHelper;
...@@ -210,7 +212,11 @@ export class GameServer { ...@@ -210,7 +212,11 @@ export class GameServer {
} }
} else { } else {
this._aiPlayerList.forEach((ai: AI) => { this._aiPlayerList.forEach((ai: AI) => {
if(this.onAIEvents) {
this.onAIEvents(data);
} else {
ai.onFrameEvent(data); ai.onFrameEvent(data);
}
}); });
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment