YAPOG  0.0.1
Yet Another Pokemon Online Game
PhysicsCore.cpp
Go to the documentation of this file.
5 
6 namespace yap
7 {
9  Vector2 (0.0f, 0.0f);
11  Vector2 (150.0f, 150.0f);
12 
14  : OnStopped ()
15  , OnMoved ()
16  , OnVelocityChanged ()
17  , velocity_ ()
18  , lastVelocity_ ()
19  , minVelocity_ (DEFAULT_MIN_VELOCITY_BOUNDS)
20  , maxVelocity_ (DEFAULT_MAX_VELOCITY_BOUNDS)
21  , move_ ()
22  {
23  }
24 
26  {
27  }
28 
30  : OnStopped ()
31  , OnMoved ()
32  , OnVelocityChanged ()
33  , velocity_ (copy.velocity_)
34  , lastVelocity_ (copy.lastVelocity_)
35  , minVelocity_ (copy.minVelocity_)
36  , maxVelocity_ (copy.maxVelocity_)
37  , move_ (copy.move_)
38  {
39  }
40 
41  void PhysicsCore::ApplyForce (const Vector2& force)
42  {
43  HandleApplyForce (force);
44  }
45 
47  {
50 
52  }
53 
54  const Vector2& PhysicsCore::GetMove () const
55  {
56  return move_;
57  }
58 
59  void PhysicsCore::Update (const Time& dt)
60  {
61  Vector2 oldMove = move_;
62 
63  move_ = velocity_ * dt.GetValue ();
64 
65  ResetVelocity (dt);
66 
67  if (move_ == oldMove)
68  return;
69 
70  if (oldMove == Vector2 (0.0f, 0.0f))
71  OnMoved (*this, EmptyEventArgs ());
72  else if (move_ == Vector2 (0.0f, 0.0f))
73  OnStopped (*this, EmptyEventArgs ());
74  }
75 
76  void PhysicsCore::SetVelocityBounds (const Vector2& min, const Vector2& max)
77  {
78  if (max.x < min.x ||
79  max.y < min.y)
80  YAPOG_THROW("[PhysicsCore] Max bound must be greater than min bound.");
81 
82  minVelocity_ = min;
83  maxVelocity_ = max;
84  }
85 
86  void PhysicsCore::RawSetVelocity (const Vector2& velocity)
87  {
88  SetVelocity (velocity);
89  }
90 
92  {
93  return velocity_;
94  }
95 
96  void PhysicsCore::SetVelocity (const Vector2& velocity)
97  {
98  if (velocity == velocity_)
99  {
100  if (velocity != lastVelocity_)
102  *this,
105  velocity_));
106 
107  return;
108  }
109 
110  BoundVelocity (velocity, velocity_);
111 
112  if (velocity_ == lastVelocity_)
113  return;
114 
116  *this,
119  velocity_));
120  }
121 
122  void PhysicsCore::ResetVelocity (const Vector2& velocity)
123  {
125 
126  BoundVelocity (velocity, velocity_);
127  }
128 
129  void PhysicsCore::BoundVelocity (const Vector2& velocity, Vector2& result)
130  {
131  result = Vector2 (
132  velocity.x < 0 ?
133  MathHelper::Clamp (velocity.x, -maxVelocity_.x, -minVelocity_.x) :
134  MathHelper::Clamp (velocity.x, minVelocity_.x, maxVelocity_.x),
135  velocity.y < 0 ?
136  MathHelper::Clamp (velocity.y, -maxVelocity_.y, -minVelocity_.y) :
137  MathHelper::Clamp (velocity.y, minVelocity_.y, maxVelocity_.y));
138  }
139 } // namespace yap