piece timer

This commit is contained in:
PolicyChanges1@gmail.com 2021-02-25 12:00:02 -05:00
parent 443529d5a2
commit e02895e128
5 changed files with 180 additions and 88 deletions

134
dist/tetrion.js vendored
View File

@ -517,7 +517,7 @@ var UserInputs = {
if(isContained) if(isContained)
this.gamepadQueue.push(finds); this.gamepadQueue.push(finds);
} }
var gamepadDirectionDasFrames = this.gamepadDirectionPadDeciFrames / 1.0; var gamepadDirectionDasFrames = this.gamepadDirectionPadDeciFrames;
if (!this.isGamepadDown) { if (!this.isGamepadDown) {
if (gamepadDirectionDasFrames >= DAS) { if (gamepadDirectionDasFrames >= DAS) {
this.gamepadDirectionPadDeciFrames = 0; this.gamepadDirectionPadDeciFrames = 0;
@ -544,6 +544,7 @@ var UserInputs = {
this.processKeyDown(17); // ctrl this.processKeyDown(17); // ctrl
this.processKeyDown(81); // q this.processKeyDown(81); // q
this.processKeyDown(82); // r this.processKeyDown(82); // r
this.processKeyDown(67); // c
}, },
// keyboard keys z,x,space // keyboard keys z,x,space
@ -597,7 +598,7 @@ var UserInputs = {
} }
var keyboardDASFrames = this.keyboardDirectionArrowsDeciframes / 1.0; // why isnt this 10? var keyboardDASFrames = this.keyboardDirectionArrowsDeciframes;
if (!this.isDirectionArrowDown) { if (!this.isDirectionArrowDown) {
@ -616,9 +617,7 @@ var UserInputs = {
keyDown(event) { keyDown(event) {
// Disable space scrolling etc // Disable space scrolling etc
//if (event.keyCode === 32) { event.preventDefault();
event.preventDefault();
//}
this.keyboardKeys[event.keyCode] = true; this.keyboardKeys[event.keyCode] = true;
this.isKeyBoardKeyDown = true; this.isKeyBoardKeyDown = true;
@ -832,7 +831,6 @@ Tetris.prototype = {
this.currentOpener = 0; this.currentOpener = 0;
this.matrix = initMatrix(consts.ROW_COUNT, consts.COLUMN_COUNT); this.matrix = initMatrix(consts.ROW_COUNT, consts.COLUMN_COUNT);
this.reset(); this.reset();
setInterval(() => {this._processTick();}, 1);
this._initEvents(); this._initEvents();
this._fireShape(); this._fireShape();
@ -846,21 +844,21 @@ Tetris.prototype = {
this.hintMino = 0; this.hintMino = 0;
this._restartHandler(); this._restartHandler();
this.currentOpener = 0; this.currentOpener = 0;
this._fireShape();
}, },
setTKIFonzieVar: function() setTKIFonzieVar: function()
{ {
this.isFreePlay = false; this.isFreePlay = false;
this._restartHandler();
this.currentOpener = 1; this.currentOpener = 1;
this._fireShape(); this._restartHandler();
}, },
setDTCannonVar: function() setDTCannonVar: function()
{ {
this.isFreePlay = false; this.isFreePlay = false;
this._restartHandler();
this.currentOpener = 2; this.currentOpener = 2;
this._fireShape(); this._restartHandler();
}, },
//Reset game //Reset game
reset: function() { reset: function() {
@ -880,13 +878,15 @@ Tetris.prototype = {
this.currentMinoInx = 0; this.currentMinoInx = 0;
this.shapeQueue = []; this.shapeQueue = [];
this.hintQueue = []; this.hintQueue = [];
this.holdQueue = []; this.holdStack = [];
// gets set to false after mino has been popped from hold stack; set back to true on mino dropped // gets set to false after mino has been popped from hold stack; set back to true on mino dropped
this.canPullFromHoldQueue = false; this.canPopFromHoldStack = false;
// rotation counter for srs extended piece lockdown // manipulation counter for srs extended piece lockdown
this.rotationCounter = 0; this.manipulationCounter = 0;
// timer for srs extened piece lockdown // timer for srs extened piece lockdown
this.pieceTimer = 0; this.lockdownTimer = 0;
this.landed = false;
clearMatrix(this.matrix); clearMatrix(this.matrix);
views.setLevel(this.level); views.setLevel(this.level);
views.setScore(this.score); views.setScore(this.score);
@ -909,24 +909,24 @@ Tetris.prototype = {
}, },
pushHoldStack: function() pushHoldStack: function()
{ {
if(this.holdQueue.length < 4) { if(this.holdStack.length < 4) {
this.holdQueue.push(this.shape); this.holdStack.push(utils.deepClone(this.shape));
this.shape = this.shapeQueue.shift(); this.shape = this.shapeQueue.shift();
this.canPullFromHoldQueue = false; this.canPopFromHoldStack = false;
this.shape.resetOrigin(); this.shape.resetOrigin();
//canvas.drawHoldShape(this.holdQueue); //canvas.drawHoldShape(this.holdStack);
this._draw(); // update? this._draw(); // update?
} }
}, },
popHoldStack: function() popHoldStack: function()
{ {
if(this.holdQueue.length >= 1 && this.canPullFromHoldQueue) if(this.holdStack.length >= 1 && this.canPopFromHoldStack)
{ {
this.canPullFromHoldQueue = false; this.canPopFromHoldStack = false;
this.shapeQueue.unshift(this.shape); this.shapeQueue.unshift(utils.deepClone(this.shape));
this.shape = this.holdQueue.pop(); this.shape = this.holdStack.pop();
this.shape.resetOrigin(); this.shape.resetOrigin();
//canvas.drawHoldShape(this.holdQueue); //canvas.drawHoldShape(this.holdStack);
this._draw(); this._draw();
} }
}, },
@ -935,13 +935,16 @@ Tetris.prototype = {
_restartHandler: function() { _restartHandler: function() {
this.reset(); this.reset();
this.start(); this.start();
this._fireShape();
}, },
// Bind game events // Bind game events
_initEvents: function() { _initEvents: function() {
setInterval(() => {this._processTick();}, 1);
setInterval(() => {this.lockDownTimer++;}, 100 );
views.btnRestart.addEventListener('click', utils.proxy(this._restartHandler, this), false); views.btnRestart.addEventListener('click', utils.proxy(this._restartHandler, this), false);
}, },
// Fire a new random shape // Fill next queue and set next shape
_fireShape: function() { _fireShape: function() {
@ -966,7 +969,6 @@ Tetris.prototype = {
this.hintQueue = []; this.hintQueue = [];
this.shapeQueue = []; this.shapeQueue = [];
this._restartHandler(); this._restartHandler();
this._fireShape();
} }
} else { } else {
while(this.shapeQueue.length <= 4) while(this.shapeQueue.length <= 4)
@ -979,6 +981,9 @@ Tetris.prototype = {
this.currentMinoInx++; this.currentMinoInx++;
} }
//todo:should be in shapes.js
this.landed = false;
this.manipulationCounter = 0;
// Reset matrix at successful end of opener // Reset matrix at successful end of opener
//if(this.shapeQueue.length == openers.length) { //if(this.shapeQueue.length == openers.length) {
// this.matrix = []; // this.matrix = [];
@ -988,24 +993,42 @@ Tetris.prototype = {
this._draw(); this._draw();
}, },
// lockdown timer with centisecond resolution
resetLockdown: function() {
if(this.shape.canDown(this.matrix) == false)
this.landed = true;
this.lockDownTimer = 0;
if(this.landed)
this.manipulationCounter++;
},
// Return if the piece can be shifted or rotated
isPieceLocked: function() {
if(this.manipulationCounter > 15) return true;
if(this.lockDownTimer >= 5) return true;
return false;
},
// Draw game data // Draw game data
_draw: function() { _draw: function() {
canvas.drawScene(); canvas.drawScene();
canvas.drawShape(this.shape); canvas.drawShape(this.shape);
canvas.drawHoldShape(this.holdQueue); canvas.drawHoldShape(this.holdStack);
canvas.drawPreviewShape(this.shapeQueue); canvas.drawPreviewShape(this.shapeQueue);
canvas.drawHintShape(this.hintMino); canvas.drawHintShape(this.hintMino);
if(this.shape != undefined) { if(this.shape != undefined) {
let clone = Object.assign(Object.create(Object.getPrototypeOf(this.shape)), this.shape); let clone = Object.assign(Object.create(Object.getPrototypeOf(this.shape)), this.shape);
//todo: put in collision detsction
var bottomY = clone.bottomAt(this.matrix); var bottomY = clone.bottomAt(this.matrix);
canvas.drawGhostShape(clone, bottomY); canvas.drawGhostShape(clone, bottomY);
} }
canvas.drawMatrix(this.matrix); canvas.drawMatrix(this.matrix);
}, },
// tick input data
_processTick: async function() { _processTick: async function() {
var deltaTime = 1.0; // 1 millisecond var deltaTime = 1.0; // 1 millisecond
@ -1024,6 +1047,10 @@ Tetris.prototype = {
inputs.processGamepadInput(); inputs.processGamepadInput();
} }
// Don't process game related events if game over
if(this.isGameOver) return;
// drain gamepad queue // drain gamepad queue
if(inputs.getTickCounter() > halfFrame) // 8 millisecons if(inputs.getTickCounter() > halfFrame) // 8 millisecons
{ {
@ -1031,20 +1058,24 @@ Tetris.prototype = {
var curkey = inputs.gamepadQueue.shift(); var curkey = inputs.gamepadQueue.shift();
if(curkey == "DPad-Left") { if(curkey == "DPad-Left") {
this.shape.goLeft(this.matrix); this.shape.goLeft(this.matrix);
this.resetLockdown();
this._draw(); this._draw();
} }
if(curkey == "DPad-Right") { if(curkey == "DPad-Right") {
this.shape.goRight(this.matrix); this.shape.goRight(this.matrix);
this.resetLockdown();
this._draw(); this._draw();
} }
if(curkey == "A") { if(curkey == "A") {
this.rotationCounter++; this.rotationCounter++;
this.shape.rotate(this.matrix); this.shape.rotate(this.matrix);
this.resetLockdown();
this._draw(); this._draw();
} }
if(curkey == "B") { if(curkey == "B") {
this.rotationCounter++; this.rotationCounter++;
this.shape.rotateClockwise(this.matrix); this.shape.rotateClockwise(this.matrix);
this.resetLockdown();
this._draw(); this._draw();
} }
if(curkey == "DPad-Down") { if(curkey == "DPad-Down") {
@ -1053,15 +1084,16 @@ Tetris.prototype = {
} }
if(curkey == "RB") { if(curkey == "RB") {
this.shape.goBottom(this.matrix); this.shape.goBottom(this.matrix);
this.lockDownTimer = 5000;
this._update(); this._update();
} }
if(curkey == "LB") { if(curkey == "LB") {
this.pushHoldStack(); this.pushHoldStack();
this._update(); this._draw();
} }
if(curkey == "DPad-Up") { if(curkey == "DPad-Up") {
this.popHoldStack(); this.popHoldStack();
this._update(); this._draw();
} }
if(curkey == "Back") { if(curkey == "Back") {
this._restartHandler(); this._restartHandler();
@ -1086,10 +1118,12 @@ Tetris.prototype = {
var curkey = inputs.inputqueue.shift(); var curkey = inputs.inputqueue.shift();
if(curkey == 37) { if(curkey == 37) {
this.shape.goLeft(this.matrix); this.shape.goLeft(this.matrix);
this.resetLockdown();
this._draw(); this._draw();
} }
if(curkey == 39){ if(curkey == 39){
this.shape.goRight(this.matrix); this.shape.goRight(this.matrix);
this.resetLockdown();
this._draw(); this._draw();
} }
if(curkey == 40) { if(curkey == 40) {
@ -1099,24 +1133,29 @@ Tetris.prototype = {
if(curkey == 90) { if(curkey == 90) {
this.rotationCounter++; this.rotationCounter++;
this.shape.rotate(this.matrix); this.shape.rotate(this.matrix);
this.resetLockdown();
this._draw(); this._draw();
} }
if(curkey == 88){ if(curkey == 88){
this.rotationCounter++; this.rotationCounter++;
this.shape.rotateClockwise(this.matrix);; this.shape.rotateClockwise(this.matrix);
this.resetLockdown();
this._draw(); this._draw();
} }
if(curkey == 32) { if(curkey == 32) {
this.shape.goBottom(this.matrix); this.shape.goBottom(this.matrix);
this.lockDownTimer = 5000;
this._update(); this._update();
} }
if(curkey == 16) { if(curkey == 16) {
this.pushHoldStack(); this.pushHoldStack();
this._update(); //this._update();
this._draw();
} }
if(curkey == 17) { if(curkey == 17 || curkey == 67) {
this.popHoldStack(); this.popHoldStack();
this._update(); //this._update();
this._draw();
} }
if(curkey == 81) { if(curkey == 81) {
if(document.getElementById("divbg").style.display == "none") if(document.getElementById("divbg").style.display == "none")
@ -1141,9 +1180,6 @@ Tetris.prototype = {
inputs.saveButtons(); inputs.saveButtons();
}, },
sleep: function(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
},
// Refresh game canvas // Refresh game canvas
_refresh: async function() { _refresh: async function() {
@ -1171,6 +1207,7 @@ Tetris.prototype = {
}, },
// check if the current piece is in the same location as the hint piece
_checkHint: function() { _checkHint: function() {
if(this.isFreePlay) if(this.isFreePlay)
@ -1179,6 +1216,8 @@ Tetris.prototype = {
{ {
new Audio('./dist/Failed.ogg').play(); new Audio('./dist/Failed.ogg').play();
this._restartHandler(); this._restartHandler();
// Restart
return 1;
} }
}, },
// Update game data // Update game data
@ -1186,11 +1225,11 @@ Tetris.prototype = {
if (this.shape.canDown(this.matrix)) { if (this.shape.canDown(this.matrix)) {
this.shape.goDown(this.matrix); this.shape.goDown(this.matrix);
} else { } else if(this.isPieceLocked()){
this.canPullFromHoldQueue = true; this.canPopFromHoldStack = true;
this.shape.copyTo(this.matrix); this.shape.copyTo(this.matrix);
this._check(); this._check();
this._checkHint(); if(this._checkHint()) return;
this._fireShape(); this._fireShape();
new Audio('./dist/Blop2.ogg').play(); new Audio('./dist/Blop2.ogg').play();
} }
@ -1205,7 +1244,6 @@ Tetris.prototype = {
}, },
// 0 - none, 1 - mini, 2 - tspin // 0 - none, 1 - mini, 2 - tspin
_tSpinType: function(tPiece, matrix) { _tSpinType: function(tPiece, matrix) {
var side1 = 0; var side1 = 0;
@ -2024,6 +2062,8 @@ var doesShapeOverlap = function(shape, matrix) {
if(x < 0) return true; if(x < 0) return true;
if(x > matrix.cols)return true; if(x > matrix.cols)return true;
if(y > rows) return true; if(y > rows) return true;
// todo: why is matrix not defined when piece popped from hold stack
if(matrix[y] == undefined) return true;
//console.log("matrix X Y: " + " " + x + " "+ y); //console.log("matrix X Y: " + " " + x + " "+ y);
return (matrix[y][x] != 0) return (matrix[y][x] != 0)
}; };
@ -2471,6 +2511,12 @@ var _isPlainObject = function(obj) {
// |obj| is a plain object, created by {} or constructed with new Object // |obj| is a plain object, created by {} or constructed with new Object
return true; return true;
}; };
// Deeper clone
var deepClone = function(copyObject) {
return Object.assign(Object.create(Object.getPrototypeOf(copyObject)), copyObject);
};
// this method source code is from jquery 2.0.x // this method source code is from jquery 2.0.x
// merge object's value and return // merge object's value and return
var extend = function() { var extend = function() {
@ -2555,7 +2601,7 @@ window.requestAnimationFrame = aniFrame;
exports.$ = $; exports.$ = $;
exports.extend = extend; exports.extend = extend;
exports.proxy = proxy; exports.proxy = proxy;
exports.deepClone = deepClone;
// export $; // export $;
// export extend; // export extend;
// export proxy; // export proxy;

View File

@ -93,7 +93,7 @@ var UserInputs = {
if(isContained) if(isContained)
this.gamepadQueue.push(finds); this.gamepadQueue.push(finds);
} }
var gamepadDirectionDasFrames = this.gamepadDirectionPadDeciFrames / 1.0; var gamepadDirectionDasFrames = this.gamepadDirectionPadDeciFrames;
if (!this.isGamepadDown) { if (!this.isGamepadDown) {
if (gamepadDirectionDasFrames >= DAS) { if (gamepadDirectionDasFrames >= DAS) {
this.gamepadDirectionPadDeciFrames = 0; this.gamepadDirectionPadDeciFrames = 0;
@ -120,6 +120,7 @@ var UserInputs = {
this.processKeyDown(17); // ctrl this.processKeyDown(17); // ctrl
this.processKeyDown(81); // q this.processKeyDown(81); // q
this.processKeyDown(82); // r this.processKeyDown(82); // r
this.processKeyDown(67); // c
}, },
// keyboard keys z,x,space // keyboard keys z,x,space
@ -173,7 +174,7 @@ var UserInputs = {
} }
var keyboardDASFrames = this.keyboardDirectionArrowsDeciframes / 1.0; // why isnt this 10? var keyboardDASFrames = this.keyboardDirectionArrowsDeciframes;
if (!this.isDirectionArrowDown) { if (!this.isDirectionArrowDown) {
@ -192,9 +193,7 @@ var UserInputs = {
keyDown(event) { keyDown(event) {
// Disable space scrolling etc // Disable space scrolling etc
//if (event.keyCode === 32) { event.preventDefault();
event.preventDefault();
//}
this.keyboardKeys[event.keyCode] = true; this.keyboardKeys[event.keyCode] = true;
this.isKeyBoardKeyDown = true; this.isKeyBoardKeyDown = true;

View File

@ -162,7 +162,6 @@ Tetris.prototype = {
this.currentOpener = 0; this.currentOpener = 0;
this.matrix = initMatrix(consts.ROW_COUNT, consts.COLUMN_COUNT); this.matrix = initMatrix(consts.ROW_COUNT, consts.COLUMN_COUNT);
this.reset(); this.reset();
setInterval(() => {this._processTick();}, 1);
this._initEvents(); this._initEvents();
this._fireShape(); this._fireShape();
@ -176,21 +175,21 @@ Tetris.prototype = {
this.hintMino = 0; this.hintMino = 0;
this._restartHandler(); this._restartHandler();
this.currentOpener = 0; this.currentOpener = 0;
this._fireShape();
}, },
setTKIFonzieVar: function() setTKIFonzieVar: function()
{ {
this.isFreePlay = false; this.isFreePlay = false;
this._restartHandler();
this.currentOpener = 1; this.currentOpener = 1;
this._fireShape(); this._restartHandler();
}, },
setDTCannonVar: function() setDTCannonVar: function()
{ {
this.isFreePlay = false; this.isFreePlay = false;
this._restartHandler();
this.currentOpener = 2; this.currentOpener = 2;
this._fireShape(); this._restartHandler();
}, },
//Reset game //Reset game
reset: function() { reset: function() {
@ -210,13 +209,15 @@ Tetris.prototype = {
this.currentMinoInx = 0; this.currentMinoInx = 0;
this.shapeQueue = []; this.shapeQueue = [];
this.hintQueue = []; this.hintQueue = [];
this.holdQueue = []; this.holdStack = [];
// gets set to false after mino has been popped from hold stack; set back to true on mino dropped // gets set to false after mino has been popped from hold stack; set back to true on mino dropped
this.canPullFromHoldQueue = false; this.canPopFromHoldStack = false;
// rotation counter for srs extended piece lockdown // manipulation counter for srs extended piece lockdown
this.rotationCounter = 0; this.manipulationCounter = 0;
// timer for srs extened piece lockdown // timer for srs extened piece lockdown
this.pieceTimer = 0; this.lockdownTimer = 0;
this.landed = false;
clearMatrix(this.matrix); clearMatrix(this.matrix);
views.setLevel(this.level); views.setLevel(this.level);
views.setScore(this.score); views.setScore(this.score);
@ -239,24 +240,24 @@ Tetris.prototype = {
}, },
pushHoldStack: function() pushHoldStack: function()
{ {
if(this.holdQueue.length < 4) { if(this.holdStack.length < 4) {
this.holdQueue.push(this.shape); this.holdStack.push(utils.deepClone(this.shape));
this.shape = this.shapeQueue.shift(); this.shape = this.shapeQueue.shift();
this.canPullFromHoldQueue = false; this.canPopFromHoldStack = false;
this.shape.resetOrigin(); this.shape.resetOrigin();
//canvas.drawHoldShape(this.holdQueue); //canvas.drawHoldShape(this.holdStack);
this._draw(); // update? this._draw(); // update?
} }
}, },
popHoldStack: function() popHoldStack: function()
{ {
if(this.holdQueue.length >= 1 && this.canPullFromHoldQueue) if(this.holdStack.length >= 1 && this.canPopFromHoldStack)
{ {
this.canPullFromHoldQueue = false; this.canPopFromHoldStack = false;
this.shapeQueue.unshift(this.shape); this.shapeQueue.unshift(utils.deepClone(this.shape));
this.shape = this.holdQueue.pop(); this.shape = this.holdStack.pop();
this.shape.resetOrigin(); this.shape.resetOrigin();
//canvas.drawHoldShape(this.holdQueue); //canvas.drawHoldShape(this.holdStack);
this._draw(); this._draw();
} }
}, },
@ -265,13 +266,16 @@ Tetris.prototype = {
_restartHandler: function() { _restartHandler: function() {
this.reset(); this.reset();
this.start(); this.start();
this._fireShape();
}, },
// Bind game events // Bind game events
_initEvents: function() { _initEvents: function() {
setInterval(() => {this._processTick();}, 1);
setInterval(() => {this.lockDownTimer++;}, 100 );
views.btnRestart.addEventListener('click', utils.proxy(this._restartHandler, this), false); views.btnRestart.addEventListener('click', utils.proxy(this._restartHandler, this), false);
}, },
// Fire a new random shape // Fill next queue and set next shape
_fireShape: function() { _fireShape: function() {
@ -296,7 +300,6 @@ Tetris.prototype = {
this.hintQueue = []; this.hintQueue = [];
this.shapeQueue = []; this.shapeQueue = [];
this._restartHandler(); this._restartHandler();
this._fireShape();
} }
} else { } else {
while(this.shapeQueue.length <= 4) while(this.shapeQueue.length <= 4)
@ -309,6 +312,9 @@ Tetris.prototype = {
this.currentMinoInx++; this.currentMinoInx++;
} }
//todo:should be in shapes.js
this.landed = false;
this.manipulationCounter = 0;
// Reset matrix at successful end of opener // Reset matrix at successful end of opener
//if(this.shapeQueue.length == openers.length) { //if(this.shapeQueue.length == openers.length) {
// this.matrix = []; // this.matrix = [];
@ -318,24 +324,42 @@ Tetris.prototype = {
this._draw(); this._draw();
}, },
// lockdown timer with centisecond resolution
resetLockdown: function() {
if(this.shape.canDown(this.matrix) == false)
this.landed = true;
this.lockDownTimer = 0;
if(this.landed)
this.manipulationCounter++;
},
// Return if the piece can be shifted or rotated
isPieceLocked: function() {
if(this.manipulationCounter > 15) return true;
if(this.lockDownTimer >= 5) return true;
return false;
},
// Draw game data // Draw game data
_draw: function() { _draw: function() {
canvas.drawScene(); canvas.drawScene();
canvas.drawShape(this.shape); canvas.drawShape(this.shape);
canvas.drawHoldShape(this.holdQueue); canvas.drawHoldShape(this.holdStack);
canvas.drawPreviewShape(this.shapeQueue); canvas.drawPreviewShape(this.shapeQueue);
canvas.drawHintShape(this.hintMino); canvas.drawHintShape(this.hintMino);
if(this.shape != undefined) { if(this.shape != undefined) {
let clone = Object.assign(Object.create(Object.getPrototypeOf(this.shape)), this.shape); let clone = Object.assign(Object.create(Object.getPrototypeOf(this.shape)), this.shape);
//todo: put in collision detsction
var bottomY = clone.bottomAt(this.matrix); var bottomY = clone.bottomAt(this.matrix);
canvas.drawGhostShape(clone, bottomY); canvas.drawGhostShape(clone, bottomY);
} }
canvas.drawMatrix(this.matrix); canvas.drawMatrix(this.matrix);
}, },
// tick input data
_processTick: async function() { _processTick: async function() {
var deltaTime = 1.0; // 1 millisecond var deltaTime = 1.0; // 1 millisecond
@ -354,6 +378,10 @@ Tetris.prototype = {
inputs.processGamepadInput(); inputs.processGamepadInput();
} }
// Don't process game related events if game over
if(this.isGameOver) return;
// drain gamepad queue // drain gamepad queue
if(inputs.getTickCounter() > halfFrame) // 8 millisecons if(inputs.getTickCounter() > halfFrame) // 8 millisecons
{ {
@ -361,20 +389,24 @@ Tetris.prototype = {
var curkey = inputs.gamepadQueue.shift(); var curkey = inputs.gamepadQueue.shift();
if(curkey == "DPad-Left") { if(curkey == "DPad-Left") {
this.shape.goLeft(this.matrix); this.shape.goLeft(this.matrix);
this.resetLockdown();
this._draw(); this._draw();
} }
if(curkey == "DPad-Right") { if(curkey == "DPad-Right") {
this.shape.goRight(this.matrix); this.shape.goRight(this.matrix);
this.resetLockdown();
this._draw(); this._draw();
} }
if(curkey == "A") { if(curkey == "A") {
this.rotationCounter++; this.rotationCounter++;
this.shape.rotate(this.matrix); this.shape.rotate(this.matrix);
this.resetLockdown();
this._draw(); this._draw();
} }
if(curkey == "B") { if(curkey == "B") {
this.rotationCounter++; this.rotationCounter++;
this.shape.rotateClockwise(this.matrix); this.shape.rotateClockwise(this.matrix);
this.resetLockdown();
this._draw(); this._draw();
} }
if(curkey == "DPad-Down") { if(curkey == "DPad-Down") {
@ -383,15 +415,16 @@ Tetris.prototype = {
} }
if(curkey == "RB") { if(curkey == "RB") {
this.shape.goBottom(this.matrix); this.shape.goBottom(this.matrix);
this.lockDownTimer = 5000;
this._update(); this._update();
} }
if(curkey == "LB") { if(curkey == "LB") {
this.pushHoldStack(); this.pushHoldStack();
this._update(); this._draw();
} }
if(curkey == "DPad-Up") { if(curkey == "DPad-Up") {
this.popHoldStack(); this.popHoldStack();
this._update(); this._draw();
} }
if(curkey == "Back") { if(curkey == "Back") {
this._restartHandler(); this._restartHandler();
@ -416,10 +449,12 @@ Tetris.prototype = {
var curkey = inputs.inputqueue.shift(); var curkey = inputs.inputqueue.shift();
if(curkey == 37) { if(curkey == 37) {
this.shape.goLeft(this.matrix); this.shape.goLeft(this.matrix);
this.resetLockdown();
this._draw(); this._draw();
} }
if(curkey == 39){ if(curkey == 39){
this.shape.goRight(this.matrix); this.shape.goRight(this.matrix);
this.resetLockdown();
this._draw(); this._draw();
} }
if(curkey == 40) { if(curkey == 40) {
@ -429,24 +464,29 @@ Tetris.prototype = {
if(curkey == 90) { if(curkey == 90) {
this.rotationCounter++; this.rotationCounter++;
this.shape.rotate(this.matrix); this.shape.rotate(this.matrix);
this.resetLockdown();
this._draw(); this._draw();
} }
if(curkey == 88){ if(curkey == 88){
this.rotationCounter++; this.rotationCounter++;
this.shape.rotateClockwise(this.matrix);; this.shape.rotateClockwise(this.matrix);
this.resetLockdown();
this._draw(); this._draw();
} }
if(curkey == 32) { if(curkey == 32) {
this.shape.goBottom(this.matrix); this.shape.goBottom(this.matrix);
this.lockDownTimer = 5000;
this._update(); this._update();
} }
if(curkey == 16) { if(curkey == 16) {
this.pushHoldStack(); this.pushHoldStack();
this._update(); //this._update();
this._draw();
} }
if(curkey == 17) { if(curkey == 17 || curkey == 67) {
this.popHoldStack(); this.popHoldStack();
this._update(); //this._update();
this._draw();
} }
if(curkey == 81) { if(curkey == 81) {
if(document.getElementById("divbg").style.display == "none") if(document.getElementById("divbg").style.display == "none")
@ -471,9 +511,6 @@ Tetris.prototype = {
inputs.saveButtons(); inputs.saveButtons();
}, },
sleep: function(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
},
// Refresh game canvas // Refresh game canvas
_refresh: async function() { _refresh: async function() {
@ -501,6 +538,7 @@ Tetris.prototype = {
}, },
// check if the current piece is in the same location as the hint piece
_checkHint: function() { _checkHint: function() {
if(this.isFreePlay) if(this.isFreePlay)
@ -509,6 +547,8 @@ Tetris.prototype = {
{ {
new Audio('./dist/Failed.ogg').play(); new Audio('./dist/Failed.ogg').play();
this._restartHandler(); this._restartHandler();
// Restart
return 1;
} }
}, },
// Update game data // Update game data
@ -516,11 +556,11 @@ Tetris.prototype = {
if (this.shape.canDown(this.matrix)) { if (this.shape.canDown(this.matrix)) {
this.shape.goDown(this.matrix); this.shape.goDown(this.matrix);
} else { } else if(this.isPieceLocked()){
this.canPullFromHoldQueue = true; this.canPopFromHoldStack = true;
this.shape.copyTo(this.matrix); this.shape.copyTo(this.matrix);
this._check(); this._check();
this._checkHint(); if(this._checkHint()) return;
this._fireShape(); this._fireShape();
new Audio('./dist/Blop2.ogg').play(); new Audio('./dist/Blop2.ogg').play();
} }
@ -535,7 +575,6 @@ Tetris.prototype = {
}, },
// 0 - none, 1 - mini, 2 - tspin // 0 - none, 1 - mini, 2 - tspin
_tSpinType: function(tPiece, matrix) { _tSpinType: function(tPiece, matrix) {
var side1 = 0; var side1 = 0;

View File

@ -492,6 +492,8 @@ var doesShapeOverlap = function(shape, matrix) {
if(x < 0) return true; if(x < 0) return true;
if(x > matrix.cols)return true; if(x > matrix.cols)return true;
if(y > rows) return true; if(y > rows) return true;
// todo: why is matrix not defined when piece popped from hold stack
if(matrix[y] == undefined) return true;
//console.log("matrix X Y: " + " " + x + " "+ y); //console.log("matrix X Y: " + " " + x + " "+ y);
return (matrix[y][x] != 0) return (matrix[y][x] != 0)
}; };

View File

@ -24,6 +24,12 @@ var _isPlainObject = function(obj) {
// |obj| is a plain object, created by {} or constructed with new Object // |obj| is a plain object, created by {} or constructed with new Object
return true; return true;
}; };
// Deeper clone
var deepClone = function(copyObject) {
return Object.assign(Object.create(Object.getPrototypeOf(copyObject)), copyObject);
};
// this method source code is from jquery 2.0.x // this method source code is from jquery 2.0.x
// merge object's value and return // merge object's value and return
var extend = function() { var extend = function() {
@ -108,7 +114,7 @@ window.requestAnimationFrame = aniFrame;
exports.$ = $; exports.$ = $;
exports.extend = extend; exports.extend = extend;
exports.proxy = proxy; exports.proxy = proxy;
exports.deepClone = deepClone;
// export $; // export $;
// export extend; // export extend;
// export proxy; // export proxy;