(function() {"use strict";var __module = CC_EDITOR ? module : {exports:{}};var __filename = 'preview-scripts/assets/scripts/Game.js';var __require = CC_EDITOR ? function (request) {return cc.require(request, require);} : function (request) {return cc.require(request, __filename);};function __define (exports, require, module) {"use strict";
cc._RF.push(module, 'ba4c6+agflOKLasAnb5x5hx', 'Game', __filename);
// scripts/Game.js
'use strict';
var _Shape = require('./Shape');
cc.Class({
extends: cc.Component,
properties: {
tilePrefab: cc.Prefab, // 方块预制
shapeBoard: cc.Node, // 添加方块的节点
gameOverNode: cc.Node, // Game Over图片节点
restartNode: cc.Node, // 重新开始按钮节点
pauseResumeBtn: cc.Node, // 暂停按钮节点
pausePic: cc.SpriteFrame, // 暂停图片
resumePic: cc.SpriteFrame, // 继续图片
scoreLabel: cc.Label, // 分数文本
ranksNode: cc.Node, // 排行榜按钮
wxSubContextView: cc.Node, // 主域视窗容器
shareNode: cc.Node, // 分享按钮
// 音频
bgAudio: {
default: null,
type: cc.AudioClip
},
btnAudio: {
default: null,
type: cc.AudioClip
},
dropAudio: {
default: null,
type: cc.AudioClip
},
pauseResumeAudio: {
default: null,
type: cc.AudioClip
},
removeAudio: {
default: null,
type: cc.AudioClip
},
loseAudio: {
default: null,
type: cc.AudioClip
}
},
// LIFE-CYCLE CALLBACKS:
onLoad: function onLoad() {
cc.debug.setDisplayStats(false);
// 微信小游戏玩家授权
this.initUserInfoButton();
// 监听被动转发
this.passiveShare();
// 行列数
this.row = 22;
this.col = 12;
// 每个区域的宽高(即方块宽高)
this.tileWidth = this.node.width / this.col;
this.tileHeight = this.node.height / this.row;
// 方块节点池
this.tilePool = new cc.NodePool();
// 制作形状
this.makeShape();
// 保存已经下落完毕的方块
this.confirmedTileArray = [];
// 添加计时器,让方块每秒往下移动一步
this.schedule(this.moveDown, 1);
// 用于判断是否已经暂停
this.isPaused = false;
// 分数
this.score = 0;
// 播放背景音乐
cc.audioEngine.playMusic(this.bgAudio, true);
cc.audioEngine.setMusicVolume(0.7);
},
makeShape: function makeShape() {
// 生成形状
this.shapeTileArray = []; // 用来保存当前形状中的所有方块
this.color = this.getColor(); // 当前形状颜色
var startX = Math.floor(Math.random() * (this.col - 4)) + 2; // 横向起始位置
var startY = 2; // 纵向起始位置
var x = startX * this.tileWidth; // 关键方块x坐标
var y = startY * this.tileHeight; // 关键方块y坐标
var keyTile = this.getTile(); // 关键方块(旋转中心点)
keyTile.color = this.color;
keyTile.position = cc.v2(x, y);
keyTile.width = this.tileWidth;
keyTile.height = this.tileHeight;
this.shapeBoard.addChild(keyTile);
this.shapeTileArray.push(keyTile);
var coords = this.getShapeCoords(); // 随机获取一个形状坐标
for (var i = 1; i < coords.length; i++) {
var _x = (coords[i][0] + startX) * this.tileWidth; // 其他方块相对于关键方块的x坐标
var _y = (coords[i][1] + startY) * this.tileHeight; // 其他方块相对于关键方块的y坐标
var tile = this.getTile(); // 生成方块
tile.color = this.color;
tile.position = cc.v2(_x, _y);
tile.width = this.tileWidth;
tile.height = this.tileHeight;
this.shapeBoard.addChild(tile);
this.shapeTileArray.push(tile);
}
},
getColor: function getColor() {
// 设置随机颜色
var red = Math.round(Math.random() * 255);
var green = Math.round(Math.random() * 255);
var blue = Math.round(Math.random() * 255);
return new cc.Color(red, green, blue);
},
getTile: function getTile() {
// 生成方块预制
var tile = null;
if (this.tilePool.size() > 0) {
// 如果节点池中有方块,那从节点池中获取
tile = this.tilePool.get();
} else {
// 否则调用cc.instantiate()生成
tile = cc.instantiate(this.tilePrefab);
}
return tile;
},
getShapeCoords: function getShapeCoords() {
// 随机获取一种形状
var shapeArray = ['squareShape', 'lineShape', 'tShape', 'zShape', 'zShapeMirror', 'lShape', 'lShapeMirror'];
this.shape = shapeArray[Math.floor(Math.random() * shapeArray.length)];
// 随机获取该形状的某种形态,形态的索引保存在this.num中
var coordsArray = _Shape.SHAPE_COORDS[this.shape];
this.num = Math.floor(Math.random() * coordsArray.length);
// 返回坐标
return coordsArray[this.num];
},
leftBtn: function leftBtn() {
// 左移
if (this.isPaused) return;
cc.audioEngine.playEffect(this.btnAudio, false);
for (var i = 0; i < this.shapeTileArray.length; i++) {
var x = Math.round(this.shapeTileArray[i].x - this.tileWidth);
var y = Math.round(this.shapeTileArray[i].y);
// 防止出界
if (x < 0) {
return;
}
// 如果与其他方块重合,则不能移动
for (var j = 0; j < this.confirmedTileArray.length; j++) {
var confirmedX = Math.round(this.confirmedTileArray[j].x);
var confirmedY = Math.round(this.confirmedTileArray[j].y);
if (confirmedX == x && confirmedY == y) {
return;
}
}
}
// 当前形状中的方块全部左移一步
for (var _i = 0; _i < this.shapeTileArray.length; _i++) {
this.shapeTileArray[_i].x -= this.tileWidth;
}
},
rotateBtn: function rotateBtn() {
// 旋转
if (this.isPaused) return;
cc.audioEngine.playEffect(this.btnAudio, false);
// 如果形状只有一种变化形式,则直接返回
var temp = this.num;
if (_Shape.SHAPE_COORDS[this.shape].length == 1) return;else {
if (this.num + 1 == _Shape.SHAPE_COORDS[this.shape].length) this.num = 0;else this.num += 1;
}
var keyTile = this.shapeTileArray[0]; // 获取关键方块
var coords = _Shape.SHAPE_COORDS[this.shape][this.num]; // 获取旋转后的坐标
// 如果旋转后会超出边界或者与其他已存在的方块重合,则不旋转
for (var i = 1; i < coords.length; i++) {
var x = Math.round(keyTile.x + coords[i][0] * this.tileWidth);
var y = Math.round(keyTile.y + coords[i][1] * this.tileHeight);
// 是否超出边界
if (x < 0 || x >= this.shapeBoard.width || Math.abs(y) >= this.shapeBoard.height) {
this.num = temp;
return;
}
// 如果与其他方块重合,则不旋转
for (var j = 0; j < this.confirmedTileArray.length; j++) {
var confirmedX = Math.round(this.confirmedTileArray[j].x);
var confirmedY = Math.round(this.confirmedTileArray[j].y);
if (confirmedX == x && confirmedY == y) {
this.num = temp;
return;
}
}
}
// 根据坐标重新