2019-02-03 19:40:28 +01:00
|
|
|
-- 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.
|
|
|
|
]]
|
|
|
|
|
2021-11-25 11:28:31 +01:00
|
|
|
local Serializer = require "birb.classes.serializable.serializer"
|
|
|
|
|
|
|
|
local Game = Serializer:extend()
|
2019-12-28 11:33:25 +01:00
|
|
|
local Characters = require "game.characters"
|
|
|
|
local Ennemies = require "game.ennemies"
|
2021-11-25 11:28:31 +01:00
|
|
|
local Loot = require "game.loot"
|
|
|
|
local CBSCore = require "game.battle"
|
|
|
|
local Difficulty = require "game.difficulty"
|
|
|
|
local Metadata = require "game.metadata"
|
2019-02-03 19:40:28 +01:00
|
|
|
|
2021-11-25 11:28:31 +01:00
|
|
|
local startdata = require "datas.gamedata.startdata"
|
2019-02-03 19:40:28 +01:00
|
|
|
|
2021-11-25 11:28:31 +01:00
|
|
|
Game.utils = require "game.utils"
|
2019-12-28 11:33:25 +01:00
|
|
|
Game.gui = require "game.modules.gui"
|
2019-02-03 19:40:28 +01:00
|
|
|
|
2021-11-25 11:28:31 +01:00
|
|
|
local VAR_TO_SERIALIZE = {
|
|
|
|
"gametime",
|
|
|
|
"destroyedGizmo",
|
|
|
|
"variables",
|
|
|
|
"flags",
|
|
|
|
"position",
|
|
|
|
"actions",
|
|
|
|
"characters",
|
|
|
|
"loot",
|
|
|
|
"difficulty"
|
|
|
|
}
|
|
|
|
|
2019-02-03 19:40:28 +01:00
|
|
|
function Game:new()
|
|
|
|
self.slot = -1
|
2021-11-25 11:28:31 +01:00
|
|
|
self.slotNumber = 3
|
|
|
|
self.version = core.conf.gameversion or "N/A"
|
|
|
|
self:reset()
|
|
|
|
self.metadata = Metadata(self)
|
|
|
|
Game.super.new(self, VAR_TO_SERIALIZE)
|
|
|
|
end
|
2019-02-04 08:36:29 +01:00
|
|
|
|
2021-11-25 11:28:31 +01:00
|
|
|
function Game:initPosition()
|
|
|
|
self.position = {}
|
|
|
|
self.position.x = startdata.position.x
|
|
|
|
self.position.y = startdata.position.y
|
|
|
|
self.position.area = startdata.position.area
|
2019-02-03 19:40:28 +01:00
|
|
|
end
|
|
|
|
|
2021-11-25 11:28:31 +01:00
|
|
|
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()
|
|
|
|
return self.metadata:get()
|
2019-02-03 19:40:28 +01:00
|
|
|
end
|
|
|
|
|
2021-11-25 11:28:31 +01:00
|
|
|
function Game:reset()
|
|
|
|
self.gametime = 0
|
|
|
|
|
|
|
|
self.characters = Characters(self)
|
|
|
|
self.ennemies = Ennemies(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
|
2019-02-03 19:40:28 +01:00
|
|
|
|
2021-11-25 11:28:31 +01:00
|
|
|
function Game:reload()
|
|
|
|
self:read(self.slot)
|
2019-02-03 19:40:28 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function Game:read(save_id)
|
|
|
|
self.slot = save_id
|
|
|
|
if (self.slot > 0) then
|
2021-11-25 11:28:31 +01:00
|
|
|
self:deserialize(self:getSaveName())
|
2019-02-03 19:40:28 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-11-25 11:28:31 +01:00
|
|
|
function Game:deleteCurrentSave()
|
2019-02-03 19:40:28 +01:00
|
|
|
if (self.slot > 0) then
|
2021-11-25 11:28:31 +01:00
|
|
|
self:delete(self:getSaveName())
|
|
|
|
self.metadata:remove(self.slot)
|
2019-02-03 19:40:28 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-11-25 11:28:31 +01:00
|
|
|
function Game:write()
|
|
|
|
if (self.slot > 0) then
|
|
|
|
self:serialize(self:getSaveName())
|
|
|
|
self.metadata:update()
|
2019-02-03 19:40:28 +01:00
|
|
|
end
|
2021-11-25 11:28:31 +01:00
|
|
|
end
|
2019-02-03 19:40:28 +01:00
|
|
|
|
2021-11-25 11:28:31 +01:00
|
|
|
function Game:getSaveName(saveslot)
|
|
|
|
local saveslot = saveslot or self.slot
|
|
|
|
return "save" .. saveslot .. ".save"
|
2019-02-03 19:40:28 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function Game:resetSaves()
|
2021-11-25 11:28:31 +01:00
|
|
|
for i=1, self.slotNumber do
|
|
|
|
self:delete(self:getSaveName(i))
|
2019-02-03 19:40:28 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function Game:update(dt)
|
|
|
|
self.gametime = self.gametime + dt
|
|
|
|
end
|
|
|
|
|
|
|
|
function Game:getTime()
|
2021-11-25 11:28:31 +01:00
|
|
|
return utils.time.getFields(self.gametime)
|
2019-02-03 19:40:28 +01:00
|
|
|
end
|
|
|
|
|
2021-11-25 11:28:31 +01:00
|
|
|
function Game:getTimeString()
|
|
|
|
return utils.time.toString(self.gametime)
|
2019-02-03 19:40:28 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function Game:printTime()
|
2021-11-25 11:28:31 +01:00
|
|
|
core.debug:print(self:getTimeString())
|
2019-02-03 19:40:28 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
return Game
|