4
1
mirror of https://github.com/QuasarApp/Snake.git synced 2025-05-08 07:29:45 +00:00

added funcrionaliti of diff objects on back-end

This commit is contained in:
Andrei Yankovich 2018-11-04 16:11:55 +03:00
parent 15048a3276
commit a7d96213a9
17 changed files with 207 additions and 25 deletions

@ -1,6 +1,8 @@
#include "controller.h"
#include <cmath>
#include <ctime>
#include "diff.h"
#include "lvls.h"
Controller::Controller() {
srand(static_cast<unsigned int>(time(nullptr)));
@ -14,12 +16,57 @@ void Controller::setDeviceSize(QPoint deviceSize) {
Global::deviceSize = deviceSize;
}
bool Controller::nextLvl() {
if (lvl + 1 >= lvls.size()) {
return true;
}
generateDiff(world.init(lvls.value(++lvl)));
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);
}
}
void Controller::update() {
world.render();
}
void Controller::newGame() {
WorldRules newGameRules = lvls.first();
lvl = 0;
generateDiff(world.init(newGameRules));
}
QObject *Controller::getGameObject(int id) {
return objectsContainer.value(id, nullptr);
}
void Controller::startTimer() {
timer->start();
timer->start();
}
void Controller::stopTimer() {

@ -4,6 +4,7 @@
#include <QObject>
#include <QTimer>
#include <QTime>
#include "diff.h"
#include "snake.h"
#include "world.h"
@ -11,10 +12,19 @@ class Controller : public QObject
{
Q_OBJECT
Q_PROPERTY(QPoint deviceSize WRITE setDeviceSize)
private:
World world;
QTimer *timer;
int lvl = 0;
QMap<int, GuiObject *> objectsContainer;
/**
* @brief nextLvl - switch to next lvl from array lvels
* @return true if all levels are passed
*/
bool nextLvl();
void generateDiff(const QMap<int, GuiObject *> &);
public:
Controller();
@ -24,6 +34,33 @@ public:
public slots:
void setDeviceSize(QPoint deviceSize);
void update();
/**
* @brief newGame - start game from first lvl
*/
void newGame();
/**
* @brief getGameObject
* @param id - id of guiObject;
* @return guiObject if (id is not valid return nullptr)
*/
QObject* getGameObject(int id);
signals:
/**
* @brief finished - imited when game over or victory
* @param victory - flag of vicrory, if it equals false then game over
* @param lvl - game over lvl
* @param distance - game over distance
*/
void finished(bool victory, int lvl, int distance);
/**
* @brief gameObjectsChanged
* @param dif
*/
void gameObjectsChanged(const Diff &dif);
};
#endif // CONTROLLER_H

21
back-end/diff.cpp Normal file

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

23
back-end/diff.h Normal file

@ -0,0 +1,23 @@
#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

@ -1,9 +1,9 @@
#include "guiobject.h"
GuiObject::GuiObject(QObject *ptr) :
GuiObject::GuiObject(QObject *ptr):
QObject (ptr) {
generateId();
}
double GuiObject::angle() const {
@ -27,6 +27,7 @@ QRectF& GuiObject::getRect() {
}
void GuiObject::setAngle(double angle) {
m_angle = angle;
emit angleChanged(m_angle);
}
@ -35,6 +36,14 @@ int GuiObject::guiId() const {
return m_guiId;
}
void GuiObject::setRect(QRectF rect) {
if (m_rect == rect)
return;
m_rect = rect;
emit rectChanged(m_rect);
}
void GuiObject::generateId() {
static int id = 0;
m_guiId = id++;
@ -44,3 +53,5 @@ void GuiObject::setTexture(const QString &texture) {
m_texture = texture;
emit textureChanged(m_texture);
}

@ -12,7 +12,7 @@ class GuiObject:public QObject, public BaseClass
Q_PROPERTY(double angle READ angle NOTIFY angleChanged)
Q_PROPERTY(QString texture READ texture NOTIFY textureChanged)
Q_PROPERTY(QRectF rect READ rect NOTIFY rectChanged)
Q_PROPERTY(QRectF rect READ rect WRITE setRect NOTIFY rectChanged)
Q_PROPERTY(int guiId READ guiId NOTIFY guiIdChanged)
private:
@ -25,6 +25,9 @@ protected:
QRectF m_rect;
void setTexture(const QString &texture);
void setRect(QRectF rect);
public:
GuiObject(QObject *ptr = nullptr);

@ -1,6 +1,7 @@
#include "guiobjectfactory.h"
#include "box.h"
#include "utils.h"
#include "head.h"
GuiObjectFactory::GuiObjectFactory() {}

@ -10,7 +10,9 @@ void Head::render(){
time = QDateTime::currentMSecsSinceEpoch();
}
Head::Head(double *spead) {
Head::Head(const QRectF &rect, double *spead):
GuiObject () {
setRect(rect);
this->speed = spead;
}

@ -12,7 +12,7 @@ private:
qint64 time;
double *speed;
public:
Head(double *speed);
Head(const QRectF &rect , double *speed);
void render();

@ -13,9 +13,10 @@ void ItemWorld::setBeckGroundObject(bool value) {
}
void ItemWorld::setSize(double x, double y) {
m_rect.setX(x);
m_rect.setY(y);
emit rectChanged(m_rect);
QRectF rect;
rect.setX(x);
rect.setY(y);
setRect(rect);
}
void ItemWorld::render() {

@ -1,6 +1,7 @@
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include "controller.h"
#include "diff.h"
int main(int argc, char *argv[])
{
@ -11,7 +12,8 @@ int main(int argc, char *argv[])
QQmlApplicationEngine engine;
qmlRegisterType <Controller> ("Controller", 1, 0,"Controller");
qmlRegisterType <GuiObject> ("GuiObject", 1, 0, "GuiObject");
qmlRegisterType <GuiObject> ();
qmlRegisterType <Diff> ();
engine.load(QUrl(QStringLiteral("qrc:/front-end/main.qml")));
if (engine.rootObjects().isEmpty())

@ -28,29 +28,48 @@ void Snake::render() {
}
}
void Snake::changeCountObjects(int count) {
if (count > 0) {
for ( int i = 0; i < count; ++i ) {
QRectF rect(0 + 10 * (count - i), 0, 10, 10);
auto obj = new Head(rect, &this->speed);
items.push_back(obj);
}
} else {
for ( int i = count; i < 0; ++i ) {
auto obj = items.first();
items.removeFirst();
delete obj;
}
}
}
QMap<int, GuiObject*> Snake::init(int size, double speed) {
QMap<int, GuiObject*> res;
if (size < 0 || speed <= 0) {
if (size <= 0 || speed <= 0) {
return res;
}
this->speed = speed;
if (!size) {
size ++;
}
changeCountObjects(size -items.size());
for ( int i = size; i >= 0; --i ) {
auto obj = new Head(&this->speed);
items.push_back(obj);
res[obj->guiId()] = obj;
for (auto i : items) {
res[i->guiId()] = i;
}
return res;
}
bool Snake::isInited() const {
return items.size();
}
Snake::~Snake() {
for (auto i : items) {
delete i;

@ -16,6 +16,7 @@ private:
double speed;
bool isClick;
int countClick;
void changeCountObjects(int count);
public:
Snake();
~Snake() override;
@ -23,6 +24,7 @@ public:
const QRectF &getRiger() const;
void render() override;
QMap<int, GuiObject *> init(int size, double speed);
bool isInited() const;
const QVector<Head*>& getItems() const;
double getMovedLong() const;
};

@ -20,6 +20,7 @@ void World::clearItems() {
}
void World::changeCountObjects(const QString &name, int count) {
if (count > 0) {
for ( int i = 0; i < count; ++i ) {
@ -46,7 +47,11 @@ QMap<int, GuiObject *> World::init(const WorldRules &rules) {
QMap<int, GuiObject*> res;
snake.init(10, 10);
auto snakeItems = snake.init(10, 10);
for (auto i = snakeItems.begin(); i != snakeItems.end(); ++i) {
res.insert(i.key(), i.value());
}
currentLong = -1;
for (auto i = rules.begin(); i != rules.end(); ++i) {
@ -58,7 +63,7 @@ QMap<int, GuiObject *> World::init(const WorldRules &rules) {
spead = rules["Spead"];
}
else {
changeCountObjects(i.key(), i.value());
changeCountObjects(i.key(), i.value() - oldRules.value(i.key()));
}
}
@ -66,6 +71,7 @@ QMap<int, GuiObject *> World::init(const WorldRules &rules) {
res[i->guiId()] = i;
}
oldRules = rules;
return res;
}
@ -103,6 +109,10 @@ bool World::isDefiat() const {
return defiat;
}
WorldRules World::currentRules() const {
return oldRules;
}
const QVector<ItemWorld *> &World::getItems() const {
return items;
}

@ -37,6 +37,7 @@ public:
bool isEnd();
const QVector<ItemWorld*>& getItems() const;
bool isDefiat() const;
WorldRules currentRules() const;
};
#endif // WORLD_H

@ -624,7 +624,7 @@
</DElement>
</base-DElement>
<object>{eac9ab00-70e1-4346-a40e-a09dfa347d20}</object>
<name>Objects Diff Interface</name>
<name> Diff </name>
<pos>x:515;y:750</pos>
<rect>x:-100;y:-30;w:200;h:60</rect>
<auto-sized>false</auto-sized>
@ -2499,7 +2499,7 @@
<uid>{eac9ab00-70e1-4346-a40e-a09dfa347d20}</uid>
</MElement>
</base-MElement>
<name>Objects Diff Interface</name>
<name> Diff </name>
<relations>
<handles>
<handles>

@ -24,7 +24,8 @@ SOURCES += \
back-end/guiobject.cpp \
back-end/lvls.cpp \
back-end/guiobjectfactory.cpp \
back-end/utils.cpp
back-end/utils.cpp \
back-end/diff.cpp
RESOURCES += qml.qrc
@ -50,7 +51,8 @@ HEADERS += \
back-end/guiobject.h \
back-end/utils.h \
back-end/lvls.h \
back-end/guiobjectfactory.h
back-end/guiobjectfactory.h \
back-end/diff.h
DISTFILES += \
doc/calassdiagramm.qmodel