FrameworkZ 4.4.2
Provides a framework for Project Zomboid with various systems.
Loading...
Searching...
No Matches
Entities.lua
Go to the documentation of this file.
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.
3
4--! \page global_variables Global Variables
5--! \section Entities Entities
6--! FrameworkZ.Entities\n
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.
10
11FrameworkZ = FrameworkZ or {}
12
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.
21--! \class ENTITY
22local ENTITY = {}
23ENTITY.__index = ENTITY
24
25--! \brief Initialize an entity.
26--! \return \string The entity's ID.
27function ENTITY:Initialize()
28 --if not self.worldObj then return end
29
30 --local entityModData = self.worldObj:getModData()["ProjectFramework_Entity"] or nil
32 return FrameworkZ.Entities:Initialize(self, self.name)
33end
34
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"]
39
40 if not entityModData then return false end
41
42 local initializedNewData = false
43
44 if not entityModData.persistData then
45 initializedNewData = true
46 entityModData.persistData = self.persistData or {}
47 else
48 for k, v in pairs(self.persistData) do
49 if not entityModData.persistData[k] then
50 initializedNewData = true
51 entityModData.persistData[k] = v
52 end
53 end
54 end
55
56 worldObject:transmitModData()
57
58 return initializedNewData
59end
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)
66 local object = {
67 name = name,
68 description = "No description available."
69 }
70
71 setmetatable(object, ENTITY)
73 return object
74end
75
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
82
83 return name
84end
85
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
91
92 return entity
93end
94
95function FrameworkZ.Entities:GetData(worldObject, index)
96 if worldObject then
97 local entityPersistData = worldObject:getModData()["PFW_ENT"]
98
99 if entityPersistData and entityPersistData[index] then
100 return entityPersistData[index]
101 end
102 end
103
104 return nil
105end
106
107function FrameworkZ.Entities:SetData(worldObject, index, value)
108 if worldObject and index and value then
109 local entityPersistData = worldObject:getModData()["PFW_ENT"]
110
111 if entityPersistData and entityPersistData.persistData and entityPersistData.persistData[index] then
112 entityPersistData.persistData[index] = value
113 worldObject:transmitModData()
114 return true
115 end
116 end
117
118 return false
119end
120
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
129 return true, id
130 end
131 end
132 end
133
134 return false, nil
135end
136
137function FrameworkZ.Entities:EmitSound(worldObject, sound)
138 if worldObject and sound then
139 getSoundManager():PlayWorldSound(sound, worldObject:getSquare(), 0, 8, 1, false)
140
141 return true
142 end
143
144 return false
145end
146
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)
151
152 if isEntity then
153 local entity = FrameworkZ.Entities:GetEntityByID(entityID)
154 local coordinates = {x = object:getX(), y = object:getY(), z = object:getZ()}
155
156 if entity then
157 entity:Initialize()
158 object:getModData()["PFW_ENT"] = {
159 id = entityID,
160 data = entity.persistData or {},
161 coordinates = coordinates or {}
162 }
163 object:transmitModData()
164
165 if entity.OnSpawn then
166 entity:OnSpawn(getPlayer(), object)
167 end
168 end
169 end
170end
171Events.OnObjectAdded.Add(FrameworkZ.Entities.OnObjectAdded)
172
173function FrameworkZ.Entities.OnObjectAboutToBeRemoved(object)
174 local isEntity, entityID = FrameworkZ.Entities:IsEntity(object)
175
176 if isEntity then
177 local entity = FrameworkZ.Entities:GetEntityByID(entityID)
178
179 if entity and entity.OnRemove then
180 entity:OnRemove(getPlayer(), object)
181 end
182 end
183end
184Events.OnObjectAboutToBeRemoved.Add(FrameworkZ.Entities.OnObjectAboutToBeRemoved)
185
186function FrameworkZ.Entities.OnPreFillWorldObjectContextMenu(player, context, worldObjects, test)
187 local playerObj = getSpecificPlayer(player)
188
189 local interact = context:addOptionOnTop("Interact")
190 local interactContext = ISContextMenu:getNew(context)
191 context:addSubMenu(interact, interactContext)
192
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)
197
198 if entity then
199 local canContext = false
200
201 entity:ValidateEntityData(v)
202
203 if entity.CanContext then
204 canContext = entity:CanContext(playerObj, v)
205 end
206
207 if canContext then
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)
212 end
213 end
214
215 interactContext:addOption("Examine " .. entity.name, entity, function(entity, playerObj) playerObj:Say(entity.description) end, playerObj)
216 else
217 interactContext:addOption("Malformed Entity")
218 end
219 end
220 end
221
222 if interactContext:isEmpty() then
223 interactContext:addOption("No Interactions Available")
224 end
225end
226
227function FrameworkZ.Entities.OnGameStart()
228 Events.OnPreFillWorldObjectContextMenu.Add(FrameworkZ.Entities.OnPreFillWorldObjectContextMenu)
229end
230
231function FrameworkZ.Entities:LoadGridsquare(square)
232 for i = 0, square:getObjects():size() - 1 do
233 local object = square:getObjects():get(i)
234
235 if object and object:getModData()["PFW_ENT"] then
236 local entityID = object:getModData()["PFW_ENT"].id
237 local entity = FrameworkZ.Entities:GetEntityByID(entityID)
238
239 if entity and not entity.isInitialized then
240 entity:OnInitialize(object)
241 entity.isInitialized = true
242 end
243 end
244 end
245end
void player
void name()
void context()
void FrameworkZ()
void local option()
void processingNotification backgroundColor a()
Entity class for FrameworkZ.
Definition Entities.lua:66
ENTITY __index
Definition Entities.lua:68
boolean ValidateEntityData(worldObject)
Validate the entity's data.
string Initialize()
Initialize an entity.
Entities module for FrameworkZ. Defines and interacts with ENTITY object.
Definition Entities.lua:13
FrameworkZ Entities __index
Definition Entities.lua:15
FrameworkZ Entities List
Definition Entities.lua:17
FrameworkZ global table.