7 bag and control fixes. need to reenable keyboard

pull/1/head
PolicyChanges1@gmail.com 2021-02-05 07:42:24 -05:00
parent d78f41f2bc
commit d0a063cebb
3 changed files with 226 additions and 102 deletions

168
dist/tetrion.js vendored
View File

@ -135,7 +135,7 @@ var tetrisCanvas = {
} }
}, },
//Draw preview shape in preview canvas //Draw preview shape in preview canvas
drawPreviewShape:function(shape,deltaHeight){ drawPreviewShape:function(shape){
if (!shape){ if (!shape){
return; return;
} }
@ -143,7 +143,7 @@ var tetrisCanvas = {
var matrix = shape.matrix(); var matrix = shape.matrix();
var gsize = this.previewGridSize; var gsize = this.previewGridSize;
var startX = (this.preview.width - gsize*shape.getColumnCount()) / 2; var startX = (this.preview.width - gsize*shape.getColumnCount()) / 2;
var startY = (this.preview.height - deltaHeight - gsize*shape.getRowCount()) / 2; var startY = (this.preview.height - gsize*shape.getRowCount()) / 2;
for(var i = 0;i<matrix.length;i++){ for(var i = 0;i<matrix.length;i++){
for(var j = 0;j<matrix[i].length;j++){ for(var j = 0;j<matrix[i].length;j++){
var value = matrix[i][j]; var value = matrix[i][j];
@ -322,8 +322,8 @@ const gamepadLeftPressedEvent = new Event('leftPressed');
var UserInputs = { var UserInputs = {
init() { init() {
document.addEventListener('keydown', this.keyDown.bind(this)); //document.addEventListener('keydown', this.keyDown.bind(this));
document.addEventListener('keyup', this.keyUp.bind(this)); //document.addEventListener('keyup', this.keyUp.bind(this));
}, },
updateGamepad() { updateGamepad() {
@ -331,10 +331,12 @@ var UserInputs = {
}, },
incFrame() { incFrame() {
this.frames++;
this.nframe++; this.nframe++;
}, },
incDeciframes() { incDeciframes() {
this.nDeciframes++; this.nDeciframes++;
this.nDeciframesKey++;
}, },
processGamepadInput() { processGamepadInput() {
@ -354,17 +356,19 @@ var UserInputs = {
return; return;
}, },
gamepadButtonsDown(finds) { gamepadButtonsDown(finds) {
var deciDAS = 3;
var deciARR = 12;
var isContained = this.gpButtons.includes(finds); var isContained = this.gpButtons.includes(finds);
if (!this.isGamepadButtonDown) { if (!this.isGamepadButtonDown) {
if (this.nDeciframes >= 20) { if (this.nDeciframes >= deciDAS) {
this.nDeciframes = 0; this.nDeciframes = 0;
this.isGamepadButtonDown = true; this.isGamepadButtonDown = true;
if(isContained)
this.gamepadQueue.push(finds);
} }
} else { } else {
if (this.nDeciframes >= 40 && isContained) { if (this.nDeciframes >= deciARR && isContained) {
//console.log("Pushdown: " + finds);
this.gamepadQueue.push(finds); this.gamepadQueue.push(finds);
this.nDeciframes = 0; this.nDeciframes = 0;
} }
@ -372,19 +376,15 @@ var UserInputs = {
}, },
gamepadDown(finds) { gamepadDown(finds) {
var DAS = 6;
var DAS = 8; var ARR = 3;
var ARR = 5;
var isContained = this.gpButtons.includes(finds); var isContained = this.gpButtons.includes(finds);
var isDas = true; //this.gpButtons.includes("DPad-Left") || this.gpButtons.includes("DPad-Right") || var isDas = true; //this.gpButtons.includes("DPad-Left") || this.gpButtons.includes("DPad-Right") ||
this.gpButtons.includes("DPad-Up") || this.gpButtons.includes("DPad-Down"); //this.gpButtons.includes("DPad-Up") || this.gpButtons.includes("DPad-Down");
if(isDas) { if(isDas) {
//console.log("frame no.: " + this.nframe + this.isGamepadDown);
if (!this.isGamepadDown) { if (!this.isGamepadDown) {
if (this.nframe >= DAS) { if (this.nframe >= DAS) {
this.nframe = 0; this.nframe = 0;
@ -400,56 +400,85 @@ var UserInputs = {
return; return;
}, },
processInput() { processKeys() {
this.processKeyDown(32); // Space
this.processKeyDown(88); // X
this.processKeyDown(90); // Z
},
processKeyShift() {
this.processInput(39); // right
this.processInput(37); // left
this.processInput(40); // down
},
processKeyDown(key)
{
var deciDAS = 2;
var deciARR = 8;
var DAS = 4;
var ARR = 2;
if (this.isDown.key == 65 || this.isDown.key == 68 || this.keyDown.key == 38) { if (!this.isKeyDown) {
return; if (this.nDeciframesKey >= deciDAS) {
} this.nDeciframesKey = 0;
//console.log("Henlo: " + this.isDown.frames); this.isKeyDown = true;
if (this.isDown) { if(this.keyboardKeys[key] == true)
console.log("Henlo: " + this.isDown.key); this.inputqueue.push(key);
this.isDown.frames++; }
} else {
if (this.nDeciframesKey >= deciARR && this.keyboardKeys[key] == true) {
this.inputqueue.push(key);
this.nDeciframesKey = 0;
}
}
},
processInput(key) {
var DAS = 1;
var ARR = 4;
if (!this.isDown.held) {
if (this.isDown.frames == DAS) { /* if (this.isDown.key == 88 || this.isDown.key == 90 || this.isDown.key == 32) {
this.isDown.frames = 0; //this.processKeys();
this.isDown.held = true; return;
}*/
// if (this.isDown) {
//this.frames++;
if (!this.held) {
if (this.frames >= DAS) {
this.frames = 0;
this.held = true;
} }
} else { } else {
if (this.isDown.frames == ARR) { if (this.frames >= ARR && this.keyboardKeys[key] == true) {
this.inputqueue.push(key);
this.inputqueue.push(this.isDown.key); this.frames = 0;
this.isDown.frames = 0;
} }
} }
} //}
}, },
keyDown(event) { keyDown(event) {
if (this.isDown == false) { this.keyboardKeys[event.keyCode] = true;
var key = {
key: event.keyCode,
held: false,
frames: 0
}
this.isDown = key;
this.inputqueue.push(this.isDown.key);
}
}, },
keyUp(event) { keyUp(event) {
this.isDown = false; this.nDeciframesKey = 0;
this.isKeyDown = false;
this.keyboardKeys[event.keyCode] = false;
}, },
isDown: false, isDown: false,
isKeyDown: false,
isGamepadDown: false, isGamepadDown: false,
isGamepadButtonDown: false, isGamepadButtonDown: false,
held: false,
nframe: 0, nframe: 0,
frames: 0,
nDeciframes: 0, nDeciframes: 0,
nDeciframesKey: 0,
gpButtons: [], gpButtons: [],
keyboardKeys: [],
inputqueue: [], inputqueue: [],
prevButton: "",
gamepadQueue: [] gamepadQueue: []
}; };
@ -655,7 +684,7 @@ Tetris.prototype = {
if (this.isGameOver || !this.shape) { if (this.isGameOver || !this.shape) {
return; return;
} }
/*
switch (e.keyCode) { switch (e.keyCode) {
case 37: { case 37: {
//this.shape.goLeft(matrix); //this.shape.goLeft(matrix);
@ -693,6 +722,7 @@ Tetris.prototype = {
} }
break; break;
} }
*/
}, },
// Restart game // Restart game
_restartHandler: function() { _restartHandler: function() {
@ -710,7 +740,7 @@ Tetris.prototype = {
this.shape = this.preparedShape || shapes.randomShape(); this.shape = this.preparedShape || shapes.randomShape();
this.preparedShape = shapes.randomShape(); this.preparedShape = shapes.randomShape();
this._draw(); this._draw();
canvas.drawPreviewShape(this.preparedShape ,80); canvas.drawPreviewShape(this.preparedShape);
}, },
// Draw game data // Draw game data
@ -727,17 +757,30 @@ Tetris.prototype = {
this.currentTime = new Date().getTime(); this.currentTime = new Date().getTime();
var deltaTime = this.currentTime - this.prevTime; var deltaTime = this.currentTime - this.prevTime;
if(deltaTime >= 1) // 600hz
inputs.incDeciframes();
if(deltaTime > 10) if(deltaTime > 10)
{ {
inputs.incFrame(); inputs.incFrame();
inputs.processGamepadInput(); inputs.processGamepadInput();
//inputs.processKeyShift();
} }
if (deltaTime > 10) { // 60hz DAS /*
if(deltaTime > 5) // 120hz
{
inputs.processKeys();
}
if (deltaTime > 10) { // 60hz
// Keyboard inputs // Keyboard inputs
inputs.processInput();
while((inputs.inputqueue != undefined && inputs.inputqueue.length >= 1)){ while((inputs.inputqueue != undefined && inputs.inputqueue.length >= 1)){
var curkey = inputs.inputqueue.pop(); var curkey = inputs.inputqueue.pop();
console.log("cur key: " + curkey);
if(curkey == 37) { if(curkey == 37) {
this.shape.goLeft(this.matrix); this.shape.goLeft(this.matrix);
this._draw(); this._draw();
@ -746,13 +789,32 @@ Tetris.prototype = {
this.shape.goRight(this.matrix); this.shape.goRight(this.matrix);
this._draw(); this._draw();
} }
if(curkey == 40) {
this.shape.goDown(this.matrix);
this._draw();
}
if(curkey == 90) {
this.shape.rotate(this.matrix);
this._draw();
}
if(curkey == 88){
this.shape.rotateClockwise(this.matrix);;
this._draw();
}
if(curkey == 32) {
this.shape.goBottom(this.matrix);
this._update();
}
} }
inputs.inputqueue = [];
} }
if(deltaTime > 1) */
inputs.incDeciframes();
inputs.updateGamepad(); inputs.updateGamepad();
if(deltaTime > 5) if(deltaTime > 5)
{ {
inputs.processButtons(); inputs.processButtons();
@ -1464,7 +1526,7 @@ var layoutView = function(container,maxW,maxH){
info.style.width = side.style.width; info.style.width = side.style.width;
} }
preview.width = 80; preview.width = 80;
preview.height = 360; preview.height = 80;
gameOver.style.width = scene.width +'px'; gameOver.style.width = scene.width +'px';

View File

@ -8,8 +8,8 @@ const gamepadLeftPressedEvent = new Event('leftPressed');
var UserInputs = { var UserInputs = {
init() { init() {
document.addEventListener('keydown', this.keyDown.bind(this)); //document.addEventListener('keydown', this.keyDown.bind(this));
document.addEventListener('keyup', this.keyUp.bind(this)); //document.addEventListener('keyup', this.keyUp.bind(this));
}, },
updateGamepad() { updateGamepad() {
@ -17,10 +17,12 @@ var UserInputs = {
}, },
incFrame() { incFrame() {
this.frames++;
this.nframe++; this.nframe++;
}, },
incDeciframes() { incDeciframes() {
this.nDeciframes++; this.nDeciframes++;
this.nDeciframesKey++;
}, },
processGamepadInput() { processGamepadInput() {
@ -40,17 +42,19 @@ var UserInputs = {
return; return;
}, },
gamepadButtonsDown(finds) { gamepadButtonsDown(finds) {
var deciDAS = 3;
var deciARR = 12;
var isContained = this.gpButtons.includes(finds); var isContained = this.gpButtons.includes(finds);
if (!this.isGamepadButtonDown) { if (!this.isGamepadButtonDown) {
if (this.nDeciframes >= 20) { if (this.nDeciframes >= deciDAS) {
this.nDeciframes = 0; this.nDeciframes = 0;
this.isGamepadButtonDown = true; this.isGamepadButtonDown = true;
if(isContained)
this.gamepadQueue.push(finds);
} }
} else { } else {
if (this.nDeciframes >= 40 && isContained) { if (this.nDeciframes >= deciARR && isContained) {
//console.log("Pushdown: " + finds);
this.gamepadQueue.push(finds); this.gamepadQueue.push(finds);
this.nDeciframes = 0; this.nDeciframes = 0;
} }
@ -58,19 +62,15 @@ var UserInputs = {
}, },
gamepadDown(finds) { gamepadDown(finds) {
var DAS = 6;
var DAS = 8; var ARR = 3;
var ARR = 5;
var isContained = this.gpButtons.includes(finds); var isContained = this.gpButtons.includes(finds);
var isDas = true; //this.gpButtons.includes("DPad-Left") || this.gpButtons.includes("DPad-Right") || var isDas = true; //this.gpButtons.includes("DPad-Left") || this.gpButtons.includes("DPad-Right") ||
this.gpButtons.includes("DPad-Up") || this.gpButtons.includes("DPad-Down"); //this.gpButtons.includes("DPad-Up") || this.gpButtons.includes("DPad-Down");
if(isDas) { if(isDas) {
//console.log("frame no.: " + this.nframe + this.isGamepadDown);
if (!this.isGamepadDown) { if (!this.isGamepadDown) {
if (this.nframe >= DAS) { if (this.nframe >= DAS) {
this.nframe = 0; this.nframe = 0;
@ -86,56 +86,85 @@ var UserInputs = {
return; return;
}, },
processInput() { processKeys() {
this.processKeyDown(32); // Space
this.processKeyDown(88); // X
this.processKeyDown(90); // Z
},
processKeyShift() {
this.processInput(39); // right
this.processInput(37); // left
this.processInput(40); // down
},
processKeyDown(key)
{
var deciDAS = 2;
var deciARR = 8;
var DAS = 4;
var ARR = 2;
if (this.isDown.key == 65 || this.isDown.key == 68 || this.keyDown.key == 38) { if (!this.isKeyDown) {
return; if (this.nDeciframesKey >= deciDAS) {
} this.nDeciframesKey = 0;
//console.log("Henlo: " + this.isDown.frames); this.isKeyDown = true;
if (this.isDown) { if(this.keyboardKeys[key] == true)
console.log("Henlo: " + this.isDown.key); this.inputqueue.push(key);
this.isDown.frames++; }
} else {
if (this.nDeciframesKey >= deciARR && this.keyboardKeys[key] == true) {
this.inputqueue.push(key);
this.nDeciframesKey = 0;
}
}
},
processInput(key) {
var DAS = 1;
var ARR = 4;
if (!this.isDown.held) {
if (this.isDown.frames == DAS) { /* if (this.isDown.key == 88 || this.isDown.key == 90 || this.isDown.key == 32) {
this.isDown.frames = 0; //this.processKeys();
this.isDown.held = true; return;
}*/
// if (this.isDown) {
//this.frames++;
if (!this.held) {
if (this.frames >= DAS) {
this.frames = 0;
this.held = true;
} }
} else { } else {
if (this.isDown.frames == ARR) { if (this.frames >= ARR && this.keyboardKeys[key] == true) {
this.inputqueue.push(key);
this.inputqueue.push(this.isDown.key); this.frames = 0;
this.isDown.frames = 0;
} }
} }
} //}
}, },
keyDown(event) { keyDown(event) {
if (this.isDown == false) { this.keyboardKeys[event.keyCode] = true;
var key = {
key: event.keyCode,
held: false,
frames: 0
}
this.isDown = key;
this.inputqueue.push(this.isDown.key);
}
}, },
keyUp(event) { keyUp(event) {
this.isDown = false; this.nDeciframesKey = 0;
this.isKeyDown = false;
this.keyboardKeys[event.keyCode] = false;
}, },
isDown: false, isDown: false,
isKeyDown: false,
isGamepadDown: false, isGamepadDown: false,
isGamepadButtonDown: false, isGamepadButtonDown: false,
held: false,
nframe: 0, nframe: 0,
frames: 0,
nDeciframes: 0, nDeciframes: 0,
nDeciframesKey: 0,
gpButtons: [], gpButtons: [],
keyboardKeys: [],
inputqueue: [], inputqueue: [],
prevButton: "",
gamepadQueue: [] gamepadQueue: []
}; };

View File

@ -198,7 +198,7 @@ Tetris.prototype = {
if (this.isGameOver || !this.shape) { if (this.isGameOver || !this.shape) {
return; return;
} }
/*
switch (e.keyCode) { switch (e.keyCode) {
case 37: { case 37: {
//this.shape.goLeft(matrix); //this.shape.goLeft(matrix);
@ -236,6 +236,7 @@ Tetris.prototype = {
} }
break; break;
} }
*/
}, },
// Restart game // Restart game
_restartHandler: function() { _restartHandler: function() {
@ -270,17 +271,30 @@ Tetris.prototype = {
this.currentTime = new Date().getTime(); this.currentTime = new Date().getTime();
var deltaTime = this.currentTime - this.prevTime; var deltaTime = this.currentTime - this.prevTime;
if(deltaTime >= 1) // 600hz
inputs.incDeciframes();
if(deltaTime > 10) if(deltaTime > 10)
{ {
inputs.incFrame(); inputs.incFrame();
inputs.processGamepadInput(); inputs.processGamepadInput();
//inputs.processKeyShift();
} }
if (deltaTime > 10) { // 60hz DAS /*
if(deltaTime > 5) // 120hz
{
inputs.processKeys();
}
if (deltaTime > 10) { // 60hz
// Keyboard inputs // Keyboard inputs
inputs.processInput();
while((inputs.inputqueue != undefined && inputs.inputqueue.length >= 1)){ while((inputs.inputqueue != undefined && inputs.inputqueue.length >= 1)){
var curkey = inputs.inputqueue.pop(); var curkey = inputs.inputqueue.pop();
console.log("cur key: " + curkey);
if(curkey == 37) { if(curkey == 37) {
this.shape.goLeft(this.matrix); this.shape.goLeft(this.matrix);
this._draw(); this._draw();
@ -289,13 +303,32 @@ Tetris.prototype = {
this.shape.goRight(this.matrix); this.shape.goRight(this.matrix);
this._draw(); this._draw();
} }
if(curkey == 40) {
this.shape.goDown(this.matrix);
this._draw();
}
if(curkey == 90) {
this.shape.rotate(this.matrix);
this._draw();
}
if(curkey == 88){
this.shape.rotateClockwise(this.matrix);;
this._draw();
}
if(curkey == 32) {
this.shape.goBottom(this.matrix);
this._update();
}
} }
inputs.inputqueue = [];
} }
if(deltaTime > 1) */
inputs.incDeciframes();
inputs.updateGamepad(); inputs.updateGamepad();
if(deltaTime > 5) if(deltaTime > 5)
{ {
inputs.processButtons(); inputs.processButtons();