Menu items in correct places again
[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     setSpeed(speed);
32     changeDirection();
33     connect(&timer_,SIGNAL(timeout()),this,SLOT(move()));
34 }
35
36 void TimerControlledGraphicsPixmapObject::startMoving()
37 {
38     if (!stoppedBecauseInvalidTime_)
39         timer_.start();
40 }
41
42 void TimerControlledGraphicsPixmapObject::stopMoving()
43 {
44     timer_.stop();
45 }
46
47 void TimerControlledGraphicsPixmapObject::setSpeed(int speed)
48 {
49     if (speed >0)
50     {
51         timer_.setInterval(1000/speed); //converts from pixels in second to milliseconds per pixel
52         stoppedBecauseInvalidTime_ = false;
53      }
54     else
55         stoppedBecauseInvalidTime_ = true;
56         timer_.stop();
57 }
58
59
60 void TimerControlledGraphicsPixmapObject::move()
61 {
62
63     int oldx = x();
64     int oldy = y();
65
66     int newx = oldx;
67     int newy = oldy;
68
69
70     //calculate the new position
71
72     if (direction_ == E || direction_ == SE || direction_ == NE)
73     {
74         newx++;
75     }
76
77     if (direction_ == W || direction_ == SW || direction_ == NW)
78     {
79         newx--;
80     }
81
82     if (direction_ == S || direction_ == SE || direction_ == SW)
83     {
84         newy++;
85     }
86
87     if (direction_ == N || direction_ == NE || direction_ == NW)
88     {
89         newy--;
90     }
91
92
93
94     //Bound the item into the scene and change direction if hitting a boundary
95     //Only works if the old position is inside the boundaries
96
97     if (!scene()) //no movement if this item does not belong to a scene
98         return;
99
100     QRect sceneRectangle = scene()->sceneRect().toRect();
101
102     if (newx < sceneRectangle.left() || newx > sceneRectangle.right()-pixmap().width())
103     {
104         changeDirection();
105         return;
106     }
107
108
109     if (newy < sceneRectangle.top() || newy > sceneRectangle.bottom()-pixmap().height())
110     {
111         changeDirection();
112         return;     //the old x and y values remain intact
113     }
114
115
116     //Set the new position
117
118     setX(newx);
119     setY(newy);
120
121
122     //check for collisions and handle them (up to subclassess to implement)
123     //return to the old position if requested
124
125     if (handleCollisions() == false)
126     {
127         setX(oldx);
128         setY(oldy);
129     }
130
131
132 }
133
134 void TimerControlledGraphicsPixmapObject::changeDirection()
135 {
136  //   qDebug () << "Supposed to change direction";
137
138     int direction = (qrand()%8);
139  //   qDebug()  << direction;
140
141     switch (direction)
142     {
143         case 0:
144             direction_ = S;
145             break;
146
147         case 1:
148             direction_ = SW;
149             break;
150
151        case 2:
152             direction_ = W;
153             break;
154
155        case 3:
156             direction_ = NW;
157             break;
158
159        case 4:
160             direction_ = N;
161             break;
162
163        case 5:
164             direction_ = NE;
165             break;
166
167        case 6:
168             direction_ = E;
169             break;
170
171       case 7:
172             direction_ = SE;
173             break;
174
175
176     }
177
178 }
179
180 bool TimerControlledGraphicsPixmapObject::handleCollisions()
181 {
182     return true;
183 }