game: replace savegame module by a radiant-like game object
This commit is contained in:
parent
fcde015f54
commit
e6355533a4
|
@ -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.
|
||||
|
||||
--[[
|
||||
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 PigManager = require "game.pigmanager"
|
||||
local Inventory = require "game.inventory"
|
||||
|
||||
local binser = require "libs.binser"
|
||||
|
||||
function Game:new()
|
||||
self.slot = -1
|
||||
self.gametime = 0
|
||||
|
||||
self.inventory = Inventory(self)
|
||||
self.pigmanager = PigManager(self)
|
||||
end
|
||||
|
||||
function Game:setData(data)
|
||||
local data = data
|
||||
self.gametime = data.gametime
|
||||
end
|
||||
|
||||
function Game:getData()
|
||||
local data = {}
|
||||
data.gametime = self.gametime
|
||||
|
||||
return data
|
||||
end
|
||||
|
||||
function Game:read(save_id)
|
||||
self.slot = save_id
|
||||
if (self.slot > 0) then
|
||||
filepath = self:getSaveFile(self.slot, true)
|
||||
if love.filesystem.exists("save" .. self.slot .. ".save") then
|
||||
local loadedDatas = binser.readFile(filepath)
|
||||
|
||||
self:setData(loadedDatas[1])
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function Game:write(save_id)
|
||||
if (self.slot > 0) then
|
||||
local data = self:getData()
|
||||
|
||||
filepath = self:getSaveFile(self.slot, true)
|
||||
binser.writeFile(filepath, data)
|
||||
end
|
||||
end
|
||||
|
||||
function Game:getSaveFile(saveslot, absolute)
|
||||
local dir = ""
|
||||
if absolute then
|
||||
dir = love.filesystem.getSaveDirectory() .. "/"
|
||||
if not love.filesystem.exists(dir) then
|
||||
love.filesystem.createDirectory( "" )
|
||||
end
|
||||
end
|
||||
|
||||
local filepath = dir .. "save" .. saveslot .. ".save"
|
||||
|
||||
return filepath
|
||||
end
|
||||
|
||||
function Game:resetSaves()
|
||||
for i=1, 3 do
|
||||
filepath = self:getSaveFile(i, true)
|
||||
if love.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()
|
||||
local time = self.gametime
|
||||
local hours, minute, seconds
|
||||
seconds = math.floor(self.gametime)
|
||||
minutes = math.floor(seconds / 60)
|
||||
hours = math.floor(minutes / 60)
|
||||
seconds = seconds % 60
|
||||
minutes = minutes % 60
|
||||
hours = hours
|
||||
|
||||
return seconds, minutes, hours
|
||||
end
|
||||
|
||||
function Game:getTimeString()
|
||||
local string
|
||||
local seconds, minutes, hours = self:getTime()
|
||||
local stringSeconds, stringMinutes, stringHours
|
||||
if (seconds <= 9) then
|
||||
stringSeconds = 0 .. seconds
|
||||
else
|
||||
stringSeconds = seconds
|
||||
end
|
||||
|
||||
if (minutes <= 9) then
|
||||
stringMinutes = 0 .. minutes
|
||||
else
|
||||
stringMinutes = minutes
|
||||
end
|
||||
|
||||
if (hours <= 9) then
|
||||
stringHours = 0 .. hours
|
||||
else
|
||||
stringHours = hours
|
||||
end
|
||||
|
||||
string = stringHours .. ":" .. stringMinutes .. ":" .. stringSeconds
|
||||
return string
|
||||
end
|
||||
|
||||
function Game:printTime()
|
||||
print(self:getTimeString())
|
||||
end
|
||||
|
||||
return Game
|
|
@ -0,0 +1,30 @@
|
|||
local Inventory = Object:extend()
|
||||
|
||||
function Inventory:new(controller)
|
||||
self.controller = controller
|
||||
|
||||
self.data = {}
|
||||
self.data.weapons = {}
|
||||
self.data.items = {}
|
||||
self.data.shields = {}
|
||||
self.data.hats = {}
|
||||
end
|
||||
|
||||
function Inventory:addItem(name)
|
||||
|
||||
if (self.data.items[name] == nil) then
|
||||
self.data.items[name] = 0
|
||||
end
|
||||
|
||||
self.data.items[name] = self.data.items[name] + 1
|
||||
end
|
||||
|
||||
function Inventory:setData(data)
|
||||
self.data = data
|
||||
end
|
||||
|
||||
function Inventory:getData(data)
|
||||
return self.data
|
||||
end
|
||||
|
||||
return Inventory
|
|
@ -0,0 +1,3 @@
|
|||
local gui = {}
|
||||
|
||||
return gui
|
|
@ -0,0 +1,50 @@
|
|||
local PigManager = Object:extend()
|
||||
|
||||
function PigManager:new(controller)
|
||||
self.controller = controller
|
||||
|
||||
self.data = {}
|
||||
end
|
||||
|
||||
function PigManager:addPig(race)
|
||||
local pig = {}
|
||||
pig.race = race
|
||||
pig.name = "Pigguy"
|
||||
|
||||
pig.atk = 1
|
||||
pig.def = 1
|
||||
pig.int = 1
|
||||
pig.spd = 1
|
||||
pig.mag = 1
|
||||
|
||||
pig.maxHP = 20
|
||||
pig.maxMP = 5
|
||||
|
||||
pig.weaponList = {1, 2, 1, 1}
|
||||
pig.hat = 0
|
||||
pig.equip = 0
|
||||
pig.shield = 0
|
||||
|
||||
table.insert(self.data, pig)
|
||||
end
|
||||
|
||||
function PigManager:getPig(pigID)
|
||||
if pigID == nil then
|
||||
error("FATAL: pigID can't be nil")
|
||||
end
|
||||
pigData = self.data[pigID]
|
||||
if pigData == nil then
|
||||
error("FATAL: savegame pig data " .. pigID .. " is empty")
|
||||
end
|
||||
return pigData
|
||||
end
|
||||
|
||||
function PigManager:getData()
|
||||
return self.data
|
||||
end
|
||||
|
||||
function PigManager:setData(data)
|
||||
self.data = data
|
||||
end
|
||||
|
||||
return PigManager
|
|
@ -3,6 +3,7 @@
|
|||
|
||||
Object = require "libs.classic"
|
||||
Core = require "core"
|
||||
Game = require "game"
|
||||
|
||||
Gamestate = require "libs.hump.gamestate"
|
||||
Camera = require "libs.hump.camera"
|
||||
|
@ -24,7 +25,6 @@ datas = require "datas"
|
|||
utils = require "libs.loveutils"
|
||||
menus = require "modules.menus"
|
||||
assets = require "modules.assets"
|
||||
save = require "modules.savegame"
|
||||
vpad = require "modules.vpad"
|
||||
|
||||
scenes = require "scenes"
|
||||
|
@ -38,12 +38,11 @@ io.stdout:setvbuf("no")
|
|||
|
||||
function love.load() -- On charge la scene de départ (pour l'instant le menu, bientôt une scene spéciale pour ça)
|
||||
core = Core()
|
||||
assets:init()
|
||||
save:init("save1")
|
||||
game = Game()
|
||||
game.pigmanager:addPig("cochon")
|
||||
|
||||
assets:init()
|
||||
virtualpad = vpad()
|
||||
save:reset()
|
||||
save:addPig("cochon")
|
||||
scenes.MainMenu()
|
||||
end
|
||||
|
||||
|
|
|
@ -1,27 +0,0 @@
|
|||
function Savegame:resetInventory()
|
||||
self.save.weapons = {}
|
||||
self.save.items = {}
|
||||
self.save.shields = {}
|
||||
self.save.hats = {}
|
||||
end
|
||||
|
||||
function Savegame:addItem(name)
|
||||
|
||||
if (self.save.items[name] == nil) then
|
||||
self.save.items[name] = 0
|
||||
end
|
||||
|
||||
self.save.items[name] = self.save.items[name] + 1
|
||||
|
||||
end
|
||||
|
||||
function Savegame:getPig(pigID)
|
||||
if pigID == nil then
|
||||
error("Error in module savegame : pigID can't be nil")
|
||||
end
|
||||
pigData = self.save.pigs[pigID]
|
||||
if pigData == nil then
|
||||
error("savegame pig data " .. pigID .. " is empty")
|
||||
end
|
||||
return pigData
|
||||
end
|
|
@ -1,36 +0,0 @@
|
|||
function Savegame:resetPigs()
|
||||
self.data.pigs = {}
|
||||
end
|
||||
|
||||
function Savegame:addPig(race)
|
||||
local pig = {}
|
||||
pig.race = race
|
||||
pig.name = "Pigguy"
|
||||
|
||||
pig.atk = 1
|
||||
pig.def = 1
|
||||
pig.int = 1
|
||||
pig.spd = 1
|
||||
pig.mag = 1
|
||||
|
||||
pig.maxHP = 20
|
||||
pig.maxMP = 5
|
||||
|
||||
pig.weaponList = {1, 2, 1, 1}
|
||||
pig.hat = 0
|
||||
pig.equip = 0
|
||||
pig.shield = 0
|
||||
|
||||
table.insert(self.data.pigs, pig)
|
||||
end
|
||||
|
||||
function Savegame:getPig(pigID)
|
||||
if pigID == nil then
|
||||
error("Error in module savegame : pigID can't be nil")
|
||||
end
|
||||
pigData = self.data.pigs[pigID]
|
||||
if pigData == nil then
|
||||
error("savegame pig data " .. pigID .. " is empty")
|
||||
end
|
||||
return pigData
|
||||
end
|
Loading…
Reference in New Issue