Skip to content

Instantly share code, notes, and snippets.

@gphg
Last active May 14, 2024 16:47
Show Gist options
  • Save gphg/d99254a6d616dbecb56d37469ff39679 to your computer and use it in GitHub Desktop.
Save gphg/d99254a6d616dbecb56d37469ff39679 to your computer and use it in GitHub Desktop.
The old way to define a component (as part of ECS). This script normally saved as individual file.
---@alias entity {} # also known as character/object/thing (unique)
---@aliss component.area { width: number, height: number } # a 2d object component, also known as size
---The component constructor/giver/assembler, I don't know, I don't really know.
---@type table<entity, component.area>|fun(width?: number, height?: number): c: component
return setmetatable({}, {
-- stored as weak reference
__mode = 'kv',
---@param list table<entity, component.size>
---@param e entity
---@param w number
---@parsm h number
__call = function(list, e, w, h)
-- notice: argument variables' name are shorter to avoid confusion
-- two dimensions object with width and height. pure data.
local area = {
width = w,
height = h
}
-- replaces the existing table regardless it is existed or not
list[e] = area
-- return is optional, assuming the components can be easily retrieved
return list[e]
end
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment