From: cassowarii <2374677+cassowarii@users.noreply.github.com> Date: Mon, 9 Sep 2024 03:31:53 +0000 (-0700) Subject: add touch support; also a couple bugfixes that didn't get committed X-Git-Url: https://www.git.cassowary.me/gitweb.cgi?a=commitdiff_plain;ds=sidebyside;p=ld54.git add touch support; also a couple bugfixes that didn't get committed --- diff --git a/game.js b/game.js index f1e5592..5633b98 100644 --- a/game.js +++ b/game.js @@ -424,6 +424,10 @@ function do_update(delta) { update_hare_movement(delta); + if (game.state === State.STAND && !intitle && !inend) { + check_victory(); + } + if (game.state === State.WIN) { if (win_title_offset > 1) { win_title_offset /= 1.08; @@ -450,6 +454,10 @@ function update_hare_movement(delta) { delete h.target_y; } h.being_pushed = false; + + if (!h.anxiety) { + eat_grass(h.x, h.y); + } } if (path.length > 0) { @@ -583,7 +591,6 @@ function update_hare_anxiety(delta) { if (h.frame > 14) { h.frame = 0; h.timer = 0; - eat_grass(h.x, h.y); } } } @@ -1040,6 +1047,8 @@ function handle_mousemove(game, e, x, canvy) { mouse_y = y; for (let b of Object.values(game.buttons)) { + if (b.state == 2) continue; + if (mouse_x >= b.x && mouse_x < b.x + b.w && mouse_y >= b.y && mouse_y < b.y + b.h) { b.state = 1; } else { diff --git a/levels.js b/levels.js index 62c4ef8..53a931f 100644 --- a/levels.js +++ b/levels.js @@ -8,7 +8,7 @@ var levels={ 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: [], }, } diff --git a/zucchinibread.js b/zucchinibread.js index 4a48d7e..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) {