FIFE
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
layer.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2013 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"
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 
60  };
61 
66  };
67 
71  public:
72  virtual ~LayerChangeListener() {};
73 
79  virtual void onLayerChanged(Layer* layer, std::vector<Instance*>& changedInstances) = 0;
80 
85  virtual void onInstanceCreate(Layer* layer, Instance* instance) = 0;
86 
92  virtual void onInstanceDelete(Layer* layer, Instance* instance) = 0;
93  };
94 
95 
98  class Layer : public FifeClass {
99  public:
104  Layer(const std::string& identifier, Map* map, CellGrid* grid);
105 
108  ~Layer();
109 
112  const std::string& getId() const;
113 
116  void setId(const std::string& id);
117 
120  Map* getMap() const;
121 
125  CellGrid* getCellGrid() const;
126 
129  void setCellGrid(CellGrid* grid);
130 
134  InstanceTree* getInstanceTree(void) const;
135 
139  bool hasInstances() const;
140 
143  Instance* createInstance(Object* object, const ModelCoordinate& p, const std::string& id="");
144 
147  Instance* createInstance(Object* object, const ExactModelCoordinate& p, const std::string& id="");
148 
152  bool addInstance(Instance* instance, const ExactModelCoordinate& p);
153 
156  void removeInstance(Instance* instance);
157 
160  void deleteInstance(Instance* instance);
161 
164  const std::vector<Instance*>& getInstances() const;
165 
168  std::vector<Instance*> getInstances(const std::string& id);
169 
174  std::vector<Instance*> getInstancesAt(Location& loc, bool use_exactcoordinates=false);
175 
179  std::list<Instance*> getInstancesIn(Rect& rec);
180 
183  Instance* getInstance(const std::string& identifier);
184 
187  void setInstancesVisible(bool vis);
188 
192  void setLayerTransparency(uint8_t transparency);
193 
197 
203  void getMinMaxCoordinates(ModelCoordinate& min, ModelCoordinate& max, const Layer* layer = 0) const;
204 
210  bool cellContainsBlockingInstance(const ModelCoordinate& cellCoordinate);
211 
217  std::vector<Instance*> getBlockingInstances(const ModelCoordinate& cellCoordinate);
218 
222  void toggleInstancesVisible();
223 
227  bool areInstancesVisible() const;
228 
232  bool update();
233 
237  void setPathingStrategy(PathingStrategy strategy);
238 
243 
247  void setSortingStrategy(SortingStrategy strategy);
248 
253 
258  void setWalkable(bool walkable);
259 
263  bool isWalkable();
264 
270  void setInteract(bool interact, const std::string& id);
271 
275  bool isInteract();
276 
280  const std::string& getWalkableId();
281 
285  void addInteractLayer(Layer* layer);
286 
290  const std::vector<Layer*>& getInteractLayers();
291 
295  void removeInteractLayer(Layer* layer);
296 
299  void createCellCache();
300 
305 
308  void destroyCellCache();
309 
313  void addChangeListener(LayerChangeListener* listener);
314 
319 
322  bool isChanged();
323 
327  std::vector<Instance*>& getChangedInstances();
328 
333  void setInstanceActivityStatus(Instance* instance, bool active);
334 
340  void setStatic(bool stati);
341 
345  bool isStatic();
346  protected:
348  std::string m_id;
356  std::vector<Instance*> m_instances;
358  std::set<Instance*> m_activeInstances;
372  std::string m_walkableId;
374  std::vector<Layer*> m_interacts;
378  std::vector<LayerChangeListener*> m_changeListeners;
380  std::vector<Instance*> m_changedInstances;
382  bool m_changed;
384  bool m_static;
385  };
386 
387 } // FIFE
388 
389 #endif
void setInstancesVisible(bool vis)
Set object visibility.
Definition: layer.cpp:310
SortingStrategy m_sortingStrategy
sorting strategy for rendering
Definition: layer.h:366
std::vector< LayerChangeListener * > m_changeListeners
listeners for layer changes
Definition: layer.h:378
Map * getMap() const
Get the map this layer is contained in.
Definition: layer.cpp:88
bool isChanged()
Returns true, if layer information was changed during previous update round.
Definition: layer.cpp:523
bool m_walkable
is walkable true/false
Definition: layer.h:368
bool m_static
true if layer is static
Definition: layer.h:384
std::vector< Instance * > getInstancesAt(Location &loc, bool use_exactcoordinates=false)
Returns instances that match given location.
Definition: layer.cpp:260
InstanceTree * m_instanceTree
The instance tree.
Definition: layer.h:360
Object class.
Definition: object.h:51
~Layer()
Destructs a Layer instance.
Definition: layer.cpp:66
std::vector< Instance * > & getChangedInstances()
Returns instances that were changed during previous update round.
Definition: layer.cpp:527
void setId(const std::string &id)
Sets the identifier for this layer.
Definition: layer.cpp:84
A CellCache is an abstract depiction of one or a few layers and contains additional information...
Definition: cellcache.h:111
void toggleInstancesVisible()
Toggle object visibility.
Definition: layer.cpp:334
void setSortingStrategy(SortingStrategy strategy)
Sets sorting strategy for the layer.
Definition: layer.cpp:395
bool hasInstances() const
Check existance of objects on this layer.
Definition: layer.cpp:104
void destroyCellCache()
Destroys the CellCache of this layer.
Definition: layer.cpp:457
std::list< Instance * > getInstancesIn(Rect &rec)
Returns instances that match given rect.
Definition: layer.cpp:279
CellCache * getCellCache()
Returns the CellCache of this layer.
Definition: layer.cpp:453
void createCellCache()
Called from Map to create a CellCache.
Definition: layer.cpp:447
Base class for all fife classes Used e.g.
Definition: fifeclass.h:42
Instance * createInstance(Object *object, const ModelCoordinate &p, const std::string &id="")
Add an instance of an object at a specific position.
Definition: layer.cpp:108
CellGrid * m_grid
layer&#39;s cellgrid
Definition: layer.h:362
bool m_instancesVisibility
if true the instances are visibility otherwise they are skipped during rendering
Definition: layer.h:352
Layer(const std::string &identifier, Map *map, CellGrid *grid)
Constructor Layers are created by calling addLayer from map, thus this method should really be called...
Definition: layer.cpp:47
bool areInstancesVisible() const
Check object visibility.
Definition: layer.cpp:338
InstanceTree * getInstanceTree(void) const
Get the instance tree.
Definition: layer.cpp:100
CellCache * m_cellCache
pointer to cellcache
Definition: layer.h:376
virtual void onInstanceDelete(Layer *layer, Instance *instance)=0
Called when some instance gets deleted on layer.
void setCellGrid(CellGrid *grid)
Set the Cellgrid.
Definition: layer.cpp:96
std::set< Instance * > m_activeInstances
all the active instances on this layer
Definition: layer.h:358
bool update()
Called periodically to update events on layer.
Definition: layer.cpp:474
void addInteractLayer(Layer *layer)
Adds a interact layer to the walkable layer.
Definition: layer.cpp:424
std::vector< Instance * > getBlockingInstances(const ModelCoordinate &cellCoordinate)
Returns instances that blocks on given cell.
Definition: layer.cpp:362
uint8_t m_transparency
transparency, value 0 means total visible, 128 semi-transparent and 255 invisibility ...
Definition: layer.h:354
void removeInstance(Instance *instance)
Remove an instance from the layer.
Definition: layer.cpp:158
void setPathingStrategy(PathingStrategy strategy)
Sets pathing strategy for the layer.
Definition: layer.cpp:386
void getMinMaxCoordinates(ModelCoordinate &min, ModelCoordinate &max, const Layer *layer=0) const
Retrieves the minimum/maximum coordinates of instances on the layer.
Definition: layer.cpp:287
unsigned char uint8_t
Definition: core.h:38
void setStatic(bool stati)
Marks this layer as visual static.
Definition: layer.cpp:531
void removeChangeListener(LayerChangeListener *listener)
Removes associated change listener.
Definition: layer.cpp:512
virtual void onLayerChanged(Layer *layer, std::vector< Instance * > &changedInstances)=0
Called when some instance is changed on layer.
bool addInstance(Instance *instance, const ExactModelCoordinate &p)
Add a valid instance at a specific position.
Definition: layer.cpp:133
const std::string & getWalkableId()
Returns the id of the walkable layer if this is a interact layer otherwise the string is empty...
Definition: layer.cpp:420
bool m_changed
true if layer (or it&#39;s instance) information was changed during previous update round ...
Definition: layer.h:382
Instance * getInstance(const std::string &identifier)
Get the first instance on this layer with the given identifier.
Definition: layer.cpp:240
A basic layer on a map.
Definition: layer.h:98
bool isWalkable()
Returns if a layer is walkable.
Definition: layer.cpp:407
Listener interface for changes happening on a layer.
Definition: layer.h:70
SortingStrategy
Definition: layer.h:62
void addChangeListener(LayerChangeListener *listener)
Adds new change listener.
Definition: layer.cpp:508
bool isStatic()
Returns true, if layer is static.
Definition: layer.cpp:535
void setLayerTransparency(uint8_t transparency)
Sets the transparency of all instances on the layer.
Definition: layer.cpp:320
CellGrid * getCellGrid() const
Get the Cellgrid.
Definition: layer.cpp:92
PathingStrategy getPathingStrategy() const
Gets pathing strategy for the layer.
Definition: layer.cpp:391
void setInteract(bool interact, const std::string &id)
Sets interact for the layer.
Definition: layer.cpp:411
const std::vector< Layer * > & getInteractLayers()
Returns all assigned interact layer.
Definition: layer.cpp:430
void setWalkable(bool walkable)
Sets walkable for the layer.
Definition: layer.cpp:403
std::string m_id
string identifier
Definition: layer.h:348
bool m_interact
is interact true/false
Definition: layer.h:370
A 3D Point.
Definition: point.h:202
PathingStrategy
Defines how pathing can be performed on this layer.
Definition: layer.h:57
const std::string & getId() const
Get the id of this layer.
Definition: layer.cpp:80
SortingStrategy getSortingStrategy() const
Gets sorting strategy for the layer.
Definition: layer.cpp:399
std::vector< Instance * > m_instances
all the instances on this layer
Definition: layer.h:356
const std::vector< Instance * > & getInstances() const
Get the list of instances on this layer.
Definition: layer.cpp:228
bool cellContainsBlockingInstance(const ModelCoordinate &cellCoordinate)
Determines if a given cell on the layer contains a blocking instance.
Definition: layer.cpp:342
void removeInteractLayer(Layer *layer)
Removes a interact layer from the walkable layer.
Definition: layer.cpp:434
std::vector< Layer * > m_interacts
all assigned interact layers
Definition: layer.h:374
virtual ~LayerChangeListener()
Definition: layer.h:72
void setInstanceActivityStatus(Instance *instance, bool active)
Sets the activity status for given instance on this layer.
Definition: layer.cpp:232
A container of Layer(s).
Definition: map.h:87
virtual void onInstanceCreate(Layer *layer, Instance *instance)=0
Called when some instance gets created on layer.
void deleteInstance(Instance *instance)
Remove an instance from the layer and delete it.
Definition: layer.cpp:192
PathingStrategy m_pathingStrategy
pathing strategy for the layer
Definition: layer.h:364
bool isInteract()
Returns if a layer is interact.
Definition: layer.cpp:416
uint8_t getLayerTransparency()
Returns the layer&#39;s transparency value.
Definition: layer.cpp:330
Map * m_map
pointer to map
Definition: layer.h:350
An Instance is an &quot;instantiation&quot; of an Object at a Location.
Definition: instance.h:97
std::string m_walkableId
walkable id
Definition: layer.h:372
std::vector< Instance * > m_changedInstances
holds changed instances after each update
Definition: layer.h:380