forked from Code-Bullet/Tetris-AI-Javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Shape.js
204 lines (173 loc) · 6.79 KB
/
Shape.js
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
class Shape {
constructor(shapeID, startingPos, game) {
this.game = game;
this.shapeID = shapeID;
this.currentPos = createVector(startingPos.x, startingPos.y);
this.startingPos = createVector(startingPos.x, startingPos.y);
this.blocks = [];
for (let pos of shapeID.blockPositions) {
this.blocks.push(new Block(createVector(pos.x, pos.y), shapeID.color));
}
this.isDead = false;
this.currentRotationCount = 0;
//vectors which control this piece into the best position
this.moveHistory = new MoveHistory();
}
clone(){
let clone = new Shape(this.shapeID, this.startingPos, this.game);
clone.currentPos = this.currentPos.copy();
clone.blocks = [];
for(let block of this.blocks){
clone.blocks.push(block.clone());
}
clone.isDead = this.isDead;
clone.currentRotationCount = this.currentRotationCount;
clone.moveHistory = this.moveHistory.clone();
return clone;
}
draw() {
push();
translate(this.currentPos.x * BLOCK_SIZE, this.currentPos.y * BLOCK_SIZE);
for (let block of this.blocks) {
block.draw();
}
pop();
}
//draws the shape with its CENTER at 0,0
drawAtOrigin() {
//get the midpoint of the shape
let sumX = 0;
let sumY = 0;
for (let block of this.blocks) {
sumX += block.currentGridPos.x + 0.5;
sumY += block.currentGridPos.y + 0.5;
}
let midpoint = createVector(sumX / this.blocks.length, sumY / this.blocks.length);
push();
//translate so that the midpoint is at 0,0
translate(-midpoint.x * BLOCK_SIZE, -midpoint.y * BLOCK_SIZE);
for (let block of this.blocks) {
block.draw();
}
pop();
}
moveShape(x, y,blockMatrix) {
if(blockMatrix){
if (this.canMoveInDirection(x, y, blockMatrix)) {
this.currentPos.x += x;
this.currentPos.y += y;
this.moveHistory.addDirectionalMove(x,y);
}
}else if (this.canMoveInDirection(x, y)) {
this.currentPos.x += x;
this.currentPos.y += y;
this.moveHistory.addDirectionalMove(x,y);
}
}
moveDown(resetAfterDeath) {
if (this.canMoveDown()) {
this.currentPos.y += 1;
} else {
this.killShape(resetAfterDeath);
}
}
resetPosition() {
this.currentPos = createVector(this.startingPos.x, this.startingPos.y);
}
killShape(resetAfterDeath) {
this.isDead = true;
if(!resetAfterDeath){
for (let block of this.blocks) {
//the block becomes disconnected from the shape and therefore the current grid position is no longer relative to the shape
block.currentGridPos.add(this.currentPos);
this.game.deadBlocks.push(block);
this.game.deadBlocksMatrix[block.currentGridPos.x][block.currentGridPos.y] = block;
}
}
}
canMoveDown(blockMatrix) {
for (let block of this.blocks) {
let futureBlockPosition = p5.Vector.add(this.currentPos, block.currentGridPos);
futureBlockPosition.y += 1;
//if a block matrix is passed into the function then look at that instead of the game
if (blockMatrix){
if(!blockMatrix.isPositionVacant(futureBlockPosition)){
return false;
}
}else{
if (!this.game.isPositionVacant(futureBlockPosition)) {
return false;
}
}
}
return true;
}
canMoveInDirection(x, y,blockMatrix ) {
//look at the future position of each block in the shape and if all those positions are vacant then we good
for (let block of this.blocks) {
let futureBlockPosition = p5.Vector.add(this.currentPos, block.currentGridPos);
futureBlockPosition.y += y;
futureBlockPosition.x += x;
//if a block matrix is passed into the function then look at that instead of the game
if (blockMatrix){
if(!blockMatrix.isPositionVacant(futureBlockPosition)){
return false;
}
}else{
if (!this.game.isPositionVacant(futureBlockPosition)) {
return false;
}
}
}
return true;
}
canRotateShape(isClockwise,blockMatrix) {
for (let i = 0; i < this.blocks.length; i++) {
let newPosition = this.getBlockPositionAfterShapeIsRotated(this.blocks[i], isClockwise);
let newAbsolutePosition = p5.Vector.add(newPosition, this.currentPos);
//if a block matrix is passed into the function then look at that instead of the game
if (blockMatrix){
if(!blockMatrix.isPositionVacant(newAbsolutePosition)){
return false;
}
}else{
if (!this.game.isPositionVacant(newAbsolutePosition)) {
return false;
}
}
}
return true;
}
getBlockPositionAfterShapeIsRotated(block, isClockwise) {
let startingPos = block.currentGridPos;
let rotationPoint = this.shapeID.rotationPoint;
let startingPosRelativeToRotationPoint = p5.Vector.sub(startingPos, rotationPoint);
let rotatedRelativePoint = startingPosRelativeToRotationPoint.rotate(
isClockwise ? Math.PI / 2 : -Math.PI / 2);
let newPosition = p5.Vector.add(rotationPoint, rotatedRelativePoint);
newPosition.x = Math.round(newPosition.x);
newPosition.y = Math.round(newPosition.y);
return newPosition;
}
rotateShape(isClockwise, blockMatrix) {
if(blockMatrix){
if (this.canRotateShape(isClockwise,blockMatrix)) {
for (let i = 0; i < this.blocks.length; i++) {
let newPosition = this.getBlockPositionAfterShapeIsRotated(this.blocks[i], isClockwise);
this.blocks[i].currentGridPos = newPosition;
}
this.currentRotationCount+=1;
this.moveHistory.addRotationMove();
}
}else{
if (this.canRotateShape(isClockwise)) {
for (let i = 0; i < this.blocks.length; i++) {
let newPosition = this.getBlockPositionAfterShapeIsRotated(this.blocks[i], isClockwise);
this.blocks[i].currentGridPos = newPosition;
}
this.currentRotationCount+=1;
this.moveHistory.addRotationMove();
}
}
}
}