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"
|
|
|
|
#include "lvls.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();
|
|
|
|
timer->setInterval(33);
|
|
|
|
connect(timer, SIGNAL(timeout()), this, SLOT(update()));
|
2018-10-11 18:09:35 +03:00
|
|
|
}
|
2018-09-11 20:10:37 +03:00
|
|
|
|
2018-11-04 16:11:55 +03:00
|
|
|
bool Controller::nextLvl() {
|
|
|
|
if (lvl + 1 >= lvls.size()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
generateDiff(world.init(lvls.value(++lvl)));
|
2018-11-25 03:24:41 +03:00
|
|
|
startTimer();
|
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-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-10-30 21:23:41 +03:00
|
|
|
}
|
|
|
|
|
2018-11-04 16:11:55 +03:00
|
|
|
void Controller::newGame() {
|
2018-11-25 03:24:41 +03:00
|
|
|
|
|
|
|
world.clear();
|
|
|
|
|
2018-11-04 16:11:55 +03:00
|
|
|
WorldRules newGameRules = lvls.first();
|
|
|
|
lvl = 0;
|
|
|
|
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-16 02:08:35 +03:00
|
|
|
void Controller::buttonPress() {
|
|
|
|
world.reversClick();
|
|
|
|
}
|
|
|
|
|