2019-01-28 08:35:10 +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-05-08 12:54:07 +02:00
|
|
|
local Serializer = require "birb.classes.serializable.serializer"
|
|
|
|
|
|
|
|
local Game = Serializer:extend()
|
2019-03-10 13:41:37 +01:00
|
|
|
local Characters = require "game.characters"
|
2019-08-15 09:35:37 +02:00
|
|
|
local Ennemies = require "game.ennemies"
|
2020-08-02 15:56:36 +02:00
|
|
|
local Loot = require "game.loot"
|
2021-04-02 22:23:39 +02:00
|
|
|
local CBSCore = require "game.battle"
|
2021-04-18 19:09:01 +02:00
|
|
|
local Difficulty = require "game.difficulty"
|
2021-05-08 12:54:07 +02:00
|
|
|
local Metadata = require "game.metadata"
|
2019-01-28 08:35:10 +01:00
|
|
|
|
2021-04-02 23:26:22 +02:00
|
|
|
local startdata = require "datas.gamedata.startdata"
|
|
|
|
|
2021-08-15 15:36:26 +02:00
|
|
|
Game.utils = require "game.utils"
|
2019-07-27 15:43:27 +02:00
|
|
|
Game.gui = require "game.modules.gui"
|
2019-07-26 20:22:18 +02:00
|
|
|
|
2021-05-08 12:54:07 +02:00
|
|
|
local VAR_TO_SERIALIZE = {
|
|
|
|
"gametime",
|
|
|
|
"destroyedGizmo",
|
|
|
|
"variables",
|
|
|
|
"flags",
|
|
|
|
"position",
|
|
|
|
"actions",
|
|
|
|
"characters",
|
|
|
|
"loot",
|
|
|
|
"difficulty"
|
|
|
|
}
|
|
|
|
|
2019-01-28 08:35:10 +01:00
|
|
|
function Game:new()
|
|
|
|
self.slot = -1
|
2020-08-02 09:59:47 +02:00
|
|
|
self.slotNumber = 3
|
2021-05-05 12:09:30 +02:00
|
|
|
self.version = core.conf.gameversion or "N/A"
|
2021-05-08 12:54:07 +02:00
|
|
|
self:reset()
|
|
|
|
self.metadata = Metadata(self)
|
|
|
|
Game.super.new(self, VAR_TO_SERIALIZE)
|
2019-01-28 08:35:10 +01:00
|
|
|
end
|
|
|
|
|
2021-04-02 23:26:22 +02:00
|
|
|
function Game:initPosition()
|
|
|
|
self.position = {}
|
|
|
|
self.position.x = startdata.position.x
|
|
|
|
self.position.y = startdata.position.y
|
|
|
|
self.position.area = startdata.position.area
|
|
|
|
end
|
|
|
|
|
2021-04-05 09:33:52 +02:00
|
|
|
function Game:getMetadataFile(absolute)
|
|
|
|
local dir = ""
|
|
|
|
if absolute then
|
|
|
|
dir = love.filesystem.getSaveDirectory() .. "/"
|
2021-04-05 12:07:21 +02:00
|
|
|
if not utils.filesystem.exists(dir) then
|
2021-04-05 09:33:52 +02:00
|
|
|
love.filesystem.createDirectory( "" )
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local filepath = dir .. "metadata.save"
|
|
|
|
|
|
|
|
return filepath
|
|
|
|
end
|
|
|
|
|
|
|
|
function Game:getMetadata()
|
2021-05-08 12:54:07 +02:00
|
|
|
return self.metadata:get()
|
2021-04-05 09:33:52 +02:00
|
|
|
end
|
|
|
|
|
2021-05-08 12:54:07 +02:00
|
|
|
function Game:reset()
|
2021-04-18 15:38:19 +02:00
|
|
|
self.gametime = 0
|
|
|
|
|
|
|
|
self.characters = Characters(self)
|
|
|
|
self.ennemies = Ennemies(self)
|
|
|
|
self.loot = Loot(self)
|
|
|
|
self.cbs = CBSCore(self)
|
2021-04-18 19:09:01 +02:00
|
|
|
self.difficulty = Difficulty(self)
|
2021-04-18 15:38:19 +02:00
|
|
|
|
|
|
|
self:initPosition()
|
|
|
|
|
|
|
|
self.flags = {}
|
|
|
|
self.destroyedGizmo = {}
|
|
|
|
self.variables = {}
|
|
|
|
self.completion = 0
|
|
|
|
self.mapName = ""
|
|
|
|
self.actions = startdata.actions
|
|
|
|
end
|
|
|
|
|
2021-04-18 16:36:40 +02:00
|
|
|
function Game:reload()
|
|
|
|
self:read(self.slot)
|
|
|
|
end
|
|
|
|
|
2019-01-28 08:35:10 +01:00
|
|
|
function Game:read(save_id)
|
|
|
|
self.slot = save_id
|
|
|
|
if (self.slot > 0) then
|
2021-05-08 12:54:07 +02:00
|
|
|
self:deserialize(self:getSaveName())
|
2019-01-28 08:35:10 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-04-18 15:38:19 +02:00
|
|
|
function Game:deleteCurrentSave()
|
|
|
|
if (self.slot > 0) then
|
2021-05-08 12:54:07 +02:00
|
|
|
self:delete(self:getSaveName())
|
|
|
|
self.metadata:remove(self.slot)
|
2021-04-18 15:38:19 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-04-05 09:33:52 +02:00
|
|
|
function Game:write()
|
2019-01-28 08:35:10 +01:00
|
|
|
if (self.slot > 0) then
|
2021-05-08 12:54:07 +02:00
|
|
|
self:serialize(self:getSaveName())
|
|
|
|
self.metadata:update()
|
2019-01-28 08:35:10 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-05-08 12:54:07 +02:00
|
|
|
function Game:getSaveName(saveslot)
|
|
|
|
local saveslot = saveslot or self.slot
|
|
|
|
return "save" .. saveslot .. ".save"
|
2019-01-28 08:35:10 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function Game:resetSaves()
|
2020-08-02 09:59:47 +02:00
|
|
|
for i=1, self.slotNumber do
|
2021-05-08 12:54:07 +02:00
|
|
|
self:delete(self:getSaveName(i))
|
2019-01-28 08:35:10 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function Game:update(dt)
|
|
|
|
self.gametime = self.gametime + dt
|
|
|
|
end
|
|
|
|
|
|
|
|
function Game:getTime()
|
2021-04-05 09:48:59 +02:00
|
|
|
return utils.time.getFields(self.gametime)
|
|
|
|
end
|
2019-01-28 08:35:10 +01:00
|
|
|
|
2021-04-05 09:48:59 +02:00
|
|
|
function Game:getTimeString()
|
|
|
|
return utils.time.toString(self.gametime)
|
2019-01-28 08:35:10 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function Game:printTime()
|
2021-04-11 15:18:47 +02:00
|
|
|
core.debug:print(self:getTimeString())
|
2019-01-28 08:35:10 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
return Game
|