6: { map: [0,0,0,0,0,0,0,0,0,0,0,0,2,2,2,0,0,0,0,0,2,2,2,3,3,0,0,0,0,2,1,0,1,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,2,1,0,0,0,1,1,2,2,3,1,1,0,0,0,0,2,2,0,2,0,0,0,0,0,0,0,0,0,0,0,],hares: [{ x:2, y:3, color: "white" }, { x:3, y:3, color: "brown" }, ],grass: [{ x:3, y:1 }, { x:1, y:6 }, { x:3, y:7 }, ], },
7: { map: [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,1,0,0,0,0,0,2,2,1,1,0,0,0,0,0,0,2,1,0,0,0,0,0,0,0,2,2,1,1,0,0,0,0,1,1,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,],hares: [{ x:4, y:2, color: "white" }, { x:5, y:2, color: "brown" }, ],grass: [{ x:2, y:3 }, { x:6, y:5 }, { x:2, y:6 }, ], },
8: { map: [0,0,0,0,0,0,0,0,0,0,0,0,0,3,0,3,0,0,0,0,0,2,2,2,3,0,0,0,0,0,1,2,0,1,0,0,0,2,3,1,3,0,1,0,0,0,0,0,2,0,0,1,0,0,0,0,0,1,2,1,3,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,],hares: [{ x:3, y:3, color: "brown" }, { x:3, y:5, color: "white" }, ],grass: [{ x:1, y:4 }, { x:4, y:6 }, { x:5, y:7 }, ], },
- 9: { map: [0,0,0,0,0,0,0,0,0,0,0,0,0,3,0,1,0,0,0,0,0,2,2,3,1,1,0,0,0,2,1,1,0,1,1,0,0,0,2,1,2,0,1,0,0,0,0,0,1,0,0,1,2,0,0,1,1,2,3,1,1,2,0,0,0,0,0,0,2,2,2,0,0,0,0,0,0,0,0,0,0,],hares: [{ x:4, y:3, color: "brown" }, { x:4, y:4, color: "white" }, ],grass: [{ x:1, y:6 }, { x:5, y:6 }, { x:7, y:7 }, ], },
+ 9: { map: [0,0,0,0,0,0,0,0,0,0,0,0,0,3,0,1,0,0,0,0,0,2,2,3,1,1,0,0,0,2,1,1,0,1,1,0,0,0,2,1,2,0,1,0,0,0,0,1,1,0,0,1,2,0,0,1,2,3,0,1,1,2,0,0,1,0,0,0,2,2,2,0,0,0,0,0,0,0,0,0,0,],hares: [{ x:4, y:3, color: "brown" }, { x:4, y:4, color: "white" }, ],grass: [{ x:5, y:6 }, { x:1, y:7 }, { x:7, y:7 }, ], },
end: { map: [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,],hares: [],grass: [], },
title: { map: [0,0,0,0,0,0,0,0,0,0,4,4,4,4,4,4,4,0,0,4,4,4,4,4,4,4,0,0,4,4,4,4,4,4,4,0,0,4,4,4,4,4,4,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,],hares: [],grass: [], },
}
}
}
+ canvas.ontouchstart = function(e) {
+ if (!game._norun) {
+ _handle_touchstart(game, e);
+ }
+ e.preventDefault();
+ }
+
+ canvas.ontouchmove = function(e) {
+ if (!game._norun) {
+ _handle_touchmove(game, e);
+ }
+ e.preventDefault();
+ }
+
+ canvas.ontouchend = function(e) {
+ if (!game._norun) {
+ _handle_touchend(game, e);
+ }
+ e.preventDefault();
+ }
+
canvas.onkeydown = function(e) {
if (!game._norun && game.events.keydown) {
game.events.keydown(game, e);
if (!game.playing) return;
const rect = game.canvas.getBoundingClientRect();
- let x = Math.round((e.clientX - rect.left) / game.draw_scale) - 1;
- let y = Math.round((e.clientY - rect.top) / game.draw_scale) - 1;
+ let x = Math.round((e.clientX - rect.left) / rect.width * game.screen_w) - 1;
+ let y = Math.round((e.clientY - rect.top) / rect.height * game.screen_h) - 1;
if (e.button === 0 && game.events.mousedown) {
game.events.mousedown(game, e, x, y);
}
}
const rect = game.canvas.getBoundingClientRect();
- let x = Math.round((e.clientX - rect.left) / game.draw_scale) - 1;
- let y = Math.round((e.clientY - rect.top) / game.draw_scale) - 1;
+ let x = Math.round((e.clientX - rect.left) / rect.width * game.screen_w) - 1;
+ let y = Math.round((e.clientY - rect.top) / rect.height * game.screen_h) - 1;
if (e.button === 0 && game.events.mouseup) {
game.events.mouseup(game, e, x, y);
}
function _handle_mousemove(game, e) {
const rect = game.canvas.getBoundingClientRect();
- let x = Math.round((e.clientX - rect.left) / game.draw_scale) - 1;
- let y = Math.round((e.clientY - rect.top) / game.draw_scale) - 1;
+ let x = Math.round((e.clientX - rect.left) / rect.width * game.screen_w) - 1;
+ let y = Math.round((e.clientY - rect.top) / rect.height * game.screen_h) - 1;
if (game.events.mousemove) {
game.events.mousemove(game, e, x, y);
}
}
+ /* ---- Touch (defaults to same as mouse) ---- */
+
+ function _handle_touchstart(game, e) {
+ if (!game.playing) return;
+
+ if (e.touches) e = e.touches[0];
+
+ const rect = game.canvas.getBoundingClientRect();
+ let x = Math.round((e.clientX - rect.left) / rect.width * game.screen_w) - 1;
+ let y = Math.round((e.clientY - rect.top) / rect.height * game.screen_h) - 1;
+ if (game.events.touchstart) {
+ game.events.touchstart(game, e, x, y);
+ } else if (game.events.mousedown) {
+ if (game.events.mousemove) {
+ game.events.mousemove(game, e, x, y);
+ }
+ game.events.mousedown(game, e, x, y);
+ }
+ }
+
+ function _handle_touchend(game, e) {
+ if (!game.playing && game.ready_to_go) {
+ /* Click to start */
+ game.play();
+ return;
+ }
+
+ const rect = game.canvas.getBoundingClientRect();
+ let x = Math.round((e.clientX - rect.left) / rect.width * game.screen_w) - 1;
+ let y = Math.round((e.clientY - rect.top) / rect.height * game.screen_h) - 1;
+ if (game.events.touchend) {
+ game.events.touchend(game, e, x, y);
+ } else if (game.events.mouseup) {
+ if (game.events.mousemove) {
+ game.events.mousemove(game, e, x, y);
+ }
+ game.events.mouseup(game, e, x, y);
+ }
+ }
+
+ function _handle_touchmove(game, e) {
+ if (!game.playing) return;
+
+ if (e.touches) e = e.touches[0];
+
+ const rect = game.canvas.getBoundingClientRect();
+ let x = Math.round((e.clientX - rect.left) / rect.width * game.screen_w) - 1;
+ let y = Math.round((e.clientY - rect.top) / rect.height * game.screen_h) - 1;
+ if (game.events.touchmove) {
+ game.events.touchmove(game, e, x, y);
+ } else if (game.events.mousemove) {
+ game.events.mousemove(game, e, x, y);
+ }
+ }
+
/* ---- Drawing stuff ---- */
function _draw(game) {