cff72a62178b05e535988ab06c003f76b32bc3fb
[ghostsoverboard] / orientationcontrolledgraphicspixmapobject.cpp
1 #include "orientationcontrolledgraphicspixmapobject.h"
2 #include <QDebug>
3 #include <QGraphicsScene>
4
5 //OrientationControlledGraphicsPixmapObject::OrientationControlledGraphicsPixmapObject (QGraphicsItem *parent) :
6 //    QObject(), QGraphicsPixmapItem (parent)
7 //{
8
9 //}
10
11 OrientationControlledGraphicsPixmapObject::OrientationControlledGraphicsPixmapObject(QPixmap pixmap, QGraphicsItem *parent) :
12     QObject(), QGraphicsPixmapItem (pixmap, parent)
13 {
14
15     connect(&rotationSensor_,SIGNAL(readingChanged()),this,SLOT(readRotationSensor()));
16
17
18
19 }
20
21 void OrientationControlledGraphicsPixmapObject::startMoving()
22 {
23     rotationSensor_.start();
24     qDebug() << "started the sensor";
25     qDebug() << rotationSensor_.isActive();
26 }
27
28
29 void OrientationControlledGraphicsPixmapObject::stopMoving()
30 {
31     rotationSensor_.stop();
32     qDebug () << "trying to stop the sensor";
33 }
34
35 void OrientationControlledGraphicsPixmapObject::readRotationSensor()
36 {
37     if (!scene()) //no movement if this item does not belong to a scene
38         return;
39
40     QRect sceneRectangle = scene()->sceneRect().toRect();
41
42
43     QRotationReading* pSensorData = rotationSensor_.reading();
44
45     int deltay = pSensorData->x(); //yes, you need the "x" value from the sensor for "y" direction in the scene...
46     int deltax = pSensorData->y(); //...and vice versa
47
48
49  //   qDebug() << deltax << " " << deltay;
50
51     int oldx = x();
52     int oldy = y();
53
54     int newx = x() + deltax/15;
55     int newy = y() + deltay/15;
56
57
58 //    qDebug() << sceneRectangle.left() << sceneRectangle.right();
59
60
61     setX(qBound(sceneRectangle.left(),newx,sceneRectangle.right()-pixmap().width()));
62     setY(qBound(sceneRectangle.top(),newy,sceneRectangle.bottom()-pixmap().height()));
63
64     QList<QGraphicsItem*>  collidesList = collidingItems();
65     if (!collidesList.isEmpty())
66     {
67         qDebug() << collidesList.at(0)->data(0);
68         if (collidesList.at(0)->data(0) == "rock")
69         {
70             setX(oldx);
71             setY(oldy);
72         }
73     }
74
75 }
76
77
78 void OrientationControlledGraphicsPixmapObject::setBoundaries(QRectF boundaryrect)
79 {
80     boundaryrect_ = boundaryrect;
81
82 }
83
84 void OrientationControlledGraphicsPixmapObject::setObstacles(int key, QList<QVariant> values)
85 {
86     obstacleKey_ = key;
87     obstacleValues_ = values;
88 }
89
90
91