From e068acbc7b02aef5102cc812ed5add904f1b7c0d Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Wed, 24 Jul 2019 10:43:22 +0200 Subject: [PATCH] feat: add a basic gamesystem modules --- CHANGELOG.md | 4 + gamecore/init.lua | 8 + gamecore/modules/gamesystem/init.lua | 145 ++++++++++++++++++ .../{ => modules/gamesystem}/libs/binser.lua | 0 gamecore/modules/gamesystem/submodule.lua | 23 +++ gamecore/options.lua | 2 +- 6 files changed, 181 insertions(+), 1 deletion(-) create mode 100644 gamecore/modules/gamesystem/init.lua rename gamecore/{ => modules/gamesystem}/libs/binser.lua (100%) create mode 100644 gamecore/modules/gamesystem/submodule.lua diff --git a/CHANGELOG.md b/CHANGELOG.md index ddfb9f9..1f16796 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased +### Added + +- Add a gamesystem module + ### Changed - **world:** extract map module from the world module diff --git a/gamecore/init.lua b/gamecore/init.lua index 1831d01..67b5204 100644 --- a/gamecore/init.lua +++ b/gamecore/init.lua @@ -56,6 +56,10 @@ function CoreSystem:new() self.lang = Lang(self) end +function CoreSystem:registerGameSystem(gamesystem) + self.game = gamesystem +end + -- MOUSE FUNCTIONS -- get directly the mouse when needed @@ -88,6 +92,10 @@ function CoreSystem:update(dt) self.debug:update(dt) self.input:update(dt) + if (self.game ~= nil) then + self.game:update(dt) + end + self.scenemanager:update(dt) end diff --git a/gamecore/modules/gamesystem/init.lua b/gamecore/modules/gamesystem/init.lua new file mode 100644 index 0000000..9c33db9 --- /dev/null +++ b/gamecore/modules/gamesystem/init.lua @@ -0,0 +1,145 @@ +-- 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 love.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 love.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 love.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() + + savepath = self:getSavePath(self.currentSlot, true) + binser.writeFile(savepath, data) + end +end + +return GameSystem diff --git a/gamecore/libs/binser.lua b/gamecore/modules/gamesystem/libs/binser.lua similarity index 100% rename from gamecore/libs/binser.lua rename to gamecore/modules/gamesystem/libs/binser.lua diff --git a/gamecore/modules/gamesystem/submodule.lua b/gamecore/modules/gamesystem/submodule.lua new file mode 100644 index 0000000..5ea5486 --- /dev/null +++ b/gamecore/modules/gamesystem/submodule.lua @@ -0,0 +1,23 @@ +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 diff --git a/gamecore/options.lua b/gamecore/options.lua index 7c1c791..d1e3424 100644 --- a/gamecore/options.lua +++ b/gamecore/options.lua @@ -25,7 +25,7 @@ local OptionsManager = Object:extend() local cwd = (...):gsub('%.options$', '') .. "." -local binser = require(cwd .. "libs.binser") +local binser = require(cwd .. "modules.gamesystem.libs.binser") local TRANSLATION_PATH = "datas/languages/"