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