Octopus speed given can now be zero or less
[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     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 }
57
58
59 void TimerControlledGraphicsPixmapObject::move()
60 {
61
62     int oldx = x();
63     int oldy = y();
64
65     int newx = oldx;
66     int newy = oldy;
67
68
69     //calculate the new position
70
71     if (direction_ == E || direction_ == SE || direction_ == NE)
72     {
73         newx++;
74     }
75
76     if (direction_ == W || direction_ == SW || direction_ == NW)
77     {
78         newx--;
79     }
80
81     if (direction_ == S || direction_ == SE || direction_ == SW)
82     {
83         newy++;
84     }
85
86     if (direction_ == N || direction_ == NE || direction_ == NW)
87     {
88         newy--;
89     }
90
91
92
93     //Bound the item into the scene and change direction if hitting a boundary
94     //Only works if the old position is inside the boundaries
95
96     if (!scene()) //no movement if this item does not belong to a scene
97         return;
98
99     QRect sceneRectangle = scene()->sceneRect().toRect();
100
101     if (newx < sceneRectangle.left() || newx > sceneRectangle.right()-pixmap().width())
102     {
103         changeDirection();
104         return;
105     }
106
107
108     if (newy < sceneRectangle.top() || newy > sceneRectangle.bottom()-pixmap().height())
109     {
110         changeDirection();
111         return;     //the old x and y values remain intact
112     }
113
114
115     //Set the new position
116
117     setX(newx);
118     setY(newy);
119
120
121     //check for collisions and handle them (up to subclassess to implement)
122     //return to the old position if requested
123
124     if (handleCollisions() == false)
125     {
126         setX(oldx);
127         setY(oldy);
128     }
129
130
131 }
132
133 void TimerControlledGraphicsPixmapObject::changeDirection()
134 {
135  //   qDebug () << "Supposed to change direction";
136
137     int direction = (qrand()%8);
138  //   qDebug()  << direction;
139
140     switch (direction)
141     {
142         case 0:
143             direction_ = S;
144             break;
145
146         case 1:
147             direction_ = SW;
148             break;
149
150        case 2:
151             direction_ = W;
152             break;
153
154        case 3:
155             direction_ = NW;
156             break;
157
158        case 4:
159             direction_ = N;
160             break;
161
162        case 5:
163             direction_ = NE;
164             break;
165
166        case 6:
167             direction_ = E;
168             break;
169
170       case 7:
171             direction_ = SE;
172             break;
173
174
175     }
176
177 }
178
179 bool TimerControlledGraphicsPixmapObject::handleCollisions()
180 {
181     return true;
182 }