Skip to content

Commit

Permalink
Fix missing gun enum dofile, add IsWand, GetHeldWand, currentCastDela…
Browse files Browse the repository at this point in the history
…y and currentRechargeTime
  • Loading branch information
TheHorscht committed May 26, 2022
1 parent f3b7c17 commit 8f2b5b1
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 11 deletions.
49 changes: 45 additions & 4 deletions EZWand.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
-- #########################################
-- ####### EZWand version v1.4.1 #######
-- ####### EZWand version v1.5.0 #######
-- #########################################

dofile_once("data/scripts/gun/procedural/gun_action_utils.lua")
dofile_once("data/scripts/gun/gun_enums.lua")
dofile_once("data/scripts/lib/utilities.lua")
dofile_once("data/scripts/gun/procedural/wands.lua")

Expand Down Expand Up @@ -90,6 +91,13 @@ wand_props = {
end,
default = 20,
},
currentCastDelay = {
validate = function(val)
return test_conditionals{
{ type(val) == "number", "currentCastDelay must be a number" },
}
end,
},
rechargeTime = {
validate = function(val)
return test_conditionals{
Expand All @@ -98,6 +106,13 @@ wand_props = {
end,
default = 40,
},
currentRechargeTime = {
validate = function(val)
return test_conditionals{
{ type(val) == "number", "currentRechargeTime must be a number" },
}
end,
},
manaMax = {
validate = function(val)
return test_conditionals{
Expand Down Expand Up @@ -579,7 +594,9 @@ local variable_mappings = {
shuffle = { target = "gun_config", name = "shuffle_deck_when_empty" },
spellsPerCast = { target = "gun_config", name="actions_per_round"},
castDelay = { target = "gunaction_config", name="fire_rate_wait"},
currentCastDelay = { target = "ability_component", name="mNextFrameUsable"},
rechargeTime = { target = "gun_config", name="reload_time"},
currentRechargeTime = { target = "ability_component", name="mReloadNextFrameUsable"},
manaMax = { target = "ability_component", name="mana_max"},
mana = { target = "ability_component", name="mana"},
manaChargeSpeed = { target = "ability_component", name="mana_charge_speed"},
Expand All @@ -593,7 +610,16 @@ function wand:_SetProperty(key, value)
local mapped_key = variable_mappings[key].name
local target_setters = {
ability_component = function(key, value)
ComponentSetValue2(self.ability_component, key, value)
if key == "mNextFrameUsable" then
ComponentSetValue2(self.ability_component, key, GameGetFrameNum() + value)
ComponentSetValue2(self.ability_component, "mCastDelayStartFrame", GameGetFrameNum())
elseif key == "mReloadNextFrameUsable" then
ComponentSetValue2(self.ability_component, key, GameGetFrameNum() + value)
ComponentSetValue2(self.ability_component, "mReloadFramesLeft", value)
ComponentSetValue2(self.ability_component, "reload_time_frames", value)
else
ComponentSetValue2(self.ability_component, key, value)
end
end,
gunaction_config = function(key, value)
ComponentObjectSetValue2(self.ability_component, "gunaction_config", key, value)
Expand Down Expand Up @@ -622,7 +648,11 @@ function wand:_GetProperty(key)
local mapped_key = variable_mappings[key].name
local target_getters = {
ability_component = function(key)
return ComponentGetValue2(self.ability_component, key, value)
if key == "mNextFrameUsable" or key == "mReloadNextFrameUsable" then
return (math.max(0, ComponentGetValue2(self.ability_component, key) - GameGetFrameNum()))
else
return ComponentGetValue2(self.ability_component, key)
end
end,
gunaction_config = function(key)
return ComponentObjectGetValue2(self.ability_component, "gunaction_config", key)
Expand Down Expand Up @@ -1075,6 +1105,15 @@ function wand:Serialize()
)
end

local function get_held_wand()
local player = EntityGetWithTag("player_unit")[1]
if player then
local inventory2_comp = EntityGetFirstComponentIncludingDisabled(player, "Inventory2Component")
local active_item = ComponentGetValue2(inventory2_comp, "mActiveItem")
return entity_is_wand(active_item) and wand:new(active_item)
end
end

return setmetatable({}, {
__call = function(self, from, rng_seed_x, rng_seed_y)
return wand:new(from, rng_seed_x, rng_seed_y)
Expand All @@ -1085,7 +1124,9 @@ return setmetatable({}, {
__index = function(self, key)
return ({
Deserialize = deserialize,
RenderTooltip = render_tooltip
RenderTooltip = render_tooltip,
IsWand = entity_is_wand,
GetHeldWand = get_held_wand,
})[key]
end
})
16 changes: 11 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# EZWand v1.4.1
# EZWand v1.5.0

A utility library for mod developers of Noita which simplifies the workflow of creating and manipulating wands. Use at your own risk I don't want to be responsible for your mod breaking :)

Expand Down Expand Up @@ -115,6 +115,9 @@ end
```
### Misc
```lua
-- Returns a boolean whether an entity is a wand or not
EZWand.IsWand(entity_id)
local wand = EZWand.GetHeldWand() -- either nil if not holding a wand or an EZWand object
local cloned_wand = wand:Clone()
-- Applies an appropriate Sprite using the games own algorithm
-- based on capacity etc, use this after changing properties,
Expand All @@ -138,18 +141,21 @@ The names for the properties resemble the one found ingame, not the ones on the
```lua
wand.shuffle -- true or false
wand.spellsPerCast
-- in frames, ingame values are based on 60 FPS, so 60 would be 1.0s
-- In frames, ingame values are based on 60 FPS, so 60 would be 1.0s
wand.castDelay
-- same as castDelay
-- Returns the current cast delay in frames, can also be set to change the current cast delay
wand.currentCastDelay
-- Same as castDelay
wand.rechargeTime
wand.currentRechargeTime
wand.manaMax
wand.mana
wand.manaChargeSpeed
wand.capacity
-- number like -13.2 without "DEG"
-- Number like -13.2 without "DEG"
wand.spread
wand.speedMultiplier
-- properties to access underlying entity/component ids:
-- Properties to access underlying entity/component ids:
wand.entity_id
wand.ability_component
```
Expand Down
7 changes: 7 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
v1.5.0:
- FIX EZWand.lua:257: table index is nil
- Add EZWand.IsWand(entity_id) function
- Add EZWand.GetHeldWand() function
- Add wand.currentCastDelay property
- Add wand.currentRechargeTime property

v1.4.1:
- FIX Spells not updating when wand is being held

Expand Down
8 changes: 6 additions & 2 deletions tests.lua
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ function test_constructors()
assert(wand.ability_component ~= nil)
-- Check if defaults were set
for k,v in pairs(wand_props) do
assert(wand[k] == v.default, string.format("Constructor didn't set default values for %s. Given: %s, Expected: %s", k, wand[k], v.default))
if v.default then
assert(wand[k] == v.default, string.format("Constructor didn't set default values for %s. Given: %s, Expected: %s", k, wand[k], v.default))
end
end
spells_count, attached_spells_count = wand:GetSpellsCount()
assert(spells_count == 0, "Spells count expected to be 0, got: " .. spells_count)
Expand All @@ -106,7 +108,9 @@ function test_constructors()
assert(wand.entity_id ~= nil)
assert(wand.ability_component ~= nil)
for k,v in pairs(wand_props) do
assert(wand[k] == v.default)
if v.default then
assert(wand[k] == v.default, string.format("Constructor didn't set default values for %s. Given: %s, Expected: %s", k, wand[k], v.default))
end
end
spells_count, attached_spells_count = wand:GetSpellsCount()
assert(spells_count == 0)
Expand Down

0 comments on commit 8f2b5b1

Please sign in to comment.