MX Player - Docking of Battle Games (双人对战游戏接入)
1. Game Init (游戏初始化)
After the game is loaded, execute it in the entry class: (游戏加载完成后在入口类中执行) onGameInit(); onGameStart();
export let onGameInit = function () {
if (!DeviceUtil.IsMXGame) {
return;
}
let info = gameManager.onGameInit();
let gameConfig: GameConfig = JSON.parse(info);
gameId_ = gameConfig.gameId;
userId_ = gameConfig.userId;
if (gameConfig.isFirstOpen !== null && gameConfig.isFirstOpen !== undefined) {
is_Guide = gameConfig.isFirstOpen;
}
gameInitComplete = true;
};
export let onGameStart = function () {
if (!DeviceUtil.IsMXGame) {
return;
}
gameManager.onGameStart();
};
2. Start matching(开始匹配)
Call the onGameLoaded() method to start matching, and the matching data will be returned by cc.game.emit("commonEventInterface") . After obtaining the matching data, you need to call onGameCleanPosters() to close the App layer matching interface, and then perform the in-game matching animation and connect to the in-game server.
调用 *onGameLoaded() 方法开始匹配,匹配数据会通过cc.game.emit("commonEventInterface")* 返回 。 获取到匹配数据后需调用onGameCleanPosters() **关闭App层匹配界面,然后进行游戏内匹配动画和连接游戏内server。
export let onGameLoaded = function () {
console.log("---------------:" + game_version);
if (!DeviceUtil.IsMXGame) {
return;
}
let info = { GameVersion: game_version };
gameManager.onGameLoaded(JSON.stringify(info));
game_start_time = Date.now();
cc.game.on("commonEventInterface", commonEventInterface_battle, this);
};
export let commonEventInterface_battle = function (event_name: string, info: any) {
switch (event_name) {
case "backPressed": {
cc.game.emit("userGamePausedCall");
cc.game.emit("userPausedViewShowCall");
break;
}
case "screenOn": {
break;
}
case "screenOff": {
cc.game.emit("userGamePausedCall");
cc.game.emit("userPausedViewShowCall");
break;
}
case "pagePause": {
cc.game.emit("eventGamePausedCall");
cc.game.emit("eventMusicPausedCall");
break;
}
case "pageResume": {
cc.game.emit("eventGameResumeCall");
cc.game.emit("eventMusicResumeCall");
break;
}
case "userPresent": {
break;
}
case "userMatched": {
battleInfo(info);
break;
}
}
};
export let onGameCleanPosters = function () {
if (!DeviceUtil.IsMXGame) {
return;
}
gameManager.onGameCleanPosters();
};
3. Battle Game Over(游戏结束)
Call onBattleGameOver() at the end of the game, you can also call this method if you want to force the end of the game.
游戏结束调用 onBattleGameOver() ,想强行结束游戏也可调用此方法。
export let onBattleGameOver = function () {
if (!DeviceUtil.IsMXGame) {
return;
}
if (is_over) {
return;
}
is_over = true;
let myScore = 0;
let otherScore = 0;
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
if (gameStore.player1Data.completeBlockArr[i][j] === 1) {
myScore++;
}
if (gameStore.player2Data.completeBlockArr[i][j] === 1) {
otherScore++;
}
}
}
let info = {
gameId: gameId_,
battleId: battleId_,
roomId: roomId_,
battle_info: {
currentscore: myScore,
oppoScore: otherScore,
winStatus: gameStore.winType,
numA: gameStore.selfMoveCount,
numB: gameStore.otherMoveCount,
numC: gameStore.createPropsNum,
numD: gameStore.usePauseSkillCount,
numE: gameStore.useSwapSkillCount,
numF: gameStore.useRandomSkillCount,
numG: gameStore.beUsedPauseSkillCount,
numH: gameStore.beUsedRandomSkillCount,
numI: gameStore.selfGetPropNum,
numJ: gameStore.otherGetPropNum,
oppoType: gameStore.isAI,
oppoUserID: gameStore.player2Data.id,
},
};
let rInfo = {
roomID: battleId_,
playtime: Math.round((Date.now() - game_start_time) / 1000)
};
gameManager.onTrack("gameExit", JSON.stringify(rInfo));
const emojiClickedInfo = JSON.stringify(gameStore.expressionStatistic);
gameManager.onTrack("emojiClicked", emojiClickedInfo);
gameManager.onBattleGameOver(JSON.stringify(info));
};
Attach the complete code at the end: 最后附上完整代码:
import { conditionId } from "../Manager/DataManager";
import SocketManager from "../Manager/SocketManager";
import UIManager from "../Manager/UIManager";
import { gameStore, WinType } from "../store/GameStore";
import MatchUI from "../UI/Match/MatchUI";
import DeviceUtil from "../utils/DeviceUtil";
export let game_version: string = "1.0.0";
export let gameId_: string = "";
export let battleId_: string = "";
export let roomId_: string = "4f4b7518761dc9533f9f2cc4c77b561d_c4d2cgjqgkce9tkehg5g";
export let userId_: string = "";
export let game_start_time: number = 0;
export let is_Guide: boolean = false;
export let gameInitComplete: boolean = false;
export interface GameConfig {
highestScore: number;
lastLevel: number;
roomType: string;
gameId: string;
gameName: string;
userId: string;
isFirstOpen: boolean;
source: string;
startType: string;
rewardType: string;
appVersion: number;
appId: string;
trackInfo: string;
roomId?: string;
tournamentId?: string;
}
interface info {
status: string,
roomId: string,
connectUrl: string,
mapId: number,
gameVersion: string,
users: user[],
battleId: string,
gameId: string,
selfUserId: string
}
interface user {
type: string,
userId: string,
playerId: string,
name: string,
avatar: string,
score: number,
playerData?: any
}
export let setGameId = function (id: string) {
gameId_ = id;
};
export let setBattleId = function (id: string) {
battleId_ = id;
};
export let setRoomId = function (id: string) {
roomId_ = id;
};
export let setGuide = function (b: boolean) {
is_Guide = b;
};
export let onGameInit = function () {
if (!DeviceUtil.IsMXGame) {
return;
}
let info = gameManager.onGameInit();
let gameConfig: GameConfig = JSON.parse(info);
gameId_ = gameConfig.gameId;
userId_ = gameConfig.userId;
if (gameConfig.isFirstOpen !== null && gameConfig.isFirstOpen !== undefined) {
is_Guide = gameConfig.isFirstOpen;
}
gameInitComplete = true;
};
export let onGameStart = function () {
if (!DeviceUtil.IsMXGame) {
return;
}
gameManager.onGameStart();
};
export let onGameLoaded = function () {
console.log("---------------:" + game_version);
if (!DeviceUtil.IsMXGame) {
return;
}
let info = { GameVersion: game_version };
gameManager.onGameLoaded(JSON.stringify(info));
game_start_time = Date.now();
cc.game.on("commonEventInterface", commonEventInterface_battle, this);
};
export let is_over: boolean = false;
export let onGameCleanPosters = function () {
if (!DeviceUtil.IsMXGame) {
return;
}
gameManager.onGameCleanPosters();
};
export let onBattleGameOver = function () {
if (!DeviceUtil.IsMXGame) {
return;
}
if (is_over) {
return;
}
is_over = true;
let myScore = 0;
let otherScore = 0;
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
if (gameStore.player1Data.completeBlockArr[i][j] === 1) {
myScore++;
}
if (gameStore.player2Data.completeBlockArr[i][j] === 1) {
otherScore++;
}
}
}
let info = {
gameId: gameId_,
battleId: battleId_,
roomId: roomId_,
battle_info: {
currentscore: myScore,
oppoScore: otherScore,
winStatus: gameStore.winType,
numA: gameStore.selfMoveCount,
numB: gameStore.otherMoveCount,
numC: gameStore.createPropsNum,
numD: gameStore.usePauseSkillCount,
numE: gameStore.useSwapSkillCount,
numF: gameStore.useRandomSkillCount,
numG: gameStore.beUsedPauseSkillCount,
numH: gameStore.beUsedRandomSkillCount,
numI: gameStore.selfGetPropNum,
numJ: gameStore.otherGetPropNum,
oppoType: gameStore.isAI,
oppoUserID: gameStore.player2Data.id,
},
};
let rInfo = {
roomID: battleId_,
playtime: Math.round((Date.now() - game_start_time) / 1000)
};
gameManager.onTrack("gameExit", JSON.stringify(rInfo));
const emojiClickedInfo = JSON.stringify(gameStore.expressionStatistic);
gameManager.onTrack("emojiClicked", emojiClickedInfo);
gameManager.onBattleGameOver(JSON.stringify(info));
};
export let commonEventInterface_battle = function (event_name: string, info: any) {
switch (event_name) {
case "backPressed": {
cc.game.emit("userGamePausedCall");
cc.game.emit("userPausedViewShowCall");
break;
}
case "screenOn": {
break;
}
case "screenOff": {
cc.game.emit("userGamePausedCall");
cc.game.emit("userPausedViewShowCall");
break;
}
case "pagePause": {
cc.game.emit("eventGamePausedCall");
cc.game.emit("eventMusicPausedCall");
break;
}
case "pageResume": {
cc.game.emit("eventGameResumeCall");
cc.game.emit("eventMusicResumeCall");
break;
}
case "userPresent": {
break;
}
case "userMatched": {
battleInfo(info);
break;
}
}
};
export let battleInfo = (info: any) => {
console.log("battleinfo" + info)
let obj: info = JSON.parse(info);
setGameId(obj.gameId);
setBattleId(obj.battleId);
setRoomId(obj.roomId);
let selfUserId: string = obj.selfUserId;
let my_index = 0;
let other_index = 1;
if (obj.users[1].userId == selfUserId) {
my_index = 1;
other_index = 0;
}
gameStore.player1Data.setId(obj.users[my_index].playerId);
gameStore.player1Data.setNickName(obj.users[my_index].name);
gameStore.player1Data.setAvatar(obj.users[my_index].avatar);
gameStore.player2Data.setId(obj.users[other_index].playerId);
gameStore.player2Data.setNickName(obj.users[other_index].name);
gameStore.player2Data.setAvatar(obj.users[other_index].avatar);
let playerData = obj.users[other_index].playerData;
if (playerData && playerData[conditionId.UseSkin]) {
let player2BlockSkin = playerData[conditionId.UseSkin].split("|")[0];
gameStore.setPlayer2BlockSkinId(Number(player2BlockSkin) % 100000);
} else {
gameStore.setPlayer2BlockSkinId(1);
}
SocketManager.instance.connecturl = obj.connectUrl;
let rInfo = {
roomID: battleId_,
};
gameManager.onTrack("gameStart", JSON.stringify(rInfo));
onGameCleanPosters();
UIManager.instance.OnMsg(MatchUI, "", null);
};
mx.d.ts
declare namespace gameManager {
declare function onGameLoaded(paramStr: string): void;
declare function onGameCleanPosters(): void;
declare function onTrack(eventName: string, paramStr: string): void;
declare function onBattleGameOver(paramStr: string): void;
declare function onGameStart(): void;
declare function onGameInit(): string;
}
|