Changed the section to user/hidden as required for Nokia Store
[ghostsoverboard] / timercontrolledgraphicspixmapobject.cpp
1 /**************************************************************************
2         Ghosts Overboard - a game for Maemo 5
3
4         Copyright (C) 2011  Heli Hyvättinen
5
6         This file is part of Ghosts Overboard
7
8         Ghosts Overboard is free software: you can redistribute it and/or modify
9         it under the terms of the GNU General Public License as published by
10         the Free Software Foundation, either version 2 of the License, or
11         (at your option) any later version.
12
13         This program is distributed in the hope that it will be useful,
14         but WITHOUT ANY WARRANTY; without even the implied warranty of
15         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16         GNU General Public License for more details.
17
18         You should have received a copy of the GNU General Public License
19         along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
21 **************************************************************************/
22
23 #include "timercontrolledgraphicspixmapobject.h"
24 #include <QGraphicsScene>
25 #include <QDebug>
26
27
28 TimerControlledGraphicsPixmapObject::TimerControlledGraphicsPixmapObject(QPixmap pixmap, int speed, QGraphicsItem* parent) :
29     QObject(), QGraphicsPixmapItem(pixmap, parent)
30 {
31     pixelsPerMove_ = 2;
32     setSpeed(speed);
33     changeDirection();
34     connect(&timer_,SIGNAL(timeout()),this,SLOT(move()));
35 }
36
37 void TimerControlledGraphicsPixmapObject::startMoving()
38 {
39     if (!stoppedBecauseInvalidTime_)
40         timer_.start();
41 }
42
43 void TimerControlledGraphicsPixmapObject::stopMoving()
44 {
45     timer_.stop();
46 }
47
48 void TimerControlledGraphicsPixmapObject::setSpeed(int speed)
49 {
50     if (speed >0)
51     {
52         timer_.setInterval(1000/(speed/pixelsPerMove_)); //converts from pixels in second to milliseconds per pixels moved at once
53         stoppedBecauseInvalidTime_ = false;
54      }
55     else
56         stoppedBecauseInvalidTime_ = true;
57         timer_.stop();
58 }
59
60
61 void TimerControlledGraphicsPixmapObject::move()
62 {
63
64     int oldx = x();
65     int oldy = y();
66
67     int newx = oldx;
68     int newy = oldy;
69
70
71     //calculate the new position
72
73     if (direction_ == E || direction_ == SE || direction_ == NE)
74     {
75         newx+= pixelsPerMove_;
76     }
77
78     if (direction_ == W || direction_ == SW || direction_ == NW)
79     {
80         newx-=pixelsPerMove_;
81     }
82
83     if (direction_ == S || direction_ == SE || direction_ == SW)
84     {
85         newy+=pixelsPerMove_;
86     }
87
88     if (direction_ == N || direction_ == NE || direction_ == NW)
89     {
90         newy-=pixelsPerMove_;
91     }
92
93
94
95     //Bound the item into the scene and change direction if hitting a boundary
96     //Only works if the old position is inside the boundaries
97
98     if (!scene()) //no movement if this item does not belong to a scene
99         return;
100
101     QRect sceneRectangle = scene()->sceneRect().toRect();
102
103     if (newx < sceneRectangle.left() || newx > sceneRectangle.right()-pixmap().width())
104     {
105         changeDirection();
106         return;
107     }
108
109
110     if (newy < sceneRectangle.top() || newy > sceneRectangle.bottom()-pixmap().height())
111     {
112         changeDirection();
113         return;     //the old x and y values remain intact
114     }
115
116
117     //Set the new position
118
119     setX(newx);
120     setY(newy);
121
122
123     //check for collisions and handle them (up to subclassess to implement)
124     //return to the old position if requested
125
126     if (handleCollisions() == false)
127     {
128         setX(oldx);
129         setY(oldy);
130     }
131
132
133 }
134
135 void TimerControlledGraphicsPixmapObject::changeDirection()
136 {
137  //   qDebug () << "Supposed to change direction";
138
139     int direction = (qrand()%8);
140  //   qDebug()  << direction;
141
142     switch (direction)
143     {
144         case 0:
145             direction_ = S;
146             break;
147
148         case 1:
149             direction_ = SW;
150             break;
151
152        case 2:
153             direction_ = W;
154             break;
155
156        case 3:
157             direction_ = NW;
158             break;
159
160        case 4:
161             direction_ = N;
162             break;
163
164        case 5:
165             direction_ = NE;
166             break;
167
168        case 6:
169             direction_ = E;
170             break;
171
172       case 7:
173             direction_ = SE;
174             break;
175
176
177     }
178
179 }
180
181 bool TimerControlledGraphicsPixmapObject::handleCollisions()
182 {
183     return true;
184 }