examples/script/qstetrix/tetrixpiece.js
changeset 0 1918ee327afb
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 TetrixShape = {
       
     2     NoShape:0,
       
     3     ZShape:1,
       
     4     SShape:2,
       
     5     LineShape:3,
       
     6     TShape:4,
       
     7     SquareShape:5,
       
     8     LShape:6,
       
     9     MirroredLShape:7
       
    10 }
       
    11 
       
    12 TetrixCoordsTable = [
       
    13     [ [ 0,   0 ], [ 0,  0 ], [ 0,  0 ], [ 0,  0 ] ],
       
    14     [ [ 0,  -1 ], [ 0,  0 ], [ -1, 0 ], [ -1, 1 ] ],
       
    15     [ [ 0,  -1 ], [ 0,  0 ], [ 1,  0 ], [ 1,  1 ] ],
       
    16     [ [ 0,  -1 ], [ 0,  0 ], [ 0,  1 ], [ 0,  2 ] ],
       
    17     [ [ -1,  0 ], [ 0,  0 ], [ 1,  0 ], [ 0,  1 ] ],
       
    18     [ [ 0,   0 ], [ 1,  0 ], [ 0,  1 ], [ 1,  1 ] ],
       
    19     [ [ -1, -1 ], [ 0, -1 ], [ 0,  0 ], [ 0,  1 ] ],
       
    20     [ [ 1,  -1 ], [ 0, -1 ], [ 0,  0 ], [ 0,  1 ] ]
       
    21 ]
       
    22 
       
    23 function TetrixPiece()
       
    24 {
       
    25     this.shape = TetrixShape.NoShape;
       
    26 }
       
    27 
       
    28 TetrixPiece.prototype.__defineGetter__(
       
    29     "shape",
       
    30     function() {
       
    31       return this._shape;
       
    32     }
       
    33 );
       
    34 
       
    35 TetrixPiece.prototype.__defineSetter__(
       
    36     "shape",
       
    37     function(shape) {
       
    38       this._shape = shape;
       
    39       this._coords = new Array(4);
       
    40       for (var i = 0; i < 4; ++i)
       
    41           this._coords[i] = TetrixCoordsTable[shape][i].slice();
       
    42     }
       
    43 );
       
    44 
       
    45 TetrixPiece.prototype.setRandomShape = function() {
       
    46     this.shape = Math.floor(((Math.random() * 100000) % 7) + 1);
       
    47 }
       
    48 
       
    49 TetrixPiece.prototype.getX = function(index) {
       
    50     return this._coords[index][0];
       
    51 }
       
    52 
       
    53 TetrixPiece.prototype.getY = function(index) {
       
    54     return this._coords[index][1];
       
    55 }
       
    56 
       
    57 TetrixPiece.prototype._setX = function(index, x) {
       
    58     this._coords[index][0] = x;
       
    59 }
       
    60 
       
    61 TetrixPiece.prototype._setY = function(index, y) {
       
    62     this._coords[index][1] = y;
       
    63 }
       
    64 
       
    65 TetrixPiece.prototype.__defineGetter__(
       
    66     "minX",
       
    67     function() {
       
    68         var min = this._coords[0][0];
       
    69         for (var i = 1; i < 4; ++i)
       
    70             min = Math.min(min, this._coords[i][0]);
       
    71         return min;
       
    72     }
       
    73 );
       
    74 
       
    75 TetrixPiece.prototype.__defineGetter__(
       
    76     "maxX",
       
    77     function() {
       
    78         var max = this._coords[0][0];
       
    79         for (var i = 1; i < 4; ++i)
       
    80             max = Math.max(max, this._coords[i][0]);
       
    81         return max;
       
    82     }
       
    83 );
       
    84 
       
    85 TetrixPiece.prototype.__defineGetter__(
       
    86     "minY",
       
    87     function() {
       
    88       var min = this._coords[0][1];
       
    89       for (var i = 1; i < 4; ++i)
       
    90           min = Math.min(min, this._coords[i][1]);
       
    91       return min;
       
    92     }
       
    93 );
       
    94 
       
    95 TetrixPiece.prototype.__defineGetter__(
       
    96     "maxY",
       
    97     function() {
       
    98         var max = this._coords[0][1];
       
    99         for (var i = 1; i < 4; ++i)
       
   100             max = Math.max(max, this._coords[i][1]);
       
   101         return max;
       
   102     }
       
   103 );
       
   104 
       
   105 TetrixPiece.prototype.rotatedLeft = function() {
       
   106     var result = new TetrixPiece();
       
   107     if (this._shape == TetrixShape.SquareShape) {
       
   108         result.shape = this._shape;
       
   109         return result;
       
   110     }
       
   111     result._shape = this._shape;
       
   112     for (var i = 0; i < 4; ++i) {
       
   113         result._setX(i, this.getY(i));
       
   114         result._setY(i, -this.getX(i));
       
   115     }
       
   116     return result;
       
   117 }
       
   118 
       
   119 TetrixPiece.prototype.rotatedRight = function() {
       
   120     var result = new TetrixPiece();
       
   121     if (this._shape == TetrixShape.SquareShape) {
       
   122         result.shape = this._shape;
       
   123         return result;
       
   124     }
       
   125     result._shape = this._shape;
       
   126     for (var i = 0; i < 4; ++i) {
       
   127         result._setX(i, -this.getY(i));
       
   128         result._setY(i, this.getX(i));
       
   129     }
       
   130     return result;
       
   131 }