engine refactoring to plugins system

This commit is contained in:
Andrei Yankovich 2021-06-06 15:53:49 +03:00
parent 3405afb40d
commit 85baeb6eed
78 changed files with 283 additions and 60 deletions

View File

@ -62,7 +62,7 @@ endif()
add_subdirectory(submodules/QuasarAppLib)
add_subdirectory(submodules/SimpleQmlNotify)
add_subdirectory(src/ClientLib)
add_subdirectory(src/Core)
add_subdirectory(src/Client)

View File

@ -1,23 +0,0 @@
#ifndef DIFF_H
#define DIFF_H
#include <QObject>
class Diff
{
Q_GADGET
private:
QList<int> removeIds;
QList<int> addedIds;
public:
explicit Diff();
Q_INVOKABLE QList<int> getRemoveIds() const;
void setRemoveIds(const QList<int> &value);
Q_INVOKABLE QList<int> getAddedIds() const;
void setAddedIds(const QList<int> &value);
};
Q_DECLARE_METATYPE(Diff)
#endif // DIFF_H

View File

@ -1,3 +0,0 @@
#include "snakeutils.h"

View File

@ -1,20 +0,0 @@
#ifndef SNAKEUTILS_H
#define SNAKEUTILS_H
#include "SnakeProject/global.h"
#include <QMap>
#include <QPoint>
#include <QVariant>
/**
* @brief WorldRules
* this map conteins:
* 1. list of generated objects and they count.
* 2. long of world (Long),
* 3. spead of world (Spead),
* !!!Note: all object show on map alltime.
*/
typedef QMap<QString, int> WorldRules;
#endif // SNAKEUTILS_Hм.

View File

@ -13,16 +13,16 @@ add_definitions(-DSnakeProject_LIBRARY)
file(GLOB SOURCE_CPP
"*SnakeProject/*.cpp"
"SnakeProject/private/*.cpp"
"private/*.cpp"
"*.qrc"
"SnakeProject/*.qrc"
"SnakeProject/private/*.qrc"
"private/*.qrc"
)
set(PUBLIC_INCUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
set(PRIVATE_INCUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/private")
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core Quick QuickControls2 Concurrent REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core Quick Quick3D Concurrent REQUIRED)
add_library(${CURRENT_PROJECT} ${SOURCE_CPP} ${SOURCE_QRC})

View File

@ -1,10 +1,11 @@
#include "clientapp.h"
#include "imageprovider.h"
#include "SnakeProject/mainmenumodel.h"
#include "mainmenumodel.h"
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <quasarapp.h>
#include <controller.h>
#include <qmlnotifyservice.h>
@ -19,9 +20,11 @@ QByteArray ClientApp::initTheme() {
}
ClientApp::ClientApp() {
contr = new Controller();
}
ClientApp::~ClientApp() {
delete contr;
}
void ClientApp::initLang() {
@ -54,7 +57,7 @@ bool ClientApp::init(QQmlApplicationEngine *engine) {
engine->addImageProvider(QLatin1String("userItems"), new ImageProvider());
root->setContextProperty("contr", &contr);
root->setContextProperty("contr", contr);
initSnakeProjectResources();
initLang();

View File

@ -1,8 +1,11 @@
#ifndef CLIENTAPP_H
#define CLIENTAPP_H
#include "controller.h"
#include "diff.h"
#include <QByteArray>
#include "global.h"
class Controller;
inline void initSnakeProjectResources() { Q_INIT_RESOURCE(SnakeProject); }
@ -11,7 +14,7 @@ class QQmlApplicationEngine;
class SNAKEPROJECT_EXPORT ClientApp
{
private:
Controller contr;
Controller *contr = nullptr;
QByteArray initTheme();

View File

@ -0,0 +1,6 @@
#include "isnake.h"
ISnake::ISnake()
{
}

View File

@ -0,0 +1,14 @@
#ifndef ISNAKE_H
#define ISNAKE_H
/**
* @brief The ISnake class This interface used for impementation player object on a World.
*
*/
class ISnake
{
public:
ISnake();
};
#endif // ISNAKE_H

View File

@ -0,0 +1,28 @@
#include "iworld.h"
#include "itemworld.h"
IWorld::IWorld() {
}
IWorld::~IWorld() {
deinit();
}
bool IWorld::init() {
}
void IWorld::deinit() {
delete _snake;
for (const auto& item : qAsConst(items)) {
delete item;
}
items.clear();
spead = 0;
currentLong = 0;
_hdrMap = "";
delete _worldRules;
}

View File

@ -0,0 +1,80 @@
#ifndef IWORLD_H
#define IWORLD_H
#include "isnake.h"
#include <QMap>
#include <QString>
class ItemWorld;
/**
* @brief WorldRules
* this map conteins:
* 1. list of generated objects and they count.
* 2. long of world (Long),
* 3. spead of world (Spead),
* !!!Note: all object show on map alltime.
*/
/**
* @brief WorldObjects This is map list of the avalable objects and its count on a lvl-long point.
*/
typedef QMap<QString, int> WorldObjects;
/**
* @brief WorldRule This is list of the lvl-long points and its rules. For get more information see the WorldOBjects typedef.
*/
typedef QMap<int, WorldObjects> WorldRule;
/**
* @brief The IWorld class use this interface for implementation your own game levels
*/
class IWorld
{
public:
IWorld();
virtual ~IWorld();
/**
* @brief initPlayer The implementation of This interface must be return playerObject.
* @return raw pointer to the player object.
* @note The Palyer object will be deleted when wold distroed.
* So do not delete your created player pbject yuorself.
*/
virtual ISnake* initPlayer() const = 0;
/**
* @brief initWorldRules The implementation of this interface must be retun initialized list of the world rules.
* For more information see the WorldRules map.
* @return a raw pointer to world a rules map.
* @note The Palyer object will be deleted when wold distroed.
* So do not delete your created player pbject yuorself.
*/
virtual WorldRule* initWorldRules() const = 0;
/**
* @brief initHdrBackGround The implementation of this method must be return valid path to the hdr image map.
* The hdr image map it is background jpeg image.
* @return path to hfr map.
*/
virtual QString initHdrBackGround() const = 0;
private:
bool init();
void deinit();
ISnake * _snake;
QMap<QString, ItemWorld*> items;
int endLong;
float spead = 0, d_spead = 0, currentLong = 0;
QString _hdrMap;
WorldRule *_worldRules = nullptr;
friend class Engine;
};
#endif // IWORLD_H

View File

@ -1,6 +1,6 @@
#include "diff.h"
QList<int> Diff::getRemoveIds() const {
const QList<int>& Diff::getRemoveIds() const {
return removeIds;
}
@ -8,11 +8,11 @@ void Diff::setRemoveIds(const QList<int> &value) {
removeIds = value;
}
QList<int> Diff::getAddedIds() const {
const QList<GuiObject*>& Diff::getAddedIds() const {
return addedIds;
}
void Diff::setAddedIds(const QList<int> &value) {
void Diff::setAddedIds(const QList<GuiObject *> &value) {
addedIds = value;
}

26
src/Core/private/diff.h Normal file
View File

@ -0,0 +1,26 @@
#ifndef DIFF_H
#define DIFF_H
#include <QObject>
class GuiObject;
/**
* @brief The Diff class contains list of the last changes on a game world.
*/
class Diff {
private:
QList<int> removeIds;
QList<GuiObject*> addedIds;
public:
explicit Diff();
Q_INVOKABLE const QList<int> &getRemoveIds() const;
void setRemoveIds(const QList<int> &value);
Q_INVOKABLE const QList<GuiObject *> &getAddedIds() const;
void setAddedIds(const QList<GuiObject*> &value);
};
Q_DECLARE_METATYPE(Diff)
#endif // DIFF_H

View File

@ -0,0 +1,66 @@
#include "engine.h"
#include <QQmlComponent>
#include <guiobject.h>
Engine::Engine() {
}
QObject *Engine::scane() {
return _scane;
}
void Engine::handleGameObjectsChanged(Diff diff) {
for (const auto &item: diff.getAddedIds()) {
add(item);
}
for (int id: diff.getRemoveIds()) {
remove(id);
}
}
bool Engine::add(GuiObject *obj) {
if (!_engine)
return false;
if (!_scane)
return false;
// Using QQmlComponent
QQmlComponent component(_engine,
QUrl::fromLocalFile("MyItem.qml"),
_scane);
QObject *object = component.create();
if (!object)
return false;
if (!object->setProperty("model", QVariant::fromValue(obj)))
return false;
_qmlObjects.insert(obj->guiId(), object);
return true;
}
bool Engine::remove(int id) {
if (!_qmlObjects.contains(id)) {
return false;
}
_qmlObjects[id]->deleteLater();
_qmlObjects.remove(id);
return true;
}
void Engine::setEngine(QQmlEngine *newEngine) {
if (_engine == newEngine)
return;
_engine = newEngine;
}

43
src/Core/private/engine.h Normal file
View File

@ -0,0 +1,43 @@
#ifndef ENGINE_H
#define ENGINE_H
#include <QObject>
#include <QQmlEngine>
#include <diff.h>
/**
* @brief The Engine class
*/
class Engine : public QObject {
Q_OBJECT
public:
Engine();
/**
* @brief scane This method return main scane of the game.
* @return pointer to main game scane.
*/
Q_INVOKABLE QObject* scane();
void setEngine(QQmlEngine *newEngine);
public slots:
/**
* @brief handleGameObjectsChanged This slot invoked when games objects changed.
* @brief diff this is changes of the lvl.
*/
void handleGameObjectsChanged(Diff diff);
private:
bool add(GuiObject* obj);
bool remove(int id);
QObject *_scane = nullptr;
QQmlEngine *_engine = nullptr;
QHash<int, QObject*> _qmlObjects;
};
#endif // ENGINE_H

View File

@ -2,7 +2,7 @@
#define NETWORKPROFILEMAINMODEL_H
#include <QObject>
#include "SnakeProject/settings.h"
#include "settings.h"
class MainMenuModel : public QObject

View File

@ -2,7 +2,7 @@
#define SETTINGSVIEWMODEL_H
#include <QObject>
#include "SnakeProject/settings.h"
#include "settings.h"
class SettingsViewModel: public QObject

View File

Before

Width:  |  Height:  |  Size: 32 KiB

After

Width:  |  Height:  |  Size: 32 KiB

View File

Before

Width:  |  Height:  |  Size: 2.4 KiB

After

Width:  |  Height:  |  Size: 2.4 KiB

View File

Before

Width:  |  Height:  |  Size: 1.3 KiB

After

Width:  |  Height:  |  Size: 1.3 KiB