big-refactor #106

Merged
kazhnuz merged 24 commits from big-refactor into master 2021-05-07 19:30:53 +02:00
7 changed files with 3 additions and 171 deletions
Showing only changes of commit 8eb28383ab - Show all commits

View file

@ -1,145 +0,0 @@
-- game :: The main game subsystem. Basically a big object that handle all the
-- game-related data like characters, monsters, etc. While the core aim to be
-- reusable at will, the game is specifically made for the current game.
-- It's also what handle the savedata for games
--[[
Copyright © 2019 Kazhnuz
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
]]
local cwd = (...):gsub('%.init$', '') .. "."
local cwd2 = (...):gsub('%.gamesystem.init$', '') .. "."
local GameSystem = Object:extend()
local binser = require(cwd2 .. "libs.binser")
local DEFAULT_SAVENUMBER = 3
function GameSystem:new()
self.currentSlot = -1
self.submodules = {}
self.playtime = 0
self:register()
end
function GameSystem:register()
core:registerGameSystem(self)
end
function GameSystem:registerSubmodules(submodule)
local name = submodule.name
self.submodules[name] = submodule
end
-- UPDATE FUNCTIONS
-- Update every submodules
function GameSystem:update(dt)
self.playtime = self.playtime + dt
for k, submodule in pairs(self.submodules) do
submodule:update(dt)
end
end
-- DATA MANAGEMENT FUNCTIONS
-- Get and set data in the gamesystem object
function GameSystem:setData(data)
local data = data
self.playtime = data.playtime
for k, submodule in pairs(self.submodules) do
submodule:setData(data[k])
end
end
function GameSystem:getData()
local data = {}
data.playtime = self.playtime
for k, submodule in pairs(self.submodules) do
data[k] = submodule:getData()
end
return data
end
-- SAVE MANAGEMENT FUNCTIONS
-- Get and set data in the gamesystem object
function GameSystem:getSaveNumber()
return DEFAULT_SAVENUMBER
end
function GameSystem:resetSave(saveid)
if utils.filesystem.exists("save" .. saveid .. ".save") then
love.filesystem.remove( "save" .. saveid .. ".save" )
end
end
function GameSystem:resetAllSaves()
for i=1, self:getSaveNumber() do
self:resetSave(i)
end
end
function GameSystem:getSavePath(saveid, absolute)
local saveDir = ""
if (absolute) then
saveDir = love.filesystem.getSaveDirectory() .. "/"
if not utils.filesystem.exists(saveDir) then
love.filesystem.createDirectory( "" )
end
end
local filepath = saveDir .. self:getSaveName(saveid)
return filepath
end
function GameSystem:getSaveName(saveid)
return "save" .. saveid .. ".save"
end
function GameSystem:saveFileExist(saveid)
return utils.filesystem.exists(self:getSaveName(saveid))
end
function GameSystem:read(saveid)
self.currentSlot = saveid or self.currentSlot
if (self.currentSlot > 0) then
local savepath = self:getSavePath(self.currentSlot, true)
if self:saveFileExist(self.currentSlot) then
local datas = binser.readFile(savepath)
self:setData(datas[1])
end
end
end
function GameSystem:write()
if (self.currentSlot > 0) then
local data = self:getData()
local savepath = self:getSavePath(self.currentSlot, true)
binser.writeFile(savepath, data)
end
end
return GameSystem

View file

@ -1,23 +0,0 @@
local SubModule = Object:extend()
function SubModule:new(game, name)
self.name = name or error("SUBMODULE must have a name")
self.game = game
self.data = {}
self:register()
end
function SubModule:register()
self.game:registerSubmodules(self)
end
function SubModule:getData()
return self.data
end
function SubModule:setData(data)
self.data = data
end
return SubModule

View file

@ -25,7 +25,7 @@
local OptionsManager = Object:extend() local OptionsManager = Object:extend()
local cwd = (...):gsub('%.options$', '') .. "." local cwd = (...):gsub('%.options$', '') .. "."
local binser = require(cwd .. "modules.gamesystem.libs.binser") local binser = require(cwd .. "libs.binser")
local TRANSLATION_PATH = "datas/languages/" local TRANSLATION_PATH = "datas/languages/"

View file

@ -31,7 +31,7 @@ local Loot = require "game.loot"
local CBSCore = require "game.battle" local CBSCore = require "game.battle"
local Difficulty = require "game.difficulty" local Difficulty = require "game.difficulty"
local binser = require "birb.modules.gamesystem.libs.binser" local binser = require "birb.libs.binser"
local startdata = require "datas.gamedata.startdata" local startdata = require "datas.gamedata.startdata"

View file

@ -1,6 +1,6 @@
local TweenManager = Object:extend() local TweenManager = Object:extend()
local tween = require "game.modules.tweenmanager.libs.tween" local tween = require "birb.libs.tween"
local Timer = require "birb.modules.world.actors.utils.timer" local Timer = require "birb.modules.world.actors.utils.timer"
function TweenManager:new(subject) function TweenManager:new(subject)