Object registration¶
prism makes use of a number of registries both for ease of use and for some core functionality. The
primary interface for registering your own objects is prism.loadModule(), which expects a
directory containing a module. Subdirectories are processed recursively.
An example module might look like this:
module/
actors/
goblins.lua
items/
potion.lua
sword.lua
systems/
.lua
Below is a walk through of how and when each type of object is loaded.
module.lua or init.lua is ran. You can use it to create move types with Collision.assignNextAvailableMovetype(),
or to perform other miscellaneous set up.
Files in module/components/ are assumed to return a single Component each. These are loaded
into prism.components.
--- @class Pushable : Component
--- @overload fun(): Pushable
local Pushable = prism.Component:extend "Pushable"
return Pushable
All files in module/targets/ are ran.
Targets must be registered by providing a name and a
factory function to prism.registerTarget(); these are loaded into prism.targets.
Factories can accept parameters.
prism.registerTarget("MoveTarget", function(range)
return prism.Target():isPrototype(prism.Vector2):range(range)
end)
All files in module/cells/ are ran.
Cells must be registered by providing a name and a
factory function to prism.registerCell(); these are loaded into prism.cells.
Cell.fromComponents() is useful here.
Caution
Factories for cells and actors can have parameters, but ensure they are optional!
prism.registerCell("Floor", function()
return prism.Cell.fromComponents {
prism.components.Name("Floor"),
prism.components.Drawable(271),
prism.components.Collider{ allowedMovetypes = { "walk" } },
}
end)
Files in module/components/ are assumed to return a single Action each.
These are loaded into prism.actions.
All files in module/actors/ are ran.
Actors must be registered by providing a name and a factory
function to prism.registerActor(); these are loaded into prism.actors.
Actor.fromComponents() is useful here.
prism.registerActor("Goblin", function(health)
return prism.Actor.fromComponents {
-- goblin stuff
prism.components.Health(health or 10)
end
end)
prism.registerActor("GoblinArcher", function()
local goblin = prism.actors.Goblin()
local inventory = goblin:expect(Inventory)
inventory:addItem(prism.actors.Bow())
return goblin
end)
Files in module/messages/ are assumed to return a single Message each.
These are loaded into prism.messages.
Files in module/decisions/ are assumed to return a single Decision each.
These are loaded into prism.decisions.
Files in module/systems/ are assumed to return a single System each.
These are loaded into prism.systems.