Debian Codegraph @

engine/core/model/structures/layer.h

raw
1 /***************************************************************************
2 * Copyright (C) 2005-2019 by the FIFE team *
3 * http://www.fifengine.net *
4 * This file is part of FIFE. *
5 * *
6 * FIFE is free software; you can redistribute it and/or *
7 * modify it under the terms of the GNU Lesser General Public *
8 * License as published by the Free Software Foundation; either *
9 * version 2.1 of the License, or (at your option) any later version. *
10 * *
11 * This library is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14 * Lesser General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Lesser General Public *
17 * License along with this library; if not, write to the *
18 * Free Software Foundation, Inc., *
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
20 ***************************************************************************/
21
22 #ifndef FIFE_LAYER_H
23 #define FIFE_LAYER_H
24
25 // Standard C++ library includes
26 #include <algorithm>
27 #include <string>
28 #include <vector>
29 #include <set>
30
31 // 3rd party library includes
32
33 // FIFE includes
34 // These includes are split up in two parts, separated by one empty line
35 // First block: files included from the FIFE root src directory
36 // Second block: files included from the same folder
37 #include "util/base/fifeclass.h"
38 #include "util/structures/rect.h"
39 #include "model/metamodel/modelcoords.h"
40 #include "model/metamodel/object.h"
41
42 #include "instance.h"
43
44 namespace FIFE {
45
46 class Map;
47 class CellGrid;
48 class Object;
49 class InstanceTree;
50 class CellCache;
51 class Trigger;
52
53 /** Defines how pathing can be performed on this layer
54 *
55 * CELL_EDGES_ONLY allows pather to use only cell edges when moving instances from cell to cell on map
56 * CELL_EDGES_AND_DIAGONALS allows pather to use both cell edges and diagonals when moving instances from cell to cell on map
57 */
58 enum PathingStrategy {
59 CELL_EDGES_ONLY,
60 CELL_EDGES_AND_DIAGONALS
61 };
62
63 enum SortingStrategy {
64 SORTING_CAMERA,
65 SORTING_LOCATION,
66 SORTING_CAMERA_AND_LOCATION
67 };
68
69 /** Listener interface for changes happening on a layer
70 */
71 class LayerChangeListener {
72 public:
73 virtual ~LayerChangeListener() {};
74
75 /** Called when some instance is changed on layer. @see InstanceChangeType
76 * @param layer where change occurred
77 * @param changedInstances list of instances containing some changes
78 * @note Does not report creations and deletions
79 */
80 virtual void onLayerChanged(Layer* layer, std::vector<Instance*>& changedInstances) = 0;
81
82 /** Called when some instance gets created on layer
83 * @param layer where change occurred
84 * @param instance which got created
85 */
86 virtual void onInstanceCreate(Layer* layer, Instance* instance) = 0;
87
88 /** Called when some instance gets deleted on layer
89 * @param layer where change occurred
90 * @param instance which will be deleted
91 * @note right after this call, instance actually gets deleted!
92 */
93 virtual void onInstanceDelete(Layer* layer, Instance* instance) = 0;
94 };
95
96
97 /** A basic layer on a map
98 */
99 class Layer : public FifeClass {
100 public:
101 /** Constructor
102 * Layers are created by calling addLayer from map, thus
103 * this method should really be called only by map or test code.
104 */
105 Layer(const std::string& identifier, Map* map, CellGrid* grid);
106
107 /** Destructs a Layer instance
108 */
109 ~Layer();
110
111 /** Get the id of this layer.
112 */
113 const std::string& getId() const;
114
115 /** Sets the identifier for this layer.
116 */
117 void setId(const std::string& id);
118
119 /** Get the map this layer is contained in
120 */
121 Map* getMap() const;
122
123 /** Get the Cellgrid
124 * @return pointer to a valid cellgrid
125 */
126 CellGrid* getCellGrid() const;
127
128 /** Set the Cellgrid
129 */
130 void setCellGrid(CellGrid* grid);
131
132 /** Get the instance tree.
133 * @return this layers instance tree.
134 */
135 InstanceTree* getInstanceTree(void) const;
136
137 /** Check existance of objects on this layer
138 * @return True, if objects exist.
139 */
140 bool hasInstances() const;
141
142 /** Add an instance of an object at a specific position
143 */
144 Instance* createInstance(Object* object, const ModelCoordinate& p, const std::string& id="");
145
146 /** Add an instance of an object at a specific position
147 */
148 Instance* createInstance(Object* object, const ExactModelCoordinate& p, const std::string& id="");
149
150 /** Add a valid instance at a specific position. This is temporary. It will be moved to a higher level
151 later so that we can ensure that each Instance only lives in one layer.
152 */
153 bool addInstance(Instance* instance, const ExactModelCoordinate& p);
154
155 /** Remove an instance from the layer
156 */
157 void removeInstance(Instance* instance);
158
159 /** Remove an instance from the layer and delete it
160 */
161 void deleteInstance(Instance* instance);
162
163 /** Get the list of instances on this layer
164 */
165 const std::vector<Instance*>& getInstances() const;
166
167 /** Get the list of instances on this layer with the given identifier.
168 */
169 std::vector<Instance*> getInstances(const std::string& id);
170
171 /** Returns instances that match given location.
172 * @param loc location where to fetch instances from
173 * @param use_exactcoordinates if true, comparison is done using exact coordinates. if not, cell coordinates are used
174 */
175 std::vector<Instance*> getInstancesAt(Location& loc, bool use_exactcoordinates=false);
176
177 /** Returns instances that match given rect.
178 * @param rec rect where to fetch instances from
179 */
180 std::list<Instance*> getInstancesIn(Rect& rec);
181
182 /** Returns instances that match given line between pt1 and pt2.
183 * @param pt1 A const reference to the ModelCoordinate where to start from.
184 * @param pt2 A const reference to the ModelCoordinate where the end is.
185 * @return A vector that contain the instances.
186 */
187 std::vector<Instance*> getInstancesInLine(const ModelCoordinate& pt1, const ModelCoordinate& pt2);
188
189 /** Returns instances that match given center and radius of the circle.
190 * @param center A const reference to the ModelCoordinate where the center of the circle is.
191 * @param radius A unsigned integer, radius of the circle.
192 * @return A vector that contain the instances.
193 */
194 std::vector<Instance*> getInstancesInCircle(const ModelCoordinate& center, uint16_t radius);
195
196 /** Returns all instances in the circle segment.
197 * @param center A const reference to the ModelCoordinate where the center of the circle is.
198 * @param radius A unsigned integer, radius of the circle.
199 * @param sangle A interger, start angle of the segment.
200 * @param eangle A interger, end angle of the segment.
201 * @return A vector that contain the instances.
202 */
203 std::vector<Instance*> getInstancesInCircleSegment(const ModelCoordinate& center, uint16_t radius, int32_t sangle, int32_t eangle);
204
205 /** Get the first instance on this layer with the given identifier.
206 */
207 Instance* getInstance(const std::string& identifier);
208
209 /** Set object visibility
210 */
211 void setInstancesVisible(bool vis);
212
213 /** Sets the transparency of all instances on the layer. 0=opaque, 255=transparent
214 * @param transparency Transparency value from 0-255.
215 */
216 void setLayerTransparency(uint8_t transparency);
217
218 /** Returns the layer's transparency value
219 */
220 uint8_t getLayerTransparency();
221
222 /** Retrieves the minimum/maximum coordinates of instances on the layer.
223 * @param min A reference to a ModelCoordinate that will hold the minimum coordinate.
224 * @param max A reference to a ModelCoordinate that will hold the maximum coordinate.
225 * @param layer A pointer to another layer that can be used to cast coordinates bettween layers.
226 */
227 void getMinMaxCoordinates(ModelCoordinate& min, ModelCoordinate& max, const Layer* layer = 0) const;
228
229 /** Calculates z offset for the layer.
230 * Is in range [-100,100], see glOrtho settings. Used by LayerCache to calculate z values.
231 */
232 float getZOffset() const;
233
234 /** Get the overall number of layers.
235 */
236 uint32_t getLayerCount() const;
237
238 /** Determines if a given cell on the layer contains a blocking instance
239 *
240 * @param cellCoordinate A const reference to a model coordinate of the cell in question.
241 * @return A boolean, true if it is blocked false otherwise.
242 */
243 bool cellContainsBlockingInstance(const ModelCoordinate& cellCoordinate);
244
245 /** Returns instances that blocks on given cell
246 *
247 * @param cellCoordinate A const reference to a model coordinate of the cell in question.
248 * @return A vector that contains instances.
249 */
250 std::vector<Instance*> getBlockingInstances(const ModelCoordinate& cellCoordinate);
251
252 /** Toggle object visibility
253 * @see setObjectsVisible
254 */
255 void toggleInstancesVisible();
256
257 /** Check object visibility
258 * @see setObjectsVisible
259 */
260 bool areInstancesVisible() const;
261
262 /** Called periodically to update events on layer
263 * @returns true if layer was changed since the last update, false otherwise
264 */
265 bool update();
266
267 /** Sets pathing strategy for the layer
268 * @see PathingStrategy
269 */
270 void setPathingStrategy(PathingStrategy strategy);
271
272 /** Gets pathing strategy for the layer
273 * @see PathingStrategy
274 */
275 PathingStrategy getPathingStrategy() const;
276
277 /** Sets sorting strategy for the layer
278 * @see SortingStrategy
279 */
280 void setSortingStrategy(SortingStrategy strategy);
281
282 /** Gets sorting strategy for the layer
283 * @see SortingStrategy
284 */
285 SortingStrategy getSortingStrategy() const;
286
287 /** Sets walkable for the layer. Only a walkable layer, can create a CellCache and
288 * only on a walkable, instances can move. Also interact layer can only be added to walkables.
289 * @param walkable A boolean that mark a layer as walkable.
290 */
291 void setWalkable(bool walkable);
292
293 /** Returns if a layer is walkable.
294 * @return A boolean, true if the layer is walkable otherwise false.
295 */
296 bool isWalkable();
297
298 /** Sets interact for the layer. The data(size, instances) from all interact layers
299 * and the walkable layer will merged into one CellCache.
300 * @param interact A boolean that mark a layer as interact.
301 * @param id A const reference to a string that should refer to the id of the walkable layer.
302 */
303 void setInteract(bool interact, const std::string& id);
304
305 /** Returns if a layer is interact.
306 * @return A boolean, true if the layer is interact otherwise false.
307 */
308 bool isInteract();
309
310 /** Returns the id of the walkable layer if this is a interact layer otherwise the string is empty.
311 * @return A const reference to a string that refer to the id of the walkable layer.
312 */
313 const std::string& getWalkableId();
314
315 /** Adds a interact layer to the walkable layer.
316 * @param layer A pointer to the interact layer that should be added.
317 */
318 void addInteractLayer(Layer* layer);
319
320 /** Returns all assigned interact layer.
321 * @return A const reference to a vector with pointers to interact layers.
322 */
323 const std::vector<Layer*>& getInteractLayers();
324
325 /** Removes a interact layer from the walkable layer.
326 * @param layer A pointer to the interact layer that should be removed.
327 */
328 void removeInteractLayer(Layer* layer);
329
330 /** Called from Map to create a CellCache. Only walkable layers can create one CellCache.
331 */
332 void createCellCache();
333
334 /** Returns the CellCache of this layer. In case there is no it returns NULL.
335 * @return A pointer to the CellCache.
336 */
337 CellCache* getCellCache();
338
339 /** Destroys the CellCache of this layer.
340 */
341 void destroyCellCache();
342
343 /** Adds new change listener
344 * @param listener to add
345 */
346 void addChangeListener(LayerChangeListener* listener);
347
348 /** Removes associated change listener
349 * @param listener to remove
350 */
351 void removeChangeListener(LayerChangeListener* listener);
352
353 /** Returns true, if layer information was changed during previous update round
354 */
355 bool isChanged();
356
357 /** Returns instances that were changed during previous update round.
358 * @note does not contain created or deleted instances
359 */
360 std::vector<Instance*>& getChangedInstances();
361
362 /** Sets the activity status for given instance on this layer.
363 * @param instance A pointer to the Instance whose activity is to be changed.
364 * @param active A boolean, true if the instance should be set active otherwise false.
365 */
366 void setInstanceActivityStatus(Instance* instance, bool active);
367
368 /** Marks this layer as visual static. The result is that everything is rendered as one texture.
369 * If you have instances with actions/animations on this layer then they are not displayed correctly.
370 * Note: Works currently only for OpenGL backend. SDL backend is restricted to the lowest layer.
371 * @param stati A boolean, true if the layer should be static.
372 */
373 void setStatic(bool stati);
374
375 /** Returns true, if layer is static.
376 * @return A boolean, true if the layer is static, otherwise false.
377 */
378 bool isStatic();
379
380 protected:
381 //! string identifier
382 std::string m_id;
383 //! pointer to map
384 Map* m_map;
385 //! if true the instances are visibility otherwise they are skipped during rendering
386 bool m_instancesVisibility;
387 //! transparency, value 0 means total visible, 128 semi-transparent and 255 invisibility
388 uint8_t m_transparency;
389 //! all the instances on this layer
390 std::vector<Instance*> m_instances;
391 //! all the active instances on this layer
392 std::set<Instance*> m_activeInstances;
393 //! The instance tree
394 InstanceTree* m_instanceTree;
395 //! layer's cellgrid
396 CellGrid* m_grid;
397 //! pathing strategy for the layer
398 PathingStrategy m_pathingStrategy;
399 //! sorting strategy for rendering
400 SortingStrategy m_sortingStrategy;
401 //! is walkable true/false
402 bool m_walkable;
403 //! is interact true/false
404 bool m_interact;
405 //! walkable id
406 std::string m_walkableId;
407 //! all assigned interact layers
408 std::vector<Layer*> m_interacts;
409 //! pointer to cellcache
410 CellCache* m_cellCache;
411 //! listeners for layer changes
412 std::vector<LayerChangeListener*> m_changeListeners;
413 //! holds changed instances after each update
414 std::vector<Instance*> m_changedInstances;
415 //! true if layer (or it's instance) information was changed during previous update round
416 bool m_changed;
417 //! true if layer is static
418 bool m_static;
419 };
420
421 } // FIFE
422
423 #endif