tetris: dropping stones
Stefan Schuermans

Stefan Schuermans commited on 2019-07-14 17:51:57
Showing 2 changed files, with 29 additions and 7 deletions.

... ...
@@ -38,11 +38,14 @@ Tetris::Tetris(const std::string &name, Mgrs &mgrs, const Directory &dirBase):
38 38
   Game(name, mgrs, dirBase),
39 39
   m_fileStoneColor(dirBase.getFile("stoneColor")),
40 40
   m_fileDelay(dirBase.getFile("delay")),
41
+  m_fileDropDelay(dirBase.getFile("dropDelay")),
41 42
   m_fileStartSound(dirBase.getFile("startSound")),
42 43
   m_stoneColor(),
43 44
   m_delay(c_delayDescr.default_),
45
+  m_dropDelay(c_dropDelayDescr.default_),
44 46
   m_pConn(NULL),
45
-  m_stone(-1), m_rot(-1), m_posX(-1), m_posY(-1), m_field()
47
+  m_stone(-1), m_rot(-1), m_posX(-1), m_posY(-1), m_dropping(false),
48
+  m_field()
46 49
 {
47 50
   // open operator connection interfaces for player
48 51
   m_mgrs.m_opMgr.open(m_name, this);
... ...
@@ -69,7 +72,8 @@ bool Tetris::updateConfigGame()
69 72
   // color file was modified -> convert color, return true for update
70 73
   // cfg value file was updated -> read new cfg value, return true for update
71 74
   if (colorUpdate(m_fileStoneColor, m_stoneColor) ||
72
-      valueUpdate(m_fileDelay, c_delayDescr, m_delay)) {
75
+      valueUpdate(m_fileDelay, c_delayDescr, m_delay) ||
76
+      valueUpdate(m_fileDropDelay, c_dropDelayDescr, m_dropDelay)) {
73 77
     ret = true;
74 78
   }
75 79
 
... ...
@@ -136,7 +140,11 @@ void Tetris::opConnRecvKey(OpConn *pConn, char key)
136 140
     return;
137 141
   }
138 142
 
139
-  // normal keys for controlling game
143
+  /** normal keys for controlling game,
144
+      deactivated if dropping stone */
145
+  if (m_dropping) {
146
+    return;
147
+  }
140 148
 
141 149
   // move left
142 150
   if (key == '4') {
... ...
@@ -192,7 +200,8 @@ void Tetris::opConnRecvKey(OpConn *pConn, char key)
192 200
 
193 201
   // drop stone
194 202
   if (key == '8') {
195
-    // TODO
203
+    m_dropping = true;
204
+    planTimeStep(); // stone falls fater now -> update time callback
196 205
     return;
197 206
   }
198 207
 }
... ...
@@ -307,6 +316,9 @@ void Tetris::newStone()
307 316
   // postion: two pixels above top middle
308 317
   m_posX = (m_width - 1) / 2;
309 318
   m_posY = -2;
319
+
320
+  // stone is not being dropped yet
321
+  m_dropping = false;
310 322
 }
311 323
 
312 324
 /// set time for next time step of game - or unset if not needed
... ...
@@ -318,8 +330,9 @@ void Tetris::planTimeStep()
318 330
     return;
319 331
   }
320 332
 
321
-  // compute interval based on score and bounce count
322
-  float interval = 1e-3f * m_delay;
333
+  // compute interval based on game state
334
+  int interval_ms = m_dropping ? m_dropDelay : m_delay;
335
+  float interval = 1e-3f * interval_ms;
323 336
 
324 337
   // request next time call
325 338
   Time stepTime;
... ...
@@ -480,5 +493,8 @@ int const Tetris::c_pixelCnt = sizeof(Tetris::RotStone::pixels) /
480 493
 /// descriptor for delay value
481 494
 Tetris::ValueDescr const Tetris::c_delayDescr = { 400, 200, 1000 };
482 495
 
496
+/// descriptor for delay value during dropping a stone
497
+Tetris::ValueDescr const Tetris::c_dropDelayDescr = { 100, 50, 250 };
498
+
483 499
 } // namespace Blinker
484 500
 
... ...
@@ -156,13 +156,18 @@ protected:
156 156
   /// descriptor for delay value
157 157
   static ValueDescr const c_delayDescr;
158 158
 
159
+  /// descriptor for delay value during dropping a stone
160
+  static ValueDescr const c_dropDelayDescr;
161
+
159 162
   ColorFile m_fileStoneColor; ///< color file for stone color
160 163
   UIntFile  m_fileDelay;      ///< file for initial delay in ms per frame
164
+  UIntFile  m_fileDropDelay;  ///< file for delay while dropping (ms / frame)
161 165
 
162 166
   NameFile m_fileStartSound; ///< "start game" sound name file
163 167
 
164 168
   ColorData    m_stoneColor; ///< stone color
165
-  unsigned int m_delay;      ///< initial delay in ms per frame (ball speed)
169
+  unsigned int m_delay;      ///< initial delay in ms per frame
170
+  unsigned int m_dropDelay;  ///< delay while dropping in ms per frame
166 171
 
167 172
   OpConn *m_pConn; ///< operator connection of player (or NULL)
168 173
 
... ...
@@ -170,6 +175,7 @@ protected:
170 175
   int   m_rot;      ///< rotation of current stone
171 176
   int   m_posX;     ///< x position of current stone
172 177
   int   m_posY;     ///< y position of current stone
178
+  bool  m_dropping; ///< whether currently dropping a stone
173 179
 
174 180
   /// tetris field (y * m_width + x), -1 for free, >= 0 for pixel from stone
175 181
   std::vector<int> m_field;
176 182