Snake/src/Core/private/engine.cpp

145 lines
2.8 KiB
C++
Raw Normal View History

2021-06-06 15:53:49 +03:00
#include "engine.h"
#include <QQmlComponent>
2021-06-09 16:38:55 +03:00
#include <SnakeProject/guiobject.h>
2021-06-08 18:00:42 +03:00
#include "SnakeProject/iworld.h"
2021-06-11 18:09:03 +03:00
#include <quasarapp.h>
2021-06-12 18:20:29 +03:00
#include "SnakeProject/icontrol.h"
2021-06-06 15:53:49 +03:00
2021-06-10 18:07:04 +03:00
Engine::Engine(QObject *parent): QObject(parent) {
2021-06-06 15:53:49 +03:00
}
QObject *Engine::scane() {
return _scane;
}
void Engine::handleGameObjectsChanged(Diff diff) {
2021-06-09 16:38:55 +03:00
for (const auto &item: qAsConst(diff.addedIds)) {
2021-06-06 15:53:49 +03:00
add(item);
}
2021-06-09 16:38:55 +03:00
for (int id: qAsConst(diff.removeIds)) {
2021-06-06 15:53:49 +03:00
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;
}
2021-06-07 17:37:03 +03:00
void Engine::setQmlEngine(QQmlEngine *newEngine) {
2021-06-06 15:53:49 +03:00
if (_engine == newEngine)
return;
_engine = newEngine;
}
2021-06-07 17:37:03 +03:00
2021-06-08 18:00:42 +03:00
void Engine::setWorld(IWorld *world) {
if (_currentWorld == world)
return ;
2021-06-08 21:56:36 +03:00
if (_currentWorld) {
disconnect(_currentWorld, &IWorld::sigOBjctsListChanged,
this, &Engine::handleGameObjectsChanged);
2021-06-11 18:09:03 +03:00
_currentWorld->deinit();
2021-06-08 21:56:36 +03:00
}
2021-06-08 18:00:42 +03:00
_currentWorld = world;
2021-06-11 18:09:03 +03:00
if (!_currentWorld->init()) {
QuasarAppUtils::Params::log("Failed to init world. World name: " + _currentWorld->name(),
QuasarAppUtils::Error);
_currentWorld = nullptr;
return;
}
2021-06-13 20:16:36 +03:00
if (!_currentWorld->userInterface()->init()) {
return;
}
2021-06-12 18:20:29 +03:00
setMenu(_currentWorld->userInterface());
2021-06-08 21:56:36 +03:00
connect(_currentWorld, &IWorld::sigOBjctsListChanged,
this, &Engine::handleGameObjectsChanged,
Qt::QueuedConnection);
2021-06-08 18:00:42 +03:00
}
2021-06-07 17:37:03 +03:00
2021-06-08 18:00:42 +03:00
QString Engine::hdr() const {
if (!_currentWorld)
return "";
return _currentWorld->hdrMap();
}
void Engine::setScane(QObject *newScane) {
if (_scane == newScane)
return;
_scane = newScane;
emit scaneChanged();
2021-06-07 17:37:03 +03:00
}
2021-06-09 16:38:55 +03:00
QObject *Engine::player() const {
if (_currentWorld)
return _currentWorld->_player;
return nullptr;
}
QObject *Engine::world() const {
return _currentWorld;
}
2021-06-12 18:20:29 +03:00
QObject *Engine::menu() const {
return _menu;
}
void Engine::setMenu(QObject *newMenu) {
if (_menu == newMenu)
return;
_menu = newMenu;
emit menuChanged();
}
2021-06-13 20:16:36 +03:00
int Engine::prepareLvlProgress() const {
return _prepareLvlProgress;
}