From 0a82411c214a72e4103d1d24bb4e3f58cd387893 Mon Sep 17 00:00:00 2001 From: Simon Date: Mon, 22 Oct 2018 21:52:07 +0200 Subject: [PATCH] Create JoypadIntersection.lua Adds a small script which, when run, only allows input to P1's controller if both P1 and P2 holds down the specific button simultaneously. Also useful since it demonstrates joypad manipulation. --- Assets/Lua/JoypadIntersection.lua | 57 +++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 Assets/Lua/JoypadIntersection.lua diff --git a/Assets/Lua/JoypadIntersection.lua b/Assets/Lua/JoypadIntersection.lua new file mode 100644 index 0000000000..31933298a8 --- /dev/null +++ b/Assets/Lua/JoypadIntersection.lua @@ -0,0 +1,57 @@ +--------------------------------------------------------- +-- Small script for only allowing input on P1 controller +-- if both P1 and P2 holds down a specific input +-- +-- Note that this script only works on systems which +-- has two or more joypads (such as NES) and not on +-- systems with just one joypad (such as Gameboy) +-- +-- Author: Gikkman +--------------------------------------------------------- + +-- Pre-made array for resetting the P1 joypad +local reset = joypad.get(1) +for k,v in pairs(reset) do + reset[k] = '' +end + +event.onframestart( function() + local p1 = joypad.get(1) + local p2 = joypad.get(2) + local consolidated = intersection(p1, p2) + + gui.drawText(0,10, 'P1: ' .. dump(p1)) + gui.drawText(0,25, 'P2: ' .. dump(p2)) + + joypad.set(consolidated, 1) +end ) + +event.onframeend( function() + joypad.set(reset, 1) +end ) + +-- Get intersection of P1 and P2 joypads +function intersection(p1, p2) + local ret = {} + for k,v in pairs(p1) do + ret[k] = p1[k] and p2[k] + end + return ret +end + +-- Print all pressed buttons +function dump(o) + local s = '' + for k,v in pairs(o) do + if v then s = s .. tostring(k) .. ' ' end + end + return s +end + + +-------------------------------------- +-- Main loop -- +-------------------------------------- +while true do + emu.frameadvance() +end