Compare commits

...

1 Commits

Author SHA1 Message Date
João Fidalgo ae7d2091d5 feat: add spell traits 4 months ago
  1. 28
      src/Spell/Spell.lua
  2. 1
      src/SpellBook/SpellBook.lua
  3. 23
      src/SpellTrait/SpellTrait.lua
  4. 2
      src/_bastion.lua

@ -12,7 +12,8 @@ local Spell = {
lastCastAt = false, lastCastAt = false,
conditions = {}, conditions = {},
target = false, target = false,
release_at = false release_at = false,
traits = {},
} }
local usableExcludes = { local usableExcludes = {
@ -325,6 +326,10 @@ end
-- Check if the spell is castable -- Check if the spell is castable
---@return boolean ---@return boolean
function Spell:Castable() function Spell:Castable()
if #self.traits > 0 then
return self:EvaluateTraits()
end
if self:GetCastableFunction() then if self:GetCastableFunction() then
return self:GetCastableFunction()(self) return self:GetCastableFunction()(self)
end end
@ -648,4 +653,25 @@ function Spell:IsFree()
return self:GetCost() == 0 return self:GetCost() == 0
end 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 return Spell

@ -14,6 +14,7 @@ function SpellBook:New()
end end
-- Get a spell from the spellbook -- Get a spell from the spellbook
---@param id number
---@return Spell ---@return Spell
function SpellBook:GetSpell(id) function SpellBook:GetSpell(id)
if self.spells[id] == nil then if self.spells[id] == nil then

@ -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

@ -96,6 +96,8 @@ function Bastion.Bootstrap()
---@type SpellBook ---@type SpellBook
Bastion.SpellBook = Bastion.require("SpellBook") Bastion.SpellBook = Bastion.require("SpellBook")
Bastion.Globals.SpellBook = Bastion.SpellBook:New() Bastion.Globals.SpellBook = Bastion.SpellBook:New()
---@type SpellTrait
Bastion.SpellTrait = Bastion.require("SpellTrait")
---@type Item ---@type Item
Bastion.Item = Bastion.require("Item") Bastion.Item = Bastion.require("Item")
---@type ItemBook ---@type ItemBook

Loading…
Cancel
Save