Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
<option value="110">Rush</option>
</select>
</div>
<button onclick="go_full_screen()">Full Screen</button><br />
<button onclick="go_full_screen()">Full Screen</button> <div id="gamePadConnected" style="visibility: hidden;display:inline-block;margin-top:-15px;margin-left:10px;">&#127918;</div>
</div>

<!--
Expand Down
88 changes: 88 additions & 0 deletions js/snake.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ SNAKE.Snake = SNAKE.Snake || (function() {
me.snakeTail = me.snakeBody["b0"];
me.snakeHead.elm.className = me.snakeHead.elm.className.replace(/\bsnake-snakebody-dead\b/,'');
me.snakeHead.elm.className += " snake-snakebody-alive";
me.gamepad = null;

// ----- private methods -----

Expand Down Expand Up @@ -236,6 +237,35 @@ SNAKE.Snake = SNAKE.Snake || (function() {
return isPaused;
};

me.setCurrentDirection = function(val) {
currentDirection = val;
};
me.getCurrentDirection = function() {
return currentDirection;
};

me.getLastMove = function() {
return lastMove;
};

me.setPreMove = function(val) {
preMove = val;
};

me.getIsFirstMove = function() {
return isFirstMove;
};
me.setIsFirstMove = function(val) {
isFirstMove = val;
};

me.getIsFirstGameMove = function() {
return isFirstGameMove;
};
me.setIsFirstGameMove = function(val) {
isFirstGameMove = val;
};

/**
* This method is called when a user presses a key. It logs arrow key presses in "currentDirection", which is used when the snake needs to make its next move.
* @method handleArrowKeys
Expand Down Expand Up @@ -1068,6 +1098,64 @@ SNAKE.Board = SNAKE.Board || (function() {
handleEndCondition(elmWin);
};

me.handleGamepad = function() {
me.gamepad = navigator.getGamepads()[0];

//if (isDead || isPaused) {return;}

if (me.getBoardState() === 1) {
me.setBoardState(2);
mySnake.go();
}

let directionFound = -1;

var axLR = me.gamepad.axes[0];
var axUD = me.gamepad.axes[1];

//console.dir(axLR + "," + axUD);

if (axLR < -0.5){
directionFound = 3;
} else if (axLR > 0.5){
directionFound = 1;
}

if (axUD < -0.5){
directionFound = 0;
} else if (axUD > 0.5){
directionFound = 2;
}

if (mySnake.getCurrentDirection() !== mySnake.getLastMove()) // Allow a queue of 1 premove so you can turn again before the first turn registers
{
mySnake.setPreMove(directionFound);
}
if (Math.abs(directionFound - mySnake.getLastMove()) !== 2 && mySnake.getIsFirstMove() || mySnake.getIsFirstGameMove()) // Prevent snake from turning 180 degrees
{
mySnake.setCurrentDirection(directionFound);
mySnake.setIsFirstMove(false);
mySnake.setIsFirstGameMove(false);
}
};


window.addEventListener("gamepadconnected", function(e) {
var gamePadConnected = document.getElementById('gamePadConnected');
gamePadConnected.style.visibility = "visible";
me.gamepad = navigator.getGamepads()[0] || e.gamepad;

me.gamePadIntervall = setInterval(me.handleGamepad, 100);
});

window.addEventListener("gamepaddisconnected", function(e) {
console.log("disconnected");
var gamePadConnected = document.getElementById('gamePadConnected');
gamePadConnected.style.visibility = "hidden";
me.gamepad = null;
clearInterval(me.gamePadIntervall);
});

// ---------------------------------------------------------------------
// Initialize
// ---------------------------------------------------------------------
Expand Down