-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from itsdorosh/gamepad-support
add gamepad support
- Loading branch information
Showing
8 changed files
with
211 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,11 +17,19 @@ <h1 id="gameOverCaption" class="hide">GAME OVER</h1> | |
<button id="playButton">Play ▶️</button> | ||
<button id="restartButton" class="hide">Restart 🔄</button> | ||
</div> | ||
|
||
<div id="generalButtons" class="position-absolute"> | ||
<span id="fullscreenMode" onclick="toggleFullScreen()">📺️</span> | ||
<span id="gamepad" class="hide">🎮</span> | ||
<span id="mouse" class="hide">🖱</span> | ||
</div> | ||
|
||
<div id="canvasContainer"></div> | ||
|
||
<script src="https://unpkg.com/[email protected]/build/three.js"></script> | ||
<script src="./src/constants.js"></script> | ||
<script src="src/enemies.js"></script> | ||
<script src="./src/helpers.js"></script> | ||
<script src="./src/controls.js"></script> | ||
<script src="./src/raycaster.js"></script> | ||
<script src="./src/viewer.js"></script> | ||
|
@@ -37,6 +45,8 @@ <h1 id="gameOverCaption" class="hide">GAME OVER</h1> | |
const playButton = document.getElementById('playButton'); | ||
const pauseButton = document.getElementById('pauseButton'); | ||
const restartButton = document.getElementById('restartButton'); | ||
const gamepadButton = document.getElementById('gamepad'); | ||
const mouseButton = document.getElementById('mouse'); | ||
|
||
// captions | ||
const greetingCaption = document.getElementById('greetingCaption'); | ||
|
@@ -52,31 +62,69 @@ <h1 id="gameOverCaption" class="hide">GAME OVER</h1> | |
}); | ||
|
||
engine.onGameOver(() => { | ||
gameButtons.classList.remove('hide'); | ||
greetingCaption.classList.add('hide'); | ||
gameOverCaption.classList.remove('hide'); | ||
playButton.classList.add('hide'); | ||
restartButton.classList.remove('hide'); | ||
hide(greetingCaption, playButton); | ||
show(gameButtons, gameOverCaption, restartButton); | ||
}); | ||
|
||
playButton.addEventListener('click', () => { | ||
gameButtons.classList.add('hide'); | ||
inGameButtons.classList.remove('hide'); | ||
hide(gameButtons); | ||
show(inGameButtons); | ||
engine.play(); | ||
}); | ||
|
||
pauseButton.addEventListener('click', () => { | ||
gameButtons.classList.remove('hide'); | ||
restartButton.classList.remove('hide'); | ||
show(gameButtons, restartButton, playButton); | ||
engine.pause(); | ||
}); | ||
|
||
restartButton.addEventListener('click', () => { | ||
greetingCaption.classList.add('hide'); | ||
gameOverCaption.classList.remove('hide'); | ||
gameButtons.classList.add('hide'); | ||
hide(gameOverCaption, gameButtons); | ||
show(greetingCaption); | ||
engine.restart(); | ||
}); | ||
|
||
gamepadButton.addEventListener('click', () => controls.switchControlsMode(CONTROLS_MODES.GAMEPAD_MODE)); | ||
|
||
mouseButton.addEventListener('click', () => controls.switchControlsMode(CONTROLS_MODES.MOUSE_MODE)); | ||
|
||
controls.on('gamepadconnected', () => { | ||
controls.switchControlsMode(CONTROLS_MODES.MOUSE_MODE); | ||
}); | ||
|
||
controls.on('gamepaddisconnected', () => { | ||
controls.switchControlsMode(CONTROLS_MODES.NO_MODE); | ||
}); | ||
|
||
controls.on('controlsModeSwitched', (mode) => { | ||
switch (mode) { | ||
case CONTROLS_MODES.MOUSE_MODE: { | ||
hide(mouseButton); | ||
show(gamepadButton); | ||
break; | ||
} | ||
|
||
case CONTROLS_MODES.GAMEPAD_MODE: { | ||
hide(gamepadButton); | ||
show(mouseButton); | ||
break; | ||
} | ||
|
||
case CONTROLS_MODES.NO_MODE: { | ||
hide(mouseButton, gamepadButton); | ||
break; | ||
} | ||
} | ||
}); | ||
|
||
function toggleFullScreen() { | ||
if (!document.fullscreenElement) { | ||
document.documentElement.requestFullscreen(); | ||
} else { | ||
if (document.exitFullscreen) { | ||
document.exitFullscreen(); | ||
} | ||
} | ||
} | ||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
function getRandomInt(min, max) { | ||
min = Math.ceil(min); | ||
max = Math.floor(max); | ||
return Math.floor(Math.random() * (max - min + 1) + min); | ||
} | ||
|
||
function getRandomFloat(min, max) { | ||
return Math.random() * (max - min) + min; | ||
} | ||
|
||
function hide(...elements) { | ||
elements.forEach(element => element.classList.add('hide')); | ||
} | ||
|
||
function show(...elements) { | ||
elements.forEach(element => element.classList.remove('hide')); | ||
} | ||
|
||
function convertCoordinatesToPixels(width, height, x = 0, y = 0) { | ||
return { | ||
x: ((width / 2) + ((width / 2) * x)), | ||
y: ((height / 2) + ((height / 2) * y)), | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.