pong: activate when player joins
Stefan Schuermans

Stefan Schuermans commited on 2019-06-15 21:44:37
Showing 4 changed files, with 44 additions and 42 deletions.

... ...
@@ -93,6 +93,12 @@ void Game::deactivate()
93 93
   sendFrame();
94 94
 }
95 95
 
96
+/// check if game is active
97
+bool Game::isActive() const
98
+{
99
+  return ! m_imgBuf.empty(); // game is active if there is an image buffer
100
+}
101
+
96 102
 /// set pixel in image buffer
97 103
 void Game::pixel(int y, int x, ColorData const &cd)
98 104
 {
... ...
@@ -68,6 +68,9 @@ protected:
68 68
   /// deactivate game: tear down image buffer, deactivate output
69 69
   void deactivate();
70 70
 
71
+  /// check if game is active
72
+  bool isActive() const;
73
+
71 74
   /// check if integer is between minimum and maximum values
72 75
   static bool checkLimitInt(int i, int min, int max)
73 76
   {
... ...
@@ -52,9 +52,6 @@ Pong::Pong(const std::string &name, Mgrs &mgrs, const Directory &dirBase):
52 52
   // open operator connection interfaces for left and right player
53 53
   m_mgrs.m_opMgr.open(m_name + OpConnSuffixLeft, this);
54 54
   m_mgrs.m_opMgr.open(m_name + OpConnSuffixRight, this);
55
-
56
-  // FIXME: activate at begin for initial development only
57
-  activate();
58 55
 }
59 56
 
60 57
 /// virtual destructor
... ...
@@ -125,15 +122,24 @@ void Pong::newOpConn(const std::string &name, OpConn *pConn)
125 122
   // left player joins if none there yet
126 123
   if (name == m_name + OpConnSuffixLeft && ! m_pConnLeft) {
127 124
     m_pConnLeft = pConn;
128
-    redraw(); // player color is different for phone / computer
129
-    return;
130 125
   }
131 126
   // right player joins if none there yet
132
-  if (name == m_name + OpConnSuffixRight && ! m_pConnRight) {
127
+  else if (name == m_name + OpConnSuffixRight && ! m_pConnRight) {
133 128
     m_pConnRight = pConn;
134
-    redraw(); // player color is different for phone / computer
129
+  }
130
+  // nothing happens
131
+  else {
135 132
     return;
136 133
   }
134
+
135
+  // already active
136
+  if (isActive()) {
137
+    redraw(); // player color is different for phone / computer
138
+  }
139
+  // first player joined
140
+  else {
141
+    activate();
142
+  }
137 143
 }
138 144
 
139 145
 /**
... ...
@@ -146,12 +152,10 @@ void Pong::opConnRecvKey(OpConn *pConn, char key)
146 152
   // left player
147 153
   if (pConn == m_pConnLeft) {
148 154
     processKey(key, m_leftPosY);
149
-    return;
150 155
   }
151 156
   // right player
152
-  if (pConn == m_pConnRight) {
157
+  else if (pConn == m_pConnRight) {
153 158
     processKey(key, m_rightPosY);
154
-    return;
155 159
   }
156 160
 }
157 161
 
... ...
@@ -177,15 +181,24 @@ void Pong::opConnClose(OpConn *pConn)
177 181
   // left player leaves
178 182
   if (pConn == m_pConnLeft) {
179 183
     m_pConnLeft = NULL;
180
-    redraw(); // player color is different for phone / computer
181
-    return;
182 184
   }
183 185
   // right player leaves
184
-  if (pConn == m_pConnRight) {
186
+  else if (pConn == m_pConnRight) {
185 187
     m_pConnRight = NULL;
186
-    redraw(); // player color is different for phone / computer
188
+  }
189
+  // nothing happens
190
+  else {
187 191
     return;
188 192
   }
193
+
194
+  // still a phone player there
195
+  if (m_pConnLeft || m_pConnRight) {
196
+    redraw(); // player color is different for phone / computer
197
+  }
198
+  // no phone players left
199
+  else {
200
+    deactivate();
201
+  }
189 202
 }
190 203
 
191 204
 /// re-initialize game (e.g. due to config change)
... ...
@@ -204,7 +217,11 @@ void Pong::reinitialize()
204 217
   color2data(m_filePadColor, m_padColor);
205 218
   color2data(m_fileComputerColor, m_computerColor);
206 219
 
207
-  // FIXME: start ball for development
220
+  // reset scores
221
+  m_scoreLeft = 0;
222
+  m_scoreRight = 0;
223
+
224
+  // start first ball
208 225
   startBall();
209 226
 }
210 227
 
... ...
@@ -241,7 +258,7 @@ void Pong::redraw()
241 258
 void Pong::timeCall()
242 259
 {
243 260
   // game is running
244
-  if (m_goalDelay <= 0 && m_ballPosX >= 0 && m_ballPosY >= 0) {
261
+  if (m_goalDelay <= 0) {
245 262
 
246 263
     // computer player: move pads
247 264
     if (! m_pConnLeft) {
... ...
@@ -528,8 +545,8 @@ void Pong::detectGoal()
528 545
 /// request next time call - or cancel request if not needed
529 546
 void Pong::planTimeCall()
530 547
 {
531
-  // no time call needed if game is not running
532
-  if (m_ballPosX < 0 ||  m_ballPosY < 0) {
548
+  // no time call needed if not active
549
+  if (! isActive()) {
533 550
     m_mgrs.m_callMgr.cancelTimeCall(this);
534 551
     return;
535 552
   }
... ...
@@ -544,27 +561,6 @@ void Pong::planTimeCall()
544 561
   m_mgrs.m_callMgr.requestTimeCall(this, Time::now() + stepTime);
545 562
 }
546 563
 
547
-/// move ball out of the field and halt it
548
-void Pong::hideBall()
549
-{
550
-  m_ballPosX = -1;
551
-  m_ballPosY = -1;
552
-  m_ballDirX = 0;
553
-  m_ballDirY = 0;
554
-
555
-  // no delays, ball has not bounced at pad
556
-  m_leftDelay = 0;
557
-  m_rightDelay = 0;
558
-  m_goalDelay = 0;
559
-  m_bounceCnt = 0;
560
-
561
-  // draw and send frame
562
-  redraw();
563
-
564
-  // update time call request
565
-  planTimeCall();
566
-}
567
-
568 564
 /// start ball
569 565
 void Pong::startBall()
570 566
 {
... ...
@@ -158,9 +158,6 @@ protected:
158 158
   /// request next time call - or cancel request if not needed
159 159
   void planTimeCall();
160 160
 
161
-  /// move ball out of the field and halt it
162
-  void hideBall();
163
-
164 161
   /// start ball
165 162
   void startBall();
166 163
 
167 164