在 Scripts/logic 下创建文件 Board.js, 维护棋盘的数据, 代码如下:
- var Board = qc.Tetris.Board = function() {
- var self = this,
- size = qc.Tetris.SIZE,
- len = qc.Tetris.BLOCK_H;
- // 构建用来转换格子坐标的矩阵
- var m = self.m = new qc.Matrix();
- m.a = len;
- m.c = len / 2;
- m.d = len * (Math.sqrt(3) / 2);
- // 初始化棋盘数据
- self.data = {};
- for (var i = -size; i <= size; i++) {
- for (var j = -size; j <= size; j++) {
- // 这些格子落在六边形外, 忽略掉
- if (i j> 0 && Math.abs(i + j)> size) continue;
- if (i j <0 && (Math.abs(i)> size || Math.abs(j)> size)) continue;
- // 计算格子的坐标和对应屏幕上的偏移
- var pos = Tetris.makePos(i, j);
- var pt = self.toWorld(new qc.Point(i, j));
- self.data[pos] = {
- value: 0,
- x: pt.x,
- y: pt.y
- };
- }
- }
- };
- Board.prototype = {};
- Board.prototype.constructor = Board;
- Object.defineProperties(Board.prototype, {
- /**
- @property {boolean} die - 当前是否已经死亡了
- @readonly
- */
- die: {
- get: function() {
- // TODO: 等待实现
- }
- }
- });
- /**
- 清空棋盘
- */
- Board.prototype.clear = function() {
- for (var pos in this.data) {
- this.data[pos].value = 0;
- }
- };
- /**
- 重新开始游戏
- */
- Board.prototype.restart = function() {
- this.clear();
- };
- // 判定形状可以放进来不
- // pos: 目标逻辑坐标
- // list: 形状的信息
- Board.prototype.checkPutIn = function(pos, list) {
- // TODO: 等待实现
- };
- // 把某个形状放进来
- Board.prototype.putIn = function(pos, list, value) {
- // TODO: 等待实现
- };
- // 根据格子的逻辑坐标, 算出所在的屏幕坐标
- // distance: 两个格子中心点之间的距离
- Board.prototype.toWorld = function(p, distance) {
- if (!distance)
- return this.m.apply(p);
- var m = new qc.Matrix();
- m.a = distance;
- m.c = distance 0.5;
- m.d = distance (Math.sqrt(3) * 0.5);
- return m.apply(p);
- };
- // 根据格子的屏幕坐标, 反算格子的逻辑坐标
- Board.prototype.toLocal = function(p) {
- return this.m.applyInverse(p);
- };
来源: http://www.qdfuns.com/article/18142/482697321b4cec677e83b247af4c4acb.html