1-- Refactor entities with
a cache of tiles
for the index and then the entity? Or pop up
option for selecting entity on
object spawned? Cache would still be needed.
2-- It might be better to extend an entity by tile, but still use the cache.
4--! \page global_variables Global Variables
5--! \section Entities Entities
7--! See Entities
for the
module on entities.\n\n
8--! FrameworkZ.Entities.List\n
9--! A list of all non-instanced entities in the game.
11FrameworkZ = FrameworkZ or {}
13--! \brief Entities module for FrameworkZ. Defines and interacts with ENTITY object.
14--! \class FrameworkZ.Entities
15FrameworkZ.Entities = {}
16FrameworkZ.Entities.__index = FrameworkZ.Entities
17FrameworkZ.Entities.List = {}
18FrameworkZ.Entities = FrameworkZ.Foundation:NewModule(FrameworkZ.Entities, "Entities")
20--! \brief Entity class for FrameworkZ.
23ENTITY.__index = ENTITY
25--! \brief Initialize an entity.
26--! \return \string The entity's ID.
27function ENTITY:Initialize()
28 --if not self.worldObj then return end
30 --local entityModData = self.worldObj:getModData()["ProjectFramework_Entity"] or nil
32 return FrameworkZ.Entities:Initialize(self, self.name)
35--! \brief Validate the entity's data.
36--! \return \boolean Whether or not any of the entity's new data was initialized.
37function ENTITY:ValidateEntityData(worldObject)
38 local entityModData = worldObject:getModData()["PFW_ENT"]
40 if not entityModData then return false end
42 local initializedNewData = false
44 if not entityModData.persistData then
45 initializedNewData = true
46 entityModData.persistData = self.persistData or {}
48 for k, v in pairs(self.persistData) do
49 if not entityModData.persistData[k] then
50 initializedNewData = true
51 entityModData.persistData[k] = v
56 worldObject:transmitModData()
58 return initializedNewData
61--! \brief Create a new entity object.
62--! \param name \string The entity's name (i.e. ID).
63--! \param square \table The square the entity is on.
64--! \return \table The entity's object table.
65function FrameworkZ.Entities:New(name)
68 description = "No description available."
71 setmetatable(object, ENTITY)
76--! \brief Initialize an entity.
77--! \param data \table The entity's object data
78--! \param name \string The entity's name (i.e. ID)
79--! \return \string Entity ID
80function FrameworkZ.Entities:Initialize(data, name)
81 FrameworkZ.Entities.List[name] = data
86--! \brief Get an entity by their ID.
87--! \param entityID \string The entity's ID.
88--! \return \table Entity Object
89function FrameworkZ.Entities:GetEntityByID(entityID)
90 local entity = FrameworkZ.Entities.List[entityID] or nil
95function FrameworkZ.Entities:GetData(worldObject, index)
97 local entityPersistData = worldObject:getModData()["PFW_ENT"]
99 if entityPersistData and entityPersistData[index] then
100 return entityPersistData[index]
107function FrameworkZ.Entities:SetData(worldObject, index, value)
108 if worldObject and index and value then
109 local entityPersistData = worldObject:getModData()["PFW_ENT"]
111 if entityPersistData and entityPersistData.persistData and entityPersistData.persistData[index] then
112 entityPersistData.persistData[index] = value
113 worldObject:transmitModData()
121--! \brief Checks if an object is an entity (needs optimization from cached entities).
122--! \param object \table The object to check.
123--! \return \boolean Whether or not the object is an entity and its entity ID if it is an entity.
124--! \return \integer The entity ID if the object is an entity.
125function FrameworkZ.Entities:IsEntity(object)
126 for id, entity in pairs(FrameworkZ.Entities.List) do
127 for k, tile in pairs(entity.tiles) do
128 if tile == object:getSprite():getName() then
137function FrameworkZ.Entities:EmitSound(worldObject, sound)
138 if worldObject and sound then
139 getSoundManager():PlayWorldSound(sound, worldObject:getSquare(), 0, 8, 1, false)
147--! \brief Called when an object is added to the world. Adds the entity to the object's mod data.
148--! \param object \table The object that was added to the world.
149function FrameworkZ.Entities.OnObjectAdded(object)
150 local isEntity, entityID = FrameworkZ.Entities:IsEntity(object)
153 local entity = FrameworkZ.Entities:GetEntityByID(entityID)
154 local coordinates = {x = object:getX(), y = object:getY(), z = object:getZ()}
158 object:getModData()["PFW_ENT"] = {
160 data = entity.persistData or {},
161 coordinates = coordinates or {}
163 object:transmitModData()
165 if entity.OnSpawn then
166 entity:OnSpawn(getPlayer(), object)
171Events.OnObjectAdded.Add(FrameworkZ.Entities.OnObjectAdded)
173function FrameworkZ.Entities.OnObjectAboutToBeRemoved(object)
174 local isEntity, entityID = FrameworkZ.Entities:IsEntity(object)
177 local entity = FrameworkZ.Entities:GetEntityByID(entityID)
179 if entity and entity.OnRemove then
180 entity:OnRemove(getPlayer(), object)
184Events.OnObjectAboutToBeRemoved.Add(FrameworkZ.Entities.OnObjectAboutToBeRemoved)
186function FrameworkZ.Entities.OnPreFillWorldObjectContextMenu(player, context, worldObjects, test)
187 local playerObj = getSpecificPlayer(player)
189 local interact = context:addOptionOnTop("Interact")
190 local interactContext = ISContextMenu:getNew(context)
191 context:addSubMenu(interact, interactContext)
193 for k, v in pairs(worldObjects) do
194 if v:getModData()["PFW_ENT"] then
195 local entityID = v:getModData()["PFW_ENT"].id
196 local entity = FrameworkZ.Entities:GetEntityByID(entityID)
199 local canContext = false
201 entity:ValidateEntityData(v)
203 if entity.CanContext then
204 canContext = entity:CanContext(playerObj, v)
208 if entity.OnContext then
209 context = entity:OnContext(playerObj, v, interactContext)
210 elseif entity.OnUse then
211 interactContext:addOptionOnTop("Use " .. entity.name, entity, entity.OnUse, playerObj, v)
215 interactContext:addOption("Examine " .. entity.name, entity, function(entity, playerObj) playerObj:Say(entity.description) end, playerObj)
217 interactContext:addOption("Malformed Entity")
222 if interactContext:isEmpty() then
223 interactContext:addOption("No Interactions Available")
227function FrameworkZ.Entities.OnGameStart()
228 Events.OnPreFillWorldObjectContextMenu.Add(FrameworkZ.Entities.OnPreFillWorldObjectContextMenu)
231function FrameworkZ.Entities:LoadGridsquare(square)
232 for i = 0, square:getObjects():size() - 1 do
233 local object = square:getObjects():get(i)
235 if object and object:getModData()["PFW_ENT"] then
236 local entityID = object:getModData()["PFW_ENT"].id
237 local entity = FrameworkZ.Entities:GetEntityByID(entityID)
239 if entity and not entity.isInitialized then
240 entity:OnInitialize(object)
241 entity.isInitialized = true
void processingNotification backgroundColor a()
Entity class for FrameworkZ.
boolean ValidateEntityData(worldObject)
Validate the entity's data.
string Initialize()
Initialize an entity.
Entities module for FrameworkZ. Defines and interacts with ENTITY object.
FrameworkZ Entities __index