From 0ff31046f171d51fe5276895f68705a8c4b7b7a9 Mon Sep 17 00:00:00 2001
From: EndrII <EndrIIMail@gmail.com>
Date: Mon, 16 Aug 2021 11:30:03 +0300
Subject: [PATCH] added initialisation of the world

---
 src/Core/Crawl/clientapp.cpp | 49 ++------------------------------
 src/Core/Crawl/clientapp.h   | 14 ---------
 src/Core/private/engine.cpp  | 55 ++++++++++++++++++++++++++++++++++--
 src/Core/private/engine.h    | 13 ++++++++-
 4 files changed, 66 insertions(+), 65 deletions(-)

diff --git a/src/Core/Crawl/clientapp.cpp b/src/Core/Crawl/clientapp.cpp
index 286555f..c2c1b8f 100644
--- a/src/Core/Crawl/clientapp.cpp
+++ b/src/Core/Crawl/clientapp.cpp
@@ -37,16 +37,6 @@ QByteArray ClientApp::initTheme() {
     }
 }
 
-ILevel *ClientApp::getLastLevel() {
-    for (const auto &data : qAsConst(_availableLvls)) {
-        if (data && data->world() && _engine->currentUser() &&
-                _engine->currentUser()->isUnlocked(data->world()->itemId())) {
-            return data;
-        }
-    }
-
-    return nullptr;
-}
 
 ClientApp::ClientApp() {
     _engine = new Engine();
@@ -54,37 +44,6 @@ ClientApp::ClientApp() {
 
 ClientApp::~ClientApp() {
     delete _engine;
-
-    for (auto it = _availableLvls.begin(); it != _availableLvls.end(); ++it) {
-        delete it.value();
-    }
-
-    _availableLvls.clear();
-}
-
-void ClientApp::initStore(QMultiHash<int, const IItem *> & result) {
-    for (const auto &data : qAsConst(_availableLvls)) {
-        if (data && data->world())
-            result.unite(data->world()->childItemsRecursive());
-    }
-}
-
-void ClientApp::changeLevel(int lvl) {
-    ILevel* data = _availableLvls.value(lvl, nullptr);
-
-    if (!data) {
-        QuasarAppUtils::Params::log("Failed to start lvl.", QuasarAppUtils::Error);
-        return;
-    }
-
-    if (!_engine) {
-        QuasarAppUtils::Params::log("Failed to start lvl, Engine not initialized.",
-                                    QuasarAppUtils::Error);
-
-        return;
-    }
-
-    _engine->setLevel(data);
 }
 
 bool ClientApp::init(QQmlApplicationEngine *engine) {
@@ -124,17 +83,13 @@ bool ClientApp::init(QQmlApplicationEngine *engine) {
     if (engine->rootObjects().isEmpty())
         return false;
 
-    QMultiHash<int, const IItem *> availabelItems;
-    initStore(availabelItems);
-    _engine->init(availabelItems);
-
-    _engine->setLevel(getLastLevel());
+    _engine->init();
 
     return true;
 }
 
 void ClientApp::addLvl(ILevel *levelWordl) {
-    _availableLvls.insert(levelWordl->world()->itemId(), levelWordl);
+    _engine->addLvl(levelWordl);
 }
 
 }
diff --git a/src/Core/Crawl/clientapp.h b/src/Core/Crawl/clientapp.h
index 80a8cb5..8880bac 100644
--- a/src/Core/Crawl/clientapp.h
+++ b/src/Core/Crawl/clientapp.h
@@ -58,13 +58,6 @@ public:
 
 private:
     QByteArray initTheme();
-    ILevel *getLastLevel();
-
-    /**
-     * @brief initStore This method push to @a result map all available store items.
-     * @param result This is result value. Hash map of the available items.
-     */
-    void initStore(QMultiHash<int, const IItem *> &result);
 
     /**
      * @brief addLvl This method should be add level to game.
@@ -72,13 +65,6 @@ private:
      */
     void addLvl(ILevel* levelWordl);
 
-    /**
-     * @brief changeLevel This method star new game in @a lvl
-     * @param lvl This is lvl name
-     */
-    void changeLevel(int lvl);
-
-    QHash<int, ILevel*> _availableLvls;
     Engine *_engine = nullptr;
 
 };
diff --git a/src/Core/private/engine.cpp b/src/Core/private/engine.cpp
index 376361b..f9ad67c 100644
--- a/src/Core/private/engine.cpp
+++ b/src/Core/private/engine.cpp
@@ -35,6 +35,12 @@ Engine::~Engine() {
     stopRenderLoop();
     delete _menu;
     delete _currentUser;
+
+    for (auto it = _availableLvls.begin(); it != _availableLvls.end(); ++it) {
+        delete it.value();
+    }
+
+    _availableLvls.clear();
 }
 
 QObject *Engine::scane() {
@@ -62,8 +68,17 @@ void Engine::setLevel(ILevel *world) {
         return;
     }
 
-    if (_currentLevel->world()) {
-        QuasarAppUtils::Params::log("Failed to init world. World name: " +
+    if (!_currentLevel->world()) {
+        QuasarAppUtils::Params::log("Failed to init world. The World Object is null: " +
+                                    _currentLevel->world()->itemName(),
+                                    QuasarAppUtils::Error);
+
+        _currentLevel = nullptr;
+        return;
+    }
+
+    if (!_currentLevel->previewScane()) {
+        QuasarAppUtils::Params::log("Failed to init world. The World Preview scane is null. World Name: " +
                                     _currentLevel->world()->itemName(),
                                     QuasarAppUtils::Error);
 
@@ -140,6 +155,25 @@ void Engine::handleUnlockedItemsListChanged(const QSet<int> &newSet) {
 
 void Engine::handleLevelChanged(int levelId) {
 
+    ILevel* data = _availableLvls.value(levelId, nullptr);
+
+    if (!data) {
+        QuasarAppUtils::Params::log("Failed to start lvl.", QuasarAppUtils::Error);
+        return;
+    }
+
+    setLevel(data);
+}
+
+ILevel *Engine::getLastLevel() {
+    for (const auto &data : qAsConst(_availableLvls)) {
+        if (data && data->world() && currentUser() &&
+               currentUser()->isUnlocked(data->world()->itemId())) {
+            return data;
+        }
+    }
+
+    return nullptr;
 }
 
 QObject *Engine::getGameObject(int id) const {
@@ -212,6 +246,14 @@ void Engine::setNewUser(User *user) {
     }
 }
 
+void Engine::addLvl(ILevel *levelWordl) {
+    if (!levelWordl->world()) {
+        QuasarAppUtils::Params::log("The Level not contains world object!!!");
+        return;
+    }
+    _availableLvls.insert(levelWordl->world()->itemId(), levelWordl);
+}
+
 Store *Engine::store() const {
     return _store;
 }
@@ -227,7 +269,14 @@ User *Engine::currentUser() const {
     return _currentUser;
 }
 
-void Engine::init(const QMultiHash<int, const IItem *> &availabelItems) {
+void Engine::init() {
+    QMultiHash<int, const IItem *> availabelItems;
+
+    for (const auto &data : qAsConst(_availableLvls)) {
+        if (data && data->world())
+            availabelItems.unite(data->world()->childItemsRecursive());
+    }
+
     _store->init(availabelItems);
     static_cast<StoreViewModel*>(_menu->storeView())->init(_store, _currentUser);
 
diff --git a/src/Core/private/engine.h b/src/Core/private/engine.h
index 1ed641c..ee47429 100644
--- a/src/Core/private/engine.h
+++ b/src/Core/private/engine.h
@@ -110,7 +110,7 @@ public:
      * @brief init This method initialize the main model. Sets available levels and items.
      * @param availabelItems This is list of available items.
      */
-    void init(const QMultiHash<int, const IItem *> &availabelItems);
+    void init();
 
     /**
      * @brief store This pointer return pointer to store.
@@ -136,6 +136,12 @@ public:
      */
     void setNewUser(User* user);
 
+    /**
+     * @brief addLvl This method should be add level to game.
+     * @param levelWordl This is world instance
+     */
+    void addLvl(ILevel* levelWordl);
+
 signals:
     void scaneChanged();
     void playerChanged();
@@ -180,10 +186,15 @@ private slots:
      */
     void handleLevelChanged(int levelId);
 private:
+
+    ILevel * getLastLevel();
+
     void renderLoop();
 
     QObject *_scane = nullptr;
     ILevel* _currentLevel = nullptr;
+    QHash<int, ILevel*> _availableLvls;
+
     MainMenuModel *_menu = nullptr;
 
     quint64 _oldTimeRender = 0;