summaryrefslogtreecommitdiffhomepage
path: root/controller.lua
diff options
context:
space:
mode:
authorAki <nthirtyone@gmail.com>2016-05-26 15:33:46 +0200
committerAki <nthirtyone@gmail.com>2016-05-26 15:33:46 +0200
commit2252cd12fdceb2799e38bc3cb375c3bbd9be5e7a (patch)
tree52f2e18d738dea5c3f220535981d350d83d54def /controller.lua
parenteb8c23fb8fff5c8faefa84e6f166bc0f3234a1ec (diff)
downloadroflnauts-2252cd12fdceb2799e38bc3cb375c3bbd9be5e7a.zip
roflnauts-2252cd12fdceb2799e38bc3cb375c3bbd9be5e7a.tar.gz
roflnauts-2252cd12fdceb2799e38bc3cb375c3bbd9be5e7a.tar.bz2
Controller changes
Diffstat (limited to 'controller.lua')
-rw-r--r--controller.lua85
1 files changed, 85 insertions, 0 deletions
diff --git a/controller.lua b/controller.lua
new file mode 100644
index 0000000..808d660
--- /dev/null
+++ b/controller.lua
@@ -0,0 +1,85 @@
+-- `Controller`
+-- Used to manage controls
+
+-- Metashit
+Controller = {
+ joystick = nil,
+ left = "left",
+ right = "right",
+ up = "up",
+ down = "down",
+ attack = "return", -- accept
+ jump = "rshift", -- cancel
+ parent = nil,
+ down = nil
+}
+
+-- Constructor
+function Controller:new(joystick, ...)
+ local o = {}
+ setmetatable(o, self)
+ self.__index = self
+ if joystick ~= nil then
+ o.joystick = joystick
+ end
+ o:setBindings(...)
+ return o
+end
+
+function Controller:setBindings(...)
+ local left, right, up, down, attack, jump = ...
+ self.left = left or "left"
+ self.right = right or "right"
+ self.up = up or "up"
+ self.down = down or "down"
+ self.attack = attack or "return"
+ self.jump = jump or "rshift"
+end
+
+function Controller:setParent(parent)
+ self.parent = parent or nil
+end
+
+function Controller:testControl(control)
+ if control == self.left then
+ return "left"
+ elseif control == self.right then
+ return "right"
+ elseif control == self.up then
+ return "up"
+ elseif control == self.down then
+ return "down"
+ elseif control == self.attack then
+ return "attack"
+ elseif control == self.jump then
+ return "jump"
+ else
+ return nil
+ end
+end
+
+function Controller:keypressed(key, scancode)
+ if self.parent ~= nil and self.joystick == nil then
+ local control = self:testControl(key)
+ if control ~= nil then
+ self.parent:controllerPressed(control)
+ end
+ end
+end
+
+function Controller:keyreleased(key, scancode)
+ if self.parent ~= nil and self.joystick == nil then
+ local control = self:testControl(key)
+ if control ~= nil then
+ self.parent:controllerReleased(control)
+ end
+ end
+end
+
+function Controller:isDown(control)
+ if self.joystick == nil then
+ return love.keyboard.isDown(self[control])
+ else
+ return self.joystick:isGamepadDown(self[control])
+ end
+end \ No newline at end of file