diff --git a/cf-lib/data/Item.lua b/cf-lib/data/Item.lua new file mode 100644 index 0000000..e7cc877 --- /dev/null +++ b/cf-lib/data/Item.lua @@ -0,0 +1,52 @@ +--- Utility class for a single item +--- @class Item +local Item = {} + +--- Pass an item name or an item table to get a Item object +--- @param value string|table The name of the item or the item table +function Item:new(value) + local name + local item + + if type(value) == "string" then + name = value + item = data.raw.item[name] + if not item then + log("Item not found: " .. name) + return nil + end + elseif type(value) == "table" then + name = value.name + item = value + end + + local obj = { + prototype = item + } + + setmetatable(obj, self) + self.__index = self + return obj +end + +--- Applies the item to the game +function Item:apply() + data:extend({ self.prototype }) +end + +--- Assigns data to the item +--- Shorthand for table.assign(item.prototype, data) +--- @param data table The data to assign +function Item:assign(data) + table.assign(self.prototype, data) + return self +end + +--- Sets the weight of the item calculated from given count per rocket. +--- @param count number The amount of items that fit into a rocket +function Item:itemsPerRocket(count) + self.prototype.weight = (1000 / count) * kg + return self +end + +return Item \ No newline at end of file diff --git a/cf-lib/data/Recipe.lua b/cf-lib/data/Recipe.lua index 1a5c67e..6ca5192 100644 --- a/cf-lib/data/Recipe.lua +++ b/cf-lib/data/Recipe.lua @@ -1,9 +1,9 @@ ----- Utility class for a single recipe ----- @class Recipe +--- Utility class for a single recipe +--- @class Recipe local Recipe = {} ----- Pass a recipe name or a recipe table to get a Recipe object ----- @param value string|table The name of the recipe or the recipe table +--- Pass a recipe name or a recipe table to get a Recipe object +--- @param value string|table The name of the recipe or the recipe table function Recipe:new(value) local name local recipe @@ -29,14 +29,14 @@ function Recipe:new(value) return obj end ----- Applies the recipe to the game +--- Applies the recipe to the game function Recipe:apply() data:extend({ self.prototype }) end ----- Assigns data to the recipe ----- Shorthand for table.assign(recipe.prototype, data) ----- @param data table The data to assign +--- Assigns data to the recipe +--- Shorthand for table.assign(recipe.prototype, data) +--- @param data table The data to assign function Recipe:assign(data) table.assign(self.prototype, data) return self @@ -148,8 +148,8 @@ function Recipe:unlockedByTechnology(technology) return self end ----- Clones the recipe ----- @param name string The name of the new recipe +--- Clones the recipe +--- @param name string The name of the new recipe function Recipe:clone(name) local clone = table.deepcopy(self.prototype) clone.name = name