|
| 1 | +package mino; |
| 2 | + |
| 3 | +import tetris.GamePanel; |
| 4 | +import tetris.KeyHandler; |
| 5 | +import tetris.PlayManager; |
| 6 | + |
| 7 | +import java.awt.*; |
| 8 | + |
| 9 | +public class Mino { |
| 10 | + |
| 11 | + public Block b [] = new Block[4]; |
| 12 | + public Block tempB[] = new Block[4]; |
| 13 | + |
| 14 | + int autoDropCounter = 0; |
| 15 | + |
| 16 | + public int direction = 1; |
| 17 | + boolean leftCollision,rightCollision,bottomCollision; |
| 18 | + public boolean active = true; |
| 19 | + |
| 20 | + // variables to create time gap b/w last and new mino |
| 21 | + public boolean deactivating; |
| 22 | + int deactivateCounter =0; |
| 23 | + |
| 24 | + |
| 25 | + |
| 26 | + |
| 27 | + |
| 28 | + public void create(Color c){ |
| 29 | + b[0]= new Block(c); |
| 30 | + b[1]= new Block(c); |
| 31 | + b[2]= new Block(c); |
| 32 | + b[3]= new Block(c); |
| 33 | + tempB[0]= new Block(c); |
| 34 | + tempB[1]= new Block(c); |
| 35 | + tempB[2]= new Block(c); |
| 36 | + tempB[3]= new Block(c); |
| 37 | + |
| 38 | + } |
| 39 | + |
| 40 | + public void setXY(int x, int y){ |
| 41 | + |
| 42 | + } |
| 43 | + |
| 44 | + public void updateXY(int direction){ |
| 45 | + |
| 46 | + checkRotationCollision(); |
| 47 | + |
| 48 | + |
| 49 | + if (leftCollision == false && rightCollision == false && bottomCollision == false){ |
| 50 | + this.direction=direction; |
| 51 | + |
| 52 | + // to store the orignal positon if collsion happens and we need to cancel |
| 53 | + // that why used temp array to store |
| 54 | + b[0].x = tempB[0].x; |
| 55 | + b[0].y = tempB[0].y; |
| 56 | + b[1].x = tempB[1].x; |
| 57 | + b[1].y = tempB[1].y; |
| 58 | + b[2].x = tempB[2].x; |
| 59 | + b[2].y = tempB[2].y; |
| 60 | + b[3].x = tempB[3].x; |
| 61 | + b[3].y = tempB[3].y; } |
| 62 | + |
| 63 | + } |
| 64 | + |
| 65 | + public void getDirection1(){ |
| 66 | + |
| 67 | + }public void getDirection2(){ |
| 68 | + |
| 69 | + }public void getDirection3(){ |
| 70 | + |
| 71 | + }public void getDirection4(){ |
| 72 | + } |
| 73 | + public void checkMovementCollision(){ |
| 74 | + |
| 75 | + leftCollision =false; |
| 76 | + rightCollision =false; |
| 77 | + bottomCollision =false; |
| 78 | + |
| 79 | + // check static block collision |
| 80 | + checkStaticBlockCollision(); |
| 81 | + |
| 82 | + // check frame collision |
| 83 | + // left wall |
| 84 | + |
| 85 | + for (int i=0 ; i< b.length ; i++){ |
| 86 | + if (b[i].x == PlayManager.left_x){ |
| 87 | + leftCollision = true; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + // right wall |
| 92 | + |
| 93 | + for (int i=0 ; i< b.length ; i++){ |
| 94 | + if (b[i].x + Block.SIZE == PlayManager.right_x){ |
| 95 | + rightCollision = true; |
| 96 | + } |
| 97 | + } |
| 98 | + // bottom |
| 99 | + for (int i=0 ; i< b.length ; i++){ |
| 100 | + if (b[i].y + Block.SIZE == PlayManager.bottom_y){ |
| 101 | + bottomCollision = true; |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + } |
| 106 | + public void checkRotationCollision(){ |
| 107 | + |
| 108 | + leftCollision =false; |
| 109 | + rightCollision =false; |
| 110 | + bottomCollision =false; |
| 111 | + |
| 112 | + // check static block collision |
| 113 | + checkStaticBlockCollision(); |
| 114 | + |
| 115 | + // check frame collision |
| 116 | + // left wall |
| 117 | + |
| 118 | + for (int i=0 ; i< b.length ; i++){ |
| 119 | + if (tempB[i].x < PlayManager.left_x){ // left x is greater than temp .x |
| 120 | + leftCollision = true; |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + // right wall |
| 125 | + |
| 126 | + for (int i=0 ; i< b.length ; i++){ |
| 127 | + if (tempB[i].x + Block.SIZE > PlayManager.right_x){ // right x is smaller than temp .x |
| 128 | + rightCollision = true; |
| 129 | + } |
| 130 | + } |
| 131 | + // bottom |
| 132 | + for (int i=0 ; i< b.length ; i++){ |
| 133 | + if (tempB[i].y + Block.SIZE > PlayManager.bottom_y){ // bottom y is smaller than temp .y |
| 134 | + bottomCollision = true; |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + } |
| 139 | + |
| 140 | + private void checkStaticBlockCollision(){ |
| 141 | + |
| 142 | + for (int i =0 ; i< PlayManager.staticBlocks.size() ;i++){ |
| 143 | + // get each blocks x and y |
| 144 | + int targetX = PlayManager.staticBlocks.get(i).x; |
| 145 | + int targetY = PlayManager.staticBlocks.get(i).y; |
| 146 | + |
| 147 | + |
| 148 | + // check down |
| 149 | + |
| 150 | + for (int j =0 ; j < b.length ; j++){ |
| 151 | + if(b[j].y + Block.SIZE == targetY && b[j].x == targetX){ |
| 152 | + bottomCollision = true; |
| 153 | + } |
| 154 | + |
| 155 | + } |
| 156 | + // left |
| 157 | + for (int j =0 ; j<b.length ; j++){ |
| 158 | + if(b[j].x - Block.SIZE == targetX && b[j].y == targetY){ |
| 159 | + leftCollision = true; |
| 160 | + } |
| 161 | + } |
| 162 | + for (int j =0 ; j<b.length ; j++){ |
| 163 | + if(b[j].x + Block.SIZE == targetX && b[j].y == targetY){ |
| 164 | + rightCollision = true; |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | + public void update(){ |
| 170 | + |
| 171 | + |
| 172 | + |
| 173 | + if (deactivating ){ |
| 174 | + deactivating(); |
| 175 | + } |
| 176 | + // lets control the mino |
| 177 | + |
| 178 | + if(KeyHandler.upPressed){ |
| 179 | + // this is bit tricky because it does rotation |
| 180 | + |
| 181 | + switch (direction){ |
| 182 | + // if current direction is 1 then this order below |
| 183 | + case 1: |
| 184 | + getDirection2(); break; // mino rotates every times w key is pressed |
| 185 | + case 2: |
| 186 | + getDirection3();break; |
| 187 | + case 3: |
| 188 | + getDirection4();break; |
| 189 | + case 4: |
| 190 | + getDirection1();break; |
| 191 | + |
| 192 | + } |
| 193 | + |
| 194 | + KeyHandler.upPressed = false; |
| 195 | + GamePanel.se.play(3,false); |
| 196 | + |
| 197 | + } |
| 198 | + |
| 199 | + checkMovementCollision(); |
| 200 | + |
| 201 | + if(KeyHandler.downPressed) { |
| 202 | + |
| 203 | + if(bottomCollision == false) |
| 204 | + { |
| 205 | + b[0].y += Block.SIZE; |
| 206 | + b[1].y += Block.SIZE; |
| 207 | + b[2].y += Block.SIZE; |
| 208 | + b[3].y += Block.SIZE; |
| 209 | + |
| 210 | + // when move down reset the auto drop counter |
| 211 | + autoDropCounter =0; |
| 212 | +// reset |
| 213 | + KeyHandler.downPressed =false; |
| 214 | + } |
| 215 | + } |
| 216 | + if(KeyHandler.leftPressed){ |
| 217 | + |
| 218 | + if (leftCollision ==false ){ |
| 219 | + |
| 220 | + |
| 221 | + b[0].x -= Block.SIZE; |
| 222 | + b[1].x -= Block.SIZE; |
| 223 | + b[2].x -= Block.SIZE; |
| 224 | + b[3].x -= Block.SIZE; |
| 225 | + |
| 226 | + KeyHandler.leftPressed =false;} |
| 227 | + } |
| 228 | + if(KeyHandler.rightPressed) { |
| 229 | + |
| 230 | + if (rightCollision == false) { |
| 231 | + |
| 232 | + |
| 233 | + b[0].x += Block.SIZE; |
| 234 | + b[1].x += Block.SIZE; |
| 235 | + b[2].x += Block.SIZE; |
| 236 | + b[3].x += Block.SIZE; |
| 237 | + |
| 238 | + KeyHandler.rightPressed = false; |
| 239 | + } |
| 240 | + } |
| 241 | + |
| 242 | + if (bottomCollision){ |
| 243 | + if(deactivating == false){ |
| 244 | + GamePanel.se.play(4,false); |
| 245 | + } |
| 246 | + deactivating = true; |
| 247 | + } |
| 248 | + else{ |
| 249 | + |
| 250 | + |
| 251 | + autoDropCounter ++; |
| 252 | + if(autoDropCounter == PlayManager.dropInterval){ |
| 253 | + // mino goes doww |
| 254 | + b[0].y +=Block.SIZE; |
| 255 | + b[1].y +=Block.SIZE; |
| 256 | + b[2].y +=Block.SIZE; |
| 257 | + b[3].y +=Block.SIZE; |
| 258 | + autoDropCounter =0; |
| 259 | + |
| 260 | + } |
| 261 | + } |
| 262 | + } |
| 263 | + |
| 264 | + private void deactivating() { |
| 265 | + |
| 266 | + deactivateCounter ++; |
| 267 | + // wait 45 frame until disActive |
| 268 | + if(deactivateCounter ==45){ |
| 269 | + |
| 270 | + deactivateCounter =0; |
| 271 | + checkMovementCollision(); // check if bottom is still hitting |
| 272 | + |
| 273 | + // if the bottom is still hitting after 45 frame , deactive the mino |
| 274 | + if(bottomCollision){ |
| 275 | + active = false; |
| 276 | + } |
| 277 | + } |
| 278 | + } |
| 279 | + |
| 280 | + public void draw(Graphics2D g2){ |
| 281 | + int margin =2; |
| 282 | + g2.setColor(b[0].c); |
| 283 | + g2.fillRect(b[0].x+margin,b[0].y+margin,Block.SIZE-(margin*2), Block.SIZE-(margin*2)); |
| 284 | + g2.fillRect(b[1].x+margin,b[1].y+margin,Block.SIZE-(margin*2), Block.SIZE-(margin*2)); |
| 285 | + g2.fillRect(b[2].x+margin,b[2].y+margin,Block.SIZE-(margin*2), Block.SIZE-(margin*2)); |
| 286 | + g2.fillRect(b[3].x+margin,b[3].y+margin,Block.SIZE-(margin*2), Block.SIZE-(margin*2)); |
| 287 | + } |
| 288 | +} |
0 commit comments