Labyrinth
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
map.hpp
1 //////////////////////////////
2 //
3 // File: lxs/game/map.hpp
4 // Author: Maciej Kaminski
5 // E-mail: maciej.a.kaminski@gmail.com
6 // Website: maciejkaminski.pl
7 //
8 // File creation date: Feb 27 2013
9 // This file is part of Project Labyrinth
10 // This file is going to be released under terms of GNU GPL in near future
11 //
12 //////////////////////////////
13 
14 #ifndef __LXS_MAP__
15 #define __LXS_MAP__
16 #include <string>
17 #include "lxs/coregraphics/primitives.hpp"
18 #include "lxs/game/mapwindow.hpp"
19 
20 namespace LxS
21 {
22  namespace CoreGraphics
23  {
24  class Renderable;
25  class R3DCamera;
26  }
27  namespace Game
28  {
29  class MapWindow;
30  class MapWindowOverlay;
31  class GameObject;
32  class Character;
33  class GridVertex;
34  class ItemCatalogue;
35  class Item;
36  //! list of grid vertices
37  typedef std::list<GridVertex>gridvertices;
38 
39  //! Vertex of grid - used in navi-grids
40  class GridVertex
41  {
42  public:
43  //! x coordinate
44  float x;
45  //! y coordinate
46  float y;
47  //! z coordinate
48  float z;
49  //! in cartesian coordinate system
50  float Distance(const GridVertex & other)const ;
51  //! exits set type
52  typedef std::list< std::pair<gridvertices::iterator,float> > estype;
53  //! ids of neighboring vertices paired with distances
55  };
56  //! comparison object (in place of std::less<T> for GridVertices)
57  struct GVcomp//less
58  {
59  //! error margin (epsilon)
60  float epsil;
61  //! functor (so it can act as comparison object)
62  bool operator()(const GridVertex&,
63  const GridVertex&);
64  //! constructor
65  GVcomp():epsil(0){}
66  };
67  //! comparison object for equality for grid vertices
68  struct GVequal
69  {
70  //! error margin (epsilon)
71  float epsil;
72  //! functor (so it can act as comparison object)
73  bool operator()(const GridVertex&,
74  const GridVertex&);
75  //! constructor
76  GVequal():epsil(0){}
77  };
78 
79 
80  //!@brief Map and it's contents - basically renderable game world!
81  //!
82  //! contains all characters, visible objects, sectors
84  {
85  friend class LxS::CoreGraphics::R3DCamera;
86  //!a type definition for members
87  typedef std::list<boost::shared_ptr<GameObject> > gameObjectsList;
88  //! all kinds of objects - including segments, characters, cameras, whatever (cameras aren't currently included?)
89  gameObjectsList members;
90  LxS::CoreGraphics::R3DCamera * p_R3DCamera;
91  //!main character
92  Character * p_Protagonist;
93  //! item_catalogue
94  ItemCatalogue * possible_items;
95  //! area where character can walk for purposes of collision detection algorithm
96  LxS::CoreGraphics::TriangleSet * walkable_area;
97 
98  void PrepareUsableGrid();
99 
100  //! load all map sections
101  void FullLoad(const std::string & filename); // upon first call to initialize
102  //! grid as set of vertices and links to exits - for purposes of pathfinding algorithm
103  gridvertices UsableGrid;
104 
105  //! time spent during battle
106  float BattleTime;
107 
108  union
109  {
110  //! currently selected battle mode enemy (enemy at whom attacks will be directed)
112  Character * TalkModeAdversary;
113  };
114  //! characters with which protagonist is currently fighting // now unused
115  std::list<Character* >enemies;
116 
117  //! overlay - HUD, where status messages are presented
118  MapWindowOverlay * overlay;
119 
120  //! window where map is presented
121  MapWindow * win;
122  //! map file (or saved game)
123  std::string map_fn;
124  //! item_lists - to save what was loaded
125  std::string item_lists;
126  //! swords, coins, potions - KINDS of items, not physically existing items
127  void LoadItems(const std::string & filename);
128  public:
129  /*! \brief remove object from map.
130  * note that this function will erase reference to this
131  * object but won't free memory
132  * \param go an object to be removed
133  */
134  void RemoveObject(GameObject * go);
135  //! to start with saved game, not a new game
136  void SetFilename(std::string s) {map_fn.assign(s);}
137 
138  //! spawn a new item of a particular type and return a pointer to it
139  Item * GetItem(const std::string & name);
140  //! get current mode of game;
142  {
143  return win->GetMode();
144  }
145  //! return grid as set of vertices - for a pathfinding algorithm
146  const gridvertices & GetUsableGrid() const {return UsableGrid;};
147  void RefreshAssets();
148  //! return pointer to main character
149  Character * GetProtagonist() { return p_Protagonist;}
150  //! return pointer to enemy, character is fighting against
152  //! retrieve preprocessed navigation grid
153  const LxS::CoreGraphics::TriangleSet * GetWalkableArea() const {return walkable_area;};
154 
155  //! default constructor - only zeroing stuff, no loading etc;
156  Map();
157  ~Map();
158  //! so that Map class can report things
159  void AttachOverlay(MapWindowOverlay * _o) {overlay=_o;}
160  //! if you want to print something over the map
161  MapWindowOverlay * GetOverlay() {return overlay;}
162  //! so that from game we can configure a window
163  void SetMapWindow(MapWindow * _win) {win=_win;}
164  //! leave battle/talk mode
165  void EnterExplorationMode();
166  //! Upon approaching friendly NPC enters talk mode
167  void EnterTalkMode(Character * adversary);
168  //! Upon approaching enemy enters battle mode
169  void EnterBattleMode(Character * enemy);
170  //! load and set up some resources
171  virtual void Initialize();
172  //! Serialize map to the internetz or to the disk file
173  void SaveFile();
174  virtual void Animate(float dt);
175  //! render
176  virtual void Render(Rendermode mode);
177  //! free textures but not the status
178  virtual void UnloadResources();
179  //! pass message that map was clicked in certain position in 3D space
180  void ClickPosition(float x, float y, float z);
181  //! set movement direction for Protagonist (in menu/combat mode other actions are taken)
182  void SetDirection(float x, float y);
183  //! retrieve dynamic camera object
184  LxS::CoreGraphics::R3DCamera * GetCamera() { return p_R3DCamera;}
185  //! quite obvious
186  void RotateView(float x, float y);
187  private:
188  void PositionCamera();
189  public:
190  //! check whenever object CB collides with something at point
191  //! x/y/z. If it does, return what did it collide against
192  //! \param cb - colliding body - object in question
193  //! \param x,y,z - x,y,z coordinates
194  //! \return a pointer to object (if collision detected) or null pointer if free way
195  GameObject * CollisionDetection(GameObject * cb, float x, float y, float z);
196  unsigned long GetTexelsCount();
197  unsigned long GetFacesCount();
198  //! 100= fully initialized; 0-uninitialized
199  virtual float GetInitializedPercentage();
200 
201 
202  //! if hint named "hint" wasn't displayed,
203  //! display it right now.
204  void DisplayHintBoxIfRequired(std::string hint);
205  };//Character class
206  }
207 }
208 
209 #endif