first work version

This commit is contained in:
Andrei Yankovich 2018-12-01 22:39:03 +03:00
parent 849374d557
commit c124804465
9 changed files with 110 additions and 28 deletions

View File

@ -15,10 +15,12 @@ bool Controller::nextLvl() {
if (lvl + 1 >= lvls.size()) {
return true;
}
m_generalLong += static_cast<int>(world.getCurrentLong());
generateDiff(world.init(lvls.value(++lvl)));
startTimer();
return false;
}
@ -62,6 +64,8 @@ void Controller::update() {
}
long_changed(static_cast<int>(world.getCurrentLong()));
generalLongchanged(generalLong());
}
void Controller::newGame() {
@ -70,6 +74,7 @@ void Controller::newGame() {
WorldRules newGameRules = lvls.first();
lvl = 0;
m_generalLong = 0;
generateDiff(world.init(newGameRules));
startTimer();
}
@ -90,11 +95,18 @@ int Controller::long_() const {
return static_cast<int>(world.getCurrentLong());
}
int Controller::generalLong() const {
return m_generalLong + long_();
}
void Controller::buttonPress() {
world.reversClick();
}
void Controller::setPause(bool p){
pause = p;
if (!pause) {
world.unPause();
}
}

View File

@ -13,6 +13,7 @@ class Controller : public QObject
Q_OBJECT
Q_PROPERTY(int long_ READ long_ NOTIFY long_changed)
Q_PROPERTY(int generalLong READ generalLong NOTIFY generalLongchanged)
private:
World world;
@ -22,6 +23,9 @@ private:
QMap<int, GuiObject *> objectsContainer;
void generateDiff(const QMap<int, GuiObject *> &);
int m_generalLong = 0;
public:
Controller();
void startTimer();
@ -29,6 +33,8 @@ public:
int long_() const;
int generalLong() const;
public slots:
void buttonPress();
@ -69,6 +75,7 @@ signals:
*/
void gameObjectsChanged(const Diff &dif);
void long_changed(int m_long);
void generalLongchanged(int generalLong);
};
#endif // CONTROLLER_H

View File

@ -13,19 +13,19 @@ void Head::render() {
if (*speed < 1) {
setColor(generalSpeadColor);
setRadius(static_cast<int>(m_w * 0.0));
setRadius(static_cast<int>(m_w * 0.1));
} else if (*speed < normSpead) {
setColor(normSpeadColor);
setRadius(static_cast<int>(m_w * 0.15));
setRadius(static_cast<int>(m_w * 0.2));
} else if (*speed < fastSpead) {
setColor(fastSpeadColor);
setRadius(static_cast<int>(m_w * 0.35));
setRadius(static_cast<int>(m_w * 0.3));
} else if (*speed < megaFastSpead) {
setColor(megaFastSpeadColor);
setRadius(static_cast<int>(m_w * 0.5));
setRadius(static_cast<int>(m_w * 0.4));
}
@ -35,6 +35,10 @@ void Head::render() {
void Head::reset() {
}
void Head::unPause() {
time = QDateTime::currentMSecsSinceEpoch();
}
Head::Head(double x, double y, double h, double w, double *spead):
GuiObject ("SnakeItem") {
setX(x);

View File

@ -25,6 +25,7 @@ public:
void setAngle(double angle) override;
void render() override;
void reset() override;
void unPause();
~Head() override;
};

View File

@ -15,6 +15,14 @@ const QVector<Head *> &Snake::getItems() const {
void Snake::render() {
auto centerX = [](const Head* head) {
return head->x()/* + (head->w() / 2)*/;
};
auto centerY = [](const Head* head) {
return head->y() /*+ (head->h() / 2)*/;
};
for (int i = items.length() - 1; i >= 0; --i) {
if (dead) {
@ -31,9 +39,10 @@ void Snake::render() {
}
isClick = false;
}
}else {
double _atan2 = atan2(items[i - 1]->rect().center().y() - items[i]->rect().center().y(),
items[i - 1]->rect().center().x() - items[i]->rect().center().x()) * 180;
} else {
double _atan2 = atan2(centerY(items[i - 1]) - centerY(items[i]),
centerX(items[i - 1]) - centerX(items[i])) * 180;
items[i]->setAngle(_atan2);
}
@ -59,6 +68,12 @@ void Snake::setRataticonDistance(double value) {
rataticonDistance = value;
}
void Snake::unPause() {
for ( int i = 0; i < items.size(); ++i ) {
items[i]->unPause();
}
}
double Snake::sizeByLvl(double lvl , int count) const {
double maxSize = 9;
double minSize = 5;
@ -66,34 +81,32 @@ double Snake::sizeByLvl(double lvl , int count) const {
double pos = (1 - (lvl / count));
QList<QPair<double, double>> snakeGradientSize {
{1, 4},
{0.9, 7},
{0.7, 5},
{0.5, 6},
{1, 5},
{0.99, 7},
{0.9, 5},
{0.8, 6},
{0.0, 3}
};
double localPos = 0;
double local = 0;
int index = 0;
while (index + 1 < snakeGradientSize.size()
&& pos <= snakeGradientSize[index].first) {
maxSize = std::max(snakeGradientSize[index].second,
snakeGradientSize[index + 1].second);
maxSize = snakeGradientSize[index].second;
minSize = snakeGradientSize[index + 1].second;
minSize = std::min(snakeGradientSize[index].second,
snakeGradientSize[index + 1].second);
auto range = snakeGradientSize[index].first -
snakeGradientSize[index + 1].first;
auto range = snakeGradientSize[index].first - snakeGradientSize[index + 1].first;
localPos = ( range - (snakeGradientSize[index].first - pos)) /
local = ( range - (snakeGradientSize[index].first - pos)) /
range;
index++;
}
return localPos * (maxSize - minSize) + minSize;
return local * (maxSize - minSize) + minSize;
}
void Snake::changeCountObjects(int count) {

View File

@ -46,6 +46,7 @@ public:
void setRataticonDistance(double value);
int getDeadTimer() const;
void setDeadTimer(int value);
void unPause();
};
#endif // SNAKE_H

View File

@ -27,6 +27,11 @@ QMultiMap<QString, ItemWorld *> World::getItems() const
return items;
}
void World::unPause() {
time = QDateTime::currentMSecsSinceEpoch();
snake.unPause();
}
void World::clearItems() {
for (auto i : items) {
delete i;

View File

@ -42,6 +42,7 @@ public:
void reversClick();
double getCurrentLong() const;
QMultiMap<QString, ItemWorld *> getItems() const;
void unPause();
};
#endif // WORLD_H

View File

@ -26,6 +26,8 @@ Item {
property var model: (contr)? contr: null;
property var arrayObjects: []
property bool showMenu: false
property bool isPause: false
function add (cppObjId) {
if (!model) {
console.log("create object fail")
@ -44,7 +46,7 @@ Item {
if (temp.status === Component.Ready) {
var obj = temp.createObject(parent) // parent - это обьект на который будет помещен соззданный элемент
obj.model = model.getGameObject(cppObjId);
obj.z = -1;
obj.z = -2;
arrayObjects.push(obj)
} else {
console.log("wrong viewTemplate in model");
@ -186,20 +188,56 @@ Item {
anchors.topMargin: point
z: 1
onClicked: {
showMenu = true;
}
visible: !showMenu
}
Button {
id: pause
text: "||"
text: (isPause)? "▶" :"||"
anchors.left: returnToMenu.right
anchors.leftMargin: point
anchors.top: parent.top
anchors.topMargin: point
z: 1
z: returnToMenu.z
onClicked: {
isPause = !isPause;
if (model) model.setPause(isPause);
}
visible: !showMenu
}
Button {
id: long_
Label {
anchors.fill: parent;
textFormat: Text.AutoText
verticalAlignment: Text.AlignVCenter
horizontalAlignment: Text.AlignHCenter
wrapMode: Text.WordWrap
text: qsTr("lvl long: ") + ((model)? model.long_: "0")
}
width: 35 * point;
height: pause.height;
anchors.left: pause.right
anchors.leftMargin: point
anchors.top: parent.top
anchors.topMargin: point
z: returnToMenu.z
visible: !showMenu
@ -214,18 +252,18 @@ Item {
horizontalAlignment: Text.AlignHCenter
wrapMode: Text.WordWrap
text: qsTr("long: ") + (model)? model.long_: "0"
text: qsTr("general long: ") + ((model)? model.generalLong: "0")
}
width: 15 * point;
height: pause.height;
width: 35 * point;
height: long_.height;
anchors.left: pause.right
anchors.left: long_.right
anchors.leftMargin: point
anchors.top: parent.top
anchors.topMargin: point
z: 1
z: returnToMenu.z
visible: !showMenu