Refactor and clean up stuff

- Rename WrapID method to Wrap
- Fix pack function in component.lua
- Change default pixelscale to 1
- Add and fix documentation
This commit is contained in:
David Vogel 2022-07-26 16:33:29 +02:00
parent bde0b2bbd8
commit 96c2da8f78
6 changed files with 27 additions and 25 deletions

View File

@ -252,7 +252,7 @@ local function captureScreenshot(pos, ensureLoaded)
-- Fetch coordinates again, as they may have changed.
local topLeftCapture, bottomRightCapture, topLeftWorld, bottomRightWorld = GenerateCaptureRectangle(pos)
local outputPixelScale = 4
local outputPixelScale = 1
-- The top left world position needs to be upscaled by the pixel scale.
-- Otherwise it's not possible to stitch the images correctly.

View File

@ -17,9 +17,9 @@
-- - A positive virtual offset moves the rendered world to the top left.
-- - The virtual offset needs to be [-2, 0] for the viewport center to be exactly centered, and chunks to align perfectly with the window.
-- - GameGetCameraBounds returned coordinates are off by a few pixels, also it doesn't have sub pixel precision.
-- - The mouse cursor coordinates in the dev build use the wrong rounding method (It rounds towards zero, instead of floor).
-- - The mouse cursor coordinates in the dev build use the wrong rounding method (They are rounded towards zero, instead of being rounded towards negative infinity).
-- - Integer world coordinates map exactly to pixel borders.
-- - The default image ratios of the virtual and internal rectangles don't exactly match, which causes a really small black border.
-- - The default image ratios of the virtual and internal rectangles don't exactly match, which causes a small line of not correctly rendered pixels at the bottom window.
--------------------------
-- Load library modules --

View File

@ -16,7 +16,7 @@ But this would be too complex, as there are a lot of edge cases and stuff that h
## Usage
1. Copy this into your mod so you get the following file path: `mods/your-mod/files/libraries/noita-api/README.md`.
1. Copy this library into your mod so you get the following file path: `mods/your-mod/files/libraries/noita-api/README.md`.
2. Add the following at the beginning of your mod's `init.lua`:
```lua

View File

@ -5,6 +5,8 @@
-- Some code to make Noita's lua conform more to standard lua.
-- TODO: Make Noita-API module to work with several mods using it
-- Stupid way to prevent this code from being called more than once per sandbox.
-- Calling this lua file with dofile_once would still cause the setup function to be called multiple times.
if _NoitaAPICompatibilityWrapperGuard_ then return function(dummy) end end

View File

@ -17,10 +17,10 @@ local NoitaComponent = {}
NoitaComponent.__index = NoitaComponent
ComponentAPI.MetaTable = NoitaComponent
---WrapID wraps the given component ID and returns a Noita component object.
---Wraps the given component ID and returns a Noita component object.
---@param id number|nil
---@return NoitaComponent|nil
function ComponentAPI.WrapID(id)
function ComponentAPI.Wrap(id)
if id == nil or type(id) ~= "number" then return nil end
return setmetatable({ ID = id }, NoitaComponent)
end
@ -142,7 +142,7 @@ end
---@param ... any
---@return table
local function pack(...)
t = {...}
local t = {...}
t.n = select("#", ...)
return t

View File

@ -18,10 +18,10 @@ local NoitaEntity = {}
NoitaEntity.__index = NoitaEntity
EntityAPI.MetaTable = NoitaEntity
---WrapID wraps the given entity ID and returns a Noita entity object.
---Wraps the given entity ID and returns a Noita entity object.
---@param id number|nil
---@return NoitaEntity|nil
function EntityAPI.WrapID(id)
function EntityAPI.Wrap(id)
if id == nil or type(id) ~= "number" then return nil end
return setmetatable({ ID = id }, NoitaEntity)
end
@ -36,7 +36,7 @@ end
---@param posY number -- Y coordinate in world (virtual) pixels.
---@return NoitaEntity|nil
function EntityAPI.Load(filename, posX, posY)
return EntityAPI.WrapID(EntityLoad(filename, posX, posY))
return EntityAPI.Wrap(EntityLoad(filename, posX, posY))
end
---
@ -45,7 +45,7 @@ end
---@param posY number -- Y coordinate in world (virtual) pixels.
---@return NoitaEntity|nil
function EntityAPI.LoadEndGameItem(filename, posX, posY)
return EntityAPI.WrapID(EntityLoadEndGameItem(filename, posX, posY))
return EntityAPI.Wrap(EntityLoadEndGameItem(filename, posX, posY))
end
---
@ -75,7 +75,7 @@ end
---@param name string
---@return NoitaEntity|nil
function EntityAPI.CreateNew(name)
return EntityAPI.WrapID(EntityCreateNew(name))
return EntityAPI.Wrap(EntityCreateNew(name))
end
---
@ -94,7 +94,7 @@ end
---@return NoitaComponent|nil
function NoitaEntity:AddComponent(componentTypeName, tableOfComponentValues)
local componentID = EntityAddComponent(self.ID, componentTypeName, tableOfComponentValues)
return ComponentAPI.WrapID(componentID)
return ComponentAPI.Wrap(componentID)
end
---
@ -109,7 +109,7 @@ function NoitaEntity:GetAllComponents()
local componentIDs = EntityGetAllComponents(self.ID) or {}
local result = {}
for _, componentID in ipairs(componentIDs) do
table.insert(result, ComponentAPI.WrapID(componentID))
table.insert(result, ComponentAPI.Wrap(componentID))
end
return result
end
@ -127,7 +127,7 @@ function NoitaEntity:GetComponents(componentTypeName, tag)
end
local result = {}
for _, componentID in ipairs(componentIDs) do
table.insert(result, ComponentAPI.WrapID(componentID))
table.insert(result, ComponentAPI.Wrap(componentID))
end
return result
end
@ -143,7 +143,7 @@ function NoitaEntity:GetFirstComponent(componentTypeName, tag)
else
componentID = EntityGetFirstComponent(self.ID, componentTypeName)
end
return ComponentAPI.WrapID(componentID)
return ComponentAPI.Wrap(componentID)
end
---Sets the transform of the entity.
@ -184,7 +184,7 @@ function NoitaEntity:GetAllChildren()
local entityIDs = EntityGetAllChildren(self.ID) or {}
local result = {}
for _, entityID in ipairs(entityIDs) do
table.insert(result, EntityAPI.WrapID(entityID))
table.insert(result, EntityAPI.Wrap(entityID))
end
return result
end
@ -192,13 +192,13 @@ end
---
---@return NoitaEntity|nil
function NoitaEntity:GetParent()
return EntityAPI.WrapID(EntityGetParent(self.ID))
return EntityAPI.Wrap(EntityGetParent(self.ID))
end
---Returns the given entity if it has no parent, otherwise walks up the parent hierarchy to the topmost parent and returns it.
---@return NoitaEntity|nil
function NoitaEntity:GetRootEntity()
return EntityAPI.WrapID(EntityGetRootEntity(self.ID))
return EntityAPI.Wrap(EntityGetRootEntity(self.ID))
end
---
@ -251,7 +251,7 @@ function EntityAPI.GetWithTag(tag)
local entityIDs = EntityGetWithTag(tag) or {}
local result = {}
for _, entityID in ipairs(entityIDs) do
table.insert(result, EntityAPI.WrapID(entityID))
table.insert(result, EntityAPI.Wrap(entityID))
end
return result
end
@ -265,7 +265,7 @@ function EntityAPI.GetInRadius(posX, posY, radius)
local entityIDs = EntityGetInRadius(posX, posY, radius) or {}
local result = {}
for _, entityID in ipairs(entityIDs) do
table.insert(result, EntityAPI.WrapID(entityID))
table.insert(result, EntityAPI.Wrap(entityID))
end
return result
end
@ -280,7 +280,7 @@ function EntityAPI.GetInRadiusWithTag(posX, posY, radius, tag)
local entityIDs = EntityGetInRadiusWithTag(posX, posY, radius, tag) or {}
local result = {}
for _, entityID in ipairs(entityIDs) do
table.insert(result, EntityAPI.WrapID(entityID))
table.insert(result, EntityAPI.Wrap(entityID))
end
return result
end
@ -290,14 +290,14 @@ end
---@param posY number -- X coordinate in world (virtual) pixels.
---@return NoitaEntity|nil
function EntityAPI.GetClosest(posX, posY)
return EntityAPI.WrapID(EntityGetClosest(posX, posY))
return EntityAPI.Wrap(EntityGetClosest(posX, posY))
end
---
---@param name string
---@return NoitaEntity|nil
function EntityAPI.GetWithName(name)
return EntityAPI.WrapID(EntityGetWithName(name))
return EntityAPI.Wrap(EntityGetWithName(name))
end
---
@ -335,7 +335,7 @@ end
---@return NoitaComponent|nil
function NoitaEntity:EntityAddComponent(componentTypeName, tableOfComponentValues)
local componentID = EntityAddComponent2(self.ID, componentTypeName, tableOfComponentValues)
return ComponentAPI.WrapID(componentID)
return ComponentAPI.Wrap(componentID)
end
-- TODO: Add missing Noita API methods and functions.