Skip to content

Instantly share code, notes, and snippets.

@lsongdev
Created September 29, 2016 02:25
Show Gist options
  • Select an option

  • Save lsongdev/b35f0f1bf8054b5076e930d8a86d0d78 to your computer and use it in GitHub Desktop.

Select an option

Save lsongdev/b35f0f1bf8054b5076e930d8a86d0d78 to your computer and use it in GitHub Desktop.
/**
* [BreakBlock description]
*/
function BreakBlock(row, col){
this.data = [];
this.position = [ 0, 0 ];
this.direction = BreakBlock.DIRECTION.NE;
};
BreakBlock.AXIS = {
X: 0x00,
Y: 0x01
};
/**
* [CONST description]
* @type {Object}
*/
BreakBlock.CONST = {
EMPTY : 0x00,
BLOCK : 0x01,
BALL : 0x02,
PANEL : 0x03
};
BreakBlock.DIRECTION = {
N: 0x01,
S: 0x02,
W: 0x03,
E: 0x04,
NW: 0x05,
NE: 0x06,
SW: 0x07,
SE: 0x08
};
/**
* [function 初始化空白数据]
* @param {[type]} row [description]
* @param {[type]} col [description]
* @return {[type]} [description]
*/
BreakBlock.prototype.init = function(row, col) {
this.data = [];
while(row--){
var arr = [], i = col;
while(i--) arr.push(BreakBlock.CONST.EMPTY);
this.data.push(arr);
}
return this;
};
/**
* [function 填充指定行数数据 from up to down]
* @param {[type]} row [description]
* @return {[type]} [description]
*/
BreakBlock.prototype.fill = function(row) {
while(row--) {
var col = this.data[row].length;
while(col--) this.data[
row ][ col
] = BreakBlock.CONST.BLOCK;
}
return this;
};
/**
* [function 移动到指定位置]
*
* Y N
* | |
* | 1 | 2
* | |
* W-----------+----------E
* | |
* | 3 | 4
* | |
* X-----------S-----------
*
* @param {[type]} x [description]
* @param {[type]} y [description]
* @return {[type]} [description]
*/
BreakBlock.prototype.next = function(){
var x = this.position[ BreakBlock.AXIS.X ];
var y = this.position[ BreakBlock.AXIS.Y ];
switch (this.direction) {
case BreakBlock.DIRECTION.N:
// y++;
// if(y is max){
// this.direction = BreakBlock.DIRECTION.S;
// }
break;
case BreakBlock.DIRECTION.S : this.direction = BreakBlock.DIRECTION.N ; break;
case BreakBlock.DIRECTION.W : this.direction = BreakBlock.DIRECTION.E ; break;
case BreakBlock.DIRECTION.E : this.direction = BreakBlock.DIRECTION.W ; break;
case BreakBlock.DIRECTION.NW:
// x--;
// y++;
// if(y is max){
//
// }
// this.direction = BreakBlock.DIRECTION.SW;
break;
case BreakBlock.DIRECTION.NE : break;
case BreakBlock.DIRECTION.SW : this.direction = BreakBlock.DIRECTION.NW ; break;
case BreakBlock.DIRECTION.SE :
x
// this.direction = BreakBlock.DIRECTION.NE ;
break;
}
return this;
};
/**
* [function]
* @return {[type]} [description]
*/
BreakBlock.prototype.get = function(x, y){
return this.data[ (this.data.length - 1) - y ][ x ];
return this;
};
/**
* [function]
* @return {[type]} [description]
*/
BreakBlock.prototype.left = function(){
return this;
};
/**
* [function]
* @return {[type]} [description]
*/
BreakBlock.prototype.right = function(){
return this;
};
var bb = new BreakBlock().init(10, 10).fill(5);
setInterval(function(){
console.log(bb.next());
}, 1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment