验证中...
开源中国 2018 年度最后一场技术盛会邀你来约~错过就要等明年啦!点此立即预约
语言: JavaScript
分类: JavaScript 工具
最后更新于 2018-12-07 11:58
片段 1 片段 2
主要控制逻辑
game.js
原始数据 复制代码
var MajiangInfo = require("MajiangInfo")
var turn = {
"turn1": "my",
"turn2": "right",
"turn3": "up",
"turn4": "left",
}//轮转标志,表示这回合该谁出牌了
var millisecond = 0;
var second = 0;
var myMjTimer;
var myMajiangs = [];//我的手牌
var rightMajiangs = [];//右边的手牌
var upMajiangs = [];//上边的手牌
var leftMajiangs = [];//左边的手牌
cc.Class({
extends: cc.Component,
properties: {
myAtlas:{
default:null,
type:cc.SpriteAtlas,
},
rightAtlas:{
default:null,
type:cc.SpriteAtlas
},
upAtlas:{
default:null,
type:cc.SpriteAtlas
},
leftAtlas:{
default:null,
type:cc.SpriteAtlas
},
emptyAtlas:{
default:null,
type:cc.SpriteAtlas
},
majiangPrefab:{
default:null,
type:cc.Prefab
},
},
onLoad: function () {
this.my = this.node.getChildByName("me");
this.myInHand = this.my.getChildByName("inHand");
this.myInOutSide = this.my.getChildByName("myInOutSide");
this.newMajiang = this.my.getChildByName("newMajiang");
this.right = this.node.getChildByName("right");
this.rightInHand = this.right.getChildByName("inHand");
this.rightInOutSide = this.right.getChildByName("rightInOutSide");
this.newMajiangRight = this.right.getChildByName("newMajiang");
this.up = this.node.getChildByName("up");
this.upInHand = this.up.getChildByName("inHand");
this.upInOutSide = this.up.getChildByName("upInOutSide");
this.newMajiangUp = this.up.getChildByName("newMajiang");
this.left = this.node.getChildByName("left");
this.leftInHand = this.left.getChildByName("inHand");
this.leftInOutSide = this.left.getChildByName("leftInOutSide");
this.newMajiangLeft = this.left.getChildByName("newMajiang");
this.btnGoNext = this.node.getChildByName("btnGoNext");
this.btnGoNext.on('click', this.goNext, this); //事件注册
this.majiongArray = this.createMaJiangArray();//创建初始数组
this.myMajiangsInOutSide = [];//我的出牌
this.rightMajiangsInOutSide = [];//右边的出牌
this.upMajiangsInOutSide = [];//上边的出牌
this.leftMajiangsInOutSide = [];//左边的出牌
this.newMajiangStr = "";//我的新麻将的名字,如1筒
this.newMajiang.on('click', this.clickNewMajiang, this);
this.newMajiangStrRight = "";//右边的新麻将的名字
this.newMajiangStrUp = "";//上边的新麻将的名字
this.newMajiangStrLeft = "";//左边的新麻将的名字
this.turn = turn.turn1;//表示当前轮到我方出牌
this.createMajiongsInHand();//随机分配四方的初始手牌
//this.getMyMajiangInHand(this.myMajiangs.length);//生成我的初始手牌
this.getMyMajiangInHand(myMajiangs.length);//生成我的初始手牌
this.getRightMajiangInHand(rightMajiangs.length);//生成左边的初始手牌
this.getUpMajiangInHand(upMajiangs.length);//生成上边的初始手牌
this.getLeftMajiangInHand(leftMajiangs.length);//生成右边的初始手牌
this.getNewMajiang();//获取新麻将
/**/
},
//创建初始牌组
createMaJiangArray: function () {
var array = [
"1筒","2筒","3筒","4筒","5筒","6筒","7筒","8筒","9筒",
"1筒","2筒","3筒","4筒","5筒","6筒","7筒","8筒","9筒",
"1筒","2筒","3筒","4筒","5筒","6筒","7筒","8筒","9筒",
"1筒","2筒","3筒","4筒","5筒","6筒","7筒","8筒","9筒",
"1条","2条","3条","4条","5条","6条","7条","8条","9条",
"1条","2条","3条","4条","5条","6条","7条","8条","9条",
"1条","2条","3条","4条","5条","6条","7条","8条","9条",
"1条","2条","3条","4条","5条","6条","7条","8条","9条",
"1万","2万","3万","4万","5万","6万","7万","8万","9万",
"1万","2万","3万","4万","5万","6万","7万","8万","9万",
"1万","2万","3万","4万","5万","6万","7万","8万","9万",
"1万","2万","3万","4万","5万","6万","7万","8万","9万",
// "东","南","西","北","发","白","中",
// "东","南","西","北","发","白","中",
// "东","南","西","北","发","白","中",
// "东","南","西","北","发","白","中",
];
return array;
},
//创建初始手牌数组
createMajiongsInHand: function () {
for (var i = 0; i < 52; i++) { //52张手牌
var randomNum = Math.floor(Math.random()*this.majiongArray.length) //洗牌
if ( i % 4 == 0 ) {
//push() 在数组末尾添加1个或多个
//this.myMajiangs.push(this.majiongArray[randomNum]);
myMajiangs.push(this.majiongArray[randomNum]);
} else if ( i % 4 == 1 ) {
rightMajiangs.push(this.majiongArray[randomNum]);
} else if ( i % 4 == 2 ) {
upMajiangs.push(this.majiongArray[randomNum]);
} else if ( i % 4 == 3 ) {
leftMajiangs.push(this.majiongArray[randomNum]);
}
this.majiongArray.splice(randomNum,1);
}
// sortMajiangs(this.myMajiangs);//排序
sortMajiangs(myMajiangs);//排序
sortMajiangs(rightMajiangs);
sortMajiangs(upMajiangs);
sortMajiangs(leftMajiangs);
},
//初始化My手牌
getMyMajiangInHand: function (num) {
for (var i = this.myInHand.childrenCount; i < num; i++) {
// var str = MajiangInfo.myMajiangInhandName[this.myMajiangs[i]];
var str = MajiangInfo.myMajiangInhandName[myMajiangs[i]];
addOneMajiang(str, this.myInHand, this.myAtlas, this.majiangPrefab, i);
this.myInHand.getChildByTag(i).on('click', this.clickMyMajiang, this);
}
},
//My手牌点击事件
clickMyMajiang: function (event) {
if ( this.turn != turn.turn1 ) {
cc.log("还没轮到你出牌呢");
return;
}
this.myInHand.removeChildByTag(event.target.tag);
var str = "B" + event.target.name.substring(1);
// this.myMajiangsInOutSide.push(str);
this.myMajiangsInOutSide.push(str);
// addOneMajiang(str, this.myInOutSide, this.upAtlas, this.majiangPrefab);
// addInOutSideMajiang(str, this.myInOutSide, this.upAtlas, this.majiangPrefab,this.turn, this.myMajiangs);
addInOutSideMajiang(str, this.myInOutSide, this.upAtlas, this.majiangPrefab,this.turn, myMajiangs);
myMajiangs.splice(event.target.tag,1);
myMajiangs.push(this.newMajiangStr);
sortMajiangs(myMajiangs);
this.myInHand.removeAllChildren();
this.getMyMajiangInHand(myMajiangs.length);
this.turn = turn.turn2;
this.newMajiang.active = false;
cc.log("........................................my出牌");
//。。。。。。。。。。。。。。。。。。。。。。。。。。。。。加时钟结尾
cc.log(this.turn+"........................................时钟结束");
//resetTime();
this.getNewMajiang();
},
getNewMajiang: function () {
//..............................................................加时钟开始
cc.log(this.turn+"........................................时钟开始");
//myMjTimer = setInterval(timer,50); //每隔50毫秒调用timer
if ( this.majiongArray.length == 0 ) {
cc.log("牌组的牌已经全部打完了,这局流局");
this.turn = "gameover";
return
}
if ( this.turn == turn.turn1 ) {
this.newMajiang.active = true;
var randomNum = Math.floor(Math.random()*this.majiongArray.length);
this.newMajiangStr = this.majiongArray[randomNum];
var str = MajiangInfo.myMajiangInhandName[this.newMajiangStr];
var frame = this.myAtlas.getSpriteFrame(str);
this.newMajiang.getComponent(cc.Sprite).spriteFrame = frame;
this.majiongArray.splice(randomNum,1);
} else if ( this.turn == turn.turn2 ) {
this.newMajiangRight.active = true;
var randomNum = Math.floor(Math.random()*this.majiongArray.length);
this.newMajiangStrRight = this.majiongArray[randomNum];
rightMajiangs.push(this.newMajiangStrRight);
var str = MajiangInfo.rightMajiangInhandName[this.newMajiangStrRight];
var frame = this.emptyAtlas.getSpriteFrame("e_mj_right");
this.newMajiangRight.getComponent(cc.Sprite).spriteFrame = frame;
this.majiongArray.splice(randomNum,1);
} else if ( this.turn == turn.turn3 ) {
this.newMajiangUp.active = true;
var randomNum = Math.floor(Math.random()*this.majiongArray.length);
this.newMajiangStrUp = this.majiongArray[randomNum];
upMajiangs.push(this.newMajiangStrUp);
var str = MajiangInfo.upMajiangInhandName[this.newMajiangStrUp];
var frame = this.emptyAtlas.getSpriteFrame("e_mj_up");
this.newMajiangUp.getComponent(cc.Sprite).spriteFrame = frame;
this.majiongArray.splice(randomNum,1);
} else if ( this.turn == turn.turn4 ) {
this.newMajiangLeft.active = true;
var randomNum = Math.floor(Math.random()*this.majiongArray.length);
this.newMajiangStrLeft = this.majiongArray[randomNum];
leftMajiangs.push(this.newMajiangStrLeft);
var str = MajiangInfo.leftMajiangInhandName[this.newMajiangStrLeft];
var frame = this.emptyAtlas.getSpriteFrame("e_mj_left");
this.newMajiangLeft.getComponent(cc.Sprite).spriteFrame = frame;
this.majiongArray.splice(randomNum,1);
}
},
clickNewMajiang: function () {
var str = MajiangInfo.upMajiangInhandName[this.newMajiangStr];
this.myMajiangsInOutSide.push(str);
// addOneMajiang(str, this.myInOutSide, this.upAtlas, this.majiangPrefab);
addInOutSideMajiang(str, this.myInOutSide, this.upAtlas, this.majiangPrefab,this.turn, myMajiangs);
this.turn = turn.turn2;
this.newMajiang.active = false;
this.getNewMajiang();
},
goNext: function () {
//。。。。。。。。。。。。。。。。。。。。。。。。。。。。。加时钟结尾
cc.log(this.turn+"........................................时钟结束");
//resetTime();
//让非我方操控的玩家随机出一张牌
if ( this.turn == turn.turn1 ) {
cc.log("轮到你的回合了,快出牌");
return
} else if ( this.turn == turn.turn2 ) {
//math.floor取整
//this.rightMajiangs.length假如为10,Math.random()*this.rightMajiangs.length就是取10以内的随机数
var randomNum = Math.floor(Math.random()*rightMajiangs.length);
var str = MajiangInfo.rightMajiangInhandName[rightMajiangs[randomNum]];
// addOneMajiang(str, this.rightInOutSide, this.rightAtlas, this.majiangPrefab);
addInOutSideMajiang(str, this.rightInOutSide, this.rightAtlas, this.majiangPrefab,this.turn, rightMajiangs);
rightMajiangs.splice(randomNum,1);
sortMajiangs(rightMajiangs);
this.rightInHand.removeAllChildren();
this.getRightMajiangInHand(rightMajiangs.length);
this.newMajiangRight.active = false;
this.turn = turn.turn3;
this.getNewMajiang();
} else if ( this.turn == turn.turn3 ) {
var randomNum = Math.floor(Math.random()*upMajiangs.length);
var str = MajiangInfo.upMajiangInhandName[upMajiangs[randomNum]];
// addOneMajiang(str, this.upInOutSide, this.upAtlas, this.majiangPrefab);
addInOutSideMajiang(str, this.upInOutSide, this.upAtlas, this.majiangPrefab,this.turn, upMajiangs);
upMajiangs.splice(randomNum,1);
sortMajiangs(upMajiangs);
this.upInHand.removeAllChildren();
this.getUpMajiangInHand(upMajiangs.length);
this.newMajiangUp.active = false;
this.turn = turn.turn4;
this.getNewMajiang();
} else if ( this.turn == turn.turn4 ) {
var randomNum = Math.floor(Math.random()*leftMajiangs.length);
var str = MajiangInfo.leftMajiangInhandName[leftMajiangs[randomNum]];
//addOneMajiang(str, this.leftInOutSide, this.leftAtlas, this.majiangPrefab);
addInOutSideMajiang(str, this.leftInOutSide, this.leftAtlas, this.majiangPrefab,this.turn, leftMajiangs);
leftMajiangs.splice(randomNum,1);
sortMajiangs(leftMajiangs);
this.leftInHand.removeAllChildren();
this.getLeftMajiangInHand(leftMajiangs.length);
this.newMajiangLeft.active = false;
this.turn = turn.turn1;
this.getNewMajiang();
}
},
getRightMajiangInHand: function (num) {
for (var i = this.rightInHand.childrenCount; i < num; i++) {
// var str = MajiangInfo.rightMajiangInhandName[this.rightMajiangs[i]];
// addOneMajiang(str, this.rightInHand, this.rightAtlas, this.majiangPrefab);
addOneMajiang("e_mj_right", this.rightInHand, this.emptyAtlas, this.majiangPrefab);
}
},
getUpMajiangInHand: function (num) {
for (var i = this.upInHand.childrenCount; i < num; i++) {
// var str = MajiangInfo.upMajiangInhandName[this.upMajiangs[i]];
// addOneMajiang(str, this.upInHand, this.upAtlas, this.majiangPrefab);
addOneMajiang("e_mj_up", this.upInHand, this.emptyAtlas, this.majiangPrefab);
}
},
getLeftMajiangInHand: function (num) {
for (var i = this.leftInHand.childrenCount; i < num; i++) {
// var str = MajiangInfo.leftMajiangInhandName[this.leftMajiangs[i]];
// addOneMajiang(str, this.leftInHand, this.leftAtlas, this.majiangPrefab);
addOneMajiang("e_mj_left", this.leftInHand, this.emptyAtlas, this.majiangPrefab);
}
},
});
//tag升序
function sortByTag (a,b) {
var aa = MajiangInfo.majiangTag[a];
var bb = MajiangInfo.majiangTag[b];
return bb - aa;
}
//根据麻将的tag,万-条-筒-东南西北发中白来排序
function sortMajiangs (array) {
array.sort(sortByTag);
}
//向舍弃牌堆加一张牌
//str:牌名、layout:牌堆所在、atlas:所属贴图、prefab:麻将贴图
function addOneMajiang (str,layout,atlas,prefab,tag) {
var majiang = cc.instantiate(prefab); //获取预制
var frame = atlas.getSpriteFrame(str); //获取sprite图片
majiang.getComponent(cc.Sprite).spriteFrame = frame; //预制变换为需要的sprite精灵图片
if ( tag != undefined ) {
majiang.tag = tag;
majiang.name = str;
}
layout.addChild(majiang); //在区域内添加一个麻将
}
function addInOutSideMajiang(str,layout,atlas,prefab,turn,majiangs){
var majiang = cc.instantiate(prefab); //获取预制
var frame = atlas.getSpriteFrame(str); //获取sprite图片
majiang.getComponent(cc.Sprite).spriteFrame = frame; //预制变换为需要的sprite精灵图片
layout.addChild(majiang); //在区域内添加一个麻将
cc.log('.............等待吃、碰、杠、胡'+layout.name);
cc.log('.............等待吃、碰、杠、胡'+turn);
cc.log('.............等待吃、碰、杠、胡'+majiangs);
//switch 判断目标牌堆
//其他3家从目标牌堆判断是否拾取
// switch(layout.name){
// case 'myInOutSide':
// break;
// case 'upInOutSide':
// //碰、杠操作判断
// cc.log(peng_type(majiangs, str));
// break;
// case 'leftInOutSide':
// cc.log(peng_type(majiangs, str));
// break;
// case 'rightInOutSide':
// cc.log(peng_type(majiangs, str));
// break;
// default:
// break;
// }
}
//碰判断 majiangs数组中是否包含两个或三个str
function peng_type(majiangs, str){
var j=0;
for(var i=1; i<majiangs.length;i++){
if(majiangs[i] == str){
j++;
}
}
return j;
}
function resetTime(){
clearInterval(myMjTimer); //关闭该计数器
millisecond = 0;
second = 0;
}
function timer(){
millisecond = millisecond + 50;
if(millisecond >= 1000){
second = second + 1;
millisecond = 0;
}
if(second == 10){
resetTime();
}
//clearInterval() //关闭该计数器
}
//吃判断 (card_index下标) 返回false 或 吃类型
function get_chi_type(card_index){
var op = 0;
if(card_index == 1 || card_index >30){
return false; //属于东南西北风中发财不能吃
}
if(card_index == 1 || card_index == 11 || card_index == 21){
return false;
}
}

评论列表( 0 )

你可以在登录后,发表评论

搜索帮助