Permalink
Browse files

Major refactor. Moved many common things into Piece class.

  • Loading branch information...
1 parent 45d7400 commit e3341da6f4bef50ecf29b5fad720545fc58a0e78 chris dillon committed Mar 23, 2009
Showing with 506 additions and 1,447 deletions.
  1. +50 −241 IPiece.pde
  2. +49 −242 JPiece.pde
  3. +48 −172 LPiece.pde
  4. +39 −56 OPiece.pde
  5. +161 −3 Piece.pde
  6. +5 −0 PieceBag.pde
  7. +21 −11 PlayState.pde
  8. +44 −236 SPiece.pde
  9. +47 −256 TPiece.pde
  10. +42 −230 ZPiece.pde
View
291 IPiece.pde
@@ -1,24 +1,17 @@
class IPiece extends Piece {
-
/*
- # 1
- # 0
- # 2
- # 3
- FF6600 (orange)
+ # 1 FF6600 (orange)
+ # 0
+ # 2
+ # 3
*/
-
- float offsetX[] = new float[4];
- float offsetY[] = new float[4];
-
public IPiece(float x, float y) {
super.setX(x);
super.setY(y);
-
- blocks[0] = new Block(x + offsetX[0], y + offsetY[0], blockSize, "#FF6600");
- blocks[1] = new Block(x + offsetX[1], y + offsetY[1], blockSize, "#FF6600");
- blocks[2] = new Block(x + offsetX[2], y + offsetY[2], blockSize, "#FF6600");
- blocks[3] = new Block(x + offsetX[3], y + offsetY[3], blockSize, "#FF6600");
+
+ for (int i=0; i<4; i++) {
+ blocks[i] = new Block(x + offsetX[i], y + offsetY[i], blockSize, "#FF6600");
+ }
update();
@@ -29,279 +22,95 @@ class IPiece extends Piece {
super.round(blocks[2]);
super.round(blocks[3]);
}
-
+
public void setRotation(float angle) {
println(angle);
-
if(angle >= radians(0.0f) && angle <= radians(90.0f)) {
this.rotation = radians(90.0f);
} else if (angle >= radians(90.0f) && angle <= radians(180.0f)) {
this.rotation = radians(0.0f);
}
-
- float ppx = pivotPoint.getX();
- float ppy = pivotPoint.getY();
}
- // TODO: refactor cleaner
- public boolean rotateCollideX(float wallStart, float wallWidth) {
- // are we more right?
- if (super.pivotPoint.getX() > wallWidth / 2) {
- if (super.pivotPoint.getX() + blockSize * 3 < wallWidth) {
- //println("nowhere close near east wall");
- return false;
- } else {
- float tmpRotation = rotation + radians(90.0f);
- float tmpOffsetX[] = new float[4];
- float tmpOffsetY[] = new float[4];
- tmpOffsetX[0] = 0;
- tmpOffsetX[1] = sin(tmpRotation + radians(0)) * blockSize;
- tmpOffsetX[2] = sin(tmpRotation + radians(180)) * blockSize;
- tmpOffsetX[3] = sin(tmpRotation + radians(180)) * blockSize * 2;
-
- if (super.pivotPoint.getX() + tmpOffsetX[0] >= wallWidth) {
- //println("denied 0");
- return true;
- }
- if (super.pivotPoint.getX() + tmpOffsetX[1] >= wallWidth) {
- //println("denied 1");
- return true;
- }
- if (super.pivotPoint.getX() + tmpOffsetX[2] >= wallWidth) {
- //println("denied 2");
- return true;
- }
- if (super.pivotPoint.getX() + tmpOffsetX[3] >= wallWidth) {
- //println("denied 3");
- return true;
- }
- //println("allowed east");
- return false;
- }
-
- // or are we more left?
- } else {
- if (super.pivotPoint.getX() - blockSize * 2 > wallStart) {
- //println("nowhere close near west wall");
- return false;
- } else {
- float tmpRotation = rotation + radians(90.0f);
- float tmpOffsetX[] = new float[4];
- float tmpOffsetY[] = new float[4];
- tmpOffsetX[0] = 0;
- tmpOffsetX[1] = sin(tmpRotation + radians(0)) * blockSize;
- tmpOffsetX[2] = sin(tmpRotation + radians(180)) * blockSize;
- tmpOffsetX[3] = sin(tmpRotation + radians(180)) * blockSize * 2;
- if (super.pivotPoint.getX() + tmpOffsetX[0] < wallStart - blockSize/2) {
- //println("denied 0");
- return true;
- }
- if (super.pivotPoint.getX() + tmpOffsetX[1] < wallStart - blockSize/2) {
- //println("denied 1");
- return true;
- }
- if (super.pivotPoint.getX() + tmpOffsetX[2] < wallStart - blockSize/2) {
- //println("denied 2");
- return true;
- }
- if (super.pivotPoint.getX() + tmpOffsetX[3] < wallStart - blockSize/2) {
- //println("denied 3");
- return true;
- }
- //println("allowed west");
- return false;
- }
- }
+ public boolean rotateCollideX(float wallStart, float wallWidth) {
+ // update test coords for coll test
+ testUpdate();
+ return(super.rotateCollideX(wallStart, wallWidth));
}
- // TODO: refactor cleaner
public boolean rotateCollideY(float roomStart, float roomWidth) {
-
// are we near the bottom?
if (super.pivotPoint.getY() < roomWidth / 2) {
if (super.pivotPoint.getY() + blockSize * 3 < roomWidth) {
// we are nowhere near the bottom
return false;
} else {
- float tmpRotation = rotation + radians(90.0f);
- float tmpOffsetX[] = new float[4];
- float tmpOffsetY[] = new float[4];
- tmpOffsetX[0] = 0;
- tmpOffsetX[1] = sin(tmpRotation + radians(0)) * blockSize;
- tmpOffsetX[2] = sin(tmpRotation + radians(180)) * blockSize;
- tmpOffsetX[3] = sin(tmpRotation + radians(180)) * blockSize * 2;
-
- if (super.pivotPoint.getX() + tmpOffsetX[0] >= roomWidth) {
- //println("denied 0");
- return true;
- }
- if (super.pivotPoint.getX() + tmpOffsetX[1] >= roomWidth) {
- //println("denied 1");
- return true;
- }
- if (super.pivotPoint.getX() + tmpOffsetX[2] >= roomWidth) {
- //println("denied 2");
- return true;
- }
- if (super.pivotPoint.getX() + tmpOffsetX[3] >= roomWidth) {
- //println("denied 3");
- return true;
- }
- println("allowed down");
+ testUpdate();
+ return(super.rotateCollideYHit(roomStart, roomWidth));
}
}
-
// default return
return false;
}
- // TODO: refactor cleaner
- public boolean rotateCollide(Block deadGrid[][], Point2d playField[]) {
-
- // center of piece x
- float cx = super.pivotPoint.getX();
- float cy = super.pivotPoint.getY();
-
- int bx = (int)((cx - playField[0].getX()) / blockSize);
- int by = (int)((cy - playField[0].getY()) / blockSize);
-
- // TODO: pass this in instead
- int gridSizeX = (int)((playField[1].getX() - playField[0].getX() ) / blockSize);
- int gridSizeY = (int)((playField[1].getY() - playField[0].getY() ) / blockSize);
-
- // check for nearby pieces, 2 blocks in each direction, 5x5 total
- boolean nearAnything = false;
- int mx;
- int my;
- for (int ix=-2; ix <= 2; ix++) {
- for (int iy=-2; iy <= 2; iy++) {
- // create temp "marked" blocks for a few tests
- mx = (int)bx+ix;
- my = (int)by+iy;
- // don't test out of bounds
- if ( mx >= 0 && mx < gridSizeX && my >= 0 && my < gridSizeY ) {
- if (deadGrid[mx][my] != null) {
- // we are near something on the deadGrid
- nearAnything = true;
- }
- }
- }
- }
-
-
- //println("NEAR ANYTHING:" + nearAnything);
- if (nearAnything) {
- float tmpRotation = rotation + radians(90.0f);
- float tmpOffsetX[] = new float[4];
- float tmpOffsetY[] = new float[4];
- tmpOffsetX[0] = 0;
- tmpOffsetX[1] = sin(tmpRotation + radians(0)) * blockSize;
- tmpOffsetX[2] = sin(tmpRotation + radians(180)) * blockSize;
- tmpOffsetX[3] = sin(tmpRotation + radians(180)) * blockSize * 2;
-
- tmpOffsetY[0] = 0;
- tmpOffsetY[1] = cos(tmpRotation + radians(180)) * blockSize;
- tmpOffsetY[2] = cos(tmpRotation + radians(0)) * blockSize;
- tmpOffsetY[3] = cos(tmpRotation + radians(0)) * blockSize * 2;
-
-
- int testX;
- int testY;
- // TODO: take out hit?
- boolean hit = false;
- for (int i=0; i<4; i++) {
- testX = (int) ((super.pivotPoint.getX()+tmpOffsetX[i]) / blockSize);
- testY = (int) ((super.pivotPoint.getY()+tmpOffsetY[i]) / blockSize);
-
- // make sure we don't test past our bounds
- if (testX < gridSizeX && testY < gridSizeY) {
- // if deadGrid isn't null then some block is there
- if (deadGrid[testX][testY] != null) {
- // TODO: take out hit?
- hit = true;
- return true;
- }
- } else {
- println("Disaster averted!");
- }
- }
-
+ public boolean rotateCollide(Block deadGrid[][], Point2d playField[], int gridSizeX, int gridSizeY) {
+ //println("rotateCollide in LPiece");
+ // are we near the grid, if not, don't waste cycles
+ if (super.nearGrid(deadGrid, playField, gridSizeX, gridSizeY)) {
+ testUpdate();
+ // call super method to check for hit
+ //println("rotateCollide in LPiece");
+ return(super.rotateCollideHit(deadGrid, gridSizeX, gridSizeY, testOffsetX, testOffsetY, playField));
}
return false;
}
/* Call this whenever moving or rotating */
public void update() {
-
offsetX[0] = 0;
- offsetY[0] = 0;
-
offsetX[1] = sin(rotation + radians(0)) * blockSize;
- offsetY[1] = cos(rotation + radians(180)) * blockSize;
-
offsetX[2] = sin(rotation + radians(180)) * blockSize;
- offsetY[2] = cos(rotation + radians(0)) * blockSize;
-
offsetX[3] = sin(rotation + radians(180)) * (blockSize * 2);
+
+ offsetY[0] = 0;
+ offsetY[1] = cos(rotation + radians(180)) * blockSize;
+ offsetY[2] = cos(rotation + radians(0)) * blockSize;
offsetY[3] = cos(rotation + radians(0)) * (blockSize * 2);
-
- blocks[0].setX(super.pivotPoint.getX() + offsetX[0]);
- blocks[0].setY(super.pivotPoint.getY() + offsetY[0]);
- blocks[1].setX(super.pivotPoint.getX() + offsetX[1]);
- blocks[1].setY(super.pivotPoint.getY() + offsetY[1]);
-
- blocks[2].setX(super.pivotPoint.getX() + offsetX[2]);
- blocks[2].setY(super.pivotPoint.getY() + offsetY[2]);
-
- blocks[3].setX(super.pivotPoint.getX() + offsetX[3]);
- blocks[3].setY(super.pivotPoint.getY() + offsetY[3]);
+ for (int i=0; i<4; i++) {
+ blocks[i].setX(super.pivotPoint.getX() + offsetX[i]);
+ blocks[i].setY(super.pivotPoint.getY() + offsetY[i]);
+ }
// rounding to line up with grid
// TODO: better way?
super.round(blocks[0]);
super.round(blocks[1]);
super.round(blocks[2]);
- super.round(blocks[3]);
-
+ super.round(blocks[3]);
}
- // setx with a wall in mind for collision detect
- public void setX(float x, float wall) {
-
- boolean wallCollide = false;
+ // this method is called to test collision without updating position, it's like look-ahead
+ public void testUpdate() {
+ testRotation = rotation + radians(90.0f);
- float xVector = 0;
- xVector = x-pivotPoint.getX();
-
- for (int i=0; i < 4; i++) {
-
- // we're going right
- if (xVector > 0 && blocks[i].getX() + xVector >= wall) {
- //println("we're going right");
- wallCollide = true;
- }
- // we're going left
- if (xVector < 0 && blocks[i].getX() + xVector < wall) {
- //println("we're going left");
- wallCollide = true;
- }
- }
- // we didn't hit a wall so set x
- if (!wallCollide){
- pivotPoint.setX(x);
+ testOffsetX[0] = 0;
+ testOffsetX[1] = sin(testRotation + radians(0)) * blockSize;
+ testOffsetX[2] = sin(testRotation + radians(180)) * blockSize;
+ testOffsetX[3] = sin(testRotation + radians(180)) * (blockSize * 2);
+
+ testOffsetY[0] = 0;
+ testOffsetY[1] = cos(testRotation + radians(180)) * blockSize;
+ testOffsetY[2] = cos(testRotation + radians(0)) * blockSize;
+ testOffsetY[3] = cos(testRotation + radians(0)) * (blockSize * 2);
+
+ // round our test offsets to blocksize
+ for (int i=0; i<4; i++) {
+ testOffsetX[i] = Math.round(testOffsetX[i] / blockSize) * blockSize;
+ testOffsetY[i] = Math.round(testOffsetY[i] / blockSize) * blockSize;
}
}
- // set x unconditionally
- public void setX(float x) {
- pivotPoint.setX(x);
- }
-
- public void setY(float y) {
- pivotPoint.setY(y);
- }
-
-}
+}
View
291 JPiece.pde
@@ -2,25 +2,18 @@ class JPiece extends Piece {
/*
# 3
- # *2
- ## 10
+ # 0
+ ## 21
663300 (brown)
*/
- // offsets are relative to pivotpoint (center)
- // block numbers are in ASCII art at top
- // * is pivot point
- float offsetX[] = new float[4];
- float offsetY[] = new float[4];
-
public JPiece(float x, float y) {
super.setX(x);
super.setY(y);
- blocks[0] = new Block(x + offsetX[0], y + offsetY[0], blockSize, "#663300");
- blocks[1] = new Block(x + offsetX[1], y + offsetY[1], blockSize, "#663300");
- blocks[2] = new Block(x + offsetX[2], y + offsetY[2], blockSize, "#663300");
- blocks[3] = new Block(x + offsetX[3], y + offsetY[3], blockSize, "#663300");
+ for (int i=0; i<4; i++) {
+ blocks[i] = new Block(x + offsetX[i], y + offsetY[i], blockSize, "#663300");
+ }
update();
@@ -31,230 +24,58 @@ class JPiece extends Piece {
super.round(blocks[2]);
super.round(blocks[3]);
}
-
- public void setRotation(float angle) {
- this.rotation = angle;
-
- float ppx = pivotPoint.getX();
- float ppy = pivotPoint.getY();
- }
-
- // TODO: refactor cleaner
- public boolean rotateCollideX(float wallStart, float wallWidth) {
-
- // are we more right?
- if (super.pivotPoint.getX() > wallWidth / 2) {
- if (super.pivotPoint.getX() + blockSize * 3 < wallWidth) {
- //println("nowhere close near east wall");
- return false;
- } else {
- float tmpRotation = rotation + 90.0f;
- float tmpOffsetX[] = new float[4];
- float tmpOffsetY[] = new float[4];
- tmpOffsetX[0] = sin(tmpRotation + radians(135)) * (blockSize + (blockSize / 2));
- tmpOffsetX[1] = sin(tmpRotation + radians(180)) * blockSize;
- tmpOffsetX[2] = sin(tmpRotation + radians(90)) * blockSize;
- tmpOffsetX[3] = sin(tmpRotation + radians(45)) * (blockSize + (blockSize / 2));
-
- if (super.pivotPoint.getX()+tmpOffsetX[0] >= wallWidth) {
- //println("denied 0");
- return true;
- }
- if (super.pivotPoint.getX()+tmpOffsetX[1] >= wallWidth) {
- //println("denied 1");
- return true;
- }
- if (super.pivotPoint.getX()+tmpOffsetX[2] >= wallWidth) {
- //println("denied 2");
- return true;
- }
- if (super.pivotPoint.getX()+tmpOffsetX[3] >= wallWidth) {
- //println("denied 3");
- return true;
- }
- //println("allowed west");
- return false;
- }
-
- // or are we more left?
- } else {
- if (super.pivotPoint.getX() - blockSize * 2 > wallStart) {
- //println("nowhere close near west wall");
- return false;
- } else {
- float tmpRotation = rotation + 90.0f;
- float tmpOffsetX[] = new float[4];
- float tmpOffsetY[] = new float[4];
- tmpOffsetX[0] = sin(tmpRotation + radians(135)) * (blockSize + (blockSize / 2));
- tmpOffsetX[1] = sin(tmpRotation + radians(180)) * blockSize;
- tmpOffsetX[2] = sin(tmpRotation + radians(90)) * blockSize;
- tmpOffsetX[3] = sin(tmpRotation + radians(45)) * (blockSize + (blockSize / 2));
-
- if (super.pivotPoint.getX()+tmpOffsetX[0] < wallStart - blockSize/2) {
- //println("denied 0");
- return true;
- }
- if (super.pivotPoint.getX()+tmpOffsetX[1] < wallStart - blockSize/2) {
- //println("denied 1");
- return true;
- }
- if (super.pivotPoint.getX()+tmpOffsetX[2] < wallStart - blockSize/2) {
- //println("denied 2");
- return true;
- }
- if (super.pivotPoint.getX()+tmpOffsetX[3] < wallStart - blockSize/2) {
- //println("denied 3");
- return true;
- }
- //println("allowed east");
- return false;
- }
- }
+
+ public boolean rotateCollideX(float wallStart, float wallWidth) {
+ // update test coords for coll test
+ testUpdate();
+ return(super.rotateCollideX(wallStart, wallWidth));
}
- // TODO: refactor cleaner
public boolean rotateCollideY(float roomStart, float roomWidth) {
-
// are we near the bottom?
if (super.pivotPoint.getY() < roomWidth / 2) {
if (super.pivotPoint.getY() + blockSize * 3 < roomWidth) {
// we are nowhere near the bottom
return false;
} else {
- float tmpRotation = rotation + 90.0f;
- float tmpOffsetX[] = new float[4];
- float tmpOffsetY[] = new float[4];
- tmpOffsetX[0] = sin(tmpRotation + radians(135)) * (blockSize + (blockSize / 2));
- tmpOffsetX[1] = sin(tmpRotation + radians(180)) * blockSize;
- tmpOffsetX[2] = sin(tmpRotation + radians(90)) * blockSize;
- tmpOffsetX[3] = sin(tmpRotation + radians(45)) * (blockSize + (blockSize / 2));
-
- if (super.pivotPoint.getX()+tmpOffsetX[0] >= roomWidth) {
- //println("denied 0");
- return true;
- }
- if (super.pivotPoint.getX()+tmpOffsetX[1] >= roomWidth) {
- //println("denied 1");
- return true;
- }
- if (super.pivotPoint.getX()+tmpOffsetX[2] >= roomWidth) {
- //println("denied 2");
- return true;
- }
- if (super.pivotPoint.getX()+tmpOffsetX[3] >= roomWidth) {
- //println("denied 3");
- return true;
- }
- println("allowed down");
+ testUpdate();
+ return(super.rotateCollideYHit(roomStart, roomWidth));
}
}
-
// default return
- return false;
+ return false;
}
- // TODO: refactor cleaner
- public boolean rotateCollide(Block deadGrid[][], Point2d playField[]) {
-
- // center of piece x
- float cx = super.pivotPoint.getX();
- float cy = super.pivotPoint.getY();
-
- int bx = (int)((cx - playField[0].getX()) / blockSize);
- int by = (int)((cy - playField[0].getY()) / blockSize);
-
- // TODO: pass this in instead
- int gridSizeX = (int)((playField[1].getX() - playField[0].getX() ) / blockSize);
- int gridSizeY = (int)((playField[1].getY() - playField[0].getY() ) / blockSize);
-
- // check for nearby pieces, 2 blocks in each direction, 5x5 total
- boolean nearAnything = false;
- int mx;
- int my;
- for (int ix=-2; ix <= 2; ix++) {
- for (int iy=-2; iy <= 2; iy++) {
- // create temp "marked" blocks for a few tests
- mx = (int)bx+ix;
- my = (int)by+iy;
- // don't test out of bounds
- if ( mx >= 0 && mx < gridSizeX && my >= 0 && my < gridSizeY ) {
- if (deadGrid[mx][my] != null) {
- // we are near something on the deadGrid
- nearAnything = true;
- }
- }
- }
- }
-
-
- //println("NEAR ANYTHING:" + nearAnything);
- if (nearAnything) {
- float tmpRotation = rotation + 90.0f;
- float tmpOffsetX[] = new float[4];
- float tmpOffsetY[] = new float[4];
- tmpOffsetX[0] = sin(tmpRotation + radians(135)) * (blockSize + (blockSize / 2));
- tmpOffsetX[1] = sin(tmpRotation + radians(180)) * blockSize;
- tmpOffsetX[2] = sin(tmpRotation + radians(90)) * blockSize;
- tmpOffsetX[3] = sin(tmpRotation + radians(45)) * (blockSize + (blockSize / 2));
-
- tmpOffsetY[0] = cos(tmpRotation + radians(315)) * (blockSize + (blockSize / 2));
- tmpOffsetY[1] = cos(tmpRotation + radians(0)) * blockSize;
- tmpOffsetY[2] = cos(tmpRotation + radians(270)) * blockSize;
- tmpOffsetY[3] = cos(tmpRotation + radians(225)) * (blockSize + (blockSize / 2));
-
-
- int testX;
- int testY;
- // TODO: take out hit?
- boolean hit = false;
- for (int i=0; i<4; i++) {
- testX = (int) ((super.pivotPoint.getX()+tmpOffsetX[i]) / blockSize);
- testY = (int) ((super.pivotPoint.getY()+tmpOffsetY[i]) / blockSize);
-
- // make sure we don't test past our bounds
- if (testX < gridSizeX && testY < gridSizeY) {
- // if deadGrid isn't null then some block is there
- if (deadGrid[testX][testY] != null) {
- // TODO: take out hit?
- hit = true;
- return true;
- }
- } else {
- println("Disaster averted!");
- }
- }
-
+ public boolean rotateCollide(Block deadGrid[][], Point2d playField[], int gridSizeX, int gridSizeY) {
+ //println("rotateCollide in LPiece");
+ // are we near the grid, if not, don't waste cycles
+ if (super.nearGrid(deadGrid, playField, gridSizeX, gridSizeY)) {
+ testUpdate();
+ // call super method to check for hit
+ //println("rotateCollide in LPiece");
+ return(super.rotateCollideHit(deadGrid, gridSizeX, gridSizeY, testOffsetX, testOffsetY, playField));
}
return false;
}
/* Call this whenever moving or rotating */
public void update() {
- offsetX[0] = sin(rotation + radians(135)) * (blockSize + (blockSize / 2));
- offsetY[0] = cos(rotation + radians(315)) * (blockSize + (blockSize / 2));
-
+ offsetX[0] = 0;
offsetX[1] = sin(rotation + radians(180)) * blockSize;
+ offsetX[2] = sin(rotation + radians(225)) * (blockSize + (blockSize / 2));
+ offsetX[3] = sin(rotation + radians(0)) * blockSize;
+
+ offsetY[0] = 0;
offsetY[1] = cos(rotation + radians(0)) * blockSize;
-
- offsetX[2] = sin(rotation + radians(90)) * blockSize;
- offsetY[2] = cos(rotation + radians(270)) * blockSize;
-
- offsetX[3] = sin(rotation + radians(45)) * (blockSize + (blockSize / 2));
- offsetY[3] = cos(rotation + radians(225)) * (blockSize + (blockSize / 2));
+ offsetY[2] = cos(rotation + radians(45)) * (blockSize + (blockSize / 2));
+ offsetY[3] = cos(rotation + radians(180)) * blockSize;
- blocks[0].setX(super.pivotPoint.getX() + offsetX[0]);
- blocks[0].setY(super.pivotPoint.getY() + offsetY[0]);
+ for (int i=0; i<4; i++) {
+ blocks[i].setX(super.pivotPoint.getX() + offsetX[i]);
+ blocks[i].setY(super.pivotPoint.getY() + offsetY[i]);
+ }
- blocks[1].setX(super.pivotPoint.getX() + offsetX[1]);
- blocks[1].setY(super.pivotPoint.getY() + offsetY[1]);
-
- blocks[2].setX(super.pivotPoint.getX() + offsetX[2]);
- blocks[2].setY(super.pivotPoint.getY() + offsetY[2]);
-
- blocks[3].setX(super.pivotPoint.getX() + offsetX[3]);
- blocks[3].setY(super.pivotPoint.getY() + offsetY[3]);
-
// rounding to line up with grid
// TODO: better way?
super.round(blocks[0]);
@@ -264,40 +85,26 @@ class JPiece extends Piece {
}
- // setx with a wall in mind for collision detect
- public void setX(float x, float wall) {
-
- boolean wallCollide = false;
+ // this method is called to test collision without updating position, it's like look-ahead
+ public void testUpdate() {
+ testRotation = rotation + radians(90.0f);
- float xVector = 0;
- xVector = x-pivotPoint.getX();
-
- for (int i=0; i < 4; i++) {
-
- // we're going right
- if (xVector > 0 && blocks[i].getX() + xVector >= wall) {
- //println("we're going right");
- wallCollide = true;
- }
- // we're going left
- if (xVector < 0 && blocks[i].getX() + xVector < wall) {
- //println("we're going left");
- wallCollide = true;
- }
- }
- // we didn't hit a wall so set x
- if (!wallCollide){
- pivotPoint.setX(x);
+ testOffsetX[0] = 0;
+ testOffsetX[1] = sin(testRotation + radians(180)) * blockSize;
+ testOffsetX[2] = sin(testRotation + radians(225)) * (blockSize + (blockSize / 2));
+ testOffsetX[3] = sin(testRotation + radians(0)) * blockSize;
+
+ testOffsetY[0] = 0;
+ testOffsetY[1] = cos(testRotation + radians(0)) * blockSize;
+ testOffsetY[2] = cos(testRotation + radians(45)) * (blockSize + (blockSize / 2));
+ testOffsetY[3] = cos(testRotation + radians(180)) * blockSize;
+
+ // round our test offsets to blocksize
+ for (int i=0; i<4; i++) {
+ testOffsetX[i] = Math.round(testOffsetX[i] / blockSize) * blockSize;
+ testOffsetY[i] = Math.round(testOffsetY[i] / blockSize) * blockSize;
}
}
- // set x unconditionally
- public void setX(float x) {
- pivotPoint.setX(x);
- }
-
- public void setY(float y) {
- pivotPoint.setY(y);
- }
}
View
220 LPiece.pde
@@ -1,27 +1,16 @@
class LPiece extends Piece {
-
/*
- 3 #
- 0 #
+ 3 # 3366CC (blue)
+ 0 #
12 ##
-
- 3366CC (blue)
*/
-
- // offsets are relative to pivotpoint (center)
- // block numbers are in ASCII art at top
- // * is pivot point
- float offsetX[] = new float[4];
- float offsetY[] = new float[4];
-
public LPiece(float x, float y) {
super.setX(x);
super.setY(y);
-
- blocks[0] = new Block(x + offsetX[0], y + offsetY[0], blockSize, "#3366CC");
- blocks[1] = new Block(x + offsetX[1], y + offsetY[1], blockSize, "#3366CC");
- blocks[2] = new Block(x + offsetX[2], y + offsetY[2], blockSize, "#3366CC");
- blocks[3] = new Block(x + offsetX[3], y + offsetY[3], blockSize, "#3366CC");
+
+ for (int i=0; i<4; i++) {
+ blocks[i] = new Block(x + offsetX[i], y + offsetY[i], blockSize, "#3366CC");
+ }
update();
@@ -33,182 +22,49 @@ class LPiece extends Piece {
super.round(blocks[3]);
}
- // TODO: refactor cleaner
- public boolean rotateCollideX(float wallStart, float wallWidth) {
-
- // are we more right?
- if (super.pivotPoint.getX() > wallWidth / 2) {
- if (super.pivotPoint.getX() + blockSize * 3 < wallWidth) {
- //println("nowhere close near east wall");
- return false;
- } else {
- float tmpRotation = rotation + radians(90.0f);
- float tmpOffsetX[] = new float[4];
- tmpOffsetX[0] = 0;
- tmpOffsetX[1] = sin(tmpRotation + radians(180)) * blockSize;
- tmpOffsetX[2] = sin(tmpRotation + radians(135)) * (blockSize + (blockSize / 2));
- tmpOffsetX[3] = sin(tmpRotation + radians(0)) * blockSize;
-
- for (int i=0; i < 4; i++) {
- float testX = super.pivotPoint.getX()+tmpOffsetX[i];
- testX = Math.round(testX / blockSize) * blockSize;
- if (testX >= wallWidth) {
- println("denied " + i);
- return true;
- }
- }
-
- //println("allowed east");
- return false;
- }
-
- // or are we more left?
- } else {
- if (super.pivotPoint.getX() > wallStart) {
- //println("nowhere close near west wall");
- return false;
- } else {
- float tmpRotation = rotation + radians(90.0f);
- float tmpOffsetX[] = new float[4];
- tmpOffsetX[0] = 0;
- tmpOffsetX[1] = sin(tmpRotation + radians(180)) * blockSize;
- tmpOffsetX[2] = sin(tmpRotation + radians(135)) * (blockSize + (blockSize / 2));
- tmpOffsetX[3] = sin(tmpRotation + radians(0)) * blockSize;
-
-
- for (int i=0; i < 4; i++) {
- float testX = super.pivotPoint.getX() + tmpOffsetX[i];
- testX = Math.round(testX / blockSize) * blockSize;
- if (testX < wallWidth - blockSize / 2) {
- //println("denied " + i);
- return true;
- }
- }
-
- //println("allowed east");
- return false;
- }
- }
+ public boolean rotateCollideX(float wallStart, float wallWidth) {
+ // update test coords for coll test
+ testUpdate();
+ return(super.rotateCollideX(wallStart, wallWidth));
}
- // TODO: refactor cleaner
public boolean rotateCollideY(float roomStart, float roomWidth) {
-
// are we near the bottom?
if (super.pivotPoint.getY() < roomWidth / 2) {
if (super.pivotPoint.getY() + blockSize * 3 < roomWidth) {
// we are nowhere near the bottom
return false;
} else {
- float tmpRotation = rotation + radians(90.0f);
- float tmpOffsetX[] = new float[4];
- tmpOffsetX[0] = 0;
- tmpOffsetX[1] = sin(tmpRotation + radians(180)) * blockSize;
- tmpOffsetX[2] = sin(tmpRotation + radians(135)) * (blockSize + (blockSize / 2));
- tmpOffsetX[3] = sin(tmpRotation + radians(0)) * blockSize;
-
- for (int i=0; i < 4; i++) {
- float testX = super.pivotPoint.getX()+tmpOffsetX[i];
- testX = Math.round(testX / blockSize) * blockSize;
- if (testX >= roomWidth) {
- //println("denied " + i);
- return true;
- }
- }
-
- println("allowed down");
+ testUpdate();
+ return(super.rotateCollideYHit(roomStart, roomWidth));
}
}
-
// default return
- return false;
+ return false;
}
- // TODO: refactor cleaner
- public boolean rotateCollide(Block deadGrid[][], Point2d playField[]) {
-
- // center of piece x
- float cx = super.pivotPoint.getX();
- float cy = super.pivotPoint.getY();
-
- int bx = (int)((cx - playField[0].getX()) / blockSize);
- int by = (int)((cy - playField[0].getY()) / blockSize);
-
- // TODO: pass this in instead
- int gridSizeX = (int)((playField[1].getX() - playField[0].getX() ) / blockSize);
- int gridSizeY = (int)((playField[1].getY() - playField[0].getY() ) / blockSize);
-
- // check for nearby pieces, 2 blocks in each direction, 5x5 total
- boolean nearAnything = false;
- int mx;
- int my;
- for (int ix=-1; ix <= 1; ix++) {
- for (int iy=-1; iy <= 1; iy++) {
- // create temp "marked" blocks for a few tests
- mx = (int)bx+ix;
- my = (int)by+iy;
- // don't test out of bounds
- if ( mx >= 0 && mx < gridSizeX && my >= 0 && my < gridSizeY ) {
- if (deadGrid[mx][my] != null) {
- // we are near something on the deadGrid
- nearAnything = true;
- }
- }
- }
- }
-
-
- //println("NEAR ANYTHING:" + nearAnything);
- if (nearAnything) {
- float tmpRotation = rotation + radians(90.0f);
- float tmpOffsetX[] = new float[4];
- float tmpOffsetY[] = new float[4];
- tmpOffsetX[0] = 0;
- tmpOffsetX[1] = sin(tmpRotation + radians(180)) * blockSize;
- tmpOffsetX[2] = sin(tmpRotation + radians(135)) * (blockSize + (blockSize / 2));
- tmpOffsetX[3] = sin(tmpRotation + radians(0)) * blockSize;
-
- tmpOffsetY[0] = 0;
- tmpOffsetY[1] = cos(tmpRotation + radians(0)) * blockSize;
- tmpOffsetY[2] = cos(tmpRotation + radians(315)) * (blockSize + (blockSize / 2));
- tmpOffsetY[3] = cos(tmpRotation + radians(180)) * blockSize;
-
-
- int testX;
- int testY;
- // TODO: take out hit?
- boolean hit = false;
- for (int i=0; i<4; i++) {
- testX = (int) ((super.pivotPoint.getX()+tmpOffsetX[i]) / blockSize);
- testY = (int) ((super.pivotPoint.getY()+tmpOffsetY[i]) / blockSize);
-
- // make sure we don't test past our bounds
- if (testX < gridSizeX && testY < gridSizeY) {
- // if deadGrid isn't null then some block is there
- if (deadGrid[testX][testY] != null) {
- // TODO: take out hit?
- hit = true;
- return true;
- }
- } else {
- println("Disaster averted!");
- }
- }
-
+ public boolean rotateCollide(Block deadGrid[][], Point2d playField[], int gridSizeX, int gridSizeY) {
+ //println("rotateCollide in LPiece");
+ // are we near the grid, if not, don't waste cycles
+ if (super.nearGrid(deadGrid, playField, gridSizeX, gridSizeY)) {
+ testUpdate();
+ // call super method to check for hit
+ //println("rotateCollide in LPiece");
+ return(super.rotateCollideHit(deadGrid, gridSizeX, gridSizeY, testOffsetX, testOffsetY, playField));
}
return false;
}
/* Call this whenever moving or rotating */
public void update() {
-
offsetX[0] = 0;
- offsetY[0] = 0;
offsetX[1] = sin(rotation + radians(180)) * blockSize;
- offsetY[1] = cos(rotation + radians(0)) * blockSize;
offsetX[2] = sin(rotation + radians(135)) * (blockSize + (blockSize / 2));
- offsetY[2] = cos(rotation + radians(315)) * (blockSize + (blockSize / 2));
offsetX[3] = sin(rotation + radians(0)) * blockSize;
+
+ offsetY[0] = 0;
+ offsetY[1] = cos(rotation + radians(0)) * blockSize;
+ offsetY[2] = cos(rotation + radians(315)) * (blockSize + (blockSize / 2));
offsetY[3] = cos(rotation + radians(180)) * blockSize;
for (int i=0; i<4; i++) {
@@ -218,11 +74,31 @@ class LPiece extends Piece {
// rounding to line up with grid
// TODO: better way?
- //super.round(blocks[0]);
- //super.round(blocks[1]);
+ super.round(blocks[0]);
+ super.round(blocks[1]);
super.round(blocks[2]);
- super.round(blocks[3]);
+ super.round(blocks[3]);
+ }
+
+ // this method is called to test collision without updating position, it's like look-ahead
+ public void testUpdate() {
+ testRotation = rotation + radians(90.0f);
+ testOffsetX[0] = 0;
+ testOffsetX[1] = sin(testRotation + radians(180)) * blockSize;
+ testOffsetX[2] = sin(testRotation + radians(135)) * (blockSize + (blockSize / 2));
+ testOffsetX[3] = sin(testRotation + radians(0)) * blockSize;
+
+ testOffsetY[0] = 0;
+ testOffsetY[1] = cos(testRotation + radians(0)) * blockSize;
+ testOffsetY[2] = cos(testRotation + radians(315)) * (blockSize + (blockSize / 2));
+ testOffsetY[3] = cos(testRotation + radians(180)) * blockSize;
+
+ // round our test offsets to blocksize
+ for (int i=0; i<4; i++) {
+ testOffsetX[i] = Math.round(testOffsetX[i] / blockSize) * blockSize;
+ testOffsetY[i] = Math.round(testOffsetY[i] / blockSize) * blockSize;
+ }
}
}
View
95 OPiece.pde
@@ -5,79 +5,39 @@ class OPiece extends Piece {
## 23
990000 (red)
*/
-
- float offsetX[] = new float[4];
- float offsetY[] = new float[4];
-
public OPiece(float x, float y) {
-
- // offsets are relative to pivotpoint (center)
- // block numbers are in ASCII art at top
- // * is pivot point
-
-
super.setX(x);
super.setY(y);
-
- blocks[0] = new Block(x + offsetX[0], y + offsetY[0], blockSize, "#AA1111");
- blocks[1] = new Block(x + offsetX[1], y + offsetY[1], blockSize, "#AA1111");
- blocks[2] = new Block(x + offsetX[2], y + offsetY[2], blockSize, "#AA1111");
- blocks[3] = new Block(x + offsetX[3], y + offsetY[3], blockSize, "#AA1111");
- // call update because our offsets aren't set yet in constructor above
+ for (int i=0; i<4; i++) {
+ blocks[i] = new Block(x + offsetX[i], y + offsetY[i], blockSize, "#AA1111");
+ }
update();
+
+ super.round(blocks[0]);
+ super.round(blocks[1]);
+ super.round(blocks[2]);
+ super.round(blocks[3]);
}
-
-
- public void setRotation(float angle) {
- // override and do nothing, square doesn't rotate
- }
-
- /*
- // TODO: refactor cleaner
- public boolean rotateCollideX(int wallStart, int wallWidth) {
- // square doesn't rotate
- return false;
- }
-
- public boolean rotateCollideY(int roomStart, int roomWidth) {
- // square doesn't rotate
- return false;
- }
-
- public boolean rotateCollide(Block deadGrid[][], Point2d playField[]) {
- // square doesn't rotate
- return false;
- }*/
-
/* Call this whenever moving or rotating */
public void update() {
offsetX[0] = 0;
- offsetY[0] = 0;
-
offsetX[1] = sin(rotation + radians(90)) * blockSize;
- offsetY[1] = cos(rotation + radians(270)) * blockSize;
-
offsetX[2] = sin(rotation + radians(180)) * blockSize;
- offsetY[2] = cos(rotation + radians(0)) * blockSize;
-
offsetX[3] = sin(rotation + radians(135)) * blockSize;
+
+ offsetY[0] = 0;
+ offsetY[1] = cos(rotation + radians(270)) * blockSize;
+ offsetY[2] = cos(rotation + radians(0)) * blockSize;
offsetY[3] = cos(rotation + radians(315)) * blockSize;
- blocks[0].setX(super.pivotPoint.getX() + offsetX[0]);
- blocks[0].setY(super.pivotPoint.getY() + offsetY[0]);
-
- blocks[1].setX(super.pivotPoint.getX() + offsetX[1]);
- blocks[1].setY(super.pivotPoint.getY() + offsetY[1]);
-
- blocks[2].setX(super.pivotPoint.getX() + offsetX[2]);
- blocks[2].setY(super.pivotPoint.getY() + offsetY[2]);
-
- blocks[3].setX(super.pivotPoint.getX() + offsetX[3]);
- blocks[3].setY(super.pivotPoint.getY() + offsetY[3]);
+ for (int i=0; i<4; i++) {
+ blocks[i].setX(super.pivotPoint.getX() + offsetX[i]);
+ blocks[i].setY(super.pivotPoint.getY() + offsetY[i]);
+ }
// rounding to line up with grid
// TODO: better way?
@@ -88,4 +48,27 @@ class OPiece extends Piece {
}
+ // this method is called to test collision without updating position, it's like look-ahead
+ public void testUpdate() {
+ testRotation = rotation + radians(90.0f);
+
+ testOffsetX[0] = 0;
+ testOffsetX[1] = sin(testRotation + radians(90)) * blockSize;
+ testOffsetX[2] = sin(testRotation + radians(180)) * blockSize;
+ testOffsetX[3] = sin(testRotation + radians(135)) * blockSize;
+
+ testOffsetY[0] = 0;
+ testOffsetY[1] = cos(testRotation + radians(270)) * blockSize;
+ testOffsetY[2] = cos(testRotation + radians(0)) * blockSize;
+ testOffsetY[3] = cos(testRotation + radians(315)) * blockSize;
+
+ // round our test offsets to blocksize
+ for (int i=0; i<4; i++) {
+ testOffsetX[i] = Math.round(testOffsetX[i] / blockSize) * blockSize;
+ testOffsetY[i] = Math.round(testOffsetY[i] / blockSize) * blockSize;
+ }
+ }
+
+
+
}
View
164 Piece.pde
@@ -7,8 +7,21 @@ class Piece {
float y; // center y
float blockSize=16.0f; // used as offset, width and height
float rotation=radians(0.0f);
- Block pivotPoint = new Block(x,y, blockSize, "#AAAAAA");;
+ Block pivotPoint = new Block(x,y, blockSize, "#AAAAAA"); // for debug
Block[] blocks = new Block[4];
+
+ // offsets are relative to pivotpoint (center)
+ // block numbers are in ASCII art at top
+ // * is pivot point
+ float offsetX[] = new float[4];
+ float offsetY[] = new float[4];
+
+
+ float testOffsetX[] = new float[4];
+ float testOffsetY[] = new float[4];
+
+ float testRotation;
+
public void setRotation(float angle) {
this.rotation = angle;
@@ -66,15 +79,160 @@ class Piece {
}
- public boolean rotateCollideX(float wallStart, float wallWidth) {
+ public boolean nearGrid(Block deadGrid[][], Point2d playField[], int gridSizeX, int gridSizeY) {
+ // center of piece x
+ float cx = pivotPoint.getX();
+ float cy = pivotPoint.getY();
+
+ int bx = (int)((cx - playField[0].getX()) / blockSize);
+ int by = (int)((cy - playField[0].getY()) / blockSize);
+
+ // TODO: pass this in instead
+ //int gridSizeX = (int)((playField[1].getX() - playField[0].getX() ) / blockSize);
+ //int gridSizeY = (int)((playField[1].getY() - playField[0].getY() ) / blockSize);
+
+ // check for nearby pieces, 2 blocks in each direction, 5x5 total
+ boolean nearAnything = false;
+ int mx;
+ int my;
+ for (int ix=-3; ix <= 3; ix++) {
+ for (int iy=-3; iy <= 3; iy++) {
+ // create temp "marked" blocks for a few tests
+ mx = (int)bx+ix;
+ my = (int)by+iy;
+ // don't test out of bounds
+ if ( mx >= 0 && mx < gridSizeX && my >= 0 && my < gridSizeY ) {
+ if (deadGrid[mx][my] != null) {
+ // we are near something on the deadGrid
+ return true;
+ }
+ }
+ }
+ }
+ // println("nearGrid thinks false");
return false;
}
+ public boolean rotateCollideX(float wallStart, float wallWidth) {
+ // are we more right?
+ if (pivotPoint.getX() > wallWidth / 2) {
+ if (pivotPoint.getX() + blockSize * 3 < wallWidth) {
+ //println("nowhere close near east wall");
+ return false;
+ } else {
+ /*
+ float tmpRotation = rotation + radians(90.0f);
+ float tmpOffsetX[] = new float[4];
+ tmpOffsetX[0] = 0;
+ tmpOffsetX[1] = sin(tmpRotation + radians(180)) * blockSize;
+ tmpOffsetX[2] = sin(tmpRotation + radians(135)) * (blockSize + (blockSize / 2));
+ tmpOffsetX[3] = sin(tmpRotation + radians(0)) * blockSize;
+ */
+
+ for (int i=0; i < 4; i++) {
+ float testX = pivotPoint.getX()+testOffsetX[i];
+ testX = Math.round(testX / blockSize) * blockSize;
+ if (testX >= wallWidth) {
+ //println("denied " + i);
+ return true;
+ }
+ }
+
+ //println("allowed east");
+ return false;
+ }
+
+ // or are we more left?
+ } else {
+ if (pivotPoint.getX() > wallStart) {
+ //println("nowhere close near west wall");
+ return false;
+ } else {
+ /*
+ float tmpRotation = rotation + radians(90.0f);
+ float tmpOffsetX[] = new float[4];
+ tmpOffsetX[0] = 0;
+ tmpOffsetX[1] = sin(tmpRotation + radians(180)) * blockSize;
+ tmpOffsetX[2] = sin(tmpRotation + radians(135)) * (blockSize + (blockSize / 2));
+ tmpOffsetX[3] = sin(tmpRotation + radians(0)) * blockSize;
+ */
+
+
+ for (int i=0; i < 4; i++) {
+ float testX = pivotPoint.getX() + testOffsetX[i];
+ testX = Math.round(testX / blockSize) * blockSize;
+ if (testX < wallWidth - blockSize / 2) {
+ //println("denied " + i);
+ return true;
+ }
+ }
+
+ //println("allowed east");
+ return false;
+ }
+ }
+ }
+
public boolean rotateCollideY(float roomStart, float roomWidth) {
return false;
}
- public boolean rotateCollide(Block deadGrid[][], Point2d playField[]) {
+ public boolean rotateCollideYHit(float roomStart, float roomWidth) {
+
+ /*
+ float tmpRotation = rotation + radians(90.0f);
+ float tmpOffsetX[] = new float[4];
+ tmpOffsetX[0] = 0;
+ tmpOffsetX[1] = sin(tmpRotation + radians(180)) * blockSize;
+ tmpOffsetX[2] = sin(tmpRotation + radians(135)) * (blockSize + (blockSize / 2));
+ tmpOffsetX[3] = sin(tmpRotation + radians(0)) * blockSize;
+ */
+
+ for (int i=0; i < 4; i++) {
+ float testX = pivotPoint.getX() + testOffsetX[i];
+ testX = Math.round(testX / blockSize) * blockSize;
+ if (testX >= roomWidth) {
+ //println("denied " + i);
+ return true;
+ }
+ }
+ //println("allowed down");
+ return false;
+
+
+ }
+
+ public boolean rotateCollide(Block deadGrid[][], Point2d playField[], int gridSizeX, int gridSizeY) {
+ // this should never be hit
+ return false;
+ }
+
+ public boolean rotateCollideHit(Block deadGrid[][], int gridSizeX, int gridSizeY, float testOffsetX[], float testOffsetY[], Point2d playField[]) {
+ int testX;
+ int testY;
+ // TODO: take out hit?
+ boolean hit = false;
+
+ for (int i=0; i<4; i++) {
+ testX = (int) ((pivotPoint.getX() - playField[0].getX() + testOffsetX[i]) / blockSize);
+ testY = (int) ((pivotPoint.getY() - playField[0].getY() + testOffsetY[i]) / blockSize);
+ //println("PIECE TESTOFFSET:" + testOffsetX[i]);
+
+ // make sure we don't test past our bounds
+ if (testX < gridSizeX && testY < gridSizeY) {
+ // if deadGrid isn't null then some block is there
+ if (deadGrid[testX][testY] != null) {
+ // TODO: take out hit?
+ hit = true;
+ //println("rotateCollideHit is TRUE");
+ return true;
+ }
+ } else {
+ //println("AT BOTTOM AND NEAR GRID OMG DO NOT ROTATE");
+ // we're at the bottom of the screen and we hit grid
+ return true;
+ }
+ }
return false;
}
View
5 PieceBag.pde
@@ -25,6 +25,11 @@ public class PieceBag {
pieceBag[4] = new ZPiece(this.x, this.y);
pieceBag[5] = new IPiece(this.x, this.y);
pieceBag[6] = new JPiece(this.x, this.y);
+
+ /*
+ for (int i=1; i<7; i++) {
+ pieceBag[i] = new LPiece(this.x, this.y);
+ }*/
}
//pieceBag[0] = new LPiece(playField[1].getX()/2, 32.0f);
View
32 PlayState.pde
@@ -34,9 +34,9 @@ public class PlayState implements GameState {
println("GRIDSIZE " + gridSizeX + " " + gridSizeY);
pieceBag = new PieceBag(playField[1].getX()/2, 32.0f);
- currentPiece = new LPiece(playField[1].getX()/2, 32.0f);
+ //currentPiece = new SPiece(playField[1].getX()/2, 32.0f);
//currentPiece = new IPiece(playField[1].getX()/2, 32.0f);
- //currentPiece = pieceBag.getPiece();
+ currentPiece = pieceBag.getPiece();
nextPiece = pieceBag.getPiece();
deadGrid = new Block[gridSizeX][gridSizeY];
@@ -177,8 +177,17 @@ public class PlayState implements GameState {
}
}*/
+ // side colli test
+ int tmp = gridSizeY-1;
+ for (int k=0; k<8; k++){
-
+ for (int i=0; i < gridSizeX; i++) {
+ if (i < 5){
+ deadGrid[i][tmp] = new Block(i*blockSize + playField[0].getX(), tmp*blockSize + playField[0].getY(), blockSize, "#44FF44");
+ }
+ }
+ tmp--;
+ }
}
@@ -284,7 +293,9 @@ public class PlayState implements GameState {
// up arrow key
if (keyCode == UP) {
// checks collision with grid
- if (! currentPiece.rotateCollide(deadGrid, playField)) {
+ //println("PLAYSTATE THINKS:" + currentPiece.rotateCollide(deadGrid, playField, gridSizeX, gridSizeY));
+
+ if (! currentPiece.rotateCollide(deadGrid, playField, gridSizeX, gridSizeY)) {
// checks rotation collision with X boundries
if (! currentPiece.rotateCollideX(playField[0].getX(), playField[1].getX())) {
// checks rotation collision with Y boundries
@@ -316,7 +327,7 @@ public class PlayState implements GameState {
// user pressed down, check for grid collide
if (gridCollideY(currentPiece.getBlocks())) {
- println("collision on grid");
+ //println("collision on grid");
copyToGrid();
}
@@ -387,7 +398,7 @@ public class PlayState implements GameState {
if (done == cols) {
// doneRows contains markers to done rows that need to be deleted
doneRows.add(j);
- println("ROW " + j + " DONE");
+ //println("ROW " + j + " DONE");
}
}
}
@@ -441,7 +452,7 @@ public class PlayState implements GameState {
emptyRows.add(rowsI);
//println("empty index" + (rowsAffected.indexOf(rowsI) > -1));
} else if (empty < cols) {
- println("ROW NONEMPTY" + rowsI);
+ //println("ROW NONEMPTY" + rowsI);
nonEmptyRows.add(rowsI);
}
}
@@ -465,7 +476,7 @@ public class PlayState implements GameState {
for(int j=0; j < emptyRows.size(); j++) {
emptyRowNum = ((Integer)emptyRows.get(j)).intValue();
- println("nonEmpty,empty:" + nonEmptyRowNum + "," + emptyRowNum);
+ //println("nonEmpty,empty:" + nonEmptyRowNum + "," + emptyRowNum);
// there is one row below us, add 1 to "need to fall" for this row
if (nonEmptyRowNum < emptyRowNum) {
@@ -474,7 +485,6 @@ public class PlayState implements GameState {
//println("sure has:" + i);
needToFall.put(nonEmptyRowNum, ++tmp);
} else {
- println("init:" + nonEmptyRowNum);
needToFall.put(nonEmptyRowNum, 1);
}
}
@@ -488,7 +498,7 @@ public class PlayState implements GameState {
while (ntfi.hasNext()) {
Map.Entry row = (Map.Entry) ntfi.next();
int rowNum = (Integer)row.getKey();
- println("moving row:"+rowNum);
+ //println("moving row:"+rowNum);
int rowDistance = (Integer)row.getValue();
// loop through columns
for (int colNum=0; colNum < cols; colNum++) {
@@ -581,7 +591,6 @@ public class PlayState implements GameState {
Float fx = (checkBlocks[i].getX() + playField[0].getX()) / blockSize;
Float fy = (checkBlocks[i].getY() + playField[0].getY()) / blockSize;
-
//println("block" + i + " " + fx * blockSize + " " + fy * blockSize);
if (direction > 0) {
// limit x to edge of playfield
@@ -600,6 +609,7 @@ public class PlayState implements GameState {
int x = fx.intValue() - 2;
if (x > 0) {
int y = fy.intValue() - 1;
+ println("testing:" + x + "," + y);
// we hit something on the grid
if(deadGrid[x-1][y-1] != null) {
println("deny left");
View
280 SPiece.pde
@@ -6,22 +6,15 @@ class SPiece extends Piece {
# 3
FFCC00 (yellow)
*/
-
- // offsets are relative to pivotpoint (center)
- // block numbers are in ASCII art at top
- // * is pivot point
- float offsetX[] = new float[4];
- float offsetY[] = new float[4];
public SPiece(float x, float y) {
super.setX(x);
super.setY(y);
- blocks[0] = new Block(x + offsetX[0], y + offsetY[0], blockSize, "#FFDD11");
- blocks[1] = new Block(x + offsetX[1], y + offsetY[1], blockSize, "#FFDD11");
- blocks[2] = new Block(x + offsetX[2], y + offsetY[2], blockSize, "#FFDD11");
- blocks[3] = new Block(x + offsetX[3], y + offsetY[3], blockSize, "#FFDD11");
-
+ for (int i=0; i<4; i++) {
+ blocks[i] = new Block(x + offsetX[i], y + offsetY[i], blockSize, "#FFDD11");
+ }
+
update();
// 45 deg pieces don't line up right
@@ -33,233 +26,62 @@ class SPiece extends Piece {
}
public void setRotation(float angle) {
- println(angle);
-
-
if(angle >= radians(0.0f) && angle <= radians(90.0f)) {
this.rotation = radians(90.0f);
} else if (angle >= radians(90.0f) && angle <= radians(180.0f)) {
this.rotation = radians(0.0f);
}
-
- float ppx = pivotPoint.getX();
- float ppy = pivotPoint.getY();
}
- // TODO: refactor cleaner
- public boolean rotateCollideX(float wallStart, float wallWidth) {
-
- // are we more right?
- if (super.pivotPoint.getX() > wallWidth / 2) {
- if (super.pivotPoint.getX() + blockSize * 3 < wallWidth) {
- //println("nowhere close near east wall");
- return false;
- } else {
- float tmpRotation = rotation + 90.0f;
- float tmpOffsetX[] = new float[4];
- float tmpOffsetY[] = new float[4];
- tmpOffsetX[0] = 0;
- tmpOffsetX[1] = sin(tmpRotation + radians(0)) * blockSize;
- tmpOffsetX[2] = sin(tmpRotation + radians(90)) * blockSize;
- tmpOffsetX[3] = sin(tmpRotation + radians(135)) * (blockSize + (blockSize / 2));
-
- if (super.pivotPoint.getX()+tmpOffsetX[0] >= wallWidth) {
- //println("denied 0");
- return true;
- }
- if (super.pivotPoint.getX()+tmpOffsetX[1] >= wallWidth) {
- //println("denied 1");
- return true;
- }
- if (super.pivotPoint.getX()+tmpOffsetX[2] >= wallWidth) {
- //println("denied 2");
- return true;
- }
- if (super.pivotPoint.getX()+tmpOffsetX[3] >= wallWidth) {
- //println("denied 3");
- return true;
- }
- //println("allowed west");
- return false;
- }
-
- // or are we more left?
- } else {
- if (super.pivotPoint.getX() - blockSize * 2 > wallStart) {
- //println("nowhere close near west wall");
- return false;
- } else {
- float tmpRotation = rotation + 90.0f;
- float tmpOffsetX[] = new float[4];
- float tmpOffsetY[] = new float[4];
- tmpOffsetX[0] = 0;
- tmpOffsetX[1] = sin(tmpRotation + radians(0)) * blockSize;
- tmpOffsetX[2] = sin(tmpRotation + radians(90)) * blockSize;
- tmpOffsetX[3] = sin(tmpRotation + radians(135)) * (blockSize + (blockSize / 2));
- if (super.pivotPoint.getX()+tmpOffsetX[0] < wallStart - blockSize/2) {
- //println("denied 0");
- return true;
- }
- if (super.pivotPoint.getX()+tmpOffsetX[1] < wallStart - blockSize/2) {
- //println("denied 1");
- return true;
- }
- if (super.pivotPoint.getX()+tmpOffsetX[2] < wallStart - blockSize/2) {
- //println("denied 2");
- return true;
- }
- if (super.pivotPoint.getX()+tmpOffsetX[3] < wallStart - blockSize/2) {
- //println("denied 3");
- return true;
- }
- //println("allowed east");
- return false;
- }
- }
+ public boolean rotateCollideX(float wallStart, float wallWidth) {
+ // update test coords for coll test
+ testUpdate();
+ return(super.rotateCollideX(wallStart, wallWidth));
}
- // TODO: refactor cleaner
public boolean rotateCollideY(float roomStart, float roomWidth) {
-
// are we near the bottom?
if (super.pivotPoint.getY() < roomWidth / 2) {
if (super.pivotPoint.getY() + blockSize * 3 < roomWidth) {
// we are nowhere near the bottom
return false;
} else {
- float tmpRotation = rotation + 90.0f;
- float tmpOffsetX[] = new float[4];
- float tmpOffsetY[] = new float[4];
- tmpOffsetX[0] = 0;
- tmpOffsetX[1] = sin(tmpRotation + radians(0)) * blockSize;
- tmpOffsetX[2] = sin(tmpRotation + radians(90)) * blockSize;
- tmpOffsetX[3] = sin(tmpRotation + radians(135)) * (blockSize + (blockSize / 2));
-
- if (super.pivotPoint.getX()+tmpOffsetX[0] >= roomWidth) {
- //println("denied 0");
- return true;
- }
- if (super.pivotPoint.getX()+tmpOffsetX[1] >= roomWidth) {
- //println("denied 1");
- return true;
- }
- if (super.pivotPoint.getX()+tmpOffsetX[2] >= roomWidth) {
- //println("denied 2");
- return true;
- }
- if (super.pivotPoint.getX()+tmpOffsetX[3] >= roomWidth) {
- //println("denied 3");
- return true;
- }
- println("allowed down");
+ testUpdate();
+ return(super.rotateCollideYHit(roomStart, roomWidth));
}
}
-
// default return
- return false;
+ return false;
}
- // TODO: refactor cleaner
- public boolean rotateCollide(Block deadGrid[][], Point2d playField[]) {
-
- // center of piece x
- float cx = super.pivotPoint.getX();
- float cy = super.pivotPoint.getY();
-
- int bx = (int)((cx - playField[0].getX()) / blockSize);
- int by = (int)((cy - playField[0].getY()) / blockSize);
-
- // TODO: pass this in instead
- int gridSizeX = (int)((playField[1].getX() - playField[0].getX() ) / blockSize);
- int gridSizeY = (int)((playField[1].getY() - playField[0].getY() ) / blockSize);
-
- // check for nearby pieces, 2 blocks in each direction, 5x5 total
- boolean nearAnything = false;
- int mx;
- int my;
- for (int ix=-2; ix <= 2; ix++) {
- for (int iy=-2; iy <= 2; iy++) {
- // create temp "marked" blocks for a few tests
- mx = (int)bx+ix;
- my = (int)by+iy;
- // don't test out of bounds
- if ( mx >= 0 && mx < gridSizeX && my >= 0 && my < gridSizeY ) {
- if (deadGrid[mx][my] != null) {
- // we are near something on the deadGrid
- nearAnything = true;
- }
- }
- }
- }
-
-
- //println("NEAR ANYTHING:" + nearAnything);
- if (nearAnything) {
- float tmpRotation = rotation + 90.0f;
- float tmpOffsetX[] = new float[4];
- float tmpOffsetY[] = new float[4];
- tmpOffsetX[0] = 0;
- tmpOffsetX[1] = sin(tmpRotation + radians(0)) * blockSize;
- tmpOffsetX[2] = sin(tmpRotation + radians(90)) * blockSize;
- tmpOffsetX[3] = sin(tmpRotation + radians(135)) * (blockSize + (blockSize / 2));
-
- tmpOffsetY[0] = 0;
- tmpOffsetY[1] = cos(tmpRotation + radians(180)) * blockSize;
- tmpOffsetY[2] = cos(tmpRotation + radians(270)) * blockSize;
- tmpOffsetY[3] = cos(tmpRotation + radians(315)) * (blockSize + (blockSize / 2));
-
-
- int testX;
- int testY;
- // TODO: take out hit?
- boolean hit = false;
- for (int i=0; i<4; i++) {
- testX = (int) ((super.pivotPoint.getX()+tmpOffsetX[i]) / blockSize);
- testY = (int) ((super.pivotPoint.getY()+tmpOffsetY[i]) / blockSize);
-
- // make sure we don't test past our bounds
- if (testX < gridSizeX && testY < gridSizeY) {
- // if deadGrid isn't null then some block is there
- if (deadGrid[testX][testY] != null) {
- // TODO: take out hit?
- hit = true;
- return true;
- }
- } else {
- println("Disaster averted!");
- }
- }
-
+ public boolean rotateCollide(Block deadGrid[][], Point2d playField[], int gridSizeX, int gridSizeY) {
+ //println("rotateCollide in LPiece");
+ // are we near the grid, if not, don't waste cycles
+ if (super.nearGrid(deadGrid, playField, gridSizeX, gridSizeY)) {
+ testUpdate();
+ // call super method to check for hit
+ //println("rotateCollide in LPiece");
+ return(super.rotateCollideHit(deadGrid, gridSizeX, gridSizeY, testOffsetX, testOffsetY, playField));
}
return false;
}
/* Call this whenever moving or rotating */
public void update() {
-
offsetX[0] = 0;
- offsetY[0] = 0;
-
offsetX[1] = sin(rotation + radians(0)) * blockSize;
- offsetY[1] = cos(rotation + radians(180)) * blockSize;
-
offsetX[2] = sin(rotation + radians(90)) * blockSize;
- offsetY[2] = cos(rotation + radians(270)) * blockSize;
-
offsetX[3] = sin(rotation + radians(135)) * (blockSize + (blockSize / 2));
+
+ offsetY[0] = 0;
+ offsetY[1] = cos(rotation + radians(180)) * blockSize;
+ offsetY[2] = cos(rotation + radians(270)) * blockSize;
offsetY[3] = cos(rotation + radians(315)) * (blockSize + (blockSize / 2));
- blocks[0].setX(super.pivotPoint.getX() + offsetX[0]);
- blocks[0].setY(super.pivotPoint.getY() + offsetY[0]);
-
- blocks[1].setX(super.pivotPoint.getX() + offsetX[1]);
- blocks[1].setY(super.pivotPoint.getY() + offsetY[1]);
-
- blocks[2].setX(super.pivotPoint.getX() + offsetX[2]);
- blocks[2].setY(super.pivotPoint.getY() + offsetY[2]);
-
- blocks[3].setX(super.pivotPoint.getX() + offsetX[3]);
- blocks[3].setY(super.pivotPoint.getY() + offsetY[3]);
+ for (int i=0; i<4; i++) {
+ blocks[i].setX(super.pivotPoint.getX() + offsetX[i]);
+ blocks[i].setY(super.pivotPoint.getY() + offsetY[i]);
+ }
// rounding to line up with grid
// TODO: better way?
@@ -270,40 +92,26 @@ class SPiece extends Piece {
}
- // setx with a wall in mind for collision detect
- public void setX(float x, float wall) {
-
- boolean wallCollide = false;
+ // this method is called to test collision without updating position, it's like look-ahead
+ public void testUpdate() {
+ testRotation = rotation + radians(90.0f);
- float xVector = 0;
- xVector = x-pivotPoint.getX();
-
- for (int i=0; i < 4; i++) {
-
- // we're going right
- if (xVector > 0 && blocks[i].getX() + xVector >= wall) {
- //println("we're going right");
- wallCollide = true;
- }
- // we're going left
- if (xVector < 0 && blocks[i].getX() + xVector < wall) {
- //println("we're going left");
- wallCollide = true;
- }
- }
- // we didn't hit a wall so set x
- if (!wallCollide){
- pivotPoint.setX(x);
+ testOffsetX[0] = 0;
+ testOffsetX[1] = sin(testRotation + radians(0)) * blockSize;
+ testOffsetX[2] = sin(testRotation + radians(90)) * blockSize;
+ testOffsetX[3] = sin(testRotation + radians(135)) * (blockSize + (blockSize / 2));
+
+ testOffsetY[0] = 0;
+ testOffsetY[1] = cos(testRotation + radians(180)) * blockSize;
+ testOffsetY[2] = cos(testRotation + radians(270)) * blockSize;
+ testOffsetY[3] = cos(testRotation + radians(315)) * (blockSize + (blockSize / 2));
+
+ // round our test offsets to blocksize
+ for (int i=0; i<4; i++) {
+ testOffsetX[i] = Math.round(testOffsetX[i] / blockSize) * blockSize;
+ testOffsetY[i] = Math.round(testOffsetY[i] / blockSize) * blockSize;
}
}
- // set x unconditionally
- public void setX(float x) {
- pivotPoint.setX(x);
- }
-
- public void setY(float y) {
- pivotPoint.setY(y);
- }
}
View
303 TPiece.pde
@@ -1,28 +1,18 @@
class TPiece extends Piece {
/*
- # 0
+ # 0 441144 (purple)
### 123
- 441144 (purple)
*/
-
- // offsets are relative to pivotpoint (center)
- // block numbers are in ASCII art at top
- // * is pivot point
- float offsetX[] = new float[4];
- float offsetY[] = new float[4];
-
public TPiece(float x, float y) {
super.setX(x);
super.setY(y);
-
- blocks[0] = new Block(x + offsetX[0], y + offsetY[0], blockSize, "#441144");
- blocks[1] = new Block(x + offsetX[1], y + offsetY[1], blockSize, "#441144");
- blocks[2] = new Block(x + offsetX[2], y + offsetY[2], blockSize, "#441144");
- blocks[3] = new Block(x + offsetX[3], y + offsetY[3], blockSize, "#441144");
- // call update because our offsets aren't set yet in constructor above
+ for (int i=0; i<4; i++) {
+ blocks[i] = new Block(x + offsetX[i], y + offsetY[i], blockSize, "#441144");
+ }
+
update();
// 45 deg pieces don't line up right
@@ -33,204 +23,35 @@ class TPiece extends Piece {
super.round(blocks[3]);
}
- public void setRotation(float angle) {
- this.rotation = angle;
-
- float ppx = pivotPoint.getX();
- float ppy = pivotPoint.getY();
- }
-
- // TODO: refactor cleaner
- public boolean rotateCollideX(float wallStart, float wallWidth) {
-
- // are we more right?
- if (super.pivotPoint.getX() > wallWidth / 2) {
- if (super.pivotPoint.getX() + blockSize * 3 < wallWidth) {
- //nowhere close near east wall so return
- return false;
- } else {
- float tmpRotation = rotation + 90.0f;
- float tmpOffsetX[] = new float[4];
- float tmpOffsetY[] = new float[4];
-
- tmpOffsetX[0] = 0;
- tmpOffsetX[1] = sin(tmpRotation + radians(225)) * (blockSize + (blockSize / 2));
- tmpOffsetX[2] = sin(tmpRotation + radians(180)) * blockSize;
- tmpOffsetX[3] = sin(tmpRotation + radians(135)) * (blockSize + (blockSize / 2));
-
- if (super.pivotPoint.getX() + tmpOffsetX[0] >= wallWidth) {
- //println("denied 0");
- return true;
- }
- if (super.pivotPoint.getX() + tmpOffsetX[1] >= wallWidth) {
- //println("denied 1");
- return true;
- }
- if (super.pivotPoint.getX() + tmpOffsetX[2] >= wallWidth) {
- //println("denied 2");
- return true;
- }
- if (super.pivotPoint.getX() + tmpOffsetX[3] >= wallWidth) {
- //println("denied 3");
- return true;
- }
- //println("allowed west");
- return false;
- }
-
- // or are we more left?
- } else {
- if (super.pivotPoint.getX() - blockSize * 2 > wallStart) {
- // nowhere close near west wall so return
- return false;
- } else {
- float tmpRotation = rotation + 90.0f;
- float tmpOffsetX[] = new float[4];
- float tmpOffsetY[] = new float[4];
-
- tmpOffsetX[0] = 0;
- tmpOffsetX[1] = sin(tmpRotation + radians(225)) * (blockSize + (blockSize / 2));
- tmpOffsetX[2] = sin(tmpRotation + radians(180)) * blockSize;
- tmpOffsetX[3] = sin(tmpRotation + radians(135)) * (blockSize + (blockSize / 2));
-
- if (super.pivotPoint.getX() + offsetX[0] < wallStart - blockSize/2) {
- //println("denied 0");
- return true;
- }
- if (super.pivotPoint.getX() + offsetX[1] < wallStart - blockSize/2) {
- //println("denied 1");
- return true;
- }
- if (super.pivotPoint.getX() + offsetX[2] < wallStart - blockSize/2) {
- //println("denied 2");
- return true;
- }
- if (super.pivotPoint.getX() + offsetX[3] < wallStart - blockSize/2) {
- //println("denied 3");
- return true;
- }
- //println("allowed east");
- return false;
- }
- }
+ public boolean rotateCollideX(float wallStart, float wallWidth) {
+ // update test coords for coll test
+ testUpdate();
+ return(super.rotateCollideX(wallStart, wallWidth));
}
- // TODO: refactor cleaner
public boolean rotateCollideY(float roomStart, float roomWidth) {
-
// are we near the bottom?
if (super.pivotPoint.getY() < roomWidth / 2) {
if (super.pivotPoint.getY() + blockSize * 3 < roomWidth) {
- println("nowhere close near bottom");
+ // we are nowhere near the bottom
return false;
} else {
- float tmpRotation = rotation + 90.0f;
- float tmpOffsetX[] = new float[4];
- float tmpOffsetY[] = new float[4];
-
- tmpOffsetX[0] = 0;
- tmpOffsetX[1] = sin(tmpRotation + radians(225)) * (blockSize + (blockSize / 2));
- tmpOffsetX[2] = sin(tmpRotation + radians(180)) * blockSize;
- tmpOffsetX[3] = sin(tmpRotation + radians(135)) * (blockSize + (blockSize / 2));
-
-
- if (super.pivotPoint.getX() + tmpOffsetX[0] >= roomWidth) {
- //println("denied 0");
- return true;
- }
- if (super.pivotPoint.getX() + tmpOffsetX[1] >= roomWidth) {
- //println("denied 1");
- return true;
- }
- if (super.pivotPoint.getX() + tmpOffsetX[2] >= roomWidth) {
- //println("denied 2");
- return true;
- }
- if (super.pivotPoint.getX() + tmpOffsetX[3] >= roomWidth) {
- //println("denied 3");
- return true;
- }
- println("allowed down");
+ testUpdate();
+ return(super.rotateCollideYHit(roomStart, roomWidth));
}
}
-
// default return
- return false;
+ return false;
}
- // TODO: refactor cleaner
- public boolean rotateCollide(Block deadGrid[][], Point2d playField[]) {
-
- // center of piece x
- float cx = super.pivotPoint.getX();
- float cy = super.pivotPoint.getY();
-
- int bx = (int)((cx - playField[0].getX()) / blockSize);
- int by = (int)((cy - playField[0].getY()) / blockSize);
-
- // TODO: pass this in instead
- int gridSizeX = (int)((playField[1].getX() - playField[0].getX() ) / blockSize);
- int gridSizeY = (int)((playField[1].getY() - playField[0].getY() ) / blockSize);
-
- // check for nearby pieces, 2 blocks in each direction, 5x5 total
- boolean nearAnything = false;
- int mx;
- int my;
- for (int ix=-2; ix <= 2; ix++) {
- for (int iy=-2; iy <= 2; iy++) {
- // create temp "marked" blocks for a few tests
- mx = (int)bx+ix;
- my = (int)by+iy;
- // don't test out of bounds
- if ( mx >= 0 && mx < gridSizeX && my >= 0 && my < gridSizeY ) {
- if (deadGrid[mx][my] != null) {
- // we are near something on the deadGrid
- nearAnything = true;
- }
- }
- }
- }
-
-
- //println("NEAR ANYTHING:" + nearAnything);
- if (nearAnything) {
- float tmpRotation = rotation + 90.0f;
- float tmpOffsetX[] = new float[4];
- float tmpOffsetY[] = new float[4];
-
-
- tmpOffsetX[0] = 0;
- tmpOffsetX[1] = sin(tmpRotation + radians(225)) * (blockSize + (blockSize / 2));
- tmpOffsetX[2] = sin(tmpRotation + radians(180)) * blockSize;
- tmpOffsetX[3] = sin(tmpRotation + radians(135)) * (blockSize + (blockSize / 2));
-
- tmpOffsetY[0] = 0;
- tmpOffsetY[1] = cos(tmpRotation + radians(45)) * (blockSize + (blockSize / 2));
- tmpOffsetY[2] = cos(tmpRotation + radians(0)) * blockSize;
- tmpOffsetY[3] = cos(tmpRotation + radians(315)) * (blockSize + (blockSize / 2));
-
-
- int testX;
- int testY;
- // TODO: take out hit?
- boolean hit = false;
- for (int i=0; i<4; i++) {
- testX = (int) ((super.pivotPoint.getX() + tmpOffsetX[i]) / blockSize);
- testY = (int) ((super.pivotPoint.getY() + tmpOffsetY[i]) / blockSize);
-
- // make sure we don't test past our bounds
- if (testX < gridSizeX && testY < gridSizeY) {
- // if deadGrid isn't null then some block is there
- if (deadGrid[testX][testY] != null) {
- // TODO: take out hit?
- hit = true;
- return true;
- }
- } else {
- println("Disaster averted!");
- }
- }
-
+ public boolean rotateCollide(Block deadGrid[][], Point2d playField[], int gridSizeX, int gridSizeY) {
+ //println("rotateCollide in LPiece");
+ // are we near the grid, if not, don't waste cycles
+ if (super.nearGrid(deadGrid, playField, gridSizeX, gridSizeY)) {
+ testUpdate();
+ // call super method to check for hit
+ //println("rotateCollide in LPiece");
+ return(super.rotateCollideHit(deadGrid, gridSizeX, gridSizeY, testOffsetX, testOffsetY, playField));
}
return false;
}
@@ -239,77 +60,47 @@ class TPiece extends Piece {
public void update() {
offsetX[0] = 0;
- offsetY[0] = 0;
-
- // corner pieces (45deg) are tricky, have to be rounded
- // TODO: figure out math to place them just right
offsetX[1] = sin(rotation + radians(225)) * (blockSize + (blockSize / 2));
- offsetY[1] = cos(rotation + radians(45)) * (blockSize + (blockSize / 2));
-
offsetX[2] = sin(rotation + radians(180)) * blockSize;
- offsetY[2] = cos(rotation + radians(0)) * blockSize;
-
- // corner pieces (45deg) are tricky, have to be rounded
- // TODO: figure out math to place them just right
offsetX[3] = sin(rotation + radians(135)) * (blockSize + (blockSize / 2));
- offsetY[3] = cos(rotation + radians(315)) * (blockSize + (blockSize / 2));
- blocks[0].setX(super.pivotPoint.getX() + offsetX[0]);
- blocks[0].setY(super.pivotPoint.getY() + offsetY[0]);
+ offsetY[0] = 0;
+ offsetY[1] = cos(rotation + radians(45)) * (blockSize + (blockSize / 2));
+ offsetY[2] = cos(rotation + radians(0)) * blockSize;
+ offsetY[3] = cos(rotation + radians(315)) * (blockSize + (blockSize / 2));
- blocks[1].setX(super.pivotPoint.getX() + offsetX[1]);
- blocks[1].setY(super.pivotPoint.getY() + offsetY[1]);
-
- blocks[2].setX(super.pivotPoint.getX() + offsetX[2]);
- blocks[2].setY(super.pivotPoint.getY() + offsetY[2]);
-
- blocks[3].setX(super.pivotPoint.getX() + offsetX[3]);
- blocks[3].setY(super.pivotPoint.getY() + offsetY[3]);
+ for (int i=0; i<4; i++) {
+ blocks[i].setX(super.pivotPoint.getX() + offsetX[i]);
+ blocks[i].setY(super.pivotPoint.getY() + offsetY[i]);
+ }
// rounding to line up with grid
// TODO: better way?
super.round(blocks[0]);
super.round(blocks[1]);
super.round(blocks[2]);
- super.round(blocks[3]);
-
+ super.round(blocks[3]);
}
- // setx with a wall in mind for collision detect
- public void setX(float x, float wall) {
-
- boolean wallCollide = false;
+ // this method is called to test collision without updating position, it's like look-ahead
+ public void testUpdate() {
+ testRotation = rotation + radians(90.0f);
- float xVector = 0;
- xVector = x-pivotPoint.getX();
-
- for (int i=0; i < 4; i++) {
-
- // we're going right
- if (xVector > 0 && blocks[i].getX() + xVector >= wall) {
- //println("we're going right");
- wallCollide = true;
- }
- // we're going left
- if (xVector < 0 && blocks[i].getX() + xVector < wall) {
- //println("we're going left");
- wallCollide = true;
- }
- }
- // we didn't hit a wall so set x
- if (!wallCollide){
- pivotPoint.setX(x);
+ testOffsetX[0] = 0;
+ testOffsetX[1] = sin(testRotation + radians(225)) * (blockSize + (blockSize / 2));
+ testOffsetX[2] = sin(testRotation + radians(180)) * blockSize;
+ testOffsetX[3] = sin(testRotation + radians(135)) * (blockSize + (blockSize / 2));
+
+ testOffsetY[0] = 0;
+ testOffsetY[1] = cos(testRotation + radians(45)) * (blockSize + (blockSize / 2));
+ testOffsetY[2] = cos(testRotation + radians(0)) * blockSize;
+ testOffsetY[3] = cos(testRotation + radians(315)) * (blockSize + (blockSize / 2));
+
+ // round our test offsets to blocksize
+ for (int i=0; i<4; i++) {
+ testOffsetX[i] = Math.round(testOffsetX[i] / blockSize) * blockSize;
+ testOffsetY[i] = Math.round(testOffsetY[i] / blockSize) * blockSize;
}
}
-
- // set x unconditionally
- public void setX(float x) {
- pivotPoint.setX(x);
- }
-
- public void setY(float y) {
- pivotPoint.setY(y);
- }
-
}
View
272 ZPiece.pde
@@ -1,15 +1,12 @@
class ZPiece extends Piece {
/*
- # 1
+ # 1 336633 (green)
## 02
# 3
- 336633 (green)
+
*/
- float offsetX[] = new float[4];
- float offsetY[] = new float[4];
-
public ZPiece(float x, float y) {
super.setX(x);
super.setY(y);
@@ -30,203 +27,42 @@ class ZPiece extends Piece {
}
public void setRotation(float angle) {
- println(angle);
-
-
if(angle >= radians(0.0f) && angle <= radians(90.0f)) {
this.rotation = radians(90.0f);
} else if (angle >= radians(90.0f) && angle <= radians(180.0f)) {
this.rotation = radians(0.0f);
}
-
- float ppx = pivotPoint.getX();
- float ppy = pivotPoint.getY();
}
- // TODO: refactor cleaner
- public boolean rotateCollideX(float wallStart, float wallWidth) {
-
- // are we more right?
- if (super.pivotPoint.getX() > wallWidth / 2) {
- if (super.pivotPoint.getX() + blockSize * 3 < wallWidth) {
- //println("nowhere close near east wall");
- return false;
- } else {
- float tmpRotation = rotation + 90.0f;
- float tmpOffsetX[] = new float[4];
- float tmpOffsetY[] = new float[4];
- tmpOffsetX[0] = 0;
- tmpOffsetX[1] = sin(tmpRotation + radians(45)) * (blockSize + (blockSize / 2));
- tmpOffsetX[2] = sin(tmpRotation + radians(90)) * blockSize;
- tmpOffsetX[3] = sin(tmpRotation + radians(180)) * blockSize;
-
- if (super.pivotPoint.getX()+tmpOffsetX[0] >= wallWidth) {
- //println("denied 0");
- return true;
- }
- if (super.pivotPoint.getX()+tmpOffsetX[1] >= wallWidth) {
- //println("denied 1");
- return true;
- }
- if (super.pivotPoint.getX()+tmpOffsetX[2] >= wallWidth) {
- //println("denied 2");
- return true;
- }
- if (super.pivotPoint.getX()+tmpOffsetX[3] >= wallWidth) {
- //println("denied 3");
- return true;