| 1 | // This file is part of Dust Racing 2D. |
| 2 | // Copyright (C) 2014 Jussi Lind <[email protected]> |
| 3 | // |
| 4 | // Dust Racing 2D is free software: you can redistribute it and/or modify |
| 5 | // it under the terms of the GNU General Public License as published by |
| 6 | // the Free Software Foundation, either version 3 of the License, or |
| 7 | // (at your option) any later version. |
| 8 | // Dust Racing 2D is distributed in the hope that it will be useful, |
| 9 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | // GNU General Public License for more details. |
| 12 | // |
| 13 | // You should have received a copy of the GNU General Public License |
| 14 | // along with Dust Racing 2D. If not, see <http://www.gnu.org/licenses/>. |
| 15 | |
| 16 | #include "scene.hpp" |
| 17 | |
| 18 | #include "ai.hpp" |
| 19 | #include "audioworker.hpp" |
| 20 | #include "bridge.hpp" |
| 21 | #include "car.hpp" |
| 22 | #include "carfactory.hpp" |
| 23 | #include "carsoundeffectmanager.hpp" |
| 24 | #include "checkeredflag.hpp" |
| 25 | #include "fadeanimation.hpp" |
| 26 | #include "game.hpp" |
| 27 | #include "inputhandler.hpp" |
| 28 | #include "intro.hpp" |
| 29 | #include "mainmenu.hpp" |
| 30 | #include "messageoverlay.hpp" |
| 31 | #include "particlefactory.hpp" |
| 32 | #include "pit.hpp" |
| 33 | #include "race.hpp" |
| 34 | #include "renderer.hpp" |
| 35 | #include "settings.hpp" |
| 36 | #include "startlights.hpp" |
| 37 | #include "startlightsoverlay.hpp" |
| 38 | #include "statemachine.hpp" |
| 39 | #include "timingoverlay.hpp" |
| 40 | #include "track.hpp" |
| 41 | #include "trackdata.hpp" |
| 42 | #include "trackobject.hpp" |
| 43 | #include "tracktile.hpp" |
| 44 | |
| 45 | #include "../common/config.hpp" |
| 46 | |
| 47 | #include <MenuManager> |
| 48 | |
| 49 | #include <MCAssetManager> |
| 50 | #include <MCCamera> |
| 51 | #include <MCFrictionGenerator> |
| 52 | #include <MCGLAmbientLight> |
| 53 | #include <MCGLDiffuseLight> |
| 54 | #include <MCGLScene> |
| 55 | #include <MCGLShaderProgram> |
| 56 | #include <MCObject> |
| 57 | #include <MCObjectFactory> |
| 58 | #include <MCPhysicsComponent> |
| 59 | #include <MCShape> |
| 60 | #include <MCSurface> |
| 61 | #include <MCSurfaceView> |
| 62 | #include <MCTextureFont> |
| 63 | |
| 64 | #include <MCWorld> |
| 65 | #include <MCWorldRenderer> |
| 66 | |
| 67 | #include <QGuiApplication> |
| 68 | #include <QObject> |
| 69 | |
| 70 | #include <algorithm> |
| 71 | #include <cassert> |
| 72 | #include <memory> |
| 73 | |
| 74 | using std::dynamic_pointer_cast; |
| 75 | |
| 76 | // Default visible scene size. |
| 77 | int Scene::m_width = 1024; |
| 78 | int Scene::m_height = 768; |
| 79 | |
| 80 | static const float METERS_PER_UNIT = 0.05f; |
| 81 | |
| 82 | Scene::Scene(Game & game, StateMachine & stateMachine, Renderer & renderer, MCWorld & world) |
| 83 | : m_game { game } |
| 84 | , m_stateMachine { stateMachine } |
| 85 | , m_renderer { renderer } |
| 86 | , m_messageOverlay { std::make_unique<MessageOverlay>() } |
| 87 | , m_race { std::make_shared<Race>(game, carCount()) } |
| 88 | , m_world { world } |
| 89 | , m_startlights { std::make_unique<Startlights>() } |
| 90 | , m_startlightsOverlay { std::make_unique<StartlightsOverlay>(*m_startlights) } |
| 91 | , m_checkeredFlag { std::make_unique<CheckeredFlag>() } |
| 92 | , m_intro { std::make_unique<Intro>() } |
| 93 | , m_particleFactory { std::make_unique<ParticleFactory>() } |
| 94 | , m_fadeAnimation { std::make_unique<FadeAnimation>() } |
| 95 | { |
| 96 | initializeComponents(); |
| 97 | connectComponents(); |
| 98 | createMenus(); |
| 99 | } |
| 100 | |
| 101 | void Scene::connectComponents() |
| 102 | { |
| 103 | connect(m_startlights.get(), &Startlights::raceStarted, m_race.get(), &Race::start); |
| 104 | connect(m_startlights.get(), &Startlights::animationEnded, &m_stateMachine, &StateMachine::endStartlightAnimation); |
| 105 | |
| 106 | connect(&m_stateMachine, &StateMachine::startlightAnimationRequested, m_startlights.get(), &Startlights::beginAnimation); |
| 107 | connect(&m_stateMachine, &StateMachine::fadeInRequested, m_fadeAnimation.get(), &FadeAnimation::beginFadeIn); |
| 108 | connect(&m_stateMachine, &StateMachine::fadeOutRequested, m_fadeAnimation.get(), &FadeAnimation::beginFadeOut); |
| 109 | connect(&m_stateMachine, &StateMachine::fadeOutFlashRequested, m_fadeAnimation.get(), &FadeAnimation::beginFadeOutFlash); |
| 110 | connect(&m_stateMachine, &StateMachine::soundsStopped, m_race.get(), &Race::stopEngineSounds); |
| 111 | |
| 112 | connect(m_fadeAnimation.get(), &FadeAnimation::fadeValueChanged, &m_renderer, &Renderer::setFadeValue); |
| 113 | connect(m_fadeAnimation.get(), &FadeAnimation::fadeInFinished, &m_stateMachine, &StateMachine::endFadeIn); |
| 114 | connect(m_fadeAnimation.get(), &FadeAnimation::fadeOutFinished, &m_stateMachine, &StateMachine::endFadeOut); |
| 115 | |
| 116 | connect(m_race.get(), &Race::finished, &m_stateMachine, &StateMachine::finishRace); |
| 117 | connect(m_race.get(), &Race::messageRequested, m_messageOverlay.get(), static_cast<void (MessageOverlay::*)(QString)>(&MessageOverlay::addMessage)); |
| 118 | |
| 119 | connect(m_startlights.get(), &Startlights::messageRequested, m_messageOverlay.get(), static_cast<void (MessageOverlay::*)(QString)>(&MessageOverlay::addMessage)); |
| 120 | connect(this, &Scene::listenerLocationChanged, &m_game.audioWorker(), &AudioWorker::setListenerLocation); |
| 121 | |
| 122 | m_game.audioWorker().connectAudioSource(*m_race); |
| 123 | } |
| 124 | |
| 125 | void Scene::initializeComponents() |
| 126 | { |
| 127 | for (size_t i = 0; i < 2; i++) |
| 128 | { |
| 129 | m_cameraOffset.at(i) = 0.0f; |
| 130 | m_timingOverlay.at(i).setRace(m_race); |
| 131 | } |
| 132 | |
| 133 | m_checkeredFlag->setDimensions(width(), height()); |
| 134 | m_intro->setDimensions(width(), height()); |
| 135 | m_startlightsOverlay->setDimensions(width(), height()); |
| 136 | m_messageOverlay->setDimensions(width(), height()); |
| 137 | |
| 138 | m_world.setMetersPerUnit(METERS_PER_UNIT); |
| 139 | |
| 140 | MCAssetManager::textureFontManager().font(m_game.fontName()).setShaderProgram(m_renderer.program("text")); |
| 141 | MCAssetManager::textureFontManager().font(m_game.fontName()).setShadowShaderProgram(m_renderer.program("textShadow")); |
| 142 | |
| 143 | const MCGLAmbientLight ambientLight(1.0f, 0.9f, 0.95f, 0.75f); |
| 144 | const MCGLDiffuseLight diffuseLight(MCVector3dF(1.0f, -1.0f, -1.0f), 1.0f, 0.9f, 0.5f, 0.75f); |
| 145 | const MCGLDiffuseLight specularLight(MCVector3dF(1.0f, -1.0f, -1.0f), 1.0f, 1.0f, 0.8f, 0.9f); |
| 146 | |
| 147 | auto & glScene = MCWorld::instance().renderer().glScene(); |
| 148 | glScene.setAmbientLight(ambientLight); |
| 149 | glScene.setDiffuseLight(diffuseLight); |
| 150 | glScene.setSpecularLight(specularLight); |
| 151 | |
| 152 | m_renderer.setFadeValue(0.0); |
| 153 | } |
| 154 | |
| 155 | void Scene::setupAudio(Car & car, size_t index) |
| 156 | { |
| 157 | CarSoundEffectManager::MultiSoundHandles handles; |
| 158 | |
| 159 | const auto indexStr = std::to_string(index); |
| 160 | const auto engine = "carEngine" + indexStr; |
| 161 | handles.engineSoundHandle = engine.c_str(); |
| 162 | |
| 163 | const auto hit = "carHit" + indexStr; |
| 164 | handles.hitSoundHandle = hit.c_str(); |
| 165 | |
| 166 | const auto skid = "skid" + indexStr; |
| 167 | handles.skidSoundHandle = skid.c_str(); |
| 168 | |
| 169 | const auto sfx = std::make_shared<CarSoundEffectManager>(car, handles); |
| 170 | m_game.audioWorker().connectAudioSource(*sfx); |
| 171 | car.setSoundEffectManager(sfx); |
| 172 | } |
| 173 | |
| 174 | void Scene::createCars() |
| 175 | { |
| 176 | m_race->removeCars(); |
| 177 | m_cars.clear(); |
| 178 | m_ai.clear(); |
| 179 | |
| 180 | for (size_t i = 0; i < carCount(); i++) |
| 181 | { |
| 182 | if (CarS car { CarFactory::buildCar(i, carCount(), m_game) }; car) |
| 183 | { |
| 184 | if (!car->isHuman()) |
| 185 | { |
| 186 | m_ai.push_back(std::make_shared<AI>(*car, m_race)); |
| 187 | } |
| 188 | |
| 189 | car->shape()->view()->setShaderProgram(m_renderer.program("car")); |
| 190 | car->shape()->view()->object()->material()->setDiffuseCoeff(3.4f); |
| 191 | |
| 192 | setupAudio(*car, i); |
| 193 | |
| 194 | m_cars.push_back(car); |
| 195 | m_race->addCar(*car); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | if (m_game.hasTwoHumanPlayers()) |
| 200 | { |
| 201 | m_timingOverlay.at(1).setCarToFollow(*m_cars.at(1)); |
| 202 | m_crashOverlay.at(1).setCarToFollow(*m_cars.at(1)); |
| 203 | } |
| 204 | |
| 205 | m_timingOverlay.at(0).setCarToFollow(*m_cars.at(0)); |
| 206 | m_crashOverlay.at(0).setCarToFollow(*m_cars.at(0)); |
| 207 | } |
| 208 | |
| 209 | void Scene::setupMinimaps() |
| 210 | { |
| 211 | const auto minimapSize = static_cast<int>(static_cast<float>(m_width) * 0.2f); |
| 212 | const auto minimapY = !m_game.hasTwoHumanPlayers() ? minimapSize : minimapSize / 2 + 10; |
| 213 | |
| 214 | for (size_t i = 0; i < std::min(static_cast<size_t>(2), m_cars.size()); i++) |
| 215 | { |
| 216 | m_minimap.at(i).initialize(*m_cars.at(i), m_activeTrack->trackData().map(), minimapSize / 2 + 10, minimapY, static_cast<size_t>(minimapSize)); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | int Scene::width() |
| 221 | { |
| 222 | return Scene::m_width; |
| 223 | } |
| 224 | |
| 225 | int Scene::height() |
| 226 | { |
| 227 | return Scene::m_height; |
| 228 | } |
| 229 | |
| 230 | void Scene::setSize(int width, int height) |
| 231 | { |
| 232 | Scene::m_width = width; |
| 233 | Scene::m_height = height; |
| 234 | } |
| 235 | |
| 236 | size_t Scene::carCount() |
| 237 | { |
| 238 | return 12; |
| 239 | } |
| 240 | |
| 241 | void Scene::createMenus() |
| 242 | { |
| 243 | m_menuManager = std::make_unique<MTFH::MenuManager>(); |
| 244 | |
| 245 | m_mainMenu = std::make_shared<MainMenu>(*m_menuManager, *this, width(), height()); |
| 246 | connect(std::static_pointer_cast<MainMenu>(m_mainMenu).get(), &MainMenu::exitGameRequested, &m_game, &Game::exitGame); |
| 247 | |
| 248 | m_menuManager->addMenu(m_mainMenu); |
| 249 | m_menuManager->enterMenu(m_mainMenu); |
| 250 | } |
| 251 | |
| 252 | void Scene::updateFrame(InputHandler & handler, std::chrono::milliseconds timeStep) |
| 253 | { |
| 254 | switch (m_stateMachine.state()) |
| 255 | { |
| 256 | case StateMachine::State::GameTransitionIn: |
| 257 | case StateMachine::State::GameTransitionOut: |
| 258 | case StateMachine::State::DoStartlights: |
| 259 | case StateMachine::State::Play: { |
| 260 | if (m_activeTrack) |
| 261 | { |
| 262 | if (m_race->started()) |
| 263 | { |
| 264 | processUserInput(handler); |
| 265 | updateAi(); |
| 266 | } |
| 267 | |
| 268 | updateWorld(timeStep); |
| 269 | updateRace(timeStep); |
| 270 | |
| 271 | if (m_game.hasTwoHumanPlayers()) |
| 272 | { |
| 273 | for (size_t i = 0; i < 2; i++) |
| 274 | { |
| 275 | updateCameraLocation(m_camera.at(i), m_cameraOffset.at(i), *m_cars.at(i)); |
| 276 | } |
| 277 | } |
| 278 | else |
| 279 | { |
| 280 | updateCameraLocation(m_camera.at(0), m_cameraOffset.at(0), *m_cars.at(0)); |
| 281 | } |
| 282 | } |
| 283 | } |
| 284 | break; |
| 285 | case StateMachine::State::Menu: |
| 286 | m_menuManager->stepTime(timeStep); |
| 287 | break; |
| 288 | default: |
| 289 | break; |
| 290 | } |
| 291 | |
| 292 | if (m_fadeAnimation->isFading()) |
| 293 | { |
| 294 | auto & glScene = MCWorld::instance().renderer().glScene(); |
| 295 | glScene.setFadeValue(m_renderer.fadeValue()); |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | void Scene::updateOverlays() |
| 300 | { |
| 301 | if (m_game.hasTwoHumanPlayers()) |
| 302 | { |
| 303 | m_timingOverlay.at(1).update(); |
| 304 | m_crashOverlay.at(1).update(); |
| 305 | } |
| 306 | |
| 307 | m_timingOverlay.at(0).update(); |
| 308 | m_crashOverlay.at(0).update(); |
| 309 | |
| 310 | m_messageOverlay->update(); |
| 311 | } |
| 312 | |
| 313 | void Scene::updateWorld(std::chrono::milliseconds timeStep) |
| 314 | { |
| 315 | m_world.stepTime(timeStep); |
| 316 | } |
| 317 | |
| 318 | void Scene::updateRace(std::chrono::milliseconds timeStep) |
| 319 | { |
| 320 | m_race->update(timeStep); |
| 321 | |
| 322 | emit listenerLocationChanged(m_cars.at(0)->location().i(), m_cars.at(0)->location().j()); |
| 323 | } |
| 324 | |
| 325 | void Scene::updateCameraLocation(MCCamera & camera, float & offset, MCObject & object) |
| 326 | { |
| 327 | // Update camera location with respect to the car speed. |
| 328 | // Make changes a bit smoother so that an abrupt decrease |
| 329 | // in the speed won't look bad. |
| 330 | offset += (object.physicsComponent().velocity().lengthFast() - offset) * 0.2f; |
| 331 | const float offsetAmplification = m_game.hasTwoHumanPlayers() ? 9.6f : 13.8f; |
| 332 | const auto loc = MCVector2dF { object.location() } + object.direction() * offset * offsetAmplification; |
| 333 | camera.setPos(loc.i(), loc.j()); |
| 334 | } |
| 335 | |
| 336 | void Scene::processUserInput(InputHandler & handler) |
| 337 | { |
| 338 | for (size_t i = 0; i < (m_game.hasTwoHumanPlayers() ? 2 : 1); i++) |
| 339 | { |
| 340 | // Handle accelerating / braking |
| 341 | if (handler.getActionState(i, InputHandler::Action::Down)) |
| 342 | { |
| 343 | if (!m_race->timing().lock()->raceCompleted(i)) |
| 344 | { |
| 345 | m_cars.at(i)->setBrakeEnabled(true); |
| 346 | } |
| 347 | } |
| 348 | else |
| 349 | { |
| 350 | m_cars.at(i)->setBrakeEnabled(false); |
| 351 | } |
| 352 | |
| 353 | if (handler.getActionState(i, InputHandler::Action::Up)) |
| 354 | { |
| 355 | if (!m_race->timing().lock()->raceCompleted(i)) |
| 356 | { |
| 357 | m_cars.at(i)->setAcceleratorEnabled(true); |
| 358 | } |
| 359 | } |
| 360 | else |
| 361 | { |
| 362 | m_cars.at(i)->setAcceleratorEnabled(false); |
| 363 | } |
| 364 | |
| 365 | // Handle turning |
| 366 | if (handler.getActionState(i, InputHandler::Action::Left)) |
| 367 | { |
| 368 | m_cars.at(i)->steer(Car::Steer::Left); |
| 369 | } |
| 370 | else if (handler.getActionState(i, InputHandler::Action::Right)) |
| 371 | { |
| 372 | m_cars.at(i)->steer(Car::Steer::Right); |
| 373 | } |
| 374 | else |
| 375 | { |
| 376 | m_cars.at(i)->steer(Car::Steer::Neutral); |
| 377 | } |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | void Scene::updateAi() |
| 382 | { |
| 383 | for (auto && ai : m_ai) |
| 384 | { |
| 385 | ai->update(m_race->timing().lock()->raceCompleted(ai->car().index())); |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | void Scene::setupCameras(Track & activeTrack) |
| 390 | { |
| 391 | m_world.renderer().removeParticleVisibilityCameras(); |
| 392 | if (m_game.hasTwoHumanPlayers()) |
| 393 | { |
| 394 | for (size_t i = 0; i < 2; i++) |
| 395 | { |
| 396 | if (m_game.splitType() == Game::SplitType::Vertical) |
| 397 | { |
| 398 | m_camera.at(i).init( |
| 399 | static_cast<float>(Scene::width() / 2), |
| 400 | static_cast<float>(Scene::height()), |
| 401 | 0, |
| 402 | 0, |
| 403 | static_cast<float>(activeTrack.width()), |
| 404 | static_cast<float>(activeTrack.height())); |
| 405 | m_world.renderer().addParticleVisibilityCamera(m_camera.at(i)); |
| 406 | } |
| 407 | else |
| 408 | { |
| 409 | m_camera.at(i).init( |
| 410 | static_cast<float>(Scene::width()), |
| 411 | static_cast<float>(Scene::height() / 2), |
| 412 | 0, |
| 413 | 0, |
| 414 | static_cast<float>(activeTrack.width()), |
| 415 | static_cast<float>(activeTrack.height())); |
| 416 | m_world.renderer().addParticleVisibilityCamera(m_camera.at(i)); |
| 417 | } |
| 418 | } |
| 419 | } |
| 420 | else |
| 421 | { |
| 422 | m_camera.at(0).init( |
| 423 | static_cast<float>(Scene::width()), |
| 424 | static_cast<float>(Scene::height()), |
| 425 | 0, |
| 426 | 0, |
| 427 | static_cast<float>(activeTrack.width()), |
| 428 | static_cast<float>(activeTrack.height())); |
| 429 | m_world.renderer().addParticleVisibilityCamera(m_camera.at(0)); |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | void Scene::setupAI(std::shared_ptr<Track> activeTrack) |
| 434 | { |
| 435 | for (auto && ai : m_ai) |
| 436 | { |
| 437 | ai->setTrack(activeTrack); |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | void Scene::setActiveTrack(std::shared_ptr<Track> activeTrack) |
| 442 | { |
| 443 | m_activeTrack = activeTrack; |
| 444 | |
| 445 | m_world.clear(); |
| 446 | |
| 447 | setupCameras(*activeTrack); |
| 448 | setWorldDimensions(); |
| 449 | |
| 450 | createCars(); |
| 451 | |
| 452 | resizeOverlays(); |
| 453 | |
| 454 | addCarsToWorld(); |
| 455 | addTrackObjectsToWorld(); |
| 456 | |
| 457 | initializeRace(); |
| 458 | |
| 459 | setupAI(activeTrack); |
| 460 | setupMinimaps(); |
| 461 | } |
| 462 | |
| 463 | void Scene::setWorldDimensions() |
| 464 | { |
| 465 | assert(m_activeTrack); |
| 466 | |
| 467 | // Update world dimensions according to the |
| 468 | // active track. |
| 469 | const size_t minX = 0; |
| 470 | const size_t maxX = m_activeTrack->width(); |
| 471 | const size_t minY = 0; |
| 472 | const size_t maxY = m_activeTrack->height(); |
| 473 | const size_t minZ = 0; |
| 474 | const size_t maxZ = 1000; |
| 475 | |
| 476 | m_world.setDimensions(minX, static_cast<float>(maxX), minY, static_cast<float>(maxY), minZ, maxZ, METERS_PER_UNIT); |
| 477 | } |
| 478 | |
| 479 | void Scene::addCarsToWorld() |
| 480 | { |
| 481 | for (auto && car : m_cars) |
| 482 | { |
| 483 | car->addToWorld(); |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | void Scene::addTrackObjectsToWorld() |
| 488 | { |
| 489 | createNormalObjects(); |
| 490 | createBridgeObjects(); |
| 491 | } |
| 492 | |
| 493 | void Scene::createNormalObjects() |
| 494 | { |
| 495 | assert(m_activeTrack); |
| 496 | |
| 497 | for (size_t i = 0; i < m_activeTrack->trackData().objects().count(); i++) |
| 498 | { |
| 499 | const auto trackObject = dynamic_pointer_cast<TrackObject>(m_activeTrack->trackData().objects().object(i)); |
| 500 | assert(trackObject); |
| 501 | |
| 502 | auto && object = trackObject->object(); |
| 503 | object.addToWorld(); |
| 504 | |
| 505 | // Set the base Z of mesh objects at ground level instead of at the object center |
| 506 | float baseZ = 0; |
| 507 | if (object.shape() && object.shape()->view() && object.shape()->view()->object()) |
| 508 | { |
| 509 | if (dynamic_cast<MCMesh *>(object.shape()->view()->object())) |
| 510 | { |
| 511 | baseZ = -object.shape()->view()->object()->minZ(); |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | object.translate(object.initialLocation() + MCVector3dF { 0, 0, baseZ }); |
| 516 | object.rotate(static_cast<float>(object.initialAngle())); |
| 517 | |
| 518 | if (const auto pit = dynamic_cast<Pit *>(&object); pit) |
| 519 | { |
| 520 | pit->reset(); |
| 521 | connect(pit, &Pit::pitStop, m_race.get(), &Race::pitStop); |
| 522 | } |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | void Scene::createBridgeObjects() |
| 527 | { |
| 528 | assert(m_activeTrack); |
| 529 | |
| 530 | auto && map = m_activeTrack->trackData().map(); |
| 531 | for (size_t j = 0; j <= map.rows(); j++) |
| 532 | { |
| 533 | for (size_t i = 0; i <= map.cols(); i++) |
| 534 | { |
| 535 | const auto tile = dynamic_pointer_cast<TrackTile>(map.getTile(i, j)); |
| 536 | if (tile && tile->tileTypeEnum() == TrackTile::TileType::Bridge) |
| 537 | { |
| 538 | const auto bridge = std::make_shared<Bridge>(); |
| 539 | bridge->translate(MCVector3dF { |
| 540 | static_cast<float>(i * TrackTile::width() + TrackTile::width() / 2), |
| 541 | static_cast<float>(j * TrackTile::height() + TrackTile::height() / 2), |
| 542 | 0 }); |
| 543 | bridge->rotate(static_cast<float>(tile->rotation())); |
| 544 | bridge->addToWorld(); |
| 545 | m_bridges.push_back(bridge); |
| 546 | } |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | Bridge::reset(); |
| 551 | } |
| 552 | |
| 553 | void Scene::resizeOverlays() |
| 554 | { |
| 555 | if (m_game.hasTwoHumanPlayers()) |
| 556 | { |
| 557 | if (m_game.splitType() == Game::SplitType::Vertical) |
| 558 | { |
| 559 | for (size_t i = 0; i < 2; i++) |
| 560 | { |
| 561 | m_timingOverlay.at(i).setDimensions(width() / 2, height()); |
| 562 | m_crashOverlay.at(i).setDimensions(width() / 2, height()); |
| 563 | } |
| 564 | } |
| 565 | else |
| 566 | { |
| 567 | for (size_t i = 0; i < 2; i++) |
| 568 | { |
| 569 | m_timingOverlay.at(i).setDimensions(width(), height() / 2); |
| 570 | m_crashOverlay.at(i).setDimensions(width(), height() / 2); |
| 571 | } |
| 572 | } |
| 573 | } |
| 574 | else |
| 575 | { |
| 576 | m_timingOverlay.at(0).setDimensions(width(), height()); |
| 577 | m_crashOverlay.at(0).setDimensions(width(), height()); |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | void Scene::initializeRace() |
| 582 | { |
| 583 | assert(m_activeTrack); |
| 584 | m_race->initialize(m_activeTrack, static_cast<size_t>(m_game.lapCount())); |
| 585 | } |
| 586 | |
| 587 | std::shared_ptr<Track> Scene::activeTrack() const |
| 588 | { |
| 589 | return m_activeTrack; |
| 590 | } |
| 591 | |
| 592 | MTFH::MenuPtr Scene::trackSelectionMenu() const |
| 593 | { |
| 594 | return m_menuManager->getMenuById("trackSelection"); |
| 595 | } |
| 596 | |
| 597 | void Scene::getSplitPositions(MCGLScene::SplitType & p0, MCGLScene::SplitType & p1) |
| 598 | { |
| 599 | if (m_game.splitType() == Game::SplitType::Vertical) |
| 600 | { |
| 601 | p1 = MCGLScene::ShowOnLeft; |
| 602 | p0 = MCGLScene::ShowOnRight; |
| 603 | } |
| 604 | else |
| 605 | { |
| 606 | p1 = MCGLScene::ShowOnTop; |
| 607 | p0 = MCGLScene::ShowOnBottom; |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | void Scene::renderTrack() |
| 612 | { |
| 613 | switch (m_stateMachine.state()) |
| 614 | { |
| 615 | case StateMachine::State::GameTransitionIn: |
| 616 | case StateMachine::State::GameTransitionOut: |
| 617 | case StateMachine::State::DoStartlights: |
| 618 | case StateMachine::State::Play: { |
| 619 | if (m_game.hasTwoHumanPlayers()) |
| 620 | { |
| 621 | MCGLScene::SplitType p1, p0; |
| 622 | getSplitPositions(p1, p0); |
| 623 | |
| 624 | auto & glScene = MCWorld::instance().renderer().glScene(); |
| 625 | |
| 626 | glScene.setSplitType(p1); |
| 627 | m_activeTrack->render(m_camera.at(1)); |
| 628 | |
| 629 | glScene.setSplitType(p0); |
| 630 | m_activeTrack->render(m_camera.at(0)); |
| 631 | |
| 632 | glScene.setSplitType(MCGLScene::ShowFullScreen); |
| 633 | } |
| 634 | else |
| 635 | { |
| 636 | m_activeTrack->render(m_camera.at(0)); |
| 637 | } |
| 638 | |
| 639 | break; |
| 640 | } |
| 641 | default: |
| 642 | break; |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | void Scene::renderMenu() |
| 647 | { |
| 648 | switch (m_stateMachine.state()) |
| 649 | { |
| 650 | case StateMachine::State::DoIntro: |
| 651 | MCWorld::instance().renderer().glScene().setSplitType(MCGLScene::ShowFullScreen); |
| 652 | m_intro->render(); |
| 653 | break; |
| 654 | |
| 655 | case StateMachine::State::Menu: |
| 656 | case StateMachine::State::MenuTransitionOut: |
| 657 | case StateMachine::State::MenuTransitionIn: |
| 658 | MCWorld::instance().renderer().glScene().setSplitType(MCGLScene::ShowFullScreen); |
| 659 | m_menuManager->render(); |
| 660 | break; |
| 661 | |
| 662 | default: |
| 663 | break; |
| 664 | } |
| 665 | } |
| 666 | |
| 667 | void Scene::renderCommonHUD() |
| 668 | { |
| 669 | switch (m_stateMachine.state()) |
| 670 | { |
| 671 | case StateMachine::State::GameTransitionIn: |
| 672 | case StateMachine::State::GameTransitionOut: |
| 673 | case StateMachine::State::DoStartlights: |
| 674 | case StateMachine::State::Play: { |
| 675 | // Setup for common scene |
| 676 | MCWorld::instance().renderer().glScene().setSplitType(MCGLScene::ShowFullScreen); |
| 677 | if (m_race->checkeredFlagEnabled() && !m_game.hasTwoHumanPlayers()) |
| 678 | { |
| 679 | m_checkeredFlag->render(); |
| 680 | } |
| 681 | m_startlightsOverlay->render(); |
| 682 | m_messageOverlay->render(); |
| 683 | break; |
| 684 | } |
| 685 | default: |
| 686 | break; |
| 687 | } |
| 688 | } |
| 689 | |
| 690 | void Scene::renderHUD() |
| 691 | { |
| 692 | switch (m_stateMachine.state()) |
| 693 | { |
| 694 | case StateMachine::State::GameTransitionIn: |
| 695 | case StateMachine::State::GameTransitionOut: |
| 696 | case StateMachine::State::DoStartlights: |
| 697 | case StateMachine::State::Play: { |
| 698 | if (m_game.hasTwoHumanPlayers()) |
| 699 | { |
| 700 | MCGLScene::SplitType p1, p0; |
| 701 | getSplitPositions(p1, p0); |
| 702 | |
| 703 | auto && glScene = MCWorld::instance().renderer().glScene(); |
| 704 | |
| 705 | glScene.setSplitType(p1); |
| 706 | m_timingOverlay.at(1).render(); |
| 707 | m_minimap.at(1).render(m_cars, *m_race); |
| 708 | m_crashOverlay.at(1).render(); |
| 709 | |
| 710 | glScene.setSplitType(p0); |
| 711 | m_timingOverlay.at(0).render(); |
| 712 | m_minimap.at(0).render(m_cars, *m_race); |
| 713 | m_crashOverlay.at(0).render(); |
| 714 | |
| 715 | glScene.setSplitType(MCGLScene::ShowFullScreen); |
| 716 | } |
| 717 | else |
| 718 | { |
| 719 | m_timingOverlay.at(0).render(); |
| 720 | m_minimap.at(0).render(m_cars, *m_race); |
| 721 | m_crashOverlay.at(0).render(); |
| 722 | } |
| 723 | |
| 724 | break; |
| 725 | } |
| 726 | default: |
| 727 | break; |
| 728 | } |
| 729 | } |
| 730 | |
| 731 | void Scene::renderWorld(MCRenderGroup renderGroup, bool prepareRendering) |
| 732 | { |
| 733 | switch (m_stateMachine.state()) |
| 734 | { |
| 735 | case StateMachine::State::GameTransitionIn: |
| 736 | case StateMachine::State::GameTransitionOut: |
| 737 | case StateMachine::State::DoStartlights: |
| 738 | case StateMachine::State::Play: { |
| 739 | if (m_game.hasTwoHumanPlayers()) |
| 740 | { |
| 741 | MCGLScene::SplitType p1, p0; |
| 742 | getSplitPositions(p1, p0); |
| 743 | |
| 744 | if (prepareRendering) |
| 745 | { |
| 746 | m_world.prepareRendering(&m_camera.at(1)); |
| 747 | m_world.prepareRendering(&m_camera.at(0)); |
| 748 | } |
| 749 | |
| 750 | auto && glScene = MCWorld::instance().renderer().glScene(); |
| 751 | glScene.setSplitType(p1); |
| 752 | m_world.render(&m_camera.at(1), renderGroup); |
| 753 | glScene.setSplitType(p0); |
| 754 | m_world.render(&m_camera.at(0), renderGroup); |
| 755 | glScene.setSplitType(MCGLScene::ShowFullScreen); |
| 756 | } |
| 757 | else |
| 758 | { |
| 759 | if (prepareRendering) |
| 760 | { |
| 761 | m_world.prepareRendering(&m_camera.at(0)); |
| 762 | } |
| 763 | |
| 764 | m_world.render(&m_camera.at(0), renderGroup); |
| 765 | } |
| 766 | |
| 767 | break; |
| 768 | } |
| 769 | default: |
| 770 | break; |
| 771 | } |
| 772 | } |
| 773 | |
| 774 | Scene::~Scene() = default; |