katbugjs/Touch.js

221 lines
4.6 KiB
JavaScript

function doNothing(e)
{
e.preventDefault();
}
function pressUp(e)
{
if (!gameState.paused)
{
catbug.vY = -6;
}
doNothing(e);
}
function releaseUp(e)
{
if (catbug.vY < 0)
{
catbug.vY = 0;
}
}
function pressDown(e)
{
if (!gameState.paused)
{
catbug.vY = 6;
}
doNothing(e);
}
function releaseDown(e)
{
if (catbug.vY > 0)
{
catbug.vY = 0;
}
}
function pressLeft(e)
{
if (!gameState.paused)
{
catbug.vX = -6;
}
doNothing(e);
}
function releaseLeft(e)
{
if (catbug.vX < 0)
{
catbug.vX = 0;
}
}
function pressRight(e)
{
if (!gameState.paused)
{
catbug.vX = 6;
}
doNothing(e);
}
function releaseRight(e)
{
if (catbug.vX > 0)
{
catbug.vX = 0;
}
}
function pressUpLeft(e)
{
pressUp(e);
pressLeft(e);
}
function releaseUpLeft(e)
{
releaseUp(e);
releaseLeft(e);
}
function pressUpRight(e)
{
pressUp(e);
pressRight(e);
}
function releaseUpRight(e)
{
releaseUp(e);
releaseRight(e);
}
function pressDownLeft(e)
{
pressDown(e);
pressLeft(e);
}
function releaseDownLeft(e)
{
releaseDown(e);
releaseLeft(e);
}
function pressDownRight(e)
{
pressDown(e);
pressRight(e);
}
function releaseDownRight(e)
{
releaseDown(e);
releaseRight(e);
}
function pressQ(e)
{
controller.q = true;
gameState.playing = false;
gameState.paused = false;
gameState.over = false;
}
function releaseQ(e)
{
controller.q = false;
}
function pressPause(e)
{
controller.space = true;
if (gameState.over)
{
gameState.over = false;
gameState.playing = false;
controller.space = false;
}
else
{
if (gameState.playing)
{
gameState.paused = !gameState.paused;
}
}
}
function releasePause(e)
{
controller.space = false;
}
function prepareTouchInput()
{
document.getElementById("dpad").style.visibility = "visible";
document.getElementById("spaceButton").style.visibility = "visible";
document.getElementById("qButton").style.visibility = "visible";
window.dPad = {};
dPad.n = document.getElementById("upButton");
dPad.s = document.getElementById("downButton");
dPad.w = document.getElementById("leftButton");
dPad.e = document.getElementById("rightButton");
dPad.nw = document.getElementById("nwButton");
dPad.ne = document.getElementById("neButton");
dPad.sw = document.getElementById("swButton");
dPad.se = document.getElementById("seButton");
dPad.n.addEventListener("touchstart",pressUp);
dPad.n.addEventListener("touchend", releaseUp);
dPad.n.addEventListener("touchcancel", releaseUp);
dPad.s.addEventListener("touchstart", pressDown);
dPad.s.addEventListener("touchend", releaseDown);
dPad.s.addEventListener("touchcancel", releaseDown);
dPad.e.addEventListener("touchstart", pressRight);
dPad.e.addEventListener("touchend", releaseRight);
dPad.e.addEventListener("touchcancel", releaseRight);
dPad.w.addEventListener("touchstart", pressLeft);
dPad.w.addEventListener("touchend", releaseLeft);
dPad.w.addEventListener("touchcancel", releaseLeft);
dPad.nw.addEventListener("touchstart", pressUpLeft);
dPad.nw.addEventListener("touchend", releaseUpLeft);
dPad.nw.addEventListener("touchcancel", releaseUpLeft);
dPad.ne.addEventListener("touchstart", pressUpRight);
dPad.ne.addEventListener("touchend", releaseUpRight);
dPad.ne.addEventListener("touchcancel", releaseUpRight);
dPad.sw.addEventListener("touchstart", pressDownLeft);
dPad.sw.addEventListener("touchend", releaseDownLeft);
dPad.sw.addEventListener("touchcancel", releaseDownLeft);
dPad.se.addEventListener("touchstart", pressDownRight);
dPad.se.addEventListener("touchend", releaseDownRight);
dPad.se.addEventListener("touchcancel", releaseDownRight);
window.pauseButton = document.getElementById("spaceButton");
pauseButton.addEventListener("touchstart", pressPause);
pauseButton.addEventListener("touchend", releasePause);
pauseButton.addEventListener("touchcancel", releasePause);
window.qButton = document.getElementById("qButton");
qButton.addEventListener("touchstart", pressQ);
qButton.addEventListener("touchend", releaseQ);
qButton.addEventListener("touchcancel", releaseQ);
dPad.n.addEventListener("touchmove", doNothing);
dPad.s.addEventListener("touchmove", doNothing);
dPad.e.addEventListener("touchmove", doNothing);
dPad.w.addEventListener("touchmove", doNothing);
dPad.nw.addEventListener("touchmove", doNothing);
dPad.ne.addEventListener("touchmove", doNothing);
dPad.sw.addEventListener("touchmove", doNothing);
dPad.se.addEventListener("touchmove", doNothing);
pauseButton.addEventListener("touchmove", doNothing);
qButton.addEventListener("touchmove", doNothing);
}