Skip to content
Browse files

Collision detection!!

Changed rendering methods
  • Loading branch information...
1 parent 275bf9e commit 11dd1586c8c05336ca88098ec0a3d4e984dba2f5 @a727891 committed May 9, 2013
Showing with 174 additions and 64 deletions.
  1. +122 −30 js/game.js
  2. +52 −34 js/render.js
View
152 js/game.js
@@ -6,19 +6,12 @@ define([], function () {
this.hasNeverStarted = true;
this.currentTime = null;
- this.WellWidth = 11;
- this.WellHeight = 20;
-
-
- this.ActiveBlock = {
- x: 6,
- y: 1,
- TypeIndex: 1,
- orientation: 0,
-
- };
+ this.WellWidth = 7;
+ this.WellHeight = 8;
this.Well = [];
+ this.FramesSinceLastFall = 0;
+ this.FramesToWaitUntilFall = 60;
/**
* Contains block definition
@@ -59,7 +52,7 @@ define([], function () {
]
]},
//Tee (t)
- {color: '#fdfd01;', orientations: [
+ {color: '#fdfd01', orientations: [
[
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
@@ -248,8 +241,11 @@ define([], function () {
this.blockCount = this.blocks.length;
this.orientationCount = 4;
+ this.ActiveBlock = this.newBlock();
+ this.NextBlock = this.newBlock();
+
this.initWell();
- console.info("Game Created.");
+// console.info("Game Created."+JSON.stringify(this.ActiveBlock)+JSON.stringify(this.NextBlock));
},
initWell: function () {
@@ -259,45 +255,141 @@ define([], function () {
this.Well[j][i] = 0;
}
}
+ this.Well[6][1] = 1;
},
updateState: function (input) {
- if (input.FallFlag) {
- console.log("User wants to fast fall");
+ this.FramesSinceLastFall++;
+ if (input.Rotate == true) {
+ this.rotateBlock();
}
- if (input.RotateFlag) {
- console.log("User wants to rotate");
+
+ if (input.Left == true) {
+ this.moveBlock(-1)
}
- if (input.LeftFlag) {
- console.log("User wants to move left");
+
+ if (input.Right == true) {
+ this.moveBlock(1);
}
- if (input.RightFlag) {
- console.log("User wants to move right");
+
+ if (input.Fall == true) {
+ this.FramesSinceLastFall += 30;
}
+ if (this.FramesSinceLastFall >= this.FramesToWaitUntilFall) {
+ if (!this.dropBlock()) {
+ this.lockBlock(this.ActiveBlock);
+// FallingBlocksApp.stop();
+ this.ActiveBlock = this.NextBlock;
+ this.NextBlock = this.newBlock();
+ }
+ this.FramesSinceLastFall = 0;
+ }
+ },
+
+ lockBlock: function (block) {
+ console.log("Lock the block");
+ var x , y ,
+ pieces = this.blocks[block.TypeIndex]['orientations'][block.orientation];
+// console.log(x, y, pieces);
+// console.dir(this.Well);
+ for (var j = pieces.length - 1; j >= 0; j--) {
+ y = block.y + j;
+ for (var i = 0; i < pieces[j].length; i++) {
+ x = block.x + i;
+ if (pieces[j][i] > 0) {
+// console.log(j, y, j + y, ':', i, x, i + x);
+// if (this.Well[y] && this.Well[ y][ x]) {
+ console.log("locking well ",y,x);
+ this.Well[y][ x] = 1;
+// } else {
+// console.log("No well space ",y,x);
+// }
+ }
+ }
+ }
+ console.dir(this.Well);
},
newBlock: function () {
- return Math.random() * 10 % this.blockCount;
+ return {
+ x: 0,
+ y: 0,
+// TypeIndex: Math.floor((Math.random()*100)%this.blockCount),
+ TypeIndex: 0,
+ orientation: 0,
+ };
+ },
+
+ dropBlock: function () {
+ var self = this,
+ block = this.ActiveBlock;
+// console.log("Drop block from(", block.x, block.y, ') to (', block.x, block.y + 1, ')');
+ if (self.checkCollision(block.x, block.y + 1, block.TypeIndex, block.orientation)) {
+ block.y++;
+ return true;
+ }
+ return false;
},
rotateBlock: function () {
- var self = this;
- if (self.checkCollision(self.ActiveBlock.x, self.ActiveBlock.y, self.ActiveBlock.TypeIndex,
- ++this.ActiveBlock.orientation % this.orientationCount)) {
- this.ActiveBlock.orientation = ++this.ActiveBlock.orientation % this.orientationCount;
+ var self = this,
+ block = this.ActiveBlock,
+ newOrientation = (block.orientation + 1) % this.orientationCount;
+ if (self.checkCollision(block.x, block.y, block.TypeIndex, newOrientation)) {
+ block.orientation = newOrientation;
+ console.log("new orientation", newOrientation);
}
},
moveBlock: function (direction) {
- var self = this;
- if (self.checkCollision(self.ActiveBlock.x + direction, self.ActiveBlock.y,
- self.ActiveBlock.TypeIndex, self.ActiveBlock.orientation)) {
+ var self = this,
+ block = this.ActiveBlock;
+ if (self.checkCollision(block.x + direction, block.y, block.TypeIndex, block.orientation)) {
+ block.x += direction;
}
},
- checkCollision: function (newX, newY, block, orientation) {
+ checkCollision: function (newX, newY, blockType, orientation) {
+ var x, y, pieces = this.blocks[blockType]['orientations'][orientation],
+ rowOutOfBounds = false,
+ debug = (newY + 5 > this.WellHeight );
+
+// if (debug) {
+// console.log("CheckCollision at nX:", newX, ', nY:', newY);
+// }
+ for (var j = pieces.length - 1; j >= 0; j--) {
+ y = newY + j;
+ rowOutOfBounds = (y >= this.Well.length);
+// if (debug) {
+// console.log(" check row ", j, "values=", pieces[j]);
+// if (rowOutOfBounds)
+// console.log(" WellRow out of bounds", y);
+// else
+// console.log(" WellRow index ", y, this.Well[y]);
+// }
+ for (var i = 0; i < pieces[j].length; i++) {
+ x = newX + i;
+ if (pieces[j][i] > 0) {
+ if (rowOutOfBounds) {
+// console.log("BOOM!!!!",j,i);
+ return false;
+ }
+ if (x < 0 || x >= this.WellWidth) {
+// console.log("BAM!!!!",j,i);
+ return false;
+ }
+ if (this.Well[y][x] != 0) {
+// console.log("Hit existing block at ",y,x,"with piece square",j,i);
+ return false;
+ }
+
+//
+// this.Well[j + y][i + x] = 1;
+ }
+ }
+ }
return true;
},
View
86 js/render.js
@@ -20,62 +20,80 @@ define([], function () {
this.TileHeight = 16;
this.TileWidth = 16;
- this.WellOffsetLeft = 10;
- this.WellOffsetTop = 10;
+ this.CellSpacing = 1;
+
+ this.WellOffsetLeft = 0;
+ this.WellOffsetTop = -32;
+
+ this.NextBlockTop = this.TileHeight * 2;
+ this.NextBlockLeft = (this.TileWidth * (this.game.WellWidth + 2))
+
+ this.Debug_Collision = false;
+ this.Debug_Collision_Color = "pink";
},
render: function (isStopped) {
var self = this;
self.context.clearRect(0, 0, self.canvas.width, self.canvas.height);
- if(isStopped){
- self.context.fillText("Game Paused. Press [SPACE] to resume",50,50);
- }else{
- self.drawWell();
- self.drawActiveBlock();
- }
+// if(isStopped){
+// self.context.fillText("Game Paused. Press [SPACE] to resume",50,50);
+// }else{
+ self.drawWell();
+ self.drawBlock(self.game.ActiveBlock,true);
+ self.drawBlock(self.game.NextBlock,false);
+// }
},
-
drawWell: function () {
- this.context.save();
- for (var j = 0; j <= this.game.WellHeight; j++) {
- for (var i = 0; i <= this.game.WellWidth; i++) {
- this.drawTile(i * this.TileWidth, j * this.TileHeight, this.game.Well[i][j]);
+ for (var j = 2; j < this.game.WellHeight; j++) {
+ for (var i = 0; i < this.game.WellWidth; i++) {
+ this.drawTile(
+ i * this.TileWidth,
+ j * this.TileHeight,
+ (this.game.Well[j][i]?'grey':'black'));
}
}
- this.context.restore();
},
- drawActiveBlock: function () {
- this.context.save();
-
- var block = this.game.ActiveBlock;
- if (block.x < 0 || block.x > this.game.WellWidth ||
- block.y < 0 || block.y > this.game.WellHeight) {
- return false;
- } else {
- var matrix = this.game.blocks[block.TypeIndex]['orientations'][block.orientation];
- this.context.fillStyle = this.game.blocks[block.TypeIndex]['color'];
- for (var j = 0; j < matrix.length; j++) {
- for (var i = 0; i < matrix[j].length; i++) {
- if (matrix[i][j] > 0)
- this.drawTile((block.x + i - 2) * this.TileWidth, (block.y + j - 2) * this.TileHeight,block);
+ drawBlock: function (block, isActive) {
+ var matrix = this.game.blocks[block.TypeIndex]['orientations'][block.orientation],
+ color = this.game.blocks[block.TypeIndex]['color'],
+ y = 0,
+ x = 0;
+ for (var j = 0; j < matrix.length; j++) {
+ y = ((block.y + j ) * this.TileHeight) + (!isActive ? this.NextBlockTop : 0);
+ for (var i = 0; i < matrix[j].length; i++) {
+ x = ((block.x + i ) * this.TileWidth) + (!isActive ? this.NextBlockLeft : 0);
+ if (matrix[i][j] > 0)
+ this.drawTile( x,y,color);
+ else if(this.Debug_Collision){
+ this.drawTile( x,y,this.Debug_Collision_Color);
}
}
}
- this.context.restore();
- return true;
},
- drawTile: function (x, y, Type) {
+ /**
+ *
+ * @param x Canvas X pixel
+ * @param y Canvas Y pixel
+ * @param CellColor Game.Well status
+ */
+ drawTile: function (x, y, CellColor) {
+ this.context.save();
+ if(CellColor){
+ this.context.fillStyle = CellColor;
+ }
this.context.fillRect(
- x + this.WellOffsetLeft,
- y + this.WellOffsetTop,
- this.TileHeight, this.TileHeight);
+ x + this.WellOffsetLeft + this.CellSpacing,
+ y + this.WellOffsetTop + this.CellSpacing,
+ this.TileWidth - this.CellSpacing, this.TileHeight - this.CellSpacing);
+ this.context.restore();
+
},

0 comments on commit 11dd158

Please sign in to comment.
Something went wrong with that request. Please try again.