| 202 | } |
| 203 | |
| 204 | bool OnUserUpdate(float fElapsedTime) override |
| 205 | { |
| 206 | // Update Dudes |
| 207 | float fSpeed = 32.0f; |
| 208 | |
| 209 | for (auto& dude : vDudes) |
| 210 | { |
| 211 | // If a dude's tick reaches 0, it will select a new state |
| 212 | dude.fTick -= fElapsedTime; |
| 213 | if (dude.fTick < 0.0f) |
| 214 | { |
| 215 | // Choose one out of the 9 randomly |
| 216 | int nAction = rand() % 9; |
| 217 | |
| 218 | // Choose for how long it should do it in seconds |
| 219 | dude.fTick = (float(rand()) / float(RAND_MAX)) * 5.0f; |
| 220 | |
| 221 | // Assign the state depending on the dice roll - (since enum |
| 222 | // we could cast here too, but, well, meh...) |
| 223 | if (nAction == 0) dude.UserState = DudeState::IDLE_STAND; |
| 224 | if (nAction == 1) dude.UserState = DudeState::WALK_S; |
| 225 | if (nAction == 2) dude.UserState = DudeState::WALK_N; |
| 226 | if (nAction == 3) dude.UserState = DudeState::WALK_E; |
| 227 | if (nAction == 4) dude.UserState = DudeState::WALK_W; |
| 228 | if (nAction == 5) dude.UserState = DudeState::YES; |
| 229 | if (nAction == 6) dude.UserState = DudeState::NO; |
| 230 | if (nAction == 7) dude.UserState = DudeState::LAUGH; |
| 231 | if (nAction == 8) dude.UserState = DudeState::CHEER; |
| 232 | |
| 233 | // State has changed, so update animation token |
| 234 | // !! - IMPORTANT - !! |
| 235 | animDude.ChangeState(dude.animstate, dude.UserState); |
| 236 | } |
| 237 | |
| 238 | // Update "physics", if walking move in that direction at speed |
| 239 | if (dude.UserState == DudeState::WALK_S) dude.pos += olc::vf2d(0, +1) * fSpeed * fElapsedTime; |
| 240 | if (dude.UserState == DudeState::WALK_N) dude.pos += olc::vf2d(0, -1) * fSpeed * fElapsedTime; |
| 241 | if (dude.UserState == DudeState::WALK_E) dude.pos += olc::vf2d(+1, 0) * fSpeed * fElapsedTime; |
| 242 | if (dude.UserState == DudeState::WALK_W) dude.pos += olc::vf2d(-1, 0) * fSpeed * fElapsedTime; |
| 243 | |
| 244 | // If walk off screen, wrap around to other side |
| 245 | if (dude.pos.x > ScreenWidth()) dude.pos.x -= ScreenWidth(); |
| 246 | if (dude.pos.y > ScreenHeight()) dude.pos.y -= ScreenHeight(); |
| 247 | if (dude.pos.x < 0) dude.pos.x += ScreenWidth(); |
| 248 | if (dude.pos.y < 0) dude.pos.y += ScreenHeight(); |
| 249 | |
| 250 | // Update animation token every frame |
| 251 | // !! - IMPORTANT - !! |
| 252 | animDude.UpdateState(dude.animstate, fElapsedTime); |
| 253 | } |
| 254 | |
| 255 | // Render Dudes |
| 256 | for (const auto& dude : vDudes) |
| 257 | { |
| 258 | // Get the frame, this contains both source image and source location rectangle |
| 259 | // !! - IMPORTANT - !! |
| 260 | const auto& frame = animDude.GetFrame(dude.animstate); |
| 261 |
nothing calls this directly
no test coverage detected