YAPOG  0.0.1
Yet Another Pokemon Online Game
GridCollidableArea.cpp
Go to the documentation of this file.
9 
10 namespace yap
11 {
14 
16  : CollidableArea ()
17  , cellSize_ ()
18  , cells_ (0, 0, nullptr)
19  {
20  }
21 
23  {
24  }
25 
27  uint vSegmentCount,
28  uint hSegmentCount)
29  {
30  if (vSegmentCount < MIN_VSEGMENT_COUNT)
32  "Invalid vertical segment count: `" +
33  StringHelper::ToString (vSegmentCount) +
34  "'.");
35 
36  if (hSegmentCount < MIN_HSEGMENT_COUNT)
38  "Invalid horizontal segment count: `" +
39  StringHelper::ToString (hSegmentCount) +
40  "'.");
41 
42  vSegmentCount_ = vSegmentCount;
43  hSegmentCount_ = hSegmentCount;
44 
45  cells_.Resize (
48  new CollidableAreaCell ());
49  }
50 
52  const ICollidable& collidable,
53  const Vector2& offset,
54  const WorldObject& parent) const
55  {
56  UIntRect collidableRect;
57  GetCollidableRectangle (collidable, offset, collidableRect);
58 
59  for (uint y = collidableRect.top;
60  y < collidableRect.top + collidableRect.height;
61  ++y)
62  for (uint x = collidableRect.left;
63  x < collidableRect.left + collidableRect.width;
64  ++x)
65  if (cells_ (x, y)->CollidesWith (collidable, offset, parent))
66  return true;
67 
68  return false;
69  }
70 
72  const ICollidable& collidable,
73  MapEventQueue& events,
74  DynamicWorldObject& parent) const
75  {
76  UIntRect collidableRect;
77  GetCollidableRectangle (collidable, VECTOR2_ZERO, collidableRect);
78 
79  for (uint y = collidableRect.top;
80  y < collidableRect.top + collidableRect.height;
81  ++y)
82  for (uint x = collidableRect.left;
83  x < collidableRect.left + collidableRect.width;
84  ++x)
85  cells_ (x, y)->GetEventsCollidingWith (collidable, events, parent);
86  }
87 
89  {
91 
92  cellSize_ = Vector2 (size.x / hSegmentCount_, size.y / vSegmentCount_);
93  }
94 
96  ICollidable* collidable,
97  const MapCollidableInfo::PtrType& mapCollidableInfo)
98  {
99  UIntRect collidableRect;
100  GetCollidableRectangle (*collidable, collidableRect);
101 
102  for (uint y = collidableRect.top;
103  y < collidableRect.top + collidableRect.height;
104  ++y)
105  for (uint x = collidableRect.left;
106  x < collidableRect.left + collidableRect.width;
107  ++x)
108  cells_ (x, y)->AddPhysicsCollidable (collidable, mapCollidableInfo);
109  }
110 
112  ICollidable* collidable)
113  {
114  UIntRect collidableRect;
115  GetCollidableRectangle (*collidable, collidableRect);
116 
117  for (uint y = collidableRect.top;
118  y < collidableRect.top + collidableRect.height;
119  ++y)
120  for (uint x = collidableRect.left;
121  x < collidableRect.left + collidableRect.width;
122  ++x)
123  cells_ (x, y)->RemovePhysicsCollidable (collidable);
124  }
125 
127  ICollidable* collidable,
128  const MapEventInfo::PtrType& mapEventInfo)
129  {
130  UIntRect collidableRect;
131  GetCollidableRectangle (*collidable, collidableRect);
132 
133  for (uint y = collidableRect.top;
134  y < collidableRect.top + collidableRect.height;
135  ++y)
136  for (uint x = collidableRect.left;
137  x < collidableRect.left + collidableRect.width;
138  ++x)
139  cells_ (x, y)->AddEventCollidable (collidable, mapEventInfo);
140  }
141 
143  ICollidable* collidable)
144  {
145  UIntRect collidableRect;
146  GetCollidableRectangle (*collidable, collidableRect);
147 
148  for (uint y = collidableRect.top;
149  y < collidableRect.top + collidableRect.height;
150  ++y)
151  for (uint x = collidableRect.left;
152  x < collidableRect.left + collidableRect.width;
153  ++x)
154  cells_ (x, y)->RemoveEventCollidable (collidable);
155  }
156 
158  const ICollidable& collidable,
159  UIntRect& rectangle) const
160  {
161  GetCollidableRectangle (collidable, Vector2 (), rectangle);
162  }
163 
165  const ICollidable& collidable,
166  const Vector2& offset,
167  UIntRect& rectangle) const
168  {
169  int left = collidable.GetTopLeft ().x + offset.x;
170  int top = collidable.GetTopLeft ().y + offset.y;
171  int right = collidable.GetBottomRight ().x + offset.x;
172  int bottom = collidable.GetBottomRight ().y + offset.y;
173 
174  rectangle.left = MathHelper::Clamp (
175  static_cast<int> (left / cellSize_.x),
176  0,
177  static_cast<int> (hSegmentCount_ - 1));
178 
179  rectangle.top = MathHelper::Clamp (
180  static_cast<int> (top / cellSize_.y),
181  0,
182  static_cast<int> (vSegmentCount_ - 1));
183 
184  rectangle.width = MathHelper::Clamp (
185  static_cast<int> ((right - left) / cellSize_.x + 1),
186  0,
187  static_cast<int> (hSegmentCount_ - rectangle.left));
188 
189  rectangle.height = MathHelper::Clamp (
190  static_cast<int> ((bottom - top) / cellSize_.y + 1),
191  0,
192  static_cast<int> (vSegmentCount_ - rectangle.top));
193  }
194 } // namespace yap