sonic-radiance/sonic-radiance.love/game/abstractmobs/parent.lua

180 lines
4.1 KiB
Lua
Raw Permalink Normal View History

2021-05-08 12:54:07 +02:00
local Serializable = require "birb.classes.serializable"
local AbstractMobParent = Serializable:extend()
2021-07-03 09:49:11 +02:00
local StatManager = require "game.abstractmobs.statmanager"
2020-07-19 13:13:54 +02:00
2021-08-08 16:00:45 +02:00
local statutStatList = require "datas.gamedata.statuses"
2021-07-03 09:49:11 +02:00
function AbstractMobParent:new(serializeFields, listSerializable, statManager)
local statManager = statManager or StatManager
2020-07-19 13:13:54 +02:00
self:initBasicElements()
2021-07-03 09:49:11 +02:00
2021-07-03 10:12:56 +02:00
self.stats = statManager(self)
2020-07-19 13:13:54 +02:00
self.skills = self:createSkills()
2021-03-12 18:51:35 +01:00
self.statuts = {}
2020-07-19 13:13:54 +02:00
self:initLife()
2021-05-08 12:54:07 +02:00
AbstractMobParent.super.new(self, serializeFields, listSerializable)
2020-07-19 13:13:54 +02:00
end
function AbstractMobParent:initBasicElements()
self.name = "PlaceHolder"
self.fullname = "PlaceHolder"
self.turns = 2
end
function AbstractMobParent:haveProtecType()
return false
end
function AbstractMobParent:getProtecTypes()
return {}
end
2020-07-19 13:13:54 +02:00
function AbstractMobParent:createSkills()
return {}
end
-- LIFE FUNCTIONS
-- Handle HP and stuff like that
2020-07-19 13:13:54 +02:00
function AbstractMobParent:initLife()
2021-07-03 09:49:11 +02:00
self.hp = self.stats:get(self.stats.HPMAX)
self.pp = self.stats:get(self.stats.PPMAX)
2020-07-19 13:13:54 +02:00
self.status = 0
end
function AbstractMobParent:setHP(newHP, relative)
if (relative) then
self.hp = self.hp + newHP
else
self.hp = newHP
end
2020-08-07 07:32:16 +02:00
self.hp = math.floor(math.max(0, self.hp))
2021-03-12 18:51:35 +01:00
if (self.hp == 0) then
self:die()
end
2021-07-03 13:18:16 +02:00
self.hp = math.min(self.hp, self.stats:get(self.stats.HPMAX))
end
function AbstractMobParent:setPP(newPP, relative)
if (relative) then
self.pp = self.pp + newPP
else
self.pp = newPP
end
2020-08-07 07:32:16 +02:00
self.pp = math.floor(math.max(0, self.pp))
2021-07-03 13:18:16 +02:00
self.pp = math.min(self.pp, self.stats:get(self.stats.PPMAX))
end
2021-03-12 18:51:35 +01:00
function AbstractMobParent:getStats()
return self.stats
end
-- STATUT HANDLING
function AbstractMobParent:addStatut(name, duration, source)
2021-08-13 20:14:34 +02:00
local statut = self:getStatutData(name)
local duration = duration or 1
if (statut.remove ~= nil) then
duration = - self:removeStatutTurns(statut.remove, duration)
end
if (duration > 0) then
if (statut.replaceAll == true) then
self:resetStatut()
else
self:removeStatut(name)
end
self.statuts[name] = {}
self.statuts[name].duration = duration
end
end
function AbstractMobParent:getStatutKey(statutName)
local statut = self:getStatutData(statutName)
local key = ""
if (self.statuts[statutName] ~= nil) then
key = statutName
end
if (statut.alternative ~= nil) then
for _, alternative in ipairs(statut.alternative) do
if (self.statuts[alternative] ~= nil) then
key = alternative
end
end
end
return key
2021-03-12 18:51:35 +01:00
end
function AbstractMobParent:haveStatuts(statutName)
2021-08-13 20:14:34 +02:00
return (self.statuts[self:getStatutKey(statutName)] ~= nil)
2021-03-12 18:51:35 +01:00
end
function AbstractMobParent:removeStatut(statutName)
2021-08-13 20:14:34 +02:00
local key = self:getStatutKey(statutName)
if (key ~= nil) then
self.statuts[key] = nil
return true
end
return false
end
function AbstractMobParent:removeStatutTurns(statutName, turns)
local statut = self:getStatutData(statutName)
if (statut.temporary == false) then
return 0
end
if (not self:haveStatuts(statutName)) then
return turns
end
local key = self:getStatutKey(statutName)
local relativeTurns = self.statuts[key].duration - turns
if (relativeTurns <= 0) then
self:removeStatut(statutName)
else
self.statuts[key].duration = relativeTurns
end
return relativeTurns
2021-03-12 18:51:35 +01:00
end
function AbstractMobParent:resetStatut()
self.statuts = {}
end
function AbstractMobParent:die()
self:addStatut("ko")
end
function AbstractMobParent:isAlive()
2021-03-12 18:51:35 +01:00
return (not self:haveStatuts("ko"))
end
2021-03-12 18:51:35 +01:00
function AbstractMobParent:removeOneTurnToStatut()
2021-08-13 20:14:34 +02:00
for name, _ in pairs(self.statuts) do
self:removeStatutTurns(name, 1)
2021-03-12 18:51:35 +01:00
end
2020-07-19 13:13:54 +02:00
end
function AbstractMobParent:getStatutsStat(statName)
local stat = 0
for statutName, _ in pairs(self.statuts) do
2021-08-13 20:14:34 +02:00
local statut = self:getStatutData(statutName)
if (statut.stats ~= nil) then
for _, statutStat in ipairs(statut.stats) do
if (statutStat[1] == statName) then
stat = stat + statutStat[2]
end
end
end
end
return stat
end
2021-08-13 20:14:34 +02:00
function AbstractMobParent:getStatutData(statutName)
return statutStatList[statutName]
end
2020-07-19 13:13:54 +02:00
return AbstractMobParent