From ad221f815dc407634dd2626b8cd78b07a5e8527d Mon Sep 17 00:00:00 2001 From: EndrII Date: Tue, 15 Jun 2021 16:04:18 +0300 Subject: [PATCH] added supprt of initalize controllers --- src/Core/SnakeProject/clientapp.cpp | 1 + src/Core/SnakeProject/iai.h | 2 +- src/Core/SnakeProject/iworld.cpp | 22 +++++++++++++++++++++- src/Core/SnakeProject/iworld.h | 13 +++++++++++++ src/Core/private/engine.cpp | 10 ++++++++++ src/Core/private/engine.h | 5 +++++ 6 files changed, 51 insertions(+), 2 deletions(-) diff --git a/src/Core/SnakeProject/clientapp.cpp b/src/Core/SnakeProject/clientapp.cpp index ea2ebf2..3b795e7 100644 --- a/src/Core/SnakeProject/clientapp.cpp +++ b/src/Core/SnakeProject/clientapp.cpp @@ -108,6 +108,7 @@ void ClientApp::start(const QString &lvl) { } _engine->setWorld(data.model); + _engine->start(); } QList ClientApp::availablePlugins() const { diff --git a/src/Core/SnakeProject/iai.h b/src/Core/SnakeProject/iai.h index 536583c..62c9c7d 100644 --- a/src/Core/SnakeProject/iai.h +++ b/src/Core/SnakeProject/iai.h @@ -15,7 +15,7 @@ class IAI { public: IAI(); - + virtual ~IAI() = default; /** * @brief startAI This method must be run ai. diff --git a/src/Core/SnakeProject/iworld.cpp b/src/Core/SnakeProject/iworld.cpp index e4dab62..a4ce629 100644 --- a/src/Core/SnakeProject/iworld.cpp +++ b/src/Core/SnakeProject/iworld.cpp @@ -6,6 +6,7 @@ #include "iground.h" #include "defaultcontrol.h" #include "worldstatus.h" +#include "iai.h" IWorld::IWorld() { @@ -35,6 +36,14 @@ void IWorld::render(unsigned int tbfMsec) { } } +void IWorld::initPlayerControl(IControl *control) { + auto controlObject = dynamic_cast(control); + + if (controlObject) { + connect(controlObject, &DefaultControl::backToMenu, this, &IWorld::handleStop); + } +} + bool IWorld::start() { _player->setposition({0,0,0}); _player->setSpeed(0); @@ -77,7 +86,6 @@ bool IWorld::init() { _userInterface = initUserInterface(); _backgroundAI = initBackGroundAI(); - if (!isInit()) { QuasarAppUtils::Params::log("Failed to init world implementation."); deinit(); @@ -91,6 +99,9 @@ bool IWorld::init() { return false; } + initPlayerControl(_userInterface); + initPlayerControl(dynamic_cast(_backgroundAI)); + generateGround(); worldChanged(*_worldRules->begin()); @@ -122,6 +133,11 @@ void IWorld::deinit() { _userInterface = nullptr; } + if (_backgroundAI) { + delete _backgroundAI; + _backgroundAI = nullptr; + } + clearItems(); _hdrMap = ""; @@ -182,6 +198,10 @@ void IWorld::setTap(bool newTap) { _tap = newTap; } +IAI *IWorld::backgroundAI() const { + return _backgroundAI; +} + IControl *IWorld::userInterface() const { return _userInterface; } diff --git a/src/Core/SnakeProject/iworld.h b/src/Core/SnakeProject/iworld.h index d87c255..227dc89 100644 --- a/src/Core/SnakeProject/iworld.h +++ b/src/Core/SnakeProject/iworld.h @@ -124,6 +124,13 @@ public: */ virtual QVector3D initCameraPosition() = 0; + /** + * @brief initPlayerControl This method should be configure all connections of @a control object. + * @brief control This is control object + * @note override this method if you have own IControl object. + */ + virtual void initPlayerControl(IControl* control); + /** * @brief start This method will be invoked when user click start button. * @note The Default implementation reset all positions for all objects. @@ -188,6 +195,12 @@ public: */ void setWorldStatus(int newWorldStatus); + /** + * @brief backgroundAI This method return current backgroundAI. + * @return Raw pointer to background AI object + */ + IAI *backgroundAI() const; + signals: /** * @brief sigGameFinished This signal emit when game are finished diff --git a/src/Core/private/engine.cpp b/src/Core/private/engine.cpp index c3507e6..6abb5fd 100644 --- a/src/Core/private/engine.cpp +++ b/src/Core/private/engine.cpp @@ -152,6 +152,16 @@ int Engine::prepareLvlProgress() const { return _prepareLvlProgress; } +bool Engine::start() const { + if (!_currentWorld) + return false; + + if (!_currentWorld->isInit()) + return false; + + return _currentWorld->start(); +} + void Engine::setPrepareLvlProgress(int newPrepareLvlProgress) { if (_prepareLvlProgress == newPrepareLvlProgress) return; diff --git a/src/Core/private/engine.h b/src/Core/private/engine.h index 4f55f96..2a68b4a 100644 --- a/src/Core/private/engine.h +++ b/src/Core/private/engine.h @@ -91,6 +91,11 @@ public: */ int prepareLvlProgress() const; + /** + * @brief start This method run current lvl + * @return true if lvl started successful. + */ + bool start() const; private slots: