-- 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. --[[ 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 Game = Object:extend() local Characters = require "game.characters" local Ennemies = require "game.ennemies" local Skills = require "game.skills" local Loot = require "game.loot" local CBSCore = require "game.battle" local Difficulty = require "game.difficulty" local binser = require "birb.modules.gamesystem.libs.binser" local startdata = require "datas.gamedata.startdata" Game.utils = require "game.modules.utils" Game.gui = require "game.modules.gui" function Game:new() self.slot = -1 self.slotNumber = 3 self.version = "0.0.7" self:resetData() end function Game:initPosition() self.position = {} self.position.x = startdata.position.x self.position.y = startdata.position.y self.position.area = startdata.position.area end function Game:setData(data) local data = data self.gametime = data.gametime self.destroyedGizmo = data.destroyedGizmo self.variables = data.variables self.flags = data.flags self.position = data.position self.actions = data.actions self.characters:setData(data.characters) self.loot:setData(data.loot) self.difficulty:setData(data.difficulty) end function Game:getData() local data = {} data.gametime = self.gametime data.characters = self.characters:getData() data.loot = self.loot:getData() data.difficulty = self.difficulty:getData() data.flags = self.flags data.destroyedGizmo = self.destroyedGizmo data.variables = self.variables data.position = self.position data.actions = self.actions return data end function Game:getMetadataFile(absolute) local dir = "" if absolute then dir = love.filesystem.getSaveDirectory() .. "/" if not utils.filesystem.exists(dir) then love.filesystem.createDirectory( "" ) end end local filepath = dir .. "metadata.save" return filepath end function Game:getMetadata() local metadata = {} local filepath = self:getMetadataFile(true) if utils.filesystem.exists("metadata.save") then metadata = binser.readFile(filepath)[1] else metadata = self:newMetadata() end return metadata end function Game:newMetadata() local metadata = {} for i = 1, self.slotNumber, 1 do local newMetadata = {} newMetadata.exist = false newMetadata.completion = 0 newMetadata.gametime = 0 newMetadata.team = {} newMetadata.rings = 0 newMetadata.emeralds = 0 newMetadata.location = "" table.insert(metadata, newMetadata) end return metadata end function Game:writeMetadata(metadata) local filepath = self:getMetadataFile(true) binser.writeFile(filepath, metadata) end function Game:addtoMetadata() local metadata = self:getMetadata() metadata[self.slot].exist = true metadata[self.slot].completion = self.completion metadata[self.slot].gametime = self.gametime metadata[self.slot].team = self.characters.team metadata[self.slot].rings = self.loot.rings metadata[self.slot].emeralds = 0 metadata[self.slot].location = self.mapName self:writeMetadata(metadata) end function Game:resetData() self.gametime = 0 self.characters = Characters(self) self.ennemies = Ennemies(self) self.skills = Skills(self) self.loot = Loot(self) self.cbs = CBSCore(self) self.difficulty = Difficulty(self) self:initPosition() self.flags = {} self.destroyedGizmo = {} self.variables = {} self.completion = 0 self.mapName = "" self.actions = startdata.actions end function Game:removeFromMetadata() local metadata = self:getMetadata() metadata[self.slot].exist = false self:writeMetadata(metadata) end function Game:reload() self:read(self.slot) end function Game:read(save_id) self.slot = save_id if (self.slot > 0) then local filepath = self:getSaveFile(self.slot, true) if utils.filesystem.exists("save" .. self.slot .. ".save") then local loadedDatas = binser.readFile(filepath) self:setData(loadedDatas[1]) else self:resetData() end end end function Game:deleteCurrentSave() if (self.slot > 0) then local filepath = self:getSaveFile(self.slot, false) local success = love.filesystem.remove(filepath) print(success) self:removeFromMetadata() end end function Game:write() if (self.slot > 0) then local data = self:getData() local filepath = self:getSaveFile(self.slot, true) binser.writeFile(filepath, data) self:addtoMetadata() end end function Game:getSaveFile(saveslot, absolute) local dir = "" if absolute then dir = love.filesystem.getSaveDirectory() .. "/" if not utils.filesystem.exists(dir) then love.filesystem.createDirectory( "" ) end end local filepath = dir .. "save" .. saveslot .. ".save" return filepath end function Game:resetSaves() for i=1, self.slotNumber do local filepath = self:getSaveFile(i, true) if utils.filesystem.exists("save" .. i .. ".save") then love.filesystem.remove( "save" .. i .. ".save" ) end end end function Game:update(dt) self.gametime = self.gametime + dt end function Game:getTime() return utils.time.getFields(self.gametime) end function Game:getTimeString() return utils.time.toString(self.gametime) end function Game:printTime() core.debug:print(self:getTimeString()) end return Game