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  class Trigger;
52 
61  };
62 
67  };
68 
72  public:
73  virtual ~LayerChangeListener() {};
74 
80  virtual void onLayerChanged(Layer* layer, std::vector<Instance*>& changedInstances) = 0;
81 
86  virtual void onInstanceCreate(Layer* layer, Instance* instance) = 0;
87 
93  virtual void onInstanceDelete(Layer* layer, Instance* instance) = 0;
94  };
95 
96 
99  class Layer : public FifeClass {
100  public:
105  Layer(const std::string& identifier, Map* map, CellGrid* grid);
106 
109  ~Layer();
110 
113  const std::string& getId() const;
114 
117  void setId(const std::string& id);
118 
121  Map* getMap() const;
122 
126  CellGrid* getCellGrid() const;
127 
130  void setCellGrid(CellGrid* grid);
131 
135  InstanceTree* getInstanceTree(void) const;
136 
140  bool hasInstances() const;
141 
144  Instance* createInstance(Object* object, const ModelCoordinate& p, const std::string& id="");
145 
148  Instance* createInstance(Object* object, const ExactModelCoordinate& p, const std::string& id="");
149 
153  bool addInstance(Instance* instance, const ExactModelCoordinate& p);
154 
157  void removeInstance(Instance* instance);
158 
161  void deleteInstance(Instance* instance);
162 
165  const std::vector<Instance*>& getInstances() const;
166 
169  std::vector<Instance*> getInstances(const std::string& id);
170 
175  std::vector<Instance*> getInstancesAt(Location& loc, bool use_exactcoordinates=false);
176 
180  std::list<Instance*> getInstancesIn(Rect& rec);
181 
187  std::vector<Instance*> getInstancesInLine(const ModelCoordinate& pt1, const ModelCoordinate& pt2);
188 
194  std::vector<Instance*> getInstancesInCircle(const ModelCoordinate& center, uint16_t radius);
195 
203  std::vector<Instance*> getInstancesInCircleSegment(const ModelCoordinate& center, uint16_t radius, int32_t sangle, int32_t eangle);
204 
207  Instance* getInstance(const std::string& identifier);
208 
211  void setInstancesVisible(bool vis);
212 
216  void setLayerTransparency(uint8_t transparency);
217 
221 
227  void getMinMaxCoordinates(ModelCoordinate& min, ModelCoordinate& max, const Layer* layer = 0) const;
228 
232  float getZOffset() const;
233 
236  uint32_t getLayerCount() const;
237 
243  bool cellContainsBlockingInstance(const ModelCoordinate& cellCoordinate);
244 
250  std::vector<Instance*> getBlockingInstances(const ModelCoordinate& cellCoordinate);
251 
255  void toggleInstancesVisible();
256 
260  bool areInstancesVisible() const;
261 
265  bool update();
266 
270  void setPathingStrategy(PathingStrategy strategy);
271 
276 
280  void setSortingStrategy(SortingStrategy strategy);
281 
286 
291  void setWalkable(bool walkable);
292 
296  bool isWalkable();
297 
303  void setInteract(bool interact, const std::string& id);
304 
308  bool isInteract();
309 
313  const std::string& getWalkableId();
314 
318  void addInteractLayer(Layer* layer);
319 
323  const std::vector<Layer*>& getInteractLayers();
324 
328  void removeInteractLayer(Layer* layer);
329 
332  void createCellCache();
333 
338 
341  void destroyCellCache();
342 
346  void addChangeListener(LayerChangeListener* listener);
347 
352 
355  bool isChanged();
356 
360  std::vector<Instance*>& getChangedInstances();
361 
366  void setInstanceActivityStatus(Instance* instance, bool active);
367 
373  void setStatic(bool stati);
374 
378  bool isStatic();
379 
380  protected:
382  std::string m_id;
390  std::vector<Instance*> m_instances;
392  std::set<Instance*> m_activeInstances;
406  std::string m_walkableId;
408  std::vector<Layer*> m_interacts;
412  std::vector<LayerChangeListener*> m_changeListeners;
414  std::vector<Instance*> m_changedInstances;
416  bool m_changed;
418  bool m_static;
419  };
420 
421 } // FIFE
422 
423 #endif
void setInstancesVisible(bool vis)
Set object visibility.
Definition: layer.cpp:430
SortingStrategy m_sortingStrategy
sorting strategy for rendering
Definition: layer.h:400
std::vector< LayerChangeListener * > m_changeListeners
listeners for layer changes
Definition: layer.h:412
Map * getMap() const
Get the map this layer is contained in.
Definition: layer.cpp:89
bool isChanged()
Returns true, if layer information was changed during previous update round.
Definition: layer.cpp:643
bool m_walkable
is walkable true/false
Definition: layer.h:402
bool m_static
true if layer is static
Definition: layer.h:418
std::vector< Instance * > getInstancesAt(Location &loc, bool use_exactcoordinates=false)
Returns instances that match given location.
Definition: layer.cpp:261
std::vector< Instance * > getInstancesInLine(const ModelCoordinate &pt1, const ModelCoordinate &pt2)
Returns instances that match given line between pt1 and pt2.
Definition: layer.cpp:288
std::vector< Instance * > getInstancesInCircleSegment(const ModelCoordinate &center, uint16_t radius, int32_t sangle, int32_t eangle)
Returns all instances in the circle segment.
Definition: layer.cpp:363
InstanceTree * m_instanceTree
The instance tree.
Definition: layer.h:394
Object class.
Definition: object.h:51
~Layer()
Destructs a Layer instance.
Definition: layer.cpp:67
std::vector< Instance * > & getChangedInstances()
Returns instances that were changed during previous update round.
Definition: layer.cpp:647
void setId(const std::string &id)
Sets the identifier for this layer.
Definition: layer.cpp:85
uint32_t getLayerCount() const
Get the overall number of layers.
Definition: layer.cpp:426
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:454
void setSortingStrategy(SortingStrategy strategy)
Sets sorting strategy for the layer.
Definition: layer.cpp:515
bool hasInstances() const
Check existance of objects on this layer.
Definition: layer.cpp:105
void destroyCellCache()
Destroys the CellCache of this layer.
Definition: layer.cpp:577
std::list< Instance * > getInstancesIn(Rect &rec)
Returns instances that match given rect.
Definition: layer.cpp:280
CellCache * getCellCache()
Returns the CellCache of this layer.
Definition: layer.cpp:573
void createCellCache()
Called from Map to create a CellCache.
Definition: layer.cpp:567
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:109
CellGrid * m_grid
layer's cellgrid
Definition: layer.h:396
bool m_instancesVisibility
if true the instances are visibility otherwise they are skipped during rendering
Definition: layer.h:386
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:48
bool areInstancesVisible() const
Check object visibility.
Definition: layer.cpp:458
InstanceTree * getInstanceTree(void) const
Get the instance tree.
Definition: layer.cpp:101
CellCache * m_cellCache
pointer to cellcache
Definition: layer.h:410
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:97
std::set< Instance * > m_activeInstances
all the active instances on this layer
Definition: layer.h:392
bool update()
Called periodically to update events on layer.
Definition: layer.cpp:594
void addInteractLayer(Layer *layer)
Adds a interact layer to the walkable layer.
Definition: layer.cpp:544
std::vector< Instance * > getBlockingInstances(const ModelCoordinate &cellCoordinate)
Returns instances that blocks on given cell.
Definition: layer.cpp:482
uint8_t m_transparency
transparency, value 0 means total visible, 128 semi-transparent and 255 invisibility ...
Definition: layer.h:388
void removeInstance(Instance *instance)
Remove an instance from the layer.
Definition: layer.cpp:159
void setPathingStrategy(PathingStrategy strategy)
Sets pathing strategy for the layer.
Definition: layer.cpp:506
void getMinMaxCoordinates(ModelCoordinate &min, ModelCoordinate &max, const Layer *layer=0) const
Retrieves the minimum/maximum coordinates of instances on the layer.
Definition: layer.cpp:385
unsigned char uint8_t
Definition: core.h:38
void setStatic(bool stati)
Marks this layer as visual static.
Definition: layer.cpp:651
void removeChangeListener(LayerChangeListener *listener)
Removes associated change listener.
Definition: layer.cpp:632
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:134
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:540
bool m_changed
true if layer (or it's instance) information was changed during previous update round ...
Definition: layer.h:416
Instance * getInstance(const std::string &identifier)
Get the first instance on this layer with the given identifier.
Definition: layer.cpp:241
A basic layer on a map.
Definition: layer.h:99
bool isWalkable()
Returns if a layer is walkable.
Definition: layer.cpp:527
Listener interface for changes happening on a layer.
Definition: layer.h:71
SortingStrategy
Definition: layer.h:63
void addChangeListener(LayerChangeListener *listener)
Adds new change listener.
Definition: layer.cpp:628
bool isStatic()
Returns true, if layer is static.
Definition: layer.cpp:655
void setLayerTransparency(uint8_t transparency)
Sets the transparency of all instances on the layer.
Definition: layer.cpp:440
unsigned short uint16_t
Definition: core.h:39
CellGrid * getCellGrid() const
Get the Cellgrid.
Definition: layer.cpp:93
PathingStrategy getPathingStrategy() const
Gets pathing strategy for the layer.
Definition: layer.cpp:511
void setInteract(bool interact, const std::string &id)
Sets interact for the layer.
Definition: layer.cpp:531
const std::vector< Layer * > & getInteractLayers()
Returns all assigned interact layer.
Definition: layer.cpp:550
void setWalkable(bool walkable)
Sets walkable for the layer.
Definition: layer.cpp:523
std::string m_id
string identifier
Definition: layer.h:382
bool m_interact
is interact true/false
Definition: layer.h:404
A 3D Point.
Definition: point.h:202
PathingStrategy
Defines how pathing can be performed on this layer.
Definition: layer.h:58
const std::string & getId() const
Get the id of this layer.
Definition: layer.cpp:81
SortingStrategy getSortingStrategy() const
Gets sorting strategy for the layer.
Definition: layer.cpp:519
std::vector< Instance * > m_instances
all the instances on this layer
Definition: layer.h:390
float getZOffset() const
Calculates z offset for the layer.
Definition: layer.cpp:408
const std::vector< Instance * > & getInstances() const
Get the list of instances on this layer.
Definition: layer.cpp:229
bool cellContainsBlockingInstance(const ModelCoordinate &cellCoordinate)
Determines if a given cell on the layer contains a blocking instance.
Definition: layer.cpp:462
void removeInteractLayer(Layer *layer)
Removes a interact layer from the walkable layer.
Definition: layer.cpp:554
std::vector< Layer * > m_interacts
all assigned interact layers
Definition: layer.h:408
virtual ~LayerChangeListener()
Definition: layer.h:73
void setInstanceActivityStatus(Instance *instance, bool active)
Sets the activity status for given instance on this layer.
Definition: layer.cpp:233
A container of Layer(s).
Definition: map.h:88
virtual void onInstanceCreate(Layer *layer, Instance *instance)=0
Called when some instance gets created on layer.
unsigned int uint32_t
Definition: core.h:40
void deleteInstance(Instance *instance)
Remove an instance from the layer and delete it.
Definition: layer.cpp:193
PathingStrategy m_pathingStrategy
pathing strategy for the layer
Definition: layer.h:398
bool isInteract()
Returns if a layer is interact.
Definition: layer.cpp:536
uint8_t getLayerTransparency()
Returns the layer's transparency value.
Definition: layer.cpp:450
Map * m_map
pointer to map
Definition: layer.h:384
std::vector< Instance * > getInstancesInCircle(const ModelCoordinate &center, uint16_t radius)
Returns instances that match given center and radius of the circle.
Definition: layer.cpp:301
An Instance is an "instantiation" of an Object at a Location.
Definition: instance.h:100
std::string m_walkableId
walkable id
Definition: layer.h:406
std::vector< Instance * > m_changedInstances
holds changed instances after each update
Definition: layer.h:414