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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
|
package TetrixPiece;
use strict;
use warnings;
use blib;
use List::Util qw(min max);
use QtCore4;
use QtGui4;
# Make it a Qt::Object, so we can use 'this'. I'm lazy :-)
use QtCore4::isa qw( Qt::Object );
use TetrixPiece;
use constant {
NoShape => 0,
ZShape => 1,
SShape => 2,
LineShape => 3,
TShape => 4,
SquareShape => 5,
LShape => 6,
MirroredLShape => 7,
};
# XXX Why doesn't Exporter work?
#require Exporter;
#use base qw( Exporter );
#our @EXPORT = qw( NoShape ZShape SShape LineShape TShape SquareShape LShape
#MirroredLShape );
my @coordsTable = (
[ [ 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 ] ]
);
sub setX {
my ( $index, $x ) = @_;
this->coords->[$index]->[0] = $x;
}
sub setY {
my ( $index, $x ) = @_;
this->coords->[$index]->[1] = $x;
}
sub NEW {
shift->SUPER::NEW();
this->{coords} = [];
this->setShape( NoShape );
}
# [0]
sub pieceShape {
return this->{pieceShape};
}
sub shape {
return this->{pieceShape};
}
sub x {
my ( $index ) = @_;
return this->coords->[$index]->[0];
}
sub y {
my ( $index ) = @_;
return this->coords->[$index]->[1];
}
sub coords {
return this->{coords};
};
# [0]
sub qrand {
# 2147483647 is the value of RAND_MAX, defined in stdlib.h, at least on my
# machine. See the Qt4 4.2 documentation on qrand() for more details.
return rand(2147483647);
}
# [0]
sub setRandomShape {
this->setShape(qrand() % 7 + 1);
}
# [0]
# [1]
sub setShape {
my ($shape) = @_;
for (my $i = 0; $i < 4 ; $i++) {
for (my $j = 0; $j < 2; ++$j) {
this->coords()->[$i]->[$j] = $coordsTable[$shape]->[$i]->[$j];
}
}
this->{pieceShape} = $shape;
# [1] //! [2]
}
# [2]
# [3]
sub minX {
my $min = this->coords()->[0]->[0];
for (my $i = 1; $i < 4; ++$i) {
$min = min($min, this->coords()->[$i]->[0]);
}
return $min;
}
sub maxX {
# [3] //! [4]
my $max = this->coords()->[0]->[0];
for (my $i = 1; $i < 4; ++$i) {
$max = max($max, this->coords()->[$i]->[0]);
}
return $max;
}
# [4]
# [5]
sub minY {
my $min = this->coords()->[0]->[1];
for (my $i = 1; $i < 4; ++$i) {
$min = min($min, this->coords()->[$i]->[1]);
}
return $min;
}
sub maxY {
# [5] //! [6]
my $max = this->coords()->[0]->[1];
for (my $i = 1; $i < 4; ++$i) {
$max = max($max, this->coords()->[$i]->[1]);
}
return $max;
}
# [6]
# [7]
sub rotatedLeft {
if (this->pieceShape() == SquareShape) {
return this;
}
my $result = TetrixPiece();
$result->{pieceShape} = this->pieceShape();
for (my $i = 0; $i < 4; ++$i) {
$result->setX($i, this->y($i));
$result->setY($i, -(this->x($i)));
}
# [7]
return $result;
}
# [9]
sub rotatedRight {
if (this->pieceShape() == SquareShape) {
return this;
}
my $result = TetrixPiece();
$result->{pieceShape} = this->pieceShape();
for (my $i = 0; $i < 4; ++$i) {
$result->setX($i, -(this->y($i)));
$result->setY($i, this->x($i));
}
# [9]
return $result;
}
1;
|