From b549e39a09c87907e7e5e6ff65c0d19455d2ed3a Mon Sep 17 00:00:00 2001 From: Loris Cro Date: Sat, 8 Jan 2022 20:50:04 +0100 Subject: [PATCH] added touch input scheme for ios Probably not the best possible way of implementing touch input support, but it does make the game playable on mobile. The input scheme is adaptive with regard to the direction that pacman is going. For example, going left creates the following touch input grid: __________ | | | | U | | |______| R | | | | | D | | |______|___| Similarly every other direction produces a corresponding "adaptive" grid. Tested it briefly with a mouse and seemed reasonable, testing on a real device might be warranted. --- src/pacman.zig | 76 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 73 insertions(+), 3 deletions(-) diff --git a/src/pacman.zig b/src/pacman.zig index 6aea98e..386c5ed 100644 --- a/src/pacman.zig +++ b/src/pacman.zig @@ -2819,9 +2819,79 @@ export fn input(ev: ?*const sapp.Event) void { else => {} } } - } - else if ((event.type == .TOUCHES_BEGAN) or (event.type == .TOUCHES_ENDED)) { - state.input.anykey = event.type == .TOUCHES_BEGAN; + } else if ((event.type == .TOUCHES_BEGAN) or (event.type == .TOUCHES_ENDED)) { + const tp = event.touches[0]; + const key_pressed = event.type == .TOUCHES_BEGAN; + if (state.input.enabled) { + state.input.anykey = key_pressed; + state.input.up = false; + state.input.down = false; + state.input.left = false; + state.input.right = false; + + if (!key_pressed) return; + + const vertical_half = sapp.heightf() / 2.0; + const horizontal_half = sapp.widthf() / 2.0; + + const vertical_third = sapp.heightf() / 3.0; + const horizontal_third = sapp.widthf() / 3.0; + + switch (state.game.pacman.actor.dir) { + .Left => { + // Left 2/3rds of the screen is UP/DOWN + // Right 1/3 is RIGHT + if (tp.pos_x > horizontal_third * 2) { + state.input.right = true; + } else { + if (tp.pos_y < vertical_half) { + state.input.up = true; + } else { + state.input.down = true; + } + } + }, + .Right => { + // Right 2/3rds of the screen is UP/DOWN + // Left 1/3 is LEFT + if (tp.pos_x < horizontal_third) { + state.input.left = true; + } else { + if (tp.pos_y < vertical_half) { + state.input.up = true; + } else { + state.input.down = true; + } + } + }, + .Up => { + // Top 2/3rds of the screen is LEFT / RIGHT + // Bottoom 1/3 is DOWN + if (tp.pos_y > vertical_third * 2) { + state.input.down = true; + } else { + if (tp.pos_x < horizontal_half) { + state.input.left = true; + } else { + state.input.right = true; + } + } + }, + .Down => { + // Bottom 2/3rds of the screen is LEFT / RIGHT + // Top 1/3 is UP + if (tp.pos_y < vertical_third) { + state.input.up = true; + } else { + if (tp.pos_x < horizontal_half) { + state.input.left = true; + } else { + state.input.right = true; + } + } + }, + } + } } }