e6b838c0592c1bb9f9fd43eef5dfe5a30cb01da1
[ghostsoverboard] / timercontrolledgraphicspixmapobject.cpp
1 #include "timercontrolledgraphicspixmapobject.h"
2 #include <QGraphicsScene>
3 #include <QDebug>
4
5
6 TimerControlledGraphicsPixmapObject::TimerControlledGraphicsPixmapObject(QPixmap pixmap, int speed, QGraphicsItem* parent) :
7     QObject(), QGraphicsPixmapItem(pixmap, parent)
8 {
9     setSpeed(speed);
10     direction_ = S;
11     connect(&timer_,SIGNAL(timeout()),this,SLOT(move()));
12 }
13
14 void TimerControlledGraphicsPixmapObject::startMoving()
15 {
16     timer_.start();
17 }
18
19 void TimerControlledGraphicsPixmapObject::stopMoving()
20 {
21     timer_.stop();
22 }
23
24 void TimerControlledGraphicsPixmapObject::setSpeed(int speed)
25 {
26     timer_.setInterval(1000/speed); //converts from pixels in second to milliseconds per pixel
27 }
28
29 void TimerControlledGraphicsPixmapObject::move()
30 {
31
32     int oldx = x();
33     int oldy = y();
34
35     int newx = oldx;
36     int newy = oldy;
37
38
39     //calculate the new position
40
41     if (direction_ == E || direction_ == SE || direction_ == NE)
42     {
43         newx++;
44     }
45
46     if (direction_ == W || direction_ == SW || direction_ == NW)
47     {
48         newx--;
49     }
50
51     if (direction_ == S || direction_ == SE || direction_ == SW)
52     {
53         newy++;
54     }
55
56     if (direction_ == N || direction_ == NE || direction_ == NW)
57     {
58         newy--;
59     }
60
61
62
63     //Bound the item into the scene and change direction if hitting a boundary
64     //Only works if the old position is inside the boundaries
65
66     if (!scene()) //no movement if this item does not belong to a scene
67         return;
68
69     QRect sceneRectangle = scene()->sceneRect().toRect();
70
71     if (newx < sceneRectangle.left() || newx > sceneRectangle.right()-40)
72     {
73         changeDirection();
74         return;
75     }
76
77
78     if (newy < sceneRectangle.top() || newy > sceneRectangle.bottom()-40)
79     {
80         changeDirection();
81         return;     //the old x and y values remain intact
82     }
83
84
85     //Set the new position
86
87     setX(newx);
88     setY(newy);
89
90
91     //check for collisions and handle them (up to subclassess to implement)
92     //return to the old position if requested
93
94     if (handleCollisions() == false)
95     {
96         setX(oldx);
97         setY(oldy);
98     }
99
100
101 }
102
103 void TimerControlledGraphicsPixmapObject::changeDirection()
104 {
105     qDebug () << "Supposed to change direction";
106
107     int direction = (qrand()%8);
108     qDebug()  << direction;
109
110     switch (direction)
111     {
112         case 0:
113             direction_ = S;
114             break;
115
116         case 1:
117             direction_ = SW;
118             break;
119
120        case 2:
121             direction_ = W;
122             break;
123
124        case 3:
125             direction_ = NW;
126             break;
127
128        case 4:
129             direction_ = N;
130             break;
131
132        case 5:
133             direction_ = NE;
134             break;
135
136        case 6:
137             direction_ = E;
138             break;
139
140       case 7:
141             direction_ = SE;
142             break;
143
144
145     }
146
147 }
148
149 bool TimerControlledGraphicsPixmapObject::handleCollisions()
150 {
151     return true;
152 }