ScummVM API documentation
GridMap2D.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 /*
23  * Copyright (C) 2006-2010 - Frictional Games
24  *
25  * This file is part of HPL1 Engine.
26  */
27 
28 #ifndef HPL_GRIDMAP2D_H
29 #define HPL_GRIDMAP2D_H
30 
31 #include "common/array.h"
32 #include "hpl1/engine/graphics/GraphicsTypes.h"
33 #include "hpl1/engine/math/MathTypes.h"
34 #include "common/stablemap.h"
35 
36 namespace hpl {
37 
38 class iEntity2D;
39 class iLowLevelGraphics;
40 
42 class cGrid2D;
43 class cGridMap2D;
44 
45 typedef Common::Array<cGrid2D *> tGrid2DPtrVec;
46 typedef tGrid2DPtrVec::iterator tGrid2DPtrVecIt;
47 
49 
50 public:
51  cGrid2DObject(iEntity2D *apEntity, cGridMap2D *apGridMap, unsigned int alHandle);
52 
53  void Update(const cRect2f &aRect);
54  void Destroy();
55 
56  unsigned int GetHandle() { return mlHandle; }
57  iEntity2D *GetEntity() { return mpEntity; }
58  bool FirstTime(unsigned int alGlobalCount) {
59  if (alGlobalCount < mlCount)
60  return false;
61  mlCount = alGlobalCount + 1;
62  return true;
63  }
64 
65 private:
66  cVector2l mvPosition;
67  cVector2f mvSize;
68  cVector2l mvGridSpan;
69 
70  tGrid2DPtrVec mvGridParents;
71 
72  iEntity2D *mpEntity;
73  cGridMap2D *mpGridMap;
74 
75  bool mbIsInOuter;
76  bool mbIsInGLobal;
77  unsigned int mlCount;
78  int mlHandle;
79 };
80 
82 
84 typedef tGrid2DObjectMap::iterator tGrid2DObjectMapIt;
85 
86 class cGrid2D {
87  friend class cGridMap2DRectIt;
88 
89 public:
90  void Add(cGrid2DObject *pObject) {
91  m_mapObjects.insert(tGrid2DObjectMap::value_type(
92  pObject->GetHandle(), pObject));
93  }
94 
95  void Erase(unsigned int alHandle) {
96  m_mapObjects.erase(alHandle);
97  }
98 
99 private:
100  tGrid2DObjectMap m_mapObjects;
101 };
102 
104 
105 class cGridMap2D;
106 
108 public:
109  virtual ~iGridMap2DIt() = default;
110  virtual bool HasNext() = 0;
111  virtual iEntity2D *Next() = 0;
112 };
113 
115 public:
116  cGridMap2DRectIt(cGridMap2D *apGridMap, cVector2l avPos, cVector2l avSize);
117  virtual ~cGridMap2DRectIt() = default;
118 
119  bool HasNext();
120  iEntity2D *Next();
121 
122 private:
123  cGridMap2D *mpGridMap;
124 
125  int mlType; // 0=global 1=outer 2=grids
126  tGrid2DObjectMapIt mIt;
127 
128  cVector2l mvPos;
129  cVector2l mvSize;
130  int mlGridNum;
131  int mlGridAdd;
132  int mlGridRowCount;
133  int mlGridColCount;
134 
135  bool mbUpdated;
136 
137  cGrid2DObject *mpObject;
138 
139  void GetGridObject();
140 };
141 
143 
145 typedef tGrid2DVec::iterator tGrid2DVecIt;
146 
147 class cGridMap2D {
148  friend class cGrid2DObject;
149  friend class cGridMap2DRectIt;
150 
151 public:
152  cGridMap2D(cVector2l avSize, cVector2l avGridSize, cVector2l avMaxGridSpan);
153  ~cGridMap2D();
154 
155  iGridMap2DIt *GetRectIterator(const cRect2f &aRect);
156 
157  cGrid2D *GetAt(int alX, int alY);
158 
159  bool AddEntity(iEntity2D *apEntity);
160  bool RemoveEntity(iEntity2D *apEntity);
161 
162  int GetMaxArraySize() { return mvMaxGridSpan.x * mvMaxGridSpan.y; }
163  const cVector2l &GetMaxGridSpan() { return mvMaxGridSpan; }
164  const cVector2l &GetGridSize() { return mvGridSize; }
165  const cVector2l &GetGridNum() { return mvGridNum; }
166 
167  tGrid2DObjectMap *GetAllMap() { return &m_mapAllObjects; }
168 
169  void DrawGrid(iLowLevelGraphics *apLowLevel, float afZ = 100, cColor aCol = cColor(1));
170  void DrawEntityGrids(iLowLevelGraphics *apLowLevel, cVector2f avWorldPos, float afZ = 101, cColor aCol = cColor(1, 0, 1, 1));
171 
172 private:
173  // The Grids
174  tGrid2DVec mvGrids;
175 
176  // all objects
177  // This is a list of all objects in the grid.
178  tGrid2DObjectMap m_mapAllObjects;
179 
180  // Global Objects
181  // These are objects that affect the entire map
182  tGrid2DObjectMap m_mapGlobalObjects;
183 
184  // Outer Objects
185  // These are objects outside the bounds of the grid
186  tGrid2DObjectMap m_mapOuterObjects;
187 
188  // The count increases every time a fetch on the map is made.
189  // (all call to GetFirstEntity)
190  unsigned int mlGlobalCount;
191 
192  unsigned int mlHandleCount;
193 
194  cVector2l mvSize;
195  cVector2l mvGridNum;
196  cVector2l mvGridSize;
197  cVector2l mvMaxGridSpan;
198 };
199 
200 } // namespace hpl
201 
202 #endif // HPL_GRIDMAP2D_H
Definition: AI.h:36
Definition: GridMap2D.h:48
Definition: util.h:213
T * iterator
Definition: array.h:54
typename TreeT::BasicIterator iterator
Definition: stablemap.h:48
Definition: GridMap2D.h:114
Definition: GridMap2D.h:147
Definition: GridMap2D.h:107
Definition: Color.h:37
Definition: GridMap2D.h:86
Definition: Entity2D.h:39
Definition: LowLevelGraphics.h:200