fix gamepad das
This commit is contained in:
parent
bb0a110bcd
commit
e172ddf8c0
167
dist/tetrion.js
vendored
167
dist/tetrion.js
vendored
@ -318,15 +318,10 @@ module.exports = gamepadAPI;
|
||||
|
||||
var gamepad = require('./gamepad.js');
|
||||
|
||||
const gamepadLeftPressedEvent = new Event('leftPressed');
|
||||
//const gamepadRightPressedEvent = new Event('rightPressed');
|
||||
//const gamepadUpPressedEvent = new Event('upPressed');
|
||||
//const gamepadPressedEvent = new Event('leftPressed');
|
||||
|
||||
var UserInputs = {
|
||||
init() {
|
||||
//document.addEventListener('keydown', this.keyDown.bind(this));
|
||||
//document.addEventListener('keyup', this.keyUp.bind(this));
|
||||
document.addEventListener('keydown', this.keyDown.bind(this));
|
||||
document.addEventListener('keyup', this.keyUp.bind(this));
|
||||
},
|
||||
|
||||
updateGamepad() {
|
||||
@ -358,18 +353,28 @@ var UserInputs = {
|
||||
//this.gamepadButtonsDown("Y");
|
||||
return;
|
||||
},
|
||||
|
||||
// X, Y, A, B , RB, LB Buttons
|
||||
gamepadButtonsDown(finds) {
|
||||
var deciDAS = 3;
|
||||
var deciARR = 12;
|
||||
var deciDAS = 10;
|
||||
var deciARR = 10;
|
||||
var isContained = this.gpButtons.includes(finds);
|
||||
|
||||
var isPrevContained = this.prevGpButtons.includes(finds);
|
||||
|
||||
if(isPrevContained != isContained ) {
|
||||
this.isGamepadButtonDown = false;
|
||||
// Do once
|
||||
if(isContained)
|
||||
this.gamepadQueue.push(finds);
|
||||
}
|
||||
|
||||
if (!this.isGamepadButtonDown) {
|
||||
|
||||
if (this.nDeciframes >= deciDAS) {
|
||||
this.nDeciframes = 0;
|
||||
this.isGamepadButtonDown = true;
|
||||
//if(isContained)
|
||||
// this.gamepadQueue.push(finds);
|
||||
}
|
||||
|
||||
} else {
|
||||
if (this.nDeciframes >= deciARR && isContained) {
|
||||
this.gamepadQueue.push(finds);
|
||||
@ -378,15 +383,20 @@ var UserInputs = {
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
// Direction Pad
|
||||
gamepadDown(finds) {
|
||||
var DAS = 5;
|
||||
var ARR = 4;
|
||||
var ARR = 2;
|
||||
var isContained = this.gpButtons.includes(finds);
|
||||
var isDas = true; //this.gpButtons.includes("DPad-Left") || this.gpButtons.includes("DPad-Right") ||
|
||||
//this.gpButtons.includes("DPad-Up") || this.gpButtons.includes("DPad-Down");
|
||||
|
||||
|
||||
if(isDas) {
|
||||
var isPrevContained = this.prevGpButtons.includes(finds);
|
||||
|
||||
if(isPrevContained != isContained ) {
|
||||
this.isGamepadDown = false;
|
||||
// Do once
|
||||
//if(isContainted)
|
||||
// this.gamepadQueue.push(finds);
|
||||
}
|
||||
if (!this.isGamepadDown) {
|
||||
if (this.nframe >= DAS) {
|
||||
this.nframe = 0;
|
||||
@ -398,7 +408,7 @@ var UserInputs = {
|
||||
this.nframe = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return;
|
||||
},
|
||||
@ -414,7 +424,7 @@ var UserInputs = {
|
||||
},
|
||||
processKeyDown(key)
|
||||
{
|
||||
var deciDAS = 2;
|
||||
var deciDAS = 4;
|
||||
var deciARR = 8;
|
||||
|
||||
|
||||
@ -439,15 +449,6 @@ var UserInputs = {
|
||||
var DAS = 1;
|
||||
var ARR = 4;
|
||||
|
||||
|
||||
/* if (this.isDown.key == 88 || this.isDown.key == 90 || this.isDown.key == 32) {
|
||||
//this.processKeys();
|
||||
return;
|
||||
}*/
|
||||
|
||||
// if (this.isDown) {
|
||||
//this.frames++;
|
||||
|
||||
if (!this.held) {
|
||||
if (this.frames >= DAS) {
|
||||
this.frames = 0;
|
||||
@ -471,8 +472,15 @@ var UserInputs = {
|
||||
},
|
||||
gamepadButtonClear() {
|
||||
gpButtons = [];
|
||||
nDeciframes = 0;
|
||||
isGamepadDown = false;
|
||||
isGamepadButtonDown = false;
|
||||
gamepadQueue = [];
|
||||
},
|
||||
saveButtons() {
|
||||
//console.log(this.gpButtons);
|
||||
this.prevGpButtons = this.gpButtons;
|
||||
//console.log("prev: " + preGpButtons);
|
||||
},
|
||||
isDown: false,
|
||||
isKeyDown: false,
|
||||
@ -484,6 +492,7 @@ var UserInputs = {
|
||||
nDeciframes: 0,
|
||||
nDeciframesKey: 0,
|
||||
gpButtons: [],
|
||||
prevGpButtons:[],
|
||||
keyboardKeys: [],
|
||||
inputqueue: [],
|
||||
gamepadQueue: []
|
||||
@ -764,17 +773,62 @@ Tetris.prototype = {
|
||||
this.currentTime = new Date().getTime();
|
||||
var deltaTime = this.currentTime - this.prevTime;
|
||||
|
||||
|
||||
if(deltaTime >= 1) { // 600hz
|
||||
inputs.incDeciframes();
|
||||
inputs.processGamepadInput();
|
||||
}
|
||||
if(deltaTime >= 10)
|
||||
if(deltaTime >= 10)
|
||||
{
|
||||
inputs.incFrame();
|
||||
|
||||
//inputs.processKeyShift();
|
||||
}
|
||||
}
|
||||
|
||||
if(deltaTime >= 1) { // 600hz
|
||||
inputs.incDeciframes();
|
||||
inputs.updateGamepad();
|
||||
inputs.processButtons();
|
||||
inputs.processGamepadInput();
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
// drain gamepad queue
|
||||
if(deltaTime > 5)
|
||||
{
|
||||
while((inputs.gamepadQueue != undefined && inputs.gamepadQueue.length >= 1)){
|
||||
var curkey = inputs.gamepadQueue.pop();
|
||||
if(curkey == "DPad-Left") {
|
||||
this.shape.goLeft(this.matrix);
|
||||
this._draw();
|
||||
}
|
||||
if(curkey == "DPad-Right") {
|
||||
this.shape.goRight(this.matrix);
|
||||
this._draw();
|
||||
}
|
||||
if(curkey == "A") {
|
||||
this.shape.rotate(this.matrix);
|
||||
this._draw();
|
||||
}
|
||||
if(curkey == "B") {
|
||||
this.shape.rotateClockwise(this.matrix);;
|
||||
this._draw();
|
||||
}
|
||||
if(curkey == "DPad-Down") {
|
||||
this.shape.goDown(this.matrix);
|
||||
this._draw();
|
||||
}
|
||||
if(curkey == "RB") {
|
||||
this.shape.goBottom(this.matrix);
|
||||
this._update();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
inputs.gamepadQueue = [];
|
||||
}
|
||||
|
||||
if(deltaTime >= 1)
|
||||
inputs.saveButtons();
|
||||
|
||||
//inputs.gamepadButtonClear();
|
||||
/*
|
||||
if(deltaTime > 5) // 120hz
|
||||
{
|
||||
@ -821,46 +875,7 @@ Tetris.prototype = {
|
||||
|
||||
*/
|
||||
|
||||
inputs.updateGamepad();
|
||||
|
||||
if(deltaTime >= 5)
|
||||
{
|
||||
inputs.processButtons();
|
||||
}
|
||||
// drain gamepad queue
|
||||
if(deltaTime > 10)
|
||||
{
|
||||
while((inputs.gamepadQueue != undefined && inputs.gamepadQueue.length >= 1)){
|
||||
var curkey = inputs.gamepadQueue.pop();
|
||||
if(curkey == "DPad-Left") {
|
||||
this.shape.goLeft(this.matrix);
|
||||
this._draw();
|
||||
}
|
||||
if(curkey == "DPad-Right") {
|
||||
this.shape.goRight(this.matrix);
|
||||
this._draw();
|
||||
}
|
||||
if(curkey == "A") {
|
||||
this.shape.rotate(this.matrix);
|
||||
this._draw();
|
||||
}
|
||||
if(curkey == "B") {
|
||||
this.shape.rotateClockwise(this.matrix);;
|
||||
this._draw();
|
||||
}
|
||||
if(curkey == "DPad-Down") {
|
||||
this.shape.goDown(this.matrix);
|
||||
this._draw();
|
||||
}
|
||||
if(curkey == "RB") {
|
||||
this.shape.goBottom(this.matrix);
|
||||
this._update();
|
||||
}
|
||||
|
||||
}
|
||||
inputs.gamepadQueue = [];
|
||||
inputs.gamepadButtonClear();
|
||||
}
|
||||
|
||||
|
||||
if (deltaTime > this.interval) {
|
||||
this._update();
|
||||
|
67
src/input.js
67
src/input.js
@ -1,15 +1,10 @@
|
||||
|
||||
var gamepad = require('./gamepad.js');
|
||||
|
||||
const gamepadLeftPressedEvent = new Event('leftPressed');
|
||||
//const gamepadRightPressedEvent = new Event('rightPressed');
|
||||
//const gamepadUpPressedEvent = new Event('upPressed');
|
||||
//const gamepadPressedEvent = new Event('leftPressed');
|
||||
|
||||
var UserInputs = {
|
||||
init() {
|
||||
//document.addEventListener('keydown', this.keyDown.bind(this));
|
||||
//document.addEventListener('keyup', this.keyUp.bind(this));
|
||||
document.addEventListener('keydown', this.keyDown.bind(this));
|
||||
document.addEventListener('keyup', this.keyUp.bind(this));
|
||||
},
|
||||
|
||||
updateGamepad() {
|
||||
@ -41,18 +36,28 @@ var UserInputs = {
|
||||
//this.gamepadButtonsDown("Y");
|
||||
return;
|
||||
},
|
||||
|
||||
// X, Y, A, B , RB, LB Buttons
|
||||
gamepadButtonsDown(finds) {
|
||||
var deciDAS = 3;
|
||||
var deciARR = 12;
|
||||
var deciDAS = 10;
|
||||
var deciARR = 10;
|
||||
var isContained = this.gpButtons.includes(finds);
|
||||
|
||||
var isPrevContained = this.prevGpButtons.includes(finds);
|
||||
|
||||
if(isPrevContained != isContained ) {
|
||||
this.isGamepadButtonDown = false;
|
||||
// Do once
|
||||
if(isContained)
|
||||
this.gamepadQueue.push(finds);
|
||||
}
|
||||
|
||||
if (!this.isGamepadButtonDown) {
|
||||
|
||||
if (this.nDeciframes >= deciDAS) {
|
||||
this.nDeciframes = 0;
|
||||
this.isGamepadButtonDown = true;
|
||||
//if(isContained)
|
||||
// this.gamepadQueue.push(finds);
|
||||
}
|
||||
|
||||
} else {
|
||||
if (this.nDeciframes >= deciARR && isContained) {
|
||||
this.gamepadQueue.push(finds);
|
||||
@ -61,15 +66,20 @@ var UserInputs = {
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
// Direction Pad
|
||||
gamepadDown(finds) {
|
||||
var DAS = 5;
|
||||
var ARR = 4;
|
||||
var ARR = 2;
|
||||
var isContained = this.gpButtons.includes(finds);
|
||||
var isDas = true; //this.gpButtons.includes("DPad-Left") || this.gpButtons.includes("DPad-Right") ||
|
||||
//this.gpButtons.includes("DPad-Up") || this.gpButtons.includes("DPad-Down");
|
||||
|
||||
|
||||
if(isDas) {
|
||||
var isPrevContained = this.prevGpButtons.includes(finds);
|
||||
|
||||
if(isPrevContained != isContained ) {
|
||||
this.isGamepadDown = false;
|
||||
// Do once
|
||||
//if(isContainted)
|
||||
// this.gamepadQueue.push(finds);
|
||||
}
|
||||
if (!this.isGamepadDown) {
|
||||
if (this.nframe >= DAS) {
|
||||
this.nframe = 0;
|
||||
@ -81,7 +91,7 @@ var UserInputs = {
|
||||
this.nframe = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return;
|
||||
},
|
||||
@ -97,7 +107,7 @@ var UserInputs = {
|
||||
},
|
||||
processKeyDown(key)
|
||||
{
|
||||
var deciDAS = 2;
|
||||
var deciDAS = 4;
|
||||
var deciARR = 8;
|
||||
|
||||
|
||||
@ -122,15 +132,6 @@ var UserInputs = {
|
||||
var DAS = 1;
|
||||
var ARR = 4;
|
||||
|
||||
|
||||
/* if (this.isDown.key == 88 || this.isDown.key == 90 || this.isDown.key == 32) {
|
||||
//this.processKeys();
|
||||
return;
|
||||
}*/
|
||||
|
||||
// if (this.isDown) {
|
||||
//this.frames++;
|
||||
|
||||
if (!this.held) {
|
||||
if (this.frames >= DAS) {
|
||||
this.frames = 0;
|
||||
@ -154,8 +155,15 @@ var UserInputs = {
|
||||
},
|
||||
gamepadButtonClear() {
|
||||
gpButtons = [];
|
||||
nDeciframes = 0;
|
||||
isGamepadDown = false;
|
||||
isGamepadButtonDown = false;
|
||||
gamepadQueue = [];
|
||||
},
|
||||
saveButtons() {
|
||||
//console.log(this.gpButtons);
|
||||
this.prevGpButtons = this.gpButtons;
|
||||
//console.log("prev: " + preGpButtons);
|
||||
},
|
||||
isDown: false,
|
||||
isKeyDown: false,
|
||||
@ -167,6 +175,7 @@ var UserInputs = {
|
||||
nDeciframes: 0,
|
||||
nDeciframesKey: 0,
|
||||
gpButtons: [],
|
||||
prevGpButtons:[],
|
||||
keyboardKeys: [],
|
||||
inputqueue: [],
|
||||
gamepadQueue: []
|
||||
|
100
src/main.js
100
src/main.js
@ -271,17 +271,62 @@ Tetris.prototype = {
|
||||
this.currentTime = new Date().getTime();
|
||||
var deltaTime = this.currentTime - this.prevTime;
|
||||
|
||||
|
||||
if(deltaTime >= 1) { // 600hz
|
||||
inputs.incDeciframes();
|
||||
inputs.processGamepadInput();
|
||||
}
|
||||
if(deltaTime >= 10)
|
||||
if(deltaTime >= 10)
|
||||
{
|
||||
inputs.incFrame();
|
||||
|
||||
//inputs.processKeyShift();
|
||||
}
|
||||
}
|
||||
|
||||
if(deltaTime >= 1) { // 600hz
|
||||
inputs.incDeciframes();
|
||||
inputs.updateGamepad();
|
||||
inputs.processButtons();
|
||||
inputs.processGamepadInput();
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
// drain gamepad queue
|
||||
if(deltaTime > 5)
|
||||
{
|
||||
while((inputs.gamepadQueue != undefined && inputs.gamepadQueue.length >= 1)){
|
||||
var curkey = inputs.gamepadQueue.pop();
|
||||
if(curkey == "DPad-Left") {
|
||||
this.shape.goLeft(this.matrix);
|
||||
this._draw();
|
||||
}
|
||||
if(curkey == "DPad-Right") {
|
||||
this.shape.goRight(this.matrix);
|
||||
this._draw();
|
||||
}
|
||||
if(curkey == "A") {
|
||||
this.shape.rotate(this.matrix);
|
||||
this._draw();
|
||||
}
|
||||
if(curkey == "B") {
|
||||
this.shape.rotateClockwise(this.matrix);;
|
||||
this._draw();
|
||||
}
|
||||
if(curkey == "DPad-Down") {
|
||||
this.shape.goDown(this.matrix);
|
||||
this._draw();
|
||||
}
|
||||
if(curkey == "RB") {
|
||||
this.shape.goBottom(this.matrix);
|
||||
this._update();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
inputs.gamepadQueue = [];
|
||||
}
|
||||
|
||||
if(deltaTime >= 1)
|
||||
inputs.saveButtons();
|
||||
|
||||
//inputs.gamepadButtonClear();
|
||||
/*
|
||||
if(deltaTime > 5) // 120hz
|
||||
{
|
||||
@ -328,46 +373,7 @@ Tetris.prototype = {
|
||||
|
||||
*/
|
||||
|
||||
inputs.updateGamepad();
|
||||
|
||||
if(deltaTime >= 5)
|
||||
{
|
||||
inputs.processButtons();
|
||||
}
|
||||
// drain gamepad queue
|
||||
if(deltaTime > 10)
|
||||
{
|
||||
while((inputs.gamepadQueue != undefined && inputs.gamepadQueue.length >= 1)){
|
||||
var curkey = inputs.gamepadQueue.pop();
|
||||
if(curkey == "DPad-Left") {
|
||||
this.shape.goLeft(this.matrix);
|
||||
this._draw();
|
||||
}
|
||||
if(curkey == "DPad-Right") {
|
||||
this.shape.goRight(this.matrix);
|
||||
this._draw();
|
||||
}
|
||||
if(curkey == "A") {
|
||||
this.shape.rotate(this.matrix);
|
||||
this._draw();
|
||||
}
|
||||
if(curkey == "B") {
|
||||
this.shape.rotateClockwise(this.matrix);;
|
||||
this._draw();
|
||||
}
|
||||
if(curkey == "DPad-Down") {
|
||||
this.shape.goDown(this.matrix);
|
||||
this._draw();
|
||||
}
|
||||
if(curkey == "RB") {
|
||||
this.shape.goBottom(this.matrix);
|
||||
this._update();
|
||||
}
|
||||
|
||||
}
|
||||
inputs.gamepadQueue = [];
|
||||
inputs.gamepadButtonClear();
|
||||
}
|
||||
|
||||
|
||||
if (deltaTime > this.interval) {
|
||||
this._update();
|
||||
|
Loading…
Reference in New Issue
Block a user