Commit b18adaa4 authored by 李维's avatar 李维

添加消息处理

parent f372b74b
{
"ver": "1.1.2",
"uuid": "696dcc51-c784-42ea-b2e2-bf0bf47d2ad6",
"isBundle": false,
"bundleName": "",
"priority": 1,
"compressionType": {},
"optimizeHotUpdate": {},
"inlineSpriteFrames": {},
"isRemoteBundle": {},
"subMetas": {}
}
\ No newline at end of file
// Learn cc.Class:
// - https://docs.cocos.com/creator/manual/en/scripting/class.html
// Learn Attribute:
// - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
// - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html
cc.Class({
extends: cc.Component,
properties: {
// foo: {
// // ATTRIBUTES:
// default: null, // The default value will be used only when the component attaching
// // to a node for the first time
// type: cc.SpriteFrame, // optional, default is typeof default
// serializable: true, // optional, default is true
// },
// bar: {
// get () {
// return this._bar;
// },
// set (value) {
// this._bar = value;
// }
// },
// 调试模式
isDebug: false,
// 是否可以点击
canClick: true,
// 是否可以转动
isRolling: false,
// 是否正在闪烁
isBlinking: false
},
// LIFE-CYCLE CALLBACKS:
// onLoad () {},
start() {
this.initEventLinister();
},
// 初始化转盘
initTurntable(debugMode = false) {
this.isDebug = debugMode;
},
initEventLinister() {
// console.log("初始化事件监听");
cc.find("dice", this.node).on(cc.Node.EventType.TOUCH_START, () => {
if(this.canClick && !this.isRolling) {
this.node.emit("Rolling_Start")
} else {
cc.log(`已屏蔽转盘点击 canClick:${this.canClick} isRolling:${this.isRolling}`)
}
})
},
// 转动到制定数字
rollNum(num) {
const dice = cc.find("/dice", this.node);
if (this.isRolling) {
return;
}
return new Promise((resolve, reject) => {
const targetAngle = Math.ceil(dice.angle / 360 + 1) * 360;
const time = (targetAngle - 360 - dice.angle) / 360;
this.isRolling = true;
if (!this.isDebug) {
cc.tween(dice)
.by(1.5, { angle: 360 }, { easing: "cubicIn" })
.to(time, { angle: targetAngle })
.by(0.5, { angle: 360 })
.by(0.5, { angle: 360 })
.by(0.5, { angle: 360 })
.by(0.5, { angle: 360 })
.by((0.5 / 6) * num, { angle: 60 * num })
.by(1.5, { angle: 360 }, { easing: "cubicOut" })
.call(() => {
this.isRolling = false;
this.node.emit("Rolling_End")
resolve(null);
})
.start();
} else {
//TODO:调试加速
cc.tween(dice)
.by(0.3, { angle: 360 }, { easing: "cubicIn" })
.to(time, { angle: targetAngle })
.by(0.1, { angle: 360 })
.by(0.1, { angle: 360 })
.by(0.1, { angle: 360 })
.by(0.1, { angle: 360 })
.by((1 / 6) * num, { angle: 60 * num })
.by(0.3, { angle: 360 }, { easing: "cubicOut" })
.call(() => {
this.isRolling = false;
this.node.emit("Rolling_End")
resolve(null);
})
.start();
}
});
},
// 放大提示
blink() {
this.isBlinking = true;
const dice = cc.find("hatBlink", this.node);
cc.tween(dice)
.set({ opacity: 0 })
.to(0.2, { opacity: 255 })
.to(0.2, { opacity: 0 })
.union()
.repeatForever()
.start();
this.node.children.forEach((node) => {
cc.tween(node)
.set({ scale: 1 })
.to(0.2, { scale: 1.1 })
.to(0.2, { scale: 1.0 })
.union()
.repeatForever()
.start();
});
const dice2 = cc.find("dice", this.node);
},
// 停止放大提示
stopBlink() {
this.isBlinking = false;
const dice = cc.find("hatBlink", this.node);
dice.stopAllActions();
this.node.children.forEach((node) => {
node.stopAllActions();
node.scale = 1;
node.opacity = 255;
});
const dice2 = cc.find("diceBg/dice", this.node);
},
// 禁用
disable() {
this.canClick = false;
this.node.children.forEach((node) => {
node.color = cc.color(150, 150, 150);
});
},
// 启用
enable() {
this.canClick = true;
this.node.children.forEach((node) => {
node.color = cc.color(255, 255, 255);
});
}
// update (dt) {},
});
{
"ver": "1.0.8",
"uuid": "81dea30c-b3f4-47bc-9828-73d6b836f325",
"isPlugin": false,
"loadPluginInWeb": true,
"loadPluginInNative": true,
"loadPluginInEditor": false,
"subMetas": {}
}
\ No newline at end of file
This diff is collapsed.
{
"ver": "1.2.9",
"uuid": "f2f17746-ad49-4524-8664-b90f6e1fef55",
"optimizationPolicy": "AUTO",
"asyncLoadAssets": false,
"readonly": false,
"subMetas": {}
}
\ No newline at end of file
{
"ver": "1.1.2",
"uuid": "7d25d89b-4bad-4cd9-838e-62eec49b208c",
"isBundle": false,
"bundleName": "",
"priority": 1,
"compressionType": {},
"optimizeHotUpdate": {},
"inlineSpriteFrames": {},
"isRemoteBundle": {},
"subMetas": {}
}
\ No newline at end of file
{
"ver": "2.3.5",
"uuid": "2ec350b5-18de-4b5f-845d-35507d5b1e85",
"type": "sprite",
"wrapMode": "clamp",
"filterMode": "bilinear",
"premultiplyAlpha": false,
"genMipmaps": false,
"packable": true,
"width": 450,
"height": 452,
"platformSettings": {},
"subMetas": {
"dice": {
"ver": "1.0.4",
"uuid": "2f1fdd73-69db-4c21-afca-d5eaca4e4642",
"rawTextureUuid": "2ec350b5-18de-4b5f-845d-35507d5b1e85",
"trimType": "auto",
"trimThreshold": 1,
"rotated": false,
"offsetX": 0,
"offsetY": 0.5,
"trimX": 0,
"trimY": 0,
"width": 450,
"height": 451,
"rawWidth": 450,
"rawHeight": 452,
"borderTop": 0,
"borderBottom": 0,
"borderLeft": 0,
"borderRight": 0,
"subMetas": {}
}
}
}
\ No newline at end of file
{
"ver": "2.3.5",
"uuid": "f3ed426b-1886-473d-ab9b-c37e130168ef",
"type": "sprite",
"wrapMode": "clamp",
"filterMode": "bilinear",
"premultiplyAlpha": false,
"genMipmaps": false,
"packable": true,
"width": 319,
"height": 319,
"platformSettings": {},
"subMetas": {
"dice_bg": {
"ver": "1.0.4",
"uuid": "f4390000-b700-40a9-a031-d5500e47c613",
"rawTextureUuid": "f3ed426b-1886-473d-ab9b-c37e130168ef",
"trimType": "auto",
"trimThreshold": 1,
"rotated": false,
"offsetX": 0,
"offsetY": 0,
"trimX": 0,
"trimY": 0,
"width": 319,
"height": 319,
"rawWidth": 319,
"rawHeight": 319,
"borderTop": 0,
"borderBottom": 0,
"borderLeft": 0,
"borderRight": 0,
"subMetas": {}
}
}
}
\ No newline at end of file
{
"ver": "2.3.5",
"uuid": "61574349-54ad-492b-8324-d0a36d932e92",
"type": "sprite",
"wrapMode": "clamp",
"filterMode": "bilinear",
"premultiplyAlpha": false,
"genMipmaps": false,
"packable": true,
"width": 129,
"height": 129,
"platformSettings": {},
"subMetas": {
"hat": {
"ver": "1.0.4",
"uuid": "48186edc-fcde-423a-bae5-7674b0ecf20c",
"rawTextureUuid": "61574349-54ad-492b-8324-d0a36d932e92",
"trimType": "auto",
"trimThreshold": 1,
"rotated": false,
"offsetX": 0,
"offsetY": 0,
"trimX": 0,
"trimY": 0,
"width": 129,
"height": 129,
"rawWidth": 129,
"rawHeight": 129,
"borderTop": 0,
"borderBottom": 0,
"borderLeft": 0,
"borderRight": 0,
"subMetas": {}
}
}
}
\ No newline at end of file
{
"ver": "2.3.5",
"uuid": "104980b7-c24e-4b57-8e27-cb1cbf90ed10",
"type": "sprite",
"wrapMode": "clamp",
"filterMode": "bilinear",
"premultiplyAlpha": false,
"genMipmaps": false,
"packable": true,
"width": 68,
"height": 109,
"platformSettings": {},
"subMetas": {
"point": {
"ver": "1.0.4",
"uuid": "aaea9af8-fa93-4cc0-964a-be8c6a8c07cb",
"rawTextureUuid": "104980b7-c24e-4b57-8e27-cb1cbf90ed10",
"trimType": "auto",
"trimThreshold": 1,
"rotated": false,
"offsetX": 0,
"offsetY": -1,
"trimX": 1,
"trimY": 2,
"width": 66,
"height": 107,
"rawWidth": 68,
"rawHeight": 109,
"borderTop": 0,
"borderBottom": 0,
"borderLeft": 0,
"borderRight": 0,
"subMetas": {}
}
}
}
\ No newline at end of file
......@@ -10,6 +10,14 @@ import { NetworkHelper } from "../script/NetworkHelper";
import { GameServer } from "../script/Server";
const { ccclass, property } = cc._decorator;
const RED = "red";
const BLUE = "blue";
// 所有消息列表
const MSG_TEST = "msg_test";
const MSG_SWITCHSIDE = "msg_switch_side";
const MSG_ROLLING = "msg_rolling";
const MSG_PROFIX_END = "__END__"
@ccclass
export default class SceneComponent extends MyCocosSceneComponent {
......@@ -27,6 +35,7 @@ export default class SceneComponent extends MyCocosSceneComponent {
playerId; // 当前玩家ID
isRoomOwner; // 是否为房主
recordWaitCount; // 录音等待倒计时
asyncToken: ""; // 同步令牌 消息发送发会把令牌一同传给其他客户端,当客户端执行异步动作完毕以后 会把同步令牌
addPreloadImage() {
// TODO 根据自己的配置预加载图片资源
......@@ -46,6 +55,7 @@ export default class SceneComponent extends MyCocosSceneComponent {
this.initData();
this.initView();
this.initListener();
this.initEvents();
}
// 初始化数据
......@@ -131,6 +141,8 @@ export default class SceneComponent extends MyCocosSceneComponent {
// 初始化界面
this.initLoadingLayer();
this.initOfflineLayer();
this.initTurntable();
this.initGemstone();
}
// update (dt) {},
......@@ -161,7 +173,9 @@ export default class SceneComponent extends MyCocosSceneComponent {
this.loadingLayer = loadingLayerNode.getComponent("LoadingLayer");
this.loadingLayer.setMaxPlayerNumber(2);
this.loadingLayer.onLoadFinished(() => {});
this.loadingLayer.onLoadFinished(() => {
this.initSide();
});
}
// 初始化掉线提示组件
......@@ -194,7 +208,9 @@ export default class SceneComponent extends MyCocosSceneComponent {
this.offlineLayer.show();
this.networkHelper.leaveRoom();
});
this.networkHelper.on("gameStart", (event) => {});
this.networkHelper.on("gameStart", (event) => {
console.log("游戏开始")
});
this.networkHelper.on("frameEvent", (event) => {
cc.log("Frame event");
event.data.frame.items.forEach(async (item) => {
......@@ -232,16 +248,183 @@ export default class SceneComponent extends MyCocosSceneComponent {
player.uuid
);
});
} else if (item.data.type == "SERVER_updateStatus") {
cc.log("SERVER_updateStatus", item.data);
} else if (item.data.type == "SERVER_playerRoll") {
cc.log("SERVER_playerRoll", item.data);
} else if (item.data.type == "SERVER_playerRight") {
cc.log("SERVER_playerRight");
} else if (item.data.type == "SERVER_playerWrong") {
cc.log("SERVER_playerWrong");
} else {
// 遍历自定义消息
for(let key in this.eventsMap) {
// 找到对应的消息类型 并且发消息的人不是自己
if(item.data.type == key && item.data.sender != this.playerId) {
this.runServerEvent(key, item.data.param)
}
}
}
});
});
}
// 用户自定义事件
eventsMap = {};
registerEvent(name, callback, endCallback?) {
if(this.eventsMap[name] != undefined) {
cc.log("该事件已经注册过,将会覆盖原有注册")
}
this.eventsMap[name] = {
fn: callback,
pending: false,
isRequireEnd: endCallback?true:false,
}
if(endCallback) {
this.eventsMap[name + MSG_PROFIX_END] = {
fn: endCallback,
pending: false,
isRequireEnd: false,
}
}
}
// 注册全局游戏事件,需要玩家之间同步执行的方法
// 无论是自己方 还是对方,只需要调用即可,方法内处理消息发送和执行
runEvent(eventName, param) {
if(this.eventsMap[eventName] == undefined) {
cc.log("该事件没有注册过");
return;
}
this.eventsMap[eventName].fn(param, ()=>{
this.eventsMap[eventName].pending = false;
});
this.eventsMap[eventName].pending = true;
this.networkHelper.sendFrame({
type: eventName,
sender: this.playerId,
param: param,
});
}
// 执行服务器发来的消息广播 和runEvent不同的地方只是 这个方法不会再次转发消息
runServerEvent(eventName, param) {
if(this.eventsMap[eventName] == undefined) {
cc.log("该事件没有注册过");
return;
}
this.eventsMap[eventName].fn(param, ()=>{
this.eventsMap[eventName].pending = false;
if(this.eventsMap[eventName].isRequireEnd) {
this.networkHelper.sendFrame({
type: eventName + MSG_PROFIX_END,
sender: this.playerId
});
}
});
this.eventsMap[eventName].pending = true;
}
// 初始化转盘
turntable: null;
initTurntable() {
this.turntable = cc.find("Canvas/Turntable").getComponent("Turntable");
cc.find("Canvas/Turntable").on("Rolling_Start", ()=>{
this.turntable.stopBlink();
this.runEvent(MSG_ROLLING, {num:1});
})
// this.turntable.initTurntable(true);
// this.turntable.blink()
// setTimeout(() => {
// this.turntable.stopBlink()
// }, 5000);
// this.turntable.disable()
// setTimeout(() => {
// this.turntable.enable();
// }, 6000);
// setTimeout(() => {
// cc.log("ABCDE")
// this.turntable.rollNum(0);
// }, 7000);
}
// 初始化宝石
gemstones = [[], [], []]
initGemstone() {
const gemstoneGroup = cc.find("Canvas/center_map/gemstone");
gemstoneGroup.children.forEach(gem=>{
let itemData = gem.addComponent('OPW_BoardGame_L5R4_word_dataitem');
itemData.word = gem.name.split("_")[0];
itemData.score = Number(gem.name.split("_")[1]);
itemData.word = gem.name.split("_")[0];
itemData.beforeNode = cc.find("before", gem);
itemData.afterNode = cc.find("after", gem);
if(itemData.score == 1) {
this.gemstones[0].push(gem);
} else if(itemData.score == 2) {
this.gemstones[1].push(gem);
} else if(itemData.score == 3) {
this.gemstones[2].push(gem);
}
itemData.afterNode.active = false;
})
}
// 选边初始化
side = RED; // 标记自己阵营颜色
currentGameSide = RED; // 当前游戏轮次的颜色
initSide() {
if(this.isRoomOwner) {
this.side = RED;
// 初始化红色阵营 先手 开始闪烁
this.turntable.blink();
} else {
this.side = BLUE;
// 蓝色阵营 后手 禁止点击
this.turntable.disable()
}
}
// 注册自定义消息事件
initEvents() {
this.registerEvent(MSG_TEST, (param, next)=>{
console.log("测试消息")
next();
})
this.registerEvent(MSG_SWITCHSIDE, (param, next)=>{
this.currentGameSide = param.side;
this.handleSwitchSide();
next();
})
this.registerEvent(MSG_ROLLING, (param, next)=>{
this.turntable.stopBlink();
this.turntable.rollNum(param.num).then(()=>{
// 异步操作执行完成后调用
next();
});
}, ()=>{
console.log(`执行${MSG_ROLLING}结束`)
if(this.side == this.currentGameSide) {
if(this.currentGameSide == RED) {
this.currentGameSide = BLUE;
} else {
this.currentGameSide = RED;
}
let side = this.currentGameSide;
this.runEvent(MSG_SWITCHSIDE, {side});
}
})
}
// 处理换边
handleSwitchSide() {
if(this.side == this.currentGameSide) {
this.turntable.enable();
this.turntable.blink();
} else {
this.turntable.disable();
this.turntable.stopBlink();
}
}
}
// Learn cc.Class:
// - https://docs.cocos.com/creator/manual/en/scripting/class.html
// Learn Attribute:
// - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
// - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html
cc.Class({
extends: cc.Component,
properties: {
// foo: {
// // ATTRIBUTES:
// default: null, // The default value will be used only when the component attaching
// // to a node for the first time
// type: cc.SpriteFrame, // optional, default is typeof default
// serializable: true, // optional, default is true
// },
// bar: {
// get () {
// return this._bar;
// },
// set (value) {
// this._bar = value;
// }
// },
word: "",
score: -1,
beforeNode: null,
afterNode: null
},
// LIFE-CYCLE CALLBACKS:
// onLoad () {},
start () {
},
// update (dt) {},
});
{
"ver": "1.0.8",
"uuid": "c89bcffe-3feb-45fc-bc5c-1c4158b87e8a",
"isPlugin": false,
"loadPluginInWeb": true,
"loadPluginInNative": true,
"loadPluginInEditor": false,
"subMetas": {}
}
\ No newline at end of file
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