diff options
author | Aki <nthirtyone@gmail.com> | 2016-08-14 22:18:43 +0200 |
---|---|---|
committer | Aki <nthirtyone@gmail.com> | 2016-08-14 22:18:43 +0200 |
commit | 3038bd171700d91e56a4050943925a02f5b1eda6 (patch) | |
tree | 4ebd70c7b477cf249754bb9ee31d17e35b879916 | |
parent | 16da6eddf1f7c5a945d7a30d760979f322dde5d9 (diff) | |
download | roflnauts-3038bd171700d91e56a4050943925a02f5b1eda6.zip roflnauts-3038bd171700d91e56a4050943925a02f5b1eda6.tar.gz roflnauts-3038bd171700d91e56a4050943925a02f5b1eda6.tar.bz2 |
Huge redesign
-rw-r--r-- | controller.lua | 158 | ||||
-rw-r--r-- | main.lua | 101 |
2 files changed, 111 insertions, 148 deletions
diff --git a/controller.lua b/controller.lua index c52b4a8..88751a2 100644 --- a/controller.lua +++ b/controller.lua @@ -1,106 +1,106 @@ -- `Controller` --- Used to manage controls +-- Module to manage player input. +-- It uses `love.keypressed`, `love.keyreleased`, `love.gamepadreleased`, `love.gamepadpressed`, `love.joystickadded`, so be sure not to use them by yourself. +-- Rather than that use functions provided by this module: `Controller.controlpressed` and `Controller.controlreleased`. +-- For information on additional functions, look below. --- Metashit +-- Metatable Controller = { - joystick = nil, - left = "left", - right = "right", - up = "up", - down = "down", - attack = "return", -- accept - jump = "rshift", -- cancel - parent = nil + sets = {} } --- Constructor --- joystick, left, right, up, down, attack, jump -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 +-- Declared to avoid calling nil. Be sure to define yours after this line is performed. +function Controller.controlpressed(set, action, key) end +function Controller.controlreleased(set, action, key) 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" +-- Create new controls set. +function Controller.registerSet(left, right, up, down, attack, jump, joystick) + local set = {} + set.left = left or "left" + set.right = right or "right" + set.up = up or "up" + set.down = down or "down" + set.attack = attack or "return" + set.jump = jump or "rshift" + table.insert(Controller.sets, set) + return set end -function Controller:setParent(parent) - self.parent = parent or nil +-- Tests all sets if they have control assigned to given key and joystick. +function Controller.testSets(key, joystick) + for i,set in pairs(Controller.sets) do + local action = Controller.testControl(set, key, joystick) + if action ~= nil then + return set, action, key + end + end + return nil, nil, key 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" +-- Tests given set if it has controll assigned to given key and joystick. +function Controller.testControl(set, key, joystick) + -- First test if it is joystick and if it is correct one + if joystick == set.joystick then + if control == set.left then + return "left" + elseif control == set.right then + return "right" + elseif control == set.up then + return "up" + elseif control == set.down then + return "down" + elseif control == set.attack then + return "attack" + elseif control == set.jump then + return "jump" + else + return nil + end else return nil end end --- Gamepad -function Controller:gamepadpressed(joystick, button) - if self.parent ~= nil and self.joystick == joystick then - local control = self:testControl(button) - if control ~= nil then - self.parent:controllerPressed(control, self) - end - end +-- Callbacks from LÖVE2D +-- Create new sets when new joystick is added +function Controller.joystickadded(joystick) + Controller.registerSet("dpleft", "dpright", "dpup", "dpdown", "a", "b", joystick) end -function Controller:gamepadreleased(joystick, button) - if self.parent ~= nil and self.joystick == joystick then - local control = self:testControl(button) - if control ~= nil then - self.parent:controllerReleased(control, self) - end +-- Gamepad input callbacks +function Controller.gamepadpressed(joystick, button) + print(button, "pressed") + for _,controller in pairs(Controllers) do + controller:gamepadpressed(joystick, button) end end - --- Keyboard -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, self) - end +function Controller.gamepadreleased(joystick, button) + print(button, "released") + for _,controller in pairs(Controllers) do + controller:gamepadreleased(joystick, button) 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, self) +-- Keyboard input callbacks +function Controller.keypressed(key) + print(key, "pressed") + for _,controller in pairs(Controllers) do + controller:keypressed(key) + end + + if key == "f6" and debug then + local map = Scene:getMapName() + local nauts = {} + for _,naut in pairs(Scene:getNautsAll()) do + table.insert(nauts, {naut.name, naut.controller}) end + local new = World:new(map, nauts) + changeScene(new) end end - --- isDown -function Controller:isDown(control) - if self.joystick == nil then - return love.keyboard.isDown(self[control]) - else - return self.joystick:isGamepadDown(self[control]) +function Controller.keyreleased(key) + print(key, "released") + for _,controller in pairs(Controllers) do + controller:keyreleased(key) end end
\ No newline at end of file @@ -43,90 +43,28 @@ require "music" -- Temporary debug debug = false +-- LÖVE2D callbacks -- Load function love.load() -- Graphics love.graphics.setBackgroundColor(90, 90, 90) love.graphics.setDefaultFilter("nearest", "nearest") - -- Font Font = love.graphics.newImageFont("assets/font-normal.png", " 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.,:;-_/\\!@#$%^&*?=+~`|'\"()[]{}<>", -1) Bold = love.graphics.newImageFont("assets/font-big.png", " 0123456789AEFILNORSTUW", -2) Font:setLineHeight(9/16) - love.graphics.setFont(Font) - - -- Menu bijaczes - m = Menu:new() - - -- Controllers - Controllers = {} - table.insert(Controllers, Controller:new()) - table.insert(Controllers, Controller:new(nil, "a", "d", "w", "s", "g", "h")) - m:assignController(Controllers[1]) - m:assignController(Controllers[2]) - - -- Scene - changeScene(m) -end - --- Gamepad -function love.joystickadded(joystick) + love.graphics.setFont(Font) + -- Controller love.joystick.loadGamepadMappings("gamecontrollerdb.txt") - table.insert(Controllers, Controller:new(joystick, "dpleft", "dpright", "dpup", "dpdown", "a", "b")) - m:assignController(Controllers[#Controllers]) -end - -function love.gamepadpressed(joystick, button) - print(button, "pressed") - for _,controller in pairs(Controllers) do - controller:gamepadpressed(joystick, button) - end -end - -function love.gamepadreleased(joystick, button) - print(button, "released") - for _,controller in pairs(Controllers) do - controller:gamepadreleased(joystick, button) - end + Controller.registerSet("left", "right", "up", "down", "return", "rshift") + Controller.registerSet("a", "d", "w", "s", "g", "h") + -- Scene + Scene = Menu:new() end - -- Update function love.update(dt) Scene:update(dt) end - --- KeyPressed -function love.keypressed(key) - -- Controllers - for _,controller in pairs(Controllers) do - controller:keypressed(key) - end - -- Misc global input - if key == "f5" then - debug = not debug - end - if key == "escape" or key == "f1" then - love.event.quit() - end - if key == "f6" and debug then - local map = Scene:getMapName() - local nauts = {} - for _,naut in pairs(Scene:getNautsAll()) do - table.insert(nauts, {naut.name, naut.controller}) - end - local new = World:new(map, nauts) - changeScene(new) - end -end - --- KeyReleased -function love.keyreleased(key) - -- Controllers - for _,controller in pairs(Controllers) do - controller:keyreleased(key) - end -end - -- Draw function love.draw() Scene:draw() @@ -138,3 +76,28 @@ function love.draw() love.graphics.print("Current FPS: "..tostring(love.timer.getFPS()), 10, 10+9*scale, 0, scale, scale) end end +-- Pass input to Controller +function love.joystickadded(joystick) Controller.joystickadded(joystick) end +function love.gamepadpressed(joystick, button) Controller.gamepadpressed(joystick, button) end +function love.gamepadreleased(joystick, button) Controller.gamepadreleased(joystick, button) end +function love.keypressed(key) Controller.keypressed(key) end +function love.keyreleased(key) Controller.keyreleased(key) end + +-- Controller callbacks +function Controller.controlpressed(set, action, key) + -- pass to current Scene + Scene:controlpressed(set, action, key) + -- global quit + if key == "escape" or key == "f1" then + love.event.quit() + end + if key == "f5" then + debug = not debug + end +end +function Controller.controlreleased(set, action, key) + -- pass to current Scene + Scene:controlreleased(set, action, key) +end + + |