added initialisation of the world

This commit is contained in:
Andrei Yankovich 2021-08-16 11:30:03 +03:00
parent 36b4ead978
commit 0ff31046f1
4 changed files with 66 additions and 65 deletions

View File

@ -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);
}
}

View File

@ -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;
};

View File

@ -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);

View File

@ -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;