pong: split large time function
Stefan Schuermans

Stefan Schuermans commited on 2019-06-10 18:16:11
Showing 2 changed files, with 66 additions and 18 deletions.

... ...
@@ -108,7 +108,31 @@ void Pong::redraw()
108 108
 /// callback when requested time reached
109 109
 void Pong::timeCall()
110 110
 {
111
-  // bounce ball at sides
111
+  // bounce ball
112
+  bounceBall();
113
+
114
+  // move ball
115
+  m_ballPosX += m_ballDirX;
116
+  m_ballPosY += m_ballDirY;
117
+
118
+  // draw and send frame
119
+  redraw();
120
+
121
+  // request next call if needed
122
+  planTimeCall();
123
+}
124
+
125
+/// bounce ball
126
+void Pong::bounceBall()
127
+{
128
+  bounceBallSide();
129
+  bounceBallLeft();
130
+  bounceBallRight();
131
+}
132
+
133
+/// bounce ball at sides
134
+void Pong::bounceBallSide()
135
+{
112 136
   if (m_ballPosY <= 0 && m_ballDirY < 0) {
113 137
     m_ballDirY = 1;
114 138
   }
... ...
@@ -121,9 +145,15 @@ void Pong::timeCall()
121 145
   if (m_ballPosX >= m_width - 1 && m_ballDirX > 0) {
122 146
     m_ballDirX = -1;
123 147
   }
148
+}
149
+
150
+/// bounce ball at left pad
151
+void Pong::bounceBallLeft()
152
+{
153
+  if (m_ballPosX != 1 || m_ballDirX >= 0) {
154
+    return;
155
+  }
124 156
 
125
-  // bounce ball at left player
126
-  if (m_ballPosX == 1 && m_ballDirX < 0) {
127 157
   // top corner
128 158
   if (m_ballPosY == m_leftPosY - 1 && m_ballDirY > 0) {
129 159
     m_ballDirX = 1;
... ...
@@ -141,8 +173,13 @@ void Pong::timeCall()
141 173
   }
142 174
 }
143 175
 
144
-  // bounce ball at right player
145
-  if (m_ballPosX == m_width - 2 && m_ballDirX > 0) {
176
+/// bounce ball at right pad
177
+void Pong::bounceBallRight()
178
+{
179
+  if (m_ballPosX != m_width - 2 || m_ballDirX <= 0) {
180
+    return;
181
+  }
182
+
146 183
   // top corner
147 184
   if (m_ballPosY == m_rightPosY - 1 && m_ballDirY > 0) {
148 185
     m_ballDirX = -1;
... ...
@@ -160,14 +199,14 @@ void Pong::timeCall()
160 199
   }
161 200
 }
162 201
 
163
-  // move ball
164
-  m_ballPosX += m_ballDirX;
165
-  m_ballPosY += m_ballDirY;
166
-
167
-  // draw and send frame
168
-  redraw();
202
+/// request next time call - or cancel request if not needed
203
+void Pong::planTimeCall()
204
+{
205
+  if (m_ballPosX < 0 ||  m_ballPosY < 0) {
206
+    m_mgrs.m_callMgr.cancelTimeCall(this);
207
+    return;
208
+  }
169 209
 
170
-  // request next call
171 210
   Time stepTime;
172 211
   stepTime.fromMs(100);
173 212
   m_mgrs.m_callMgr.requestTimeCall(this, Time::now() + stepTime);
... ...
@@ -181,8 +220,8 @@ void Pong::hideBall()
181 220
   m_ballDirX = 0;
182 221
   m_ballDirY = 0;
183 222
 
184
-  // cancel time callback request
185
-  m_mgrs.m_callMgr.cancelTimeCall(this);
223
+  // update time call request
224
+  planTimeCall();
186 225
 }
187 226
 
188 227
 /// start ball
... ...
@@ -195,10 +234,8 @@ void Pong::startBall()
195 234
   m_ballDirX = (rand() & 1) * 2 - 1;
196 235
   m_ballDirY = (rand() & 1) * 2 - 1;
197 236
 
198
-  // request first time call
199
-  Time stepTime;
200
-  stepTime.fromMs(100);
201
-  m_mgrs.m_callMgr.requestTimeCall(this, Time::now() + stepTime);
237
+  // request first time call if needed
238
+  planTimeCall();
202 239
 }
203 240
 
204 241
 } // namespace Blinker
... ...
@@ -61,6 +61,21 @@ protected:
61 61
   /// callback when requested time reached
62 62
   virtual void timeCall();
63 63
 
64
+  /// bounce ball
65
+  void bounceBall();
66
+
67
+  /// bounce ball at sides
68
+  void bounceBallSide();
69
+
70
+  /// bounce ball at left pad
71
+  void bounceBallLeft();
72
+
73
+  /// bounce ball at right pad
74
+  void bounceBallRight();
75
+
76
+  /// request next time call - or cancel request if not needed
77
+  void planTimeCall();
78
+
64 79
   /// move ball out of the field and halt it
65 80
   void hideBall();
66 81
 
67 82