2018-09-11 20:10:37 +03:00
|
|
|
#include "controller.h"
|
2018-10-11 18:09:35 +03:00
|
|
|
#include <cmath>
|
|
|
|
#include <ctime>
|
2018-11-04 16:11:55 +03:00
|
|
|
#include "diff.h"
|
2019-02-19 16:07:38 +03:00
|
|
|
#include <lvls.h>
|
2019-07-29 11:45:04 +03:00
|
|
|
#include "ProfileViewItems/mainmenumodel.h"
|
2018-09-11 20:10:37 +03:00
|
|
|
|
2018-10-11 18:09:35 +03:00
|
|
|
Controller::Controller() {
|
|
|
|
srand(static_cast<unsigned int>(time(nullptr)));
|
2018-11-04 15:02:03 +03:00
|
|
|
timer = new QTimer();
|
2018-12-01 21:06:40 +03:00
|
|
|
timer->setInterval(1);
|
2019-07-29 11:45:04 +03:00
|
|
|
|
|
|
|
_networkModel = new MainMenuModel(this);
|
|
|
|
|
2018-11-04 15:02:03 +03:00
|
|
|
connect(timer, SIGNAL(timeout()), this, SLOT(update()));
|
2019-07-29 11:45:04 +03:00
|
|
|
connect(_networkModel, &MainMenuModel::newGame, this, &Controller::handleNewGame);
|
|
|
|
|
2018-10-11 18:09:35 +03:00
|
|
|
}
|
2018-09-11 20:10:37 +03:00
|
|
|
|
2019-07-28 15:14:51 +03:00
|
|
|
Controller::~Controller() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-11-04 16:11:55 +03:00
|
|
|
bool Controller::nextLvl() {
|
|
|
|
if (lvl + 1 >= lvls.size()) {
|
|
|
|
return true;
|
|
|
|
}
|
2018-12-01 22:39:03 +03:00
|
|
|
m_generalLong += static_cast<int>(world.getCurrentLong());
|
2018-11-04 16:11:55 +03:00
|
|
|
|
|
|
|
generateDiff(world.init(lvls.value(++lvl)));
|
2018-11-25 03:24:41 +03:00
|
|
|
startTimer();
|
2018-11-04 16:11:55 +03:00
|
|
|
|
2018-12-01 22:39:03 +03:00
|
|
|
|
2018-11-04 16:11:55 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Controller::generateDiff(const QMap<int, GuiObject *>& objs) {
|
|
|
|
|
|
|
|
auto removeIds = objectsContainer.keys();
|
|
|
|
QList<int> addedIds;
|
|
|
|
|
|
|
|
for (auto i = objs.begin(); i != objs.end(); ++i) {
|
|
|
|
if (objectsContainer.contains(i.key())) {
|
|
|
|
removeIds.removeOne(i.key());
|
|
|
|
} else {
|
|
|
|
objectsContainer.insert(i.key(), i.value());
|
|
|
|
addedIds.push_back(i.key());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (removeIds.size() || addedIds.size()) {
|
|
|
|
Diff diff;
|
|
|
|
|
|
|
|
diff.setRemoveIds(removeIds);
|
|
|
|
diff.setAddedIds(addedIds);
|
|
|
|
emit gameObjectsChanged(diff);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-30 21:23:41 +03:00
|
|
|
void Controller::update() {
|
2018-11-28 22:17:30 +03:00
|
|
|
if (pause) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-11-04 15:02:03 +03:00
|
|
|
world.render();
|
2018-11-14 23:15:36 +03:00
|
|
|
if(world.isDefiat()) {
|
|
|
|
stopTimer();
|
2018-11-25 03:24:41 +03:00
|
|
|
emit finished(false, lvl, world.getCurrentLong());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (world.isEnd()) {
|
|
|
|
stopTimer();
|
|
|
|
emit finished(true, lvl, world.getCurrentLong());
|
2018-11-14 23:15:36 +03:00
|
|
|
}
|
2018-11-25 18:10:08 +03:00
|
|
|
|
|
|
|
long_changed(static_cast<int>(world.getCurrentLong()));
|
2018-12-01 22:39:03 +03:00
|
|
|
generalLongchanged(generalLong());
|
|
|
|
|
2018-10-30 21:23:41 +03:00
|
|
|
}
|
|
|
|
|
2019-07-29 11:45:04 +03:00
|
|
|
void Controller::handleNewGame() {
|
2018-11-25 03:24:41 +03:00
|
|
|
|
2018-11-25 18:10:08 +03:00
|
|
|
world.resetPosition();
|
2018-11-25 03:24:41 +03:00
|
|
|
|
2018-11-04 16:11:55 +03:00
|
|
|
WorldRules newGameRules = lvls.first();
|
|
|
|
lvl = 0;
|
2018-12-01 22:39:03 +03:00
|
|
|
m_generalLong = 0;
|
2018-11-04 16:11:55 +03:00
|
|
|
generateDiff(world.init(newGameRules));
|
2018-11-14 23:15:36 +03:00
|
|
|
startTimer();
|
2018-11-04 16:11:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
QObject *Controller::getGameObject(int id) {
|
|
|
|
return objectsContainer.value(id, nullptr);
|
|
|
|
}
|
|
|
|
|
2018-11-04 15:02:03 +03:00
|
|
|
void Controller::startTimer() {
|
2018-11-04 16:11:55 +03:00
|
|
|
timer->start();
|
2018-10-30 21:23:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Controller::stopTimer() {
|
|
|
|
timer->stop();
|
|
|
|
}
|
|
|
|
|
2018-11-25 18:10:08 +03:00
|
|
|
int Controller::long_() const {
|
|
|
|
return static_cast<int>(world.getCurrentLong());
|
|
|
|
}
|
|
|
|
|
2018-12-01 22:39:03 +03:00
|
|
|
int Controller::generalLong() const {
|
|
|
|
return m_generalLong + long_();
|
|
|
|
}
|
|
|
|
|
2019-07-29 11:45:04 +03:00
|
|
|
QObject *Controller::mainMenuModel() const {
|
|
|
|
return _networkModel;
|
|
|
|
}
|
|
|
|
|
2018-11-16 02:08:35 +03:00
|
|
|
void Controller::buttonPress() {
|
|
|
|
world.reversClick();
|
|
|
|
}
|
|
|
|
|
2018-11-28 22:17:30 +03:00
|
|
|
void Controller::setPause(bool p){
|
|
|
|
pause = p;
|
2018-12-01 22:39:03 +03:00
|
|
|
if (!pause) {
|
|
|
|
world.unPause();
|
|
|
|
}
|
2018-11-28 22:17:30 +03:00
|
|
|
}
|
|
|
|
|