From ae7d2091d5709bee70fcc95efc0e3b6edb0372d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Fidalgo?= Date: Sat, 8 Jun 2024 02:11:45 +0100 Subject: [PATCH] feat: add spell traits --- src/Spell/Spell.lua | 28 +++++++++++++++++++++++++++- src/SpellBook/SpellBook.lua | 1 + src/SpellTrait/SpellTrait.lua | 23 +++++++++++++++++++++++ src/_bastion.lua | 2 ++ 4 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 src/SpellTrait/SpellTrait.lua diff --git a/src/Spell/Spell.lua b/src/Spell/Spell.lua index 0aad894..e84f100 100644 --- a/src/Spell/Spell.lua +++ b/src/Spell/Spell.lua @@ -12,7 +12,8 @@ local Spell = { lastCastAt = false, conditions = {}, target = false, - release_at = false + release_at = false, + traits = {}, } local usableExcludes = { @@ -325,6 +326,10 @@ end -- Check if the spell is castable ---@return boolean function Spell:Castable() + if #self.traits > 0 then + return self:EvaluateTraits() + end + if self:GetCastableFunction() then return self:GetCastableFunction()(self) end @@ -648,4 +653,25 @@ function Spell:IsFree() return self:GetCost() == 0 end +-- AddTraits +---@param traits table +---@return Spell +function Spell:AddTraits(traits) + for _, trait in ipairs(traits) do + table.insert(self.traits, trait) + end + return self +end + +-- EvaluateTraits +---@return boolean +function Spell:EvaluateTraits() + for _, trait in ipairs(self.traits) do + if not trait:Evaluate(self) then + return false + end + end + return true +end + return Spell diff --git a/src/SpellBook/SpellBook.lua b/src/SpellBook/SpellBook.lua index 243a4bd..f2ba6d5 100644 --- a/src/SpellBook/SpellBook.lua +++ b/src/SpellBook/SpellBook.lua @@ -14,6 +14,7 @@ function SpellBook:New() end -- Get a spell from the spellbook +---@param id number ---@return Spell function SpellBook:GetSpell(id) if self.spells[id] == nil then diff --git a/src/SpellTrait/SpellTrait.lua b/src/SpellTrait/SpellTrait.lua new file mode 100644 index 0000000..ffbc085 --- /dev/null +++ b/src/SpellTrait/SpellTrait.lua @@ -0,0 +1,23 @@ +local Tinkr, Bastion = ... + +---@class SpellTrait +local SpellTrait = {} +SpellTrait.__index = SpellTrait + +-- Constructor +---@param func function +---@return SpellTrait +function SpellTrait:New(func) + local self = setmetatable({}, SpellTrait) + self.func = func + return self +end + +-- Evaluate the trait +---@param spell Spell +---@return boolean +function SpellTrait:Evaluate(spell) + return self.func(spell) +end + +return SpellTrait diff --git a/src/_bastion.lua b/src/_bastion.lua index 6d92449..ebace7a 100644 --- a/src/_bastion.lua +++ b/src/_bastion.lua @@ -96,6 +96,8 @@ function Bastion.Bootstrap() ---@type SpellBook Bastion.SpellBook = Bastion.require("SpellBook") Bastion.Globals.SpellBook = Bastion.SpellBook:New() + ---@type SpellTrait + Bastion.SpellTrait = Bastion.require("SpellTrait") ---@type Item Bastion.Item = Bastion.require("Item") ---@type ItemBook