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.
|
|
|
|
]]
|
|
|
|
|
2019-03-10 13:41:37 +01:00
|
|
|
local Game = Object:extend()
|
|
|
|
local Characters = require "game.characters"
|
2019-08-15 09:35:37 +02:00
|
|
|
local Ennemies = require "game.ennemies"
|
2019-08-22 21:33:13 +02:00
|
|
|
local Skills = require "game.skills"
|
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"
|
2019-01-28 08:35:10 +01:00
|
|
|
|
2019-07-26 17:17:53 +02:00
|
|
|
local binser = require "core.modules.gamesystem.libs.binser"
|
2019-01-28 08:35:10 +01:00
|
|
|
|
2021-04-02 23:26:22 +02:00
|
|
|
local startdata = require "datas.gamedata.startdata"
|
|
|
|
|
2019-07-26 20:22:18 +02:00
|
|
|
Game.utils = require "game.modules.utils"
|
2019-07-27 15:43:27 +02:00
|
|
|
Game.gui = require "game.modules.gui"
|
2019-07-26 20:22:18 +02:00
|
|
|
|
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
|
2019-01-28 08:35:10 +01:00
|
|
|
self.gametime = 0
|
2019-03-10 13:41:37 +01:00
|
|
|
|
|
|
|
self.characters = Characters(self)
|
2019-08-15 09:35:37 +02:00
|
|
|
self.ennemies = Ennemies(self)
|
2019-08-22 21:33:13 +02:00
|
|
|
self.skills = Skills(self)
|
2020-08-02 15:56:36 +02:00
|
|
|
self.loot = Loot(self)
|
2021-04-02 22:23:39 +02:00
|
|
|
self.cbs = CBSCore(self)
|
2020-08-01 10:31:09 +02:00
|
|
|
|
2021-04-02 23:26:22 +02:00
|
|
|
self:initPosition()
|
|
|
|
|
2021-03-22 19:00:54 +01:00
|
|
|
self.flags = {}
|
|
|
|
self.destroyedGizmo = {}
|
|
|
|
self.variables = {}
|
2021-04-05 09:33:52 +02:00
|
|
|
self.completion = 0
|
|
|
|
self.mapName = ""
|
2021-04-10 17:59:01 +02:00
|
|
|
self.actions = startdata.actions
|
2021-03-22 19:00:54 +01:00
|
|
|
|
2020-08-01 10:31:09 +02:00
|
|
|
self.version = "0.0.0"
|
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
|
|
|
|
|
2019-01-28 08:35:10 +01:00
|
|
|
function Game:setData(data)
|
|
|
|
local data = data
|
2019-03-10 13:41:37 +01:00
|
|
|
self.gametime = data.gametime
|
2021-03-22 19:00:54 +01:00
|
|
|
self.destroyedGizmo = data.destroyedGizmo
|
|
|
|
self.variables = data.variables
|
|
|
|
self.flags = data.flags
|
2021-04-02 23:26:22 +02:00
|
|
|
self.position = data.position
|
2021-04-10 17:59:01 +02:00
|
|
|
self.actions = data.actions
|
2019-03-10 13:41:37 +01:00
|
|
|
self.characters:setData(data.characters)
|
2021-04-04 16:33:48 +02:00
|
|
|
self.loot:setData(data.loot)
|
2019-01-28 08:35:10 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function Game:getData()
|
|
|
|
local data = {}
|
|
|
|
data.gametime = self.gametime
|
2019-03-10 13:41:37 +01:00
|
|
|
data.characters = self.characters:getData()
|
2021-04-04 16:33:48 +02:00
|
|
|
data.loot = self.loot:getData()
|
2021-03-22 19:00:54 +01:00
|
|
|
data.flags = self.flags
|
|
|
|
data.destroyedGizmo = self.destroyedGizmo
|
|
|
|
data.variables = self.variables
|
2021-04-02 23:26:22 +02:00
|
|
|
data.position = self.position
|
2021-04-10 17:59:01 +02:00
|
|
|
data.actions = self.actions
|
2019-01-28 08:35:10 +01:00
|
|
|
|
|
|
|
return data
|
|
|
|
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()
|
|
|
|
local metadata = {}
|
|
|
|
local filepath = self:getMetadataFile(true)
|
2021-04-05 12:07:21 +02:00
|
|
|
if utils.filesystem.exists("metadata.save") then
|
2021-04-05 09:33:52 +02:00
|
|
|
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
|
|
|
|
|
2019-01-28 08:35:10 +01:00
|
|
|
function Game:read(save_id)
|
|
|
|
self.slot = save_id
|
|
|
|
if (self.slot > 0) then
|
2020-09-13 16:57:06 +02:00
|
|
|
local filepath = self:getSaveFile(self.slot, true)
|
2021-04-05 12:07:21 +02:00
|
|
|
if utils.filesystem.exists("save" .. self.slot .. ".save") then
|
2019-01-28 08:35:10 +01:00
|
|
|
local loadedDatas = binser.readFile(filepath)
|
|
|
|
|
|
|
|
self:setData(loadedDatas[1])
|
|
|
|
end
|
|
|
|
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
|
|
|
|
local data = self:getData()
|
|
|
|
|
2020-09-13 16:57:06 +02:00
|
|
|
local filepath = self:getSaveFile(self.slot, true)
|
2019-01-28 08:35:10 +01:00
|
|
|
binser.writeFile(filepath, data)
|
2021-04-05 09:33:52 +02:00
|
|
|
self:addtoMetadata()
|
2019-01-28 08:35:10 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function Game:getSaveFile(saveslot, 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
|
2019-01-28 08:35:10 +01:00
|
|
|
love.filesystem.createDirectory( "" )
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local filepath = dir .. "save" .. saveslot .. ".save"
|
|
|
|
|
|
|
|
return filepath
|
|
|
|
end
|
|
|
|
|
|
|
|
function Game:resetSaves()
|
2020-08-02 09:59:47 +02:00
|
|
|
for i=1, self.slotNumber do
|
2020-09-13 16:57:06 +02:00
|
|
|
local filepath = self:getSaveFile(i, true)
|
2021-04-05 12:07:21 +02:00
|
|
|
if utils.filesystem.exists("save" .. i .. ".save") then
|
2019-01-28 08:35:10 +01:00
|
|
|
love.filesystem.remove( "save" .. i .. ".save" )
|
|
|
|
end
|
|
|
|
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()
|
|
|
|
print(self:getTimeString())
|
|
|
|
end
|
|
|
|
|
|
|
|
return Game
|