From: cassowarii Date: Sun, 6 Oct 2024 22:45:24 +0000 (-0700) Subject: replace with the version that has touch support, and fix frog sfx bug X-Git-Url: https://git.cassowary.me/gitweb.cgi?a=commitdiff_plain;h=8447d5f19374a4297606f7bf838f3847a7cefafc;p=ld56.git replace with the version that has touch support, and fix frog sfx bug --- diff --git a/game.js b/game.js index c43d87e..4a193d9 100644 --- a/game.js +++ b/game.js @@ -104,7 +104,7 @@ zb.ready(function() { volume: 0.95, }, hop: { - path: 'sfx/win.wav', + path: 'sfx/hop.wav', volume: 0.95, }, }); diff --git a/zucchinibread.js b/zucchinibread.js index f7b8ed3..d3c820a 100644 --- a/zucchinibread.js +++ b/zucchinibread.js @@ -351,6 +351,27 @@ let zb = (function() { } } + 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); @@ -500,8 +521,8 @@ let zb = (function() { 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); } @@ -537,8 +558,8 @@ let zb = (function() { } 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); } @@ -546,13 +567,68 @@ let zb = (function() { 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) { @@ -779,7 +855,7 @@ let zb = (function() { let offset = game.transition.timer / game.transition.end_time * game.screen_h; game.ctx.global.drawImage(game.ctx.copy.canvas, 0, -offset); - game.ctx.global.drawImage(game.ctx.draw.canvas, 0, game.screen_h - offset); + game.ctx.global.drawImage(ctx.canvas, 0, game.screen_h - offset); } else if (game.transition.type == TransitionType.SLIDE_UP) { let offset = game.transition.timer / game.transition.end_time * game.screen_h;