summaryrefslogtreecommitdiffstats
path: root/examples/script/qstetrix/tetrixpiece.js
blob: e99fa8a1796b4c0b60d9019c723a1f5584084a71 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
TetrixShape = {
    NoShape:0,
    ZShape:1,
    SShape:2,
    LineShape:3,
    TShape:4,
    SquareShape:5,
    LShape:6,
    MirroredLShape:7
}

TetrixCoordsTable = [
    [ [ 0,   0 ], [ 0,  0 ], [ 0,  0 ], [ 0,  0 ] ],
    [ [ 0,  -1 ], [ 0,  0 ], [ -1, 0 ], [ -1, 1 ] ],
    [ [ 0,  -1 ], [ 0,  0 ], [ 1,  0 ], [ 1,  1 ] ],
    [ [ 0,  -1 ], [ 0,  0 ], [ 0,  1 ], [ 0,  2 ] ],
    [ [ -1,  0 ], [ 0,  0 ], [ 1,  0 ], [ 0,  1 ] ],
    [ [ 0,   0 ], [ 1,  0 ], [ 0,  1 ], [ 1,  1 ] ],
    [ [ -1, -1 ], [ 0, -1 ], [ 0,  0 ], [ 0,  1 ] ],
    [ [ 1,  -1 ], [ 0, -1 ], [ 0,  0 ], [ 0,  1 ] ]
]

function TetrixPiece()
{
    this.shape = TetrixShape.NoShape;
}

TetrixPiece.prototype.__defineGetter__(
    "shape",
    function() {
      return this._shape;
    }
);

TetrixPiece.prototype.__defineSetter__(
    "shape",
    function(shape) {
      this._shape = shape;
      this._coords = new Array(4);
      for (var i = 0; i < 4; ++i)
          this._coords[i] = TetrixCoordsTable[shape][i].slice();
    }
);

TetrixPiece.prototype.setRandomShape = function() {
    this.shape = Math.floor(((Math.random() * 100000) % 7) + 1);
}

TetrixPiece.prototype.getX = function(index) {
    return this._coords[index][0];
}

TetrixPiece.prototype.getY = function(index) {
    return this._coords[index][1];
}

TetrixPiece.prototype._setX = function(index, x) {
    this._coords[index][0] = x;
}

TetrixPiece.prototype._setY = function(index, y) {
    this._coords[index][1] = y;
}

TetrixPiece.prototype.__defineGetter__(
    "minX",
    function() {
        var min = this._coords[0][0];
        for (var i = 1; i < 4; ++i)
            min = Math.min(min, this._coords[i][0]);
        return min;
    }
);

TetrixPiece.prototype.__defineGetter__(
    "maxX",
    function() {
        var max = this._coords[0][0];
        for (var i = 1; i < 4; ++i)
            max = Math.max(max, this._coords[i][0]);
        return max;
    }
);

TetrixPiece.prototype.__defineGetter__(
    "minY",
    function() {
      var min = this._coords[0][1];
      for (var i = 1; i < 4; ++i)
          min = Math.min(min, this._coords[i][1]);
      return min;
    }
);

TetrixPiece.prototype.__defineGetter__(
    "maxY",
    function() {
        var max = this._coords[0][1];
        for (var i = 1; i < 4; ++i)
            max = Math.max(max, this._coords[i][1]);
        return max;
    }
);

TetrixPiece.prototype.rotatedLeft = function() {
    var result = new TetrixPiece();
    if (this._shape == TetrixShape.SquareShape) {
        result.shape = this._shape;
        return result;
    }
    result._shape = this._shape;
    for (var i = 0; i < 4; ++i) {
        result._setX(i, this.getY(i));
        result._setY(i, -this.getX(i));
    }
    return result;
}

TetrixPiece.prototype.rotatedRight = function() {
    var result = new TetrixPiece();
    if (this._shape == TetrixShape.SquareShape) {
        result.shape = this._shape;
        return result;
    }
    result._shape = this._shape;
    for (var i = 0; i < 4; ++i) {
        result._setX(i, -this.getY(i));
        result._setY(i, this.getX(i));
    }
    return result;
}