1/*
2 Copyright (C) 1998-2001 Andreas Zehender <az@azweb.de>
3
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2 of the License, or
6 (at your option) any later version.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16*/
17
18#include "spritebase.h"
19
20#include <math.h>
21
22#include <QGraphicsScene>
23#include <QSvgRenderer>
24
25#include <kdebug.h>
26
27SimpleSprite::SimpleSprite(QSvgRenderer* svg, const QString& element)
28 :QGraphicsSvgItem(0)
29{
30 setSharedRenderer(svg);
31 setElementId(element);
32 init();
33}
34
35void SimpleSprite::init()
36{
37 m_width = boundingRect().width();
38 m_height = boundingRect().height();
39 m_center = QPointF(m_width/2.0f,m_height/2.0f);
40}
41
42int SimpleSprite::width()
43{
44 return m_width;
45}
46
47int SimpleSprite::height()
48{
49 return m_height;
50}
51
52QPointF SimpleSprite::center()
53{
54 return m_center;
55}
56
57MobileSprite::MobileSprite(QSvgRenderer* svg, const QString& element, int pn)
58 :SimpleSprite(svg, element)
59{
60 stopped=false;
61 playerNumber=pn;
62}
63
64void MobileSprite::forward(double mult, int fr)
65{
66 if(!stopped)
67 {
68 QGraphicsSvgItem::moveBy(xVelocity()*mult,yVelocity()*mult);
69 checkBounds();
70 // FIXME
71 //setFrame(fr);
72 }
73 // else
74 // setFrame(fr);
75}
76
77void MobileSprite::forward(double mult)
78{
79 if(!stopped)
80 {
81 QGraphicsSvgItem::moveBy(xVelocity()*mult,yVelocity()*mult);
82 checkBounds();
83 }
84}
85
86void MobileSprite::checkBounds()
87{
88 double cx, cy;
89 int ch, cw;
90
91 cx = x();
92 cy = y();
93 ch = (int)scene()->height();
94 cw = (int)scene()->width();
95
96 if ( (int)(cx+0.5) < 0 )
97 cx = cw - 1 - fmod( -cx, cw );
98 else if ( (int)(cx+0.5) > ( cw-1 ) )
99 cx = fmod( cx-cw-1, cw );
100 if ( (int)(cy+0.5) < 0 )
101 cy = ch-1 - fmod( -cy, ch );
102 else if ( (int)(cy+0.5) > ( ch-1 ) )
103 cy = fmod( cy-ch-1, ch );
104 if ( (cx != x()) || (cy != y()) )
105 {
106 // printf("%5.2f %5.2f %5.2f %5.2f\n", x(), y(), cx, cy);
107 setPos(QPointF(cx, cy));
108 }
109}
110
111void MobileSprite::calculateGravity(double gravity,double mult)
112{
113 double abs_2,nx,ny,ex,ey,sq,eg;
114
115 if(!stopped)
116 {
117 ex=x()-scene()->width()/2.0;
118 ey=y()-scene()->height()/2.0;
119
120 abs_2=ex*ex+ey*ey;
121 sq=sqrt(abs_2);
122
123 nx=ex/sq;
124 ny=ey/sq;
125 eg=gravity*mult;
126 setVelocity(xVelocity()-eg*nx/abs_2,
127 yVelocity()-eg*ny/abs_2);
128 }
129}
130
131int MobileSprite::spriteFieldWidth()
132{
133 return (int)scene()->width();
134}
135
136int MobileSprite::spriteFieldHeight()
137{
138 return (int)scene()->height();
139}
140
141void MobileSprite::setVelocity(double vx, double vy)
142{
143 xvel = vx;
144 yvel = vy;
145}
146
147AiSprite MobileSprite::toAiSprite()
148{
149 // y direction: screen: bottom to top
150 // mathematical: top to bottom
151 AiSprite as;
152 as.x=x()-scene()->width()/2.0;
153 as.y=-(y()-scene()->height()/2.0);
154 as.dx=xVelocity();
155 as.dy=-yVelocity();
156 as.sun=false;
157 as.border=false;
158 return as;
159}
160
161AnimatedSprite::AnimatedSprite(QSvgRenderer* svg,
162 const QList<QString> &animation,
163 int pn)
164 : MobileSprite(svg,animation[0],pn)
165{
166 frames = animation;
167 currentFrame = 0;
168}
169
170void AnimatedSprite::setFrame(int frame)
171{
172 if (!frames.isEmpty()) {
173 currentFrame = frame % frames.size();
174 setElementId(frames.at(currentFrame));
175 }
176}
177
178void AnimatedSprite::advance(int phase)
179{
180 if (phase == 1 && !frames.isEmpty()) {
181 currentFrame = (currentFrame + 1) % frames.size();
182 setElementId(frames.at(currentFrame));
183 init();
184 }
185}
186
187QPainterPath AnimatedSprite::shape() const
188{
189 QPainterPath path;
190 path.addRect(0, 0,
191 boundingRect().width(),
192 boundingRect().height());
193 return path;
194}
195
196void AnimatedSprite::setAnimation(const QList<QString> &animation)
197{
198 frames = animation;
199 setFrame(0);
200}
201