Created
January 9, 2015 21:49
-
-
Save pablomayobre/7b8765aa2e5f613d328a to your computer and use it in GitHub Desktop.
Trigger functions after a predefined sequence of keys have been pressed (LÖVE)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
------Copyright 2015 Pablo A. Mayobre------ | |
--Licensed under the terms of MIT Lincense-- | |
local seq = {} | |
local split = function (text,pat) | |
local t = {} | |
while true do | |
local pos1, pos2 = text:find(pat, 1, true) | |
if not pos1 then | |
t[#t + 1] = text | |
return t | |
end | |
t[#t + 1] = text:sub(1, pos1 - 1) | |
text = text:sub(pos2 + 1) | |
end | |
end | |
local unpack = unpack or table.unpack | |
seq.new = function (self,f,t) | |
local steps = {} | |
for k,v in ipairs(split(t, ",")) do | |
steps[k] = split(v,"+") | |
end | |
self.inst[#self.inst + 1] = { | |
f = f; | |
steps = steps; | |
current = 1; | |
} | |
end | |
seq.update = function (self) | |
for k,v in ipairs(self.inst) do | |
if love.keyboard.isDown(unpack(v.steps[v.current])) then | |
v.current = v.current + 1 | |
end | |
if v.current > #v.steps then | |
v.f() | |
self.inst[k] = nil | |
end | |
end | |
end | |
return seq |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Define a new sequence using
sequences:new()
as follows:You can also define key combinations that should be pressed at the same time as follows:
Or a combination of both things
Remember to call
sequences:update()
in youlove.update
callback