From a013d70115171764ce0941fbbe18a5447447a7eb Mon Sep 17 00:00:00 2001 From: Aki Date: Thu, 6 Apr 2017 19:59:51 +0200 Subject: Settings moved to /not/, cleaned-up a bit --- main.lua | 2 +- not/Settings.lua | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ settings.lua | 66 --------------------------------------------------- 3 files changed, 73 insertions(+), 67 deletions(-) create mode 100644 not/Settings.lua delete mode 100644 settings.lua diff --git a/main.lua b/main.lua index eed787f..7e1232e 100644 --- a/main.lua +++ b/main.lua @@ -28,7 +28,7 @@ require "not.World" require "camera" require "not.Menu" require "controller" -require "settings" +require "not.Settings" -- Temporary debug debug = false diff --git a/not/Settings.lua b/not/Settings.lua new file mode 100644 index 0000000..e3316f9 --- /dev/null +++ b/not/Settings.lua @@ -0,0 +1,72 @@ +--- `Settings` +-- Stores, loads, saves and changes game settings including Controller sets. +Settings = { + current = {} +} + +function Settings.load() + if Controller then + if not love.filesystem.exists("settings") then + local def = love.filesystem.newFile("settings.default") + local new = love.filesystem.newFile("settings") + new:open("w") def:open("r") + new:write(def:read()) + new:close() def:close() + end + local getSettings = love.filesystem.load("settings") + Settings.current = getSettings() + Controller.reset() + local joysticksList = love.joystick.getJoysticks() -- local list for editing + for _,set in pairs(Settings.current) do + local isJoystick = set[7] + local joystick + if isJoystick then + -- take and remove first joystick from list + joystick = joysticksList[1] + table.remove(joysticksList, 1) + end + if not isJoystick or joystick then + Controller.registerSet(set[1], set[2], set[3], set[4], set[5], set[6], joystick) + end + end + end +end + +function Settings.save() + local new = love.filesystem.newFile("settings") + local sets = Settings.current + local string = "return {\n" + for i,set in pairs(sets) do + string = string .. "\t{" + for j,word in pairs(set) do + if j ~= 7 then + string = string .. "\"" .. word .. "\", " + else + if word then + string = string .. "true" + else + string = string .. "false" + end + end + end + string = string .. "},\n" + end + string = string .. "}" + new:open("w") + new:write(string) + new:close() +end + +function Settings.change(n, left, right, up, down, attack, jump, joystick) + local bool + if joystick then + bool = true + else + bool = false + end + -- Save current settings + Settings.current[n] = {left, right, up, down, attack, jump, bool} + Settings.save() + -- Load settings + Settings.load() +end diff --git a/settings.lua b/settings.lua deleted file mode 100644 index e73b997..0000000 --- a/settings.lua +++ /dev/null @@ -1,66 +0,0 @@ -Settings = {} -Settings.current = {} -function Settings.load() - if Controller then - if not love.filesystem.exists("settings") then - local def = love.filesystem.newFile("settings.default") - local new = love.filesystem.newFile("settings") - new:open("w") def:open("r") - new:write(def:read()) - new:close() def:close() - end - local getSettings = love.filesystem.load("settings") - Settings.current = getSettings() - Controller.reset() - local joysticksList = love.joystick.getJoysticks() -- local list for editing - for _,set in pairs(Settings.current) do - local isJoystick = set[7] - local joystick - if isJoystick then - -- take and remove first joystick from list - joystick = joysticksList[1] - table.remove(joysticksList, 1) - end - if not isJoystick or joystick then - Controller.registerSet(set[1], set[2], set[3], set[4], set[5], set[6], joystick) - end - end - end -end -function Settings.save() - local new = love.filesystem.newFile("settings") - local sets = Settings.current - local string = "return {\n" - for i,set in pairs(sets) do - string = string .. "\t{" - for j,word in pairs(set) do - if j ~= 7 then - string = string .. "\"" .. word .. "\", " - else - if word then - string = string .. "true" - else - string = string .. "false" - end - end - end - string = string .. "},\n" - end - string = string .. "}" - new:open("w") - new:write(string) - new:close() -end -function Settings.change(n, left, right, up, down, attack, jump, joystick) - local bool - if joystick then - bool = true - else - bool = false - end - -- Save current settings - Settings.current[n] = {left, right, up, down, attack, jump, bool} - Settings.save() - -- Load settings - Settings.load() -end \ No newline at end of file -- cgit v1.1