diff --git a/src/Core/Crawl/iworld.cpp b/src/Core/Crawl/iworld.cpp index 8286d56..c2acae0 100644 --- a/src/Core/Crawl/iworld.cpp +++ b/src/Core/Crawl/iworld.cpp @@ -35,6 +35,11 @@ IControl *IWorld::initUserInterface() const { void IWorld::render(unsigned int tbfMsec) { + if (!_running) { + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + return; + } + _ItemsMutex.lock(); for (auto i = _items.begin(); i != _items.end(); ++i) { @@ -77,6 +82,8 @@ bool IWorld::start() { worldChanged(*_worldRules->begin()); setTargetFps(60); + setRunning(true); + return true; } @@ -109,15 +116,7 @@ IWorldItem *IWorld::generate(const QString &objectType) const { } bool IWorld::stop() { - start(); - - setWorldStatus(WorldStatus::Background); - _player->setControl(dynamic_cast(_backgroundAI)); - - _backgroundAI->startAI(); - - setTargetFps(30); - + setRunning(false); return true; } @@ -233,6 +232,7 @@ void IWorld::removeItem(int id, QList *removedObjectsList) { } void IWorld::reset() { + if (_player) { delete _player; _player = nullptr; @@ -309,6 +309,14 @@ void IWorld::removeAnyItemFromGroup(const QString &group, removeItem(anyObjectId, removedObjectsList); } +bool IWorld::running() const { + return _running; +} + +void IWorld::setRunning(bool newRunning) { + _running = newRunning; +} + int IWorld::targetFps() const { return _targetFps; } @@ -357,7 +365,7 @@ void IWorld::setCameraReleativePosition(const QVector3D &newCameraReleativePosit } void IWorld::handleStop() { - stop(); + runAsBackGround(); } const QVector3D &IWorld::cameraReleativePosition() const { @@ -400,3 +408,14 @@ void IWorld::setWorldStatus(int newWorldStatus) { const QString &IWorld::hdr() const { return _hdrMap; } + +void IWorld::runAsBackGround() { + start(); + + setWorldStatus(WorldStatus::Background); + _player->setControl(dynamic_cast(_backgroundAI)); + + _backgroundAI->startAI(); + + setTargetFps(30); +} diff --git a/src/Core/Crawl/iworld.h b/src/Core/Crawl/iworld.h index a5dd534..2eb1dfd 100644 --- a/src/Core/Crawl/iworld.h +++ b/src/Core/Crawl/iworld.h @@ -222,6 +222,12 @@ public: */ const QString &hdr() const; + /** + * @brief runAsBackGround This method run this world as a backgroud. + * The player start new game and sets new control from the backgroundAI method. + */ + void runAsBackGround(); + signals: /** * @brief sigGameFinished This signal emit when game are finished @@ -375,6 +381,19 @@ private: bool prepare(); void reset(); + + /** + * @brief running This varibale check in render function if the running is true then render loop are working correctly + * @return + */ + bool running() const; + + /** + * @brief setRunning + * @param newRunning + */ + void setRunning(bool newRunning); + /** * @brief worldChanged This method generate diff for the qml * @param objects This is list of object on lvl @@ -447,6 +466,7 @@ private: QHash> _registeredTypes; int _targetFps = 60; + bool _running = false; // engine friend class Engine; diff --git a/src/Core/Crawl/iworlditem.cpp b/src/Core/Crawl/iworlditem.cpp index 4475555..a0a49d7 100644 --- a/src/Core/Crawl/iworlditem.cpp +++ b/src/Core/Crawl/iworlditem.cpp @@ -31,12 +31,16 @@ const IWorldItem *IWorldItem::getPlayer() const { } void IWorldItem::render(unsigned int) { - if (_playerObject->position().x() - position().x() > - _world->cameraReleativePosition().z() * 2) { - setX(_playerObject->position().x() + _world->cameraReleativePosition().z() * 4); + if (_playerObject->position().x() - _world->cameraReleativePosition().z() > + position().x()) { - float dY = rand() % static_cast(_world->cameraReleativePosition().z() * 2 - - _world->cameraReleativePosition().z()); + float dX = _world->cameraReleativePosition().z() * 2 + + (rand() % static_cast(_world->cameraReleativePosition().z())); + + setX(_playerObject->position().x() + dX); + + float dY = (rand() % static_cast(_world->cameraReleativePosition().z() * 4) + - _world->cameraReleativePosition().z() * 2); setY(_playerObject->position().y() + dY); } diff --git a/src/Core/Crawl/snakeitem.cpp b/src/Core/Crawl/snakeitem.cpp index cc458cf..a94d1a8 100644 --- a/src/Core/Crawl/snakeitem.cpp +++ b/src/Core/Crawl/snakeitem.cpp @@ -26,7 +26,11 @@ void SnakeItem::render(unsigned int tbfMsec) { auto ratationVector = (_prevObject->position() - position()); if (auto claster = static_cast(parentClaster())) { - if (ratationVector.length() > claster->lengthBetwinItems()) { + float ratationLength = ratationVector.length(); + + if (ratationLength > claster->lengthBetwinItems() * 10) { + setposition(_prevObject->position()); + } else if (ratationLength > claster->lengthBetwinItems()) { setMovableVector(ratationVector.normalized() * claster->currentMovableVector().length()); } } diff --git a/src/Core/private/defaultbackgroundai.cpp b/src/Core/private/defaultbackgroundai.cpp index 32b57da..9c7e998 100644 --- a/src/Core/private/defaultbackgroundai.cpp +++ b/src/Core/private/defaultbackgroundai.cpp @@ -11,6 +11,8 @@ DefaultBackgroundAI::DefaultBackgroundAI() { _timer = new QTimer(); _timer->setInterval(1000); + + connect(_timer, &QTimer::timeout, this, &DefaultBackgroundAI::handleTimerTriger); } DefaultBackgroundAI::~DefaultBackgroundAI() { @@ -27,7 +29,7 @@ void DefaultBackgroundAI::stopAI() { } void DefaultBackgroundAI::handleTimerTriger() { - _timer->setInterval(rand() % 1000 + 10); + _timer->setInterval(rand() % 2000 + 200); emit userTap(); } diff --git a/src/Core/private/engine.cpp b/src/Core/private/engine.cpp index 5171f71..cdbc819 100644 --- a/src/Core/private/engine.cpp +++ b/src/Core/private/engine.cpp @@ -46,9 +46,16 @@ void Engine::setWorld(IWorld *world) { _currentWorld = world; emit worldChanged(); - prepareNewWorld(); + if (!prepareNewWorld()) { + QuasarAppUtils::Params::log("Failed to init world. World name: " + _currentWorld->name(), + QuasarAppUtils::Error); + + _currentWorld = nullptr; + return; + } startRenderLoop(); + _currentWorld->runAsBackGround(); } void Engine::setScane(QObject *newScane) { @@ -129,20 +136,18 @@ void Engine::setPrepareLvlProgress(int newPrepareLvlProgress) { emit prepareLvlProgressChanged(); } -void Engine::prepareNewWorld() { +bool Engine::prepareNewWorld() { if (!_currentWorld->prepare()) { - QuasarAppUtils::Params::log("Failed to init world. World name: " + _currentWorld->name(), - QuasarAppUtils::Error); - - _currentWorld = nullptr; - return; + return false; } if (!_currentWorld->userInterface()->init()) { - return; + return false; } setMenu(_currentWorld->userInterface()); + + return true; } void Engine::renderLoop() { diff --git a/src/Core/private/engine.h b/src/Core/private/engine.h index 173365e..b4cc171 100644 --- a/src/Core/private/engine.h +++ b/src/Core/private/engine.h @@ -132,7 +132,7 @@ signals: private: void setPrepareLvlProgress(int newPrepareLvlProgress); - void prepareNewWorld(); + bool prepareNewWorld(); void renderLoop(); diff --git a/src/CrawlTestLvl b/src/CrawlTestLvl index 0f022f4..9f06054 160000 --- a/src/CrawlTestLvl +++ b/src/CrawlTestLvl @@ -1 +1 @@ -Subproject commit 0f022f42093a1e5311cbcd13c9ed40b54163c251 +Subproject commit 9f06054ae836016881dc132d336c5da5bb3dca6c