Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local mod = RegisterMod("ExampleFrameworkMod", 1)
- --[==[
- KubeRoot's simple stat item framework
- Free to use for any purpose as long as this notice is preserved. (No need to keep the "How to use")
- --]==]
- --[==[
- How to use:
- Put everything from 'local items' to the last 'end' at the top of your mod just below your RegisterMod.
- Read any comments in the code and adjust the code as written
- Put your item declarations into the items table, with your item's name as the table key.
- For example:
- items["Mysterious fabric"] = {
- CanFly = true,
- TearFlags = 3,
- Damage = 1,
- TearColor = Color(0.9,0.9,1,0.6,0,0,0),
- }
- items["Pegasus Boots"] = {
- MoveSpeed = 2,
- TearFlags = 8,
- ShotSpeed = 1,
- Damage = -1,
- TearColor = Color(0.3, 1, 0.4, 1, 0, 50, 0)
- }
- Make sure all the items are included in a valid items.xml for your mod.
- NOTE:
- At the current time (18.01.2017) MaxFireDelay appears to have a broken cache evaluation - the callback
- triggers, but the stat changes aren't preserved. I am not including any fixes for that for futureproofing
- reasons, as well as to raise awareness of the issue.
- --]==]
- local items
- do
- local mod = mod -- !!! CHANGE THIS IF YOUR RegisterMod VARIABLE ISN'T mod !!!
- local mt, mt2 = {}, {}
- items = setmetatable({}, mt)
- local nextCache = 0
- local game = Game()
- local stats = {
- [CacheFlag.CACHE_DAMAGE] = {"Damage"},
- [CacheFlag.CACHE_FIREDELAY] = {"MaxFireDelay"},
- [CacheFlag.CACHE_SHOTSPEED] = {"ShotSpeed"},
- [CacheFlag.CACHE_RANGE] = { "TearHeight", "TearFallingSpeed", "TearFallingAcceleration" },
- [CacheFlag.CACHE_SPEED] = {"MoveSpeed"},
- [CacheFlag.CACHE_TEARFLAG] = {"TearFlags"},
- [CacheFlag.CACHE_TEARCOLOR] = {"TearColor", "LaserColor"},
- [CacheFlag.CACHE_FLYING] = {"CanFly"},
- [CacheFlag.CACHE_LUCK] = {"Luck"},
- --[CacheFlag.CACHE_WEAPON] = ,
- --[CacheFlag.CACHE_FAMILIARS] = ,
- }
- local statLookup = {}
- for k, v in pairs(stats) do
- for _, v2 in pairs(v) do
- statLookup[v2] = k
- end
- end
- local bitwiseStats = {
- TearFlags = true
- }
- local function __players(n, i)
- i=i+1
- if i<n then
- return i, game:GetPlayer(i)
- end
- end
- local function players()
- return __players, game:GetNumPlayers(), -1
- end
- function mt:__newindex(name, item)
- local t = {}
- t.id = Isaac.GetItemIdByName(name)
- t.name = name
- t.__orig = item
- local itemflags = 0
- for flag, flagtab in pairs(stats) do
- for _, flagname in pairs(flagtab) do
- if item[flagname] then
- itemflags = itemflags | flag
- break
- end
- end
- end
- t.cacheflags = itemflags
- rawset(self, name, setmetatable(t, mt2))
- end
- function mt2:__index(k)
- return self.__orig[k]
- end
- function mt2:__newindex(k, v)
- local stat = statLookup[k]
- if stat then
- nextCache = nextCache | stat
- self.cacheflags = self.cacheflags | stat
- end
- self.__orig[k] = v
- end
- local isColor
- do
- local function colorTest(c)
- local _ = (c.R and c.G and c.B and c.A and c.RO and c.__mul) or error()
- end
- isColor = function(c)
- return type(c)=="userdata" and pcall(colorTest, c)
- end
- end
- local cwhite = Color(1,1,1,1,0,0,0)
- local function cloneColor(c)
- return Color.Lerp(c, cwhite, 0)
- end
- local function onEvaluateCache(_mod, ply, flag)
- for name, item in pairs(items) do
- if ply:HasCollectible(item.id) and (flag & item.cacheflags) ~= 0 then
- local n = ply:GetCollectibleNum(item.id)
- for k, v in pairs(item.__orig) do
- if (flag & statLookup[k]) ~= 0 then
- if type(v) == "number" then
- if bitwiseStats[k] then
- ply[k] = ply[k] | v
- else
- ply[k] = ply[k] + v * n
- end
- elseif isColor(v) then
- ply[k] = cloneColor(v) * ply[k]
- else
- ply[k] = v
- end
- end
- end
- end
- end
- end
- local function onUpdate(_mod)
- if nextCache ~= 0 then
- for k, ply in players() do
- ply:AddCacheFlags(nextCache)
- ply:EvaluateItems()
- end
- nextCache = 0
- end
- end
- mod:AddCallback(ModCallbacks.MC_EVALUATE_CACHE, onEvaluateCache)
- mod:AddCallback(ModCallbacks.MC_POST_UPDATE, onUpdate)
- end
- items["Mysterious fabric"] = {
- CanFly = true,
- TearFlags = 3,
- Damage = 1,
- TearColor = Color(0.9,0.9,1,0.6,0,0,0),
- }
- items["Pegasus Boots"] = {
- MoveSpeed = 2,
- TearFlags = 8,
- ShotSpeed = 1,
- Damage = -1,
- TearColor = Color(0.3, 1, 0.4, 1, 0, 50, 0)
- }
- items["Pegasus Boots"].Damage = -2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement