diff --git a/birb/modules/gamesystem/init.lua b/birb/modules/gamesystem/init.lua new file mode 100644 index 0000000..c0ea266 --- /dev/null +++ b/birb/modules/gamesystem/init.lua @@ -0,0 +1,86 @@ +-- modules/gamesystem :: a simple save system, based on the Deserialization feature + +--[[ + Copyright © 2021 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 GameSystem = require("birb.classes.serializable.serializer"):extend() + +function GameSystem:new(varToSerialize, slotNumber) + self.slot = -1 + self.slotNumber = slotNumber or 1 + self.version = core.conf.gameversion or "N/A" + varToSerialize = varToSerialize or {} + GameSystem.super.new(self, varToSerialize) +end + +function GameSystem:reload() + self:read(self.slot) +end + +function GameSystem:read(save_id) + self.slot = save_id + if (self.slot > 0) then + self:deserialize(self:getSaveName()) + end +end + +function GameSystem:deleteCurrentSave() + if (self.slot > 0) then + self:delete(self:getSaveName()) + self.metadata:remove(self.slot) + end +end + +function GameSystem:write() + if (self.slot > 0) then + self:serialize(self:getSaveName()) + self.metadata:update() + end +end + +function GameSystem:getSaveName(saveslot) + local saveslot = saveslot or self.slot + return "save" .. saveslot .. ".save" +end + +function GameSystem:resetSaves() + for i = 1, self.slotNumber do + self:delete(self:getSaveName(i)) + end +end + +function GameSystem:update(dt) + self.gametime = self.gametime + dt +end + +function GameSystem:getTime() + return utils.time.getFields(self.gametime) +end + +function GameSystem:getTimeString() + return utils.time.toString(self.gametime) +end + +function GameSystem:printTime() + core.debug:print(self:getTimeString()) +end + +return GameSystem