YAPOG  0.0.1
Yet Another Pokemon Online Game
DynamicWorldObject.cpp
Go to the documentation of this file.
8 
9 namespace yap
10 {
13  Vector2 (150.0f, 150.0f);
14 
16  : WorldObject (id)
17  , OnMoved ()
18  , OnVelocityChanged ()
19  , OnStateChanged ()
20  , worldID_ ()
21  , state_ (DEFAULT_INACTIVE_STATE)
22  , physicsCore_ (nullptr)
23  , maxVelocity_ (DEFAULT_MAX_VELOCITY)
24  , events_ ()
25  , triggerBoundingBoxes_ ()
26  , sourceBoundingBoxes_ ()
27  {
28  }
29 
31  {
32  delete physicsCore_;
33  physicsCore_ = nullptr;
34  }
35 
37  : WorldObject (copy)
38  , OnMoved ()
39  , OnVelocityChanged ()
40  , OnStateChanged ()
41  , worldID_ (copy.worldID_)
42  , state_ (copy.state_)
43  , physicsCore_ (nullptr)
44  , maxVelocity_ (copy.maxVelocity_)
45  , events_ ()
46  , triggerBoundingBoxes_ (copy.triggerBoundingBoxes_)
47  , sourceBoundingBoxes_ (copy.sourceBoundingBoxes_)
48  {
49  if (copy.physicsCore_ != nullptr)
51  }
52 
54  {
55  visitor.VisitDynamicWorldObject (*this);
56  }
57 
59  IDynamicWorldObjectConstVisitor& visitor) const
60  {
61  visitor.VisitDynamicWorldObject (*this);
62  }
63 
65  {
66  return worldID_;
67  }
68 
70  {
71  worldID_ = id;
72 
73  HandleSetWorldID (id);
74  }
75 
77  {
79  }
80 
82  {
83  return maxVelocity_;
84  }
85 
86  void DynamicWorldObject::SetMaxVelocity (const Vector2& maxVelocity)
87  {
88  maxVelocity_ = maxVelocity;
89  }
90 
92  {
93  if (physicsCore_ == physicsCore)
94  return;
95 
96  delete physicsCore_;
97 
98  physicsCore_ = physicsCore;
99 
101  [this] (const PhysicsCore& sender, const EmptyEventArgs& args)
102  {
103  TryChangeState ("Moving");
104  };
105 
107  [this] (const PhysicsCore& sender, const EmptyEventArgs& args)
108  {
109  SetInactive ();
110  };
111 
113  [this] (const PhysicsCore& sender,
115  {
116  HandleOnVelocityChanged (args.Old, args.Current);
117  };
118  }
119 
121  {
122  physicsCore_->ApplyForce (force);
123 
124  HandleApplyForce (force);
125  }
126 
128  {
129  return physicsCore_->GetMove ();
130  }
131 
133  {
134  physicsCore_->RawSetVelocity (velocity);
135  }
136 
138  {
140  }
141 
143  {
144  return state_.GetName ();
145  }
146 
148  {
149  return state_.GetLogicalName ();
150  }
151 
153  {
154  if (IsActive ())
155  {
156  if (state_.IsJoinedTo (state))
157  {
158  SetState (state);
159  return true;
160  }
161 
162  if (state != DEFAULT_INACTIVE_STATE)
163  return GetState () == state;
164 
165  SetInactive ();
166 
167  return true;
168  }
169 
170  SetState (state);
171 
172  return true;
173  }
174 
176  {
178 
180  }
181 
183  {
184  SetState (state);
185  }
186 
188  {
189  const String& oldState = state_.GetName ();
190 
191  state_ = state;
192 
195  oldState,
196  state_.GetName ()));
197 
198  HandleSetState (state);
199  }
200 
202  {
203  return GetLogicalState () == "Moving";
204  }
205 
207  {
208  AdjustCollidablePosition (*boundingBox);
209 
211  }
212 
214  {
215  events_.Add (event);
216 
217  event->AddToEventBoundingBoxCollection (sourceBoundingBoxes_, *this);
218  }
219 
221  {
222  events_.Remove (event);
223 
224  event->RemoveFromEventBoundingBoxCollection (sourceBoundingBoxes_);
225  }
226 
228  const CollidableArea& collidableArea,
229  MapEventQueue& events) const
230  {
231  triggerBoundingBoxes_.GetEventsCollidingWith (collidableArea, events);
232  }
233 
235  {
236  HandleUpdate (dt);
237  }
238 
240  {
241  }
242 
244  CollidableArea* collidableArea)
245  {
246  WorldObject::HandleSetCollidableArea (collidableArea);
247 
248  triggerBoundingBoxes_.SetCollidableArea (*this, collidableArea);
249  sourceBoundingBoxes_.SetCollidableArea (*this, collidableArea);
250  }
251 
253  {
254  }
255 
257  {
258  physicsCore_->Update (dt);
259  }
260 
262  {
263  }
264 
266  {
267  WorldObject::HandleMove (offset);
268 
269  triggerBoundingBoxes_.Move (offset);
270  sourceBoundingBoxes_.Move (offset);
271 
272  OnMoved (*this, offset);
273  }
274 
276  {
277  WorldObject::HandleScale (factor);
278 
279  triggerBoundingBoxes_.Scale (factor);
280  sourceBoundingBoxes_.Scale (factor);
281  }
282 
284  {
286 
289  }
290 
292  {
294 
297  }
298 
300  const Vector2& oldVelocity,
301  const Vector2& currentVelocity)
302  {
304  *this,
306  oldVelocity,
307  currentVelocity));
308  }
309 } // namespace yap