Compare commits

..

7 commits

Author SHA1 Message Date
Simon Brodtmann
59d2c45ee3 1.4.0 2026-01-10 22:50:32 +01:00
Simon Brodtmann
68b4d0c444 Add Entity.setFilename 2026-01-05 23:43:12 +01:00
Simon Brodtmann
ce885fb595 1.3.0 2025-12-24 23:41:56 +01:00
Simon Brodtmann
e6b2bbe154 Add Recipe:addIndicatorIcon() 2025-12-24 23:39:48 +01:00
Simon Brodtmann
48325fd78d :apply() returns self 2025-12-24 08:58:26 +01:00
Simon Brodtmann
5a0f1975ef 1.2.0 2025-12-14 18:22:36 +01:00
Simon Brodtmann
7509698888 Add class Entity 2025-12-14 18:08:19 +01:00
6 changed files with 89 additions and 2 deletions

View file

@ -1,4 +1,20 @@
--------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------
Version: 1.4.0
Date: 10.01.2026
Changes:
- Add Entity.setFilename
---------------------------------------------------------------------------------------------------
Version: 1.3.0
Date: 24.12.2025
Changes:
- :apply() returns self
- Add Recipe:addIndicatorIcon()
---------------------------------------------------------------------------------------------------
Version: 1.2.0
Date: 14.12.2025
Changes:
- Add class Entity
---------------------------------------------------------------------------------------------------
Version: 1.1.0 Version: 1.1.0
Date: 12.12.2025 Date: 12.12.2025
Changes: Changes:

46
cf-lib/data/Entity.lua Normal file
View file

@ -0,0 +1,46 @@
--- Utility class for entity prototype definitions
--- @class Entity
local Entity = {}
--- Generates a selection box definition using width and height of the entity in tiles.
--- @param width number The width of the entity in tiles
--- @param height number The height of the entity in tiles
--- @return table A table containing the selection box definition
Entity.selectionBox = function(width, height)
return {
{ -width / 2, -height / 2 },
{ width / 2, height / 2 }
}
end
--- Generates a collision box definition using width and height of the entity in tiles.
--- @param width number The width of the entity in tiles
--- @param height number The height of the entity in tiles
--- @param margin number The margin to apply to the collision box compared to the selection box (default: 0.3)
--- @return table A table containing the collision box definition
Entity.collisionBox = function(width, height, margin)
margin = margin or 0.1
return {
{ -width / 2 + margin, -height / 2 + margin },
{ width / 2 - margin, height / 2 - margin }
}
end
--- Sets the file name on a picture considering various formats.
--- If sheets or layers are found, use the optional param to select the right one.
--- @param sprite table The sprite to replace the filename in
--- @param filename string The new filename
--- @param sheetLayer number The sheet or layer index to use. Defaults to 1.
Entity.setFilename = function(sprite, filename, sheetLayer)
if sprite.filename then
sprite.filename = filename
elseif sprite.sheet then
sprite.sheet.filename = filename
elseif sprite.sheets then
sprite.sheets[sheetLayer or 1].filename = filename
elseif sprite.layers then
sprite.layers[sheetLayer or 1].filename = filename
end
end
return Entity

View file

@ -32,6 +32,7 @@ end
--- Applies the item to the game --- Applies the item to the game
function Item:apply() function Item:apply()
data:extend({ self.prototype }) data:extend({ self.prototype })
return self
end end
--- Assigns data to the item --- Assigns data to the item
@ -50,7 +51,7 @@ function Item:merge(data)
return self return self
end end
--- Sets the weight of the item calculated from given count per rocket. --- Sets the weight of the item calculated from given count per rocket
--- @param count number The amount of items that fit into a rocket --- @param count number The amount of items that fit into a rocket
function Item:itemsPerRocket(count) function Item:itemsPerRocket(count)
self.prototype.weight = (1000 / count) * kg self.prototype.weight = (1000 / count) * kg

View file

@ -32,6 +32,7 @@ end
--- Applies the recipe to the game --- Applies the recipe to the game
function Recipe:apply() function Recipe:apply()
data:extend({ self.prototype }) data:extend({ self.prototype })
return self
end end
--- Assigns data to the recipe --- Assigns data to the recipe
@ -225,4 +226,26 @@ function Recipe:clone(name)
return Recipe:new(clone) return Recipe:new(clone)
end end
--- Adds a small icon in the corner of the regular item
--- It currently assumes an icon_size of 64
--- @param icon string The filename of the icon
--- @param position string The position of the icon. One of ["top-left", "top-right", "bottom-left", "bottom-right"]. Defaults to "bottom-right".
function Recipe:addIndicatorIcon(icon, position)
local icons = self.prototype.icons or {}
local mainIcon = icons[1] or self.prototype.icon or nil
if not mainIcon then
local result = self.prototype.results[1]
local resultItem = data.raw[result.type][result.name]
icons = resultItem.icons or {{ icon = resultItem.icon }}
end
local shift = { 8, 8 }
if position == "top-left" then shift = { -8, -8 } end
if position == "top-right" then shift = { 8, -8 } end
if position == "bottom-left" then shift = { -8, 8 } end
table.insert(icons, { icon = icon, scale = 0.25, shift = shift })
self.prototype.icons = icons
log(serpent.block(self.prototype.icons))
return self
end
return Recipe return Recipe

View file

@ -40,6 +40,7 @@ end
--- Applies the technology to the game --- Applies the technology to the game
function Technology:apply() function Technology:apply()
data:extend({ self.prototype }) data:extend({ self.prototype })
return self
end end
--- Assigns data to the technology --- Assigns data to the technology

View file

@ -1,6 +1,6 @@
{ {
"name": "cf-lib", "name": "cf-lib",
"version": "1.1.0", "version": "1.4.0",
"title": "cackling fiends library", "title": "cackling fiends library",
"description": "Because I'd like to have my own library :-)", "description": "Because I'd like to have my own library :-)",
"author": "cackling fiend", "author": "cackling fiend",