From f892b1ce4eecfc61110b26f150e961ec178c2f49 Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Wed, 10 Aug 2022 19:17:24 +0200 Subject: [PATCH] fix: add gamesystem as a top-level element --- birb/gamesystem/init.lua | 102 +++++++++++++++++++++++++++++++++++++++ examples/game/init.lua | 2 +- 2 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 birb/gamesystem/init.lua diff --git a/birb/gamesystem/init.lua b/birb/gamesystem/init.lua new file mode 100644 index 0000000..aebf3dc --- /dev/null +++ b/birb/gamesystem/init.lua @@ -0,0 +1,102 @@ +-- GameSystem :: The main GameSystem subsystem. Basically a big object that handle all the +-- GameSystem-related data like characters, monsters, etc. While the core aim to be +-- reusable at will, the GameSystem is specifically made for the current GameSystem. + +--[[ + 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 Serializer = require "birb.classes.serializable.serializer" +local GameSystem = Serializer:extend() + +local VAR_TO_SERIALIZE = { + "gametime", + "destroyedGizmo", + "variables", + "flags" +} + +function GameSystem:new() + self.slot = -1 + self.slotNumber = 3 + self.version = core.conf.gameversion or "N/A" + self:reset() + GameSystem.super.new(self, VAR_TO_SERIALIZE) +end + +function GameSystem:reset() + self.gametime = 0 + + self.flags = {} + self.destroyedGizmo = {} + self.variables = {} + self.mapName = "" +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()) + 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 \ No newline at end of file diff --git a/examples/game/init.lua b/examples/game/init.lua index 27835df..aba2b14 100644 --- a/examples/game/init.lua +++ b/examples/game/init.lua @@ -1,4 +1,4 @@ -local GameSystem = require "birb.modules.gamesystem" +local GameSystem = require "birb.gamesystem" local Game = GameSystem:extend() function Game:new()