SDXFrameWork  0.12
SDXFrameWork
 All Classes Namespaces Functions Variables Enumerations Enumerator Pages
Motion.h
1 //Copyright © 2014 SDXFramework
2 //[License]GNU Affero General Public License, version 3
3 //[Contact]http://sourceforge.jp/projects/dxframework/
4 #pragma once
5 #include <Framework/Shape.h>
6 #include <Utility/IMotion.h>
7 
8 namespace SDX
9 {
10  namespace MOTION
11  {
12 
13  //コピペ用ひな形
14  /*
15  template <class TSpeed>
16  class Base : public IMotion
17  {
18  private:
19  TSpeed speed;
20  public:
21  Base(const TSpeed &速度):
22  speed(速度)
23  {}
24 
25  void Update(TShape* 移動対象) override
26  {
27  移動対象
28  }
29  };
30  */
31 
32 
34  class Stop : public IMotion
35  {
36  public:
37  Stop()
38  {}
39  };
40 
41  template <class TSpeed>
44  class ToFront : public IMotion
45  {
46  private:
47  TSpeed speed;
48  public:
49 
51  ToFront(const TSpeed &速度) :
52  speed(速度)
53  {}
54 
55  bool Update(IPosition* 移動対象) override
56  {
57  移動対象->MoveA(speed.Update(), 移動対象->GetAngle());
58 
59  return true;
60  }
61  };
62 
63  template <class TSpeed>
66  class Bound : public IMotion
67  {
68  private:
69  TSpeed speed;
70  double 進行方向;
71  Rect 移動範囲;
72  public:
73 
75  Bound(const TSpeed &速度, const Rect& 移動範囲, double 進行方向) :
76  speed(速度),
77  移動範囲(移動範囲),
78  進行方向(進行方向)
79  {}
80 
81  bool Update(IPosition* 移動対象) override
82  {
83  //範囲外にいる
84  if (移動対象->GetX() < 移動範囲.GetLeft())
85  {
86  if (進行方向 > PAI / 2 && 進行方向 < PAI * 3 / 2)
87  {
88  進行方向 = -PAI - 進行方向;
89  }
90  }
91 
92  if (移動対象->GetX() > 移動範囲.GetRight())
93  {
94  if (進行方向 < PAI / 2 || 進行方向 > PAI * 3 / 2)
95  {
96  進行方向 = PAI - 進行方向;
97  }
98  }
99  else if (移動対象->GetY() < 移動範囲.GetTop())
100  {
101  if (進行方向 > PAI)
102  {
103  進行方向 = PAI * 2 - 進行方向;
104  }
105  }
106  else if (移動対象->GetY() > 移動範囲.GetBottom())
107  {
108  if (進行方向 < PAI)
109  {
110  進行方向 = -進行方向;
111  }
112  }
113 
114  if (進行方向 < 0) 進行方向 += PAI * 2;
115  if (進行方向 > PAI * 2) 進行方向 -= PAI * 2;
116 
117  移動対象->MoveA(speed.Update(), 進行方向);
118 
119  return true;
120  }
121  };
122 
123  template <class TSpeed>
126  class ToPoint : public IMotion
127  {
128  private:
129  TSpeed speed;
130  Point 目標座標;
131  public:
133  ToPoint(const TSpeed &速度, const Point& 目標座標) :
134  speed(速度),
135  目標座標(目標座標)
136  {}
137 
138  bool Update(IPosition* 移動対象) override
139  {
140  //移動対象
141  const double nowSpeed = speed.Update();
142  const double lx = 目標座標.GetX() - 移動対象->GetX();
143  const double ly = 目標座標.GetY() - 移動対象->GetY();
144 
145  if (lx * lx + ly * ly <= nowSpeed * nowSpeed)
146  {
147  移動対象->SetPos(目標座標.GetX(), 目標座標.GetY());
148  return false;
149  }
150  else
151  {
152  const double angle = atan2(ly, lx);
153  移動対象->MoveA(nowSpeed, angle);
154  }
155 
156  return true;
157  }
158  };
159 
160  template <class TSpeed>
163  class Orbit : public IMotion
164  {
165  private:
166  TSpeed speed;
167  Point 軌道中心;
168  double 半径X;
169  double 半径Y;
170  double 角度;
171  double 前移動量X;
172  double 前移動量Y;
173  public:
174 
176  Orbit(const TSpeed &角速度 , double 半径X , double 半径Y , double 初期進行方向 = 0.0) :
177  speed(角速度),
178  半径X(半径X),
179  半径Y(半径Y),
180  角度(初期進行方向 - PAI / 2),
181  前移動量X(cos(初期進行方向 - PAI/2) * 半径X),
182  前移動量Y(sin(初期進行方向 - PAI/2) * 半径Y)
183  {}
184 
185  bool Update(IPosition* 移動対象) override
186  {
187  //移動対象
188  角度 += speed.Update();
189 
190  const double nextX = cos(角度) * 半径X;
191  const double nextY = sin(角度) * 半径Y;
192 
193  移動対象->Move(nextX - 前移動量X, nextY - 前移動量Y);
194 
195  前移動量X = nextX;
196  前移動量Y = nextY;
197 
198  return true;
199  }
200  };
201 
204  class Vibrate : public IMotion
205  {
206  private:
207  double 最大振れ幅;
208  double 移動X = 0;
209  double 移動Y = 0;
210  public:
211 
213  Vibrate(double 最大振れ幅) :
214  最大振れ幅(最大振れ幅)
215  {}
216 
217  bool Update(IPosition* 移動対象) override
218  {
219  double randX = Rand::Get(-最大振れ幅, 最大振れ幅);
220  double randY = Rand::Get(-最大振れ幅, 最大振れ幅);
221  移動対象->Move(randX-移動X, randY-移動Y);
222  移動X = randX;
223  移動Y = randY;
224 
225  return true;
226  }
227  };
228  }
229 }
目標座標に移動.
Definition: Motion.h:126
const double PAI
円周率
Definition: SDX.h:26
矩形を表す図形クラス.
Definition: Rect.h:22
virtual double GetAngle() const
角度を取得する.
Definition: IPosition.h:56
double GetBottom() const
下端のY座標を取得.
Definition: Rect.h:136
double GetTop() const
上端のY座標を取得.
Definition: Rect.h:124
double GetRight() const
右端のX座標を取得.
Definition: Rect.h:130
double GetLeft() const
左端のX座標を取得.
Definition: Rect.h:118
double GetY() const override
Y座標を取得.
Definition: Point.h:68
円周上を移動.
Definition: Motion.h:163
Bound(const TSpeed &速度, const Rect &移動範囲, double 進行方向)
速度と移動範囲、最初の移動方向を指定.
Definition: Motion.h:75
点を表す図形クラス.
Definition: Point.h:22
移動方法のインターフェース.
Definition: IMotion.h:14
ToFront(const TSpeed &速度)
速度を指定.
Definition: Motion.h:51
bool Update(IPosition *移動対象) override
Motion終了時はfalseを返す.
Definition: Motion.h:138
virtual double GetY() const =0
Y座標を取得.
bool Update(IPosition *移動対象) override
Motion終了時はfalseを返す.
Definition: Motion.h:217
Vibrate(double 最大振れ幅)
コンストラクタ.
Definition: Motion.h:213
virtual void Move(double X移動量, double Y移動量)=0
相対座標で移動.
ToPoint(const TSpeed &速度, const Point &目標座標)
コンストラクタ.
Definition: Motion.h:133
bool Update(IPosition *移動対象) override
Motion終了時はfalseを返す.
Definition: Motion.h:185
bool Update(IPosition *移動対象) override
Motion終了時はfalseを返す.
Definition: Motion.h:81
範囲内で跳ね返る.
Definition: Motion.h:66
その場所で停止.
Definition: Motion.h:34
前方に移動.
Definition: Motion.h:44
virtual void SetPos(double X座標, double Y座標)=0
指定座標に移動.
位置と方向を持つクラスのインターフェース.
Definition: IPosition.h:11
範囲内で振動.
Definition: Motion.h:204
virtual double GetX() const =0
X座標を取得.
static double Get(double 最大値)
0~最大値の乱数を取得.
Definition: Rand.h:29
bool Update(IPosition *移動対象) override
Motion終了時はfalseを返す.
Definition: Motion.h:55
double GetX() const override
X座標を取得.
Definition: Point.h:63
void MoveA(double 距離, double 方向)
極座標で移動.
Definition: IPosition.h:41
Orbit(const TSpeed &角速度, double 半径X, double 半径Y, double 初期進行方向=0.0)
コンストラクタ.
Definition: Motion.h:176