fix show dynamic created objects

This commit is contained in:
Andrei Yankovich 2021-06-02 18:29:07 +03:00
parent e7253c4edc
commit d62b997bfd
19 changed files with 205 additions and 326 deletions

View File

@ -12,8 +12,6 @@
<file>SnakeProjectModule/Scene.qml</file>
<file>SnakeProjectModule/SettingsView.qml</file>
<file>SnakeProjectModule/SnakeItem.qml</file>
<file>SnakeProjectModule/3dPrimitive.qml</file>
<file>SnakeProjectModule/AbstractItem.qml</file>
</qresource>
<qresource prefix="/SnakeTr">
<file>languages/en.qm</file>

View File

@ -2,19 +2,18 @@
BackGround::BackGround(double x, double y): ItemWorld (x, y) {
this->setSize(200, 400);
this->setTexture("qrc:/texture/asphalt");
setBeckGroundObject(true);
}
void BackGround::render() {
auto wPart = w() / 2;
auto wPart = size().x() / 2;
if (_x + wPart < 200) {
if (position().x() + wPart < 200) {
setX(wPart);
}
}
void BackGround::reset() {
setX(0 - w());
setX(0 - size().x());
render();
}

View File

@ -14,6 +14,4 @@ void BackGroundItem::reset() {
auto radius = rand() % 200;
setSize(radius , radius);
setRadius(radius / 2);
}

View File

@ -3,9 +3,10 @@
#include <QColor>
Box::Box(double x, double y):
ItemWorld (x, y, "3dPrimitive") {
ItemWorld (x, y) {
this->setSize(10, 10);
setMash("#Cube");
setColor(QColor(100, 100, 100).name());
}

View File

@ -91,8 +91,8 @@ void Controller::update() {
}
long_changed(static_cast<int>(world.getCurrentLong()));
generalLongchanged(generalLong());
emit long_changed(static_cast<int>(world.getCurrentLong()));
emit generalLongchanged(generalLong());
}

View File

@ -1,5 +1,7 @@
#include "guiobject.h"
#include <QVector3D>
GuiObject::GuiObject(const QString &viewTempalte, QObject *ptr):
QObject (ptr) {
_viewTemplate = viewTempalte;
@ -7,9 +9,6 @@ GuiObject::GuiObject(const QString &viewTempalte, QObject *ptr):
}
void GuiObject::render() {
}
QString GuiObject::color() const {
return _color;
}
@ -49,103 +48,46 @@ void GuiObject::setGuiId(int guiId) {
emit guiIdChanged(guiId);
}
float GuiObject::x() const {
return _x;
}
void GuiObject::setX(float newX) {
if (qFuzzyCompare(_x, newX))
if (qFuzzyCompare(_position.x(), newX))
return;
_x = newX;
emit xChanged();
}
float GuiObject::y() const {
return _y;
_position.setX(newX);
emit positionChanged();
}
void GuiObject::setY(float newY) {
if (qFuzzyCompare(_y, newY))
if (qFuzzyCompare(_position.y(), newY))
return;
_y = newY;
emit yChanged();
}
float GuiObject::z() const {
return _z;
_position.setY(newY);
emit positionChanged();
}
void GuiObject::setZ(float newZ) {
if (qFuzzyCompare(_z, newZ))
if (qFuzzyCompare(_position.z(), newZ))
return;
_z = newZ;
emit zChanged();
}
float GuiObject::dx() const {
return _dx;
_position.setZ(newZ);
emit positionChanged();
}
void GuiObject::setDx(float newDx) {
if (qFuzzyCompare(_dx, newDx))
if (qFuzzyCompare(size().x(), newDx))
return;
_dx = newDx;
emit dxChanged();
}
float GuiObject::dy() const {
return _dy;
_size.setX(newDx);
emit sizeChanged();
}
void GuiObject::setDy(float newDy) {
if (qFuzzyCompare(_dy, newDy))
if (qFuzzyCompare(size().y(), newDy))
return;
_dy = newDy;
emit dyChanged();
}
float GuiObject::dz() const {
return _dz;
_size.setY(newDy);
emit sizeChanged();
}
void GuiObject::setDz(float newDz) {
if (qFuzzyCompare(_dz, newDz))
if (qFuzzyCompare(size().z(), newDz))
return;
_dz = newDz;
emit dzChanged();
}
float GuiObject::rx() const {
return _rx;
}
void GuiObject::setRx(float newRx) {
if (qFuzzyCompare(_rx, newRx))
return;
_rx = newRx;
emit rxChanged();
}
float GuiObject::ry() const {
return _ry;
}
void GuiObject::setRy(float newRy) {
if (qFuzzyCompare(_ry, newRy))
return;
_ry = newRy;
emit ryChanged();
}
float GuiObject::rz() const {
return _rz;
}
void GuiObject::setRz(float newRz) {
if (qFuzzyCompare(_rz, newRz))
return;
_rz = newRz;
emit rzChanged();
_size.setZ(newDz);
emit sizeChanged();
}
const QString &GuiObject::baseColorMap() const {
@ -163,3 +105,51 @@ const QString &GuiObject::normalMap() const {
const QString &GuiObject::emissiveMap() const {
return _emissiveMap;
}
QVector3D GuiObject::center() const {
return _position + (_size / 2);
}
const QVector3D &GuiObject::position() const {
return _position;
}
void GuiObject::setposition(const QVector3D &newposition) {
if (_position == newposition)
return;
_position = newposition;
emit positionChanged();
}
const QVector3D &GuiObject::size() const {
return _size;
}
void GuiObject::setSize(const QVector3D &newSize) {
if (_size == newSize)
return;
_size = newSize;
emit sizeChanged();
}
const QQuaternion &GuiObject::ratation() const {
return _ratation;
}
void GuiObject::setRatation(const QQuaternion &newRatation) {
if (_ratation == newRatation)
return;
_ratation = newRatation;
emit ratationChanged();
}
const QString &GuiObject::mash() const {
return _mash;
}
void GuiObject::setMash(const QString &newMash) {
if (_mash == newMash)
return;
_mash = newMash;
emit mashChanged();
}

View File

@ -4,29 +4,22 @@
#include "baseclass.h"
#include "QObject"
#include <QQuaternion>
#include <QRectF>
#include <QVector3D>
class GuiObject:public QObject, public BaseClass
{
Q_OBJECT
// @todo: add color
Q_PROPERTY(QString color READ color WRITE setColor NOTIFY colorChanged)
Q_PROPERTY(int guiId READ guiId NOTIFY guiIdChanged)
Q_PROPERTY(QString viewTemplate READ viewTemplate NOTIFY viewTemplateChanged)
Q_PROPERTY(float x READ x WRITE setX NOTIFY xChanged)
Q_PROPERTY(float y READ y WRITE setY NOTIFY yChanged)
Q_PROPERTY(float z READ z WRITE setZ NOTIFY zChanged)
Q_PROPERTY(QVector3D position READ position WRITE setposition NOTIFY positionChanged)
Q_PROPERTY(QVector3D size READ size WRITE setSize NOTIFY sizeChanged)
Q_PROPERTY(QQuaternion ratation READ ratation WRITE setRatation NOTIFY ratationChanged)
// size of object for each axis
Q_PROPERTY(float dx READ dx WRITE setDx NOTIFY dxChanged)
Q_PROPERTY(float dy READ dy WRITE setDy NOTIFY dyChanged)
Q_PROPERTY(float dz READ dz WRITE setDz NOTIFY dzChanged)
// rotation foeach axis
Q_PROPERTY(float rx READ rx WRITE setRx NOTIFY rxChanged)
Q_PROPERTY(float ry READ ry WRITE setRy NOTIFY ryChanged)
Q_PROPERTY(float rz READ rz WRITE setRz NOTIFY rzChanged)
Q_PROPERTY(QString mash READ mash WRITE setMash NOTIFY mashChanged)
// textures
Q_PROPERTY(QString baseColorMap READ baseColorMap NOTIFY baseColorMapChanged)
@ -38,8 +31,6 @@ class GuiObject:public QObject, public BaseClass
public:
GuiObject(const QString& viewTempalte = "GraphicItem", QObject *ptr = nullptr);
void render() override;
QString color() const;
void setColor(QString color);
@ -49,90 +40,86 @@ public:
int guiId() const;
void setGuiId(int guiId);
float x() const;
void setX(float newX);
float y() const;
void setY(float newY);
float z() const;
void setZ(float newZ);
float dx() const;
void setDx(float newDx);
float dy() const;
void setDy(float newDy);
float dz() const;
void setDz(float newDz);
float rx() const;
void setRx(float newRx);
float ry() const;
void setRy(float newRy);
float rz() const;
void setRz(float newRz);
const QString &baseColorMap() const;
const QString &roughnessMap() const;
const QString &normalMap() const;
const QString &emissiveMap() const;
public slots:
/**
* @brief center This method return center of object
* @return 3d point of the object center.
* @warning This method calc center in runtime.
*/
QVector3D center() const;
void setRadius(int radius);
/**
* @brief intersects This method check if this object contains @a point object.
* @param point This is checked point
* @return true if the point contains in the object cube.
*/
bool intersects(const QVector3D& point) const;
// /**
// * @brief intersects This method check intersects betwin current object and @a object.
// * @param object This is input object.
// * @return true if the two objects has common points.
// */
// bool intersects(const GuiObject& object) const;
const QVector3D &position() const;
void setposition(const QVector3D &newposition);
const QVector3D &size() const;
void setSize(const QVector3D &newSize);
const QQuaternion &ratation() const;
void setRatation(const QQuaternion &newRatation);
const QString &mash() const;
void setMash(const QString &newMash);
signals:
void guiIdChanged(int guiId);
void colorChanged(QString color);
void viewTemplateChanged(QString viewTemplate);
void xChanged();
void yChanged();
void zChanged();
void dxChanged();
void dyChanged();
void dzChanged();
void rxChanged();
void ryChanged();
void rzChanged();
void baseColorMapChanged();
void roughnessMapChanged();
void normalMapChanged();
void emissiveMapChanged();
void positionChanged();
void sizeChanged();
void ratationChanged();
void mashChanged();
protected:
int _guiId = -1;
QString _color = "";
float _x;
float _y;
float _z;
float _dx;
float _dy;
float _dz;
float _rx;
float _ry;
float _rz;
void setTexture(const QString &texture);
private:
void generateId();
QString _baseColorMap;
QString _roughnessMap;
QString _normalMap;
QString _emissiveMap;
QString _viewTemplate;
QVector3D _position;
QVector3D _size;
QQuaternion _ratation;
QString _mash;
};
#endif // GUIOBJECT_H

View File

@ -8,28 +8,22 @@ void Head::render() {
qint64 tempTime = QDateTime::currentMSecsSinceEpoch() - time;
time = QDateTime::currentMSecsSinceEpoch();
double my = (_y + (*speed * 0.55) * sin(m_angle * TO_RADIAN));
_y += (my - _y) / 1000 * tempTime;
double my = (position().y() + (*speed * 0.55) * sin(ratation().scalar() * TO_RADIAN));
setY((my - position().y()) / 1000 * tempTime);
if (*speed < 1) {
setColor(generalSpeadColor);
setRadius(static_cast<int>(m_w * 0.4));
} else if (*speed < normSpead) {
setColor(normSpeadColor);
setRadius(static_cast<int>(m_w * 0.5));
} else if (*speed < fastSpead) {
setColor(fastSpeadColor);
setRadius(static_cast<int>(m_w * 0.5));
} else if (*speed < megaFastSpead) {
setColor(megaFastSpeadColor);
setRadius(static_cast<int>(m_w * 0.4));
}
emit yChanged();
}
void Head::reset() {
@ -39,18 +33,13 @@ void Head::unPause() {
time = QDateTime::currentMSecsSinceEpoch();
}
Head::Head(double x, double y, double h, double w, double *spead):
Head::Head(float x, float y, float h, float w, float thickness, float *spead):
GuiObject ("SnakeItem") {
setX(x);
setY(y);
setW(w);
setH(h);
this->speed = spead;
}
setposition({x, y, 0});
setSize({w, h, thickness});
void Head::setAngle(double angle) {
m_angle = angle;
emit angleChanged(m_angle);
this->speed = spead;
setMash("#Cube");
}
Head::~Head() {

View File

@ -10,7 +10,7 @@ class Head : public GuiObject
{
private:
qint64 time;
double *speed;
float *speed;
const int megaFastSpead = 200;
const int fastSpead = 100;
const int normSpead = 50;
@ -21,8 +21,7 @@ private:
const QString megaFastSpeadColor = "#ec7063";
public:
Head(double x , double y, double h, double w, double *speed);
void setAngle(double angle);
Head(float x , float y, float h, float w, float thickness, float *speed);
void render() override;
void reset() override;
void unPause();

View File

@ -7,7 +7,7 @@
#define POINT 100
ItemWorld::ItemWorld(double x, double y, const QString& guiTemplate):
GuiObject3D (guiTemplate) {
GuiObject (guiTemplate) {
setLoc(x, y);
}
@ -16,8 +16,8 @@ void ItemWorld::setBeckGroundObject(bool value) {
}
void ItemWorld::setSize(double h, double w) {
setH(h);
setW(w);
setDx(h);
setDy(w);
}
void ItemWorld::setLoc(double x, double y) {
@ -26,19 +26,16 @@ void ItemWorld::setLoc(double x, double y) {
}
void ItemWorld::render() {
if (_x + w() < 0) {
_x = (rand() % 400) + 200;
_y = rand() % 100;
emit xChanged();
emit yChanged();
if (position().y() + size().y() < 0) {
setX((rand() % 400) + 200);
setY(rand() % 100);
}
}
bool ItemWorld::move(const GuiObject *snakeRiger, double dx) {
_x -= dx;
emit xChanged();
setX(position().x() - dx);
return snakeRiger->rect().intersects(rect()) && !beckGroundObject;
return false;// snakeRiger->rect().intersects(rect()) && !beckGroundObject;
}
bool ItemWorld::isBeckGroundObject() {

View File

@ -16,11 +16,11 @@ const QVector<Head *> &Snake::getItems() const {
void Snake::render() {
auto centerX = [](const Head* head) {
return head->x()/* + (head->w() / 2)*/;
return head->position().x() + (head->size().x() / 2);
};
auto centerY = [](const Head* head) {
return head->y() /*+ (head->h() / 2)*/;
return head->position().y() + (head->size().y() / 2);
};
for (int i = items.length() - 1; i >= 0; --i) {
@ -33,33 +33,26 @@ void Snake::render() {
if (i == 0) {
if (isClick) {
if (countClick & 1){
items[i]->setAngle(45);
items[i]->setRatation(QQuaternion::fromEulerAngles(0,0, 45));
} else {
items[i]->setAngle(315);
items[i]->setRatation(QQuaternion::fromEulerAngles(0,0, 315));
}
isClick = false;
}
} else {
// fix me
// old implementation use 2d eulor coordina system. so for this implementation is depricated.
double _atan2 = atan2(centerY(items[i - 1]) - centerY(items[i]),
centerX(items[i - 1]) - centerX(items[i])) * 90;
items[i]->setAngle(_atan2);
items[i]->setRatation(QQuaternion::fromEulerAngles(0,0, _atan2));
}
items[i]->render();
}
}
double Snake::checDistance(int i) {
auto result = (items[i]->rect().y() -
items[i - 1]->rect().y()) / rataticonDistance;
return result;
}
double Snake::getRataticonDistance() const {
return rataticonDistance;
}
@ -75,17 +68,17 @@ void Snake::unPause() {
}
double Snake::sizeByLvl(double lvl , int count) const {
double maxSize = 9;
double minSize = 5;
double maxSize = 550;
double minSize = 350;
double pos = (1 - (lvl / count));
QList<QPair<double, double>> snakeGradientSize {
{1, 4},
{0.99, 7},
{0.8, 5},
{0.6, 6},
{0.0, 3}
{1, 40},
{0.99, 70},
{0.8, 50},
{0.6, 60},
{0.0, 30}
};
double local = 0;
@ -116,10 +109,10 @@ void Snake::changeCountObjects(int count) {
auto size = sizeByLvl(i, count);
auto obj = new Head(margin * (count - i),
50, size , size,
50, size , size, size,
this->speed);
obj->setY(50 + obj->h() / 2);
obj->setY(50 + obj->size().y() / 2);
items.push_back(obj);
}
@ -133,7 +126,7 @@ void Snake::changeCountObjects(int count) {
}
}
QMap<int, GuiObject*> Snake::init(int size, double *speed) {
QMap<int, GuiObject*> Snake::init(int size, float *speed) {
QMap<int, GuiObject*> res;
@ -170,8 +163,7 @@ void Snake::resetPosotion() {
for ( int i = 0; i < items.size(); ++i ) {
items[i]->setX(margin * (items.size() - i));
items[i]->setY(50 + items[i]->h() / 2);
items[i]->setAngle(0);
items[i]->setY(50 + items[i]->size().y() / 2);
}
}

View File

@ -16,14 +16,13 @@ class Snake : public BaseClass
private:
double rataticonDistance = 1;
QVector<Head*> items;
double *speed = nullptr;
float *speed = nullptr;
bool isClick = false;
int countClick = 0;
bool dead = false;
double sizeByLvl(double lvl, int count) const;
void changeCountObjects(int count);
double checDistance(int i);
void clearItems();
@ -38,7 +37,7 @@ public:
void reverse();
void render() override;
QMap<int, GuiObject *> init(int size, double *speed);
QMap<int, GuiObject *> init(int size, float *speed);
bool isInited() const;
const QVector<Head*>& getItems() const;
double getMovedLong() const;

View File

@ -33,7 +33,7 @@ void World::unPause() {
}
void World::clearItems() {
for (auto i : items) {
for (auto i : qAsConst(items)) {
delete i;
}
oldRules.clear();
@ -54,7 +54,7 @@ void World::changeCountObjects(const QString &name, int count) {
break;
}
items.insertMulti(name, obj);
items.insert(name, obj);
}
} else {
@ -94,7 +94,7 @@ QMap<int, GuiObject *> World::init(WorldRules rules) {
res.insert(i.key(), i.value());
}
for (auto i : items) {
for (auto i : qAsConst(items)) {
res[i->guiId()] = i;
}
@ -128,17 +128,17 @@ void World::render() {
(*i)->render();
}
defiat |= (rig->y()< 0 || rig->y() > 100);
defiat |= (rig->position().y()< 0 || rig->position().y() > 100);
if (!snake.isDead() && defiat) {
snake.kill();
}
// if (!snake.isDead() && defiat) {
// snake.kill();
// }
currentLong += dx;
}
void World::resetPosition() {
for (auto i : items) {
for (auto i : qAsConst(items)) {
i->reset();
}
spead = 0;

View File

@ -17,7 +17,7 @@ private:
QMultiMap<QString, ItemWorld*> items;
double currentLong = 0;
int endLong;
double spead = 0, d_spead = 0;
float spead = 0, d_spead = 0;
QString background;
qint64 time;
bool defiat = false;

View File

@ -1,25 +0,0 @@
import QtQuick
import QtQuick3D
Node {
id: rootNode
property var model: null
Model {
id: suzanne
rotation: Qt.quaternion(0.707107, -0.707107, 0, 0)
scale.x: 100
scale.y: 100
scale.z: 100
source: "meshes/suzanne.mesh"
DefaultMaterial {
id: material_001_material
diffuseColor: "#ffcccccc"
}
materials: [
material_001_material
]
}
}

View File

@ -1,6 +0,0 @@
import QtQuick
Item {
property var model: null
property int guiId: (model) ? model.guiId : -1;
}

View File

@ -1,45 +1,24 @@
import QtQuick
import QtQuick3D
AbstractItem {
Model {
id: graphicItem
property real angle: (model) ? model.angle : 0;
property string texture: (model) ? model.texture : "";
property double devX: width / 2
property double devY: height / 2
property alias color: privateRoot.color
property alias radius: privateRoot.radius
Rectangle {
id: privateRoot
Image {
id: name
visible: texture.length
source: texture
anchors.fill: parent;
}
color: (model) ? model.color : "#11ff32";
radius: (model) ? model.radius * metrix.gamePt : 0;
anchors.fill: parent
property var model: null
property int guiId: (model) ? model.guiId : -1;
DefaultMaterial {
id: material_001_material
diffuseColor: "#ffcccccc"
}
width: (model) ? model.w * metrix.gamePt: 0;
height: (model) ? model.h * metrix.gamePt: 0;
materials: [
material_001_material
]
x: (model) ? model.x * metrix.gamePt - devX: 0;
y: (model) ? model.y * metrix.gamePt - devY: 0;
z:-1
transform: Rotation {
origin.x: devX;
origin.y: devY;
angle: graphicItem.angle;
}
rotation: (model)? model.ratation: Qt.quaternion(0, 0, 0, 0)
scale: (model)? model.size: Qt.vector3d(0, 0, 0);
source: (model)? model.mash: "#Cube";
position: (model) ? model.position: Qt.vector3d(0,0,0);
}

View File

@ -6,23 +6,6 @@ import QtQuick.Layouts
View3D {
id: scene;
z: -2
Rectangle {
id: background;
color: "#ffffff"
anchors.fill: parent;
Behavior on color {
ColorAnimation {
duration: 5000
}
}
z: -3
}
property var model: null;
property var arrayObjects: []
@ -45,9 +28,8 @@ View3D {
var temp = Qt.createComponent( viewTemplate + ".qml")
if (temp.status === Component.Ready) {
var obj = temp.createObject(parent) // parent - это обьект на который будет помещен соззданный элемент
var obj = temp.createObject(mainScane) // parent - это обьект на который будет помещен соззданный элемент
obj.model = model.getGameObject(cppObjId);
obj.z = -2;
arrayObjects.push(obj)
} else {
console.log("wrong viewTemplate in model. Message: " + temp.errorString());
@ -69,26 +51,37 @@ View3D {
function updateBackgroundColor(lvl) {
switch(lvl % 7) {
case 0: background.color = "#d6eaf8"; break;
case 1: background.color = "#d0ece7"; break;
case 2: background.color = "#d4efdf"; break;
case 3: background.color = "#fcf3cf"; break;
case 4: background.color = "#f6ddcc"; break;
case 5: background.color = "#f2d7d5"; break;
case 6: background.color = "#ebdef0"; break;
case 7: background.color = "#fbfcfc"; break;
case 0: background.clearColor = "#d6eaf8"; break;
case 1: background.clearColor = "#d0ece7"; break;
case 2: background.clearColor = "#d4efdf"; break;
case 3: background.clearColor = "#fcf3cf"; break;
case 4: background.clearColor = "#f6ddcc"; break;
case 5: background.clearColor = "#f2d7d5"; break;
case 6: background.clearColor = "#ebdef0"; break;
case 7: background.clearColor = "#fbfcfc"; break;
}
}
PerspectiveCamera {
id: camera
position: Qt.vector3d(0, 200, 300)
eulerRotation.x: -30
position: Qt.vector3d(0, 0, 600)
// eulerRotation.y: -90
}
DirectionalLight {
eulerRotation.x: -30
eulerRotation.y: -90
}
environment: SceneEnvironment {
id: background
clearColor: window.color
backgroundMode: SceneEnvironment.SkyBox
probeOrientation: Qt.vector3d(0, -90, 0)
}
Node {
id: mainScane
}
Timer {

View File

@ -1,16 +1,5 @@
import QtQuick
GraphicItem {
Behavior on color {
ColorAnimation {
duration: 2000
}
}
Behavior on radius {
NumberAnimation {
duration: 2000
}
}
}