File indexing completed on 2024-04-28 04:04:41
0001 /* 0002 SPDX-FileCopyrightText: 1998-2001 Andreas Zehender <az@azweb.de> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "spritebase.h" 0008 0009 #include <math.h> 0010 0011 #include <QGraphicsScene> 0012 #include <QSvgRenderer> 0013 0014 0015 SimpleSprite::SimpleSprite(QSvgRenderer* svg, const QString& element) 0016 :QGraphicsSvgItem(nullptr) 0017 { 0018 setSharedRenderer(svg); 0019 setElementId(element); 0020 init(); 0021 } 0022 0023 void SimpleSprite::init() 0024 { 0025 m_width = boundingRect().width(); 0026 m_height = boundingRect().height(); 0027 m_center = QPointF(m_width/2.0f,m_height/2.0f); 0028 } 0029 0030 int SimpleSprite::width() 0031 { 0032 return m_width; 0033 } 0034 0035 int SimpleSprite::height() 0036 { 0037 return m_height; 0038 } 0039 0040 QPointF SimpleSprite::center() 0041 { 0042 return m_center; 0043 } 0044 0045 MobileSprite::MobileSprite(QSvgRenderer* svg, const QString& element, int pn) 0046 :SimpleSprite(svg, element) 0047 { 0048 stopped=false; 0049 playerNumber=pn; 0050 } 0051 0052 void MobileSprite::forward(double mult, int fr) 0053 { 0054 Q_UNUSED(fr); 0055 0056 if(!stopped) 0057 { 0058 QGraphicsSvgItem::moveBy(xVelocity()*mult,yVelocity()*mult); 0059 checkBounds(); 0060 // FIXME 0061 //setFrame(fr); 0062 } 0063 // else 0064 // setFrame(fr); 0065 } 0066 0067 void MobileSprite::forward(double mult) 0068 { 0069 if(!stopped) 0070 { 0071 QGraphicsSvgItem::moveBy(xVelocity()*mult,yVelocity()*mult); 0072 checkBounds(); 0073 } 0074 } 0075 0076 void MobileSprite::checkBounds() 0077 { 0078 double cx, cy; 0079 int ch, cw; 0080 0081 cx = x(); 0082 cy = y(); 0083 ch = (int)scene()->height(); 0084 cw = (int)scene()->width(); 0085 0086 if ( (int)(cx+0.5) < 0 ) 0087 cx = cw - 1 - fmod( -cx, cw ); 0088 else if ( (int)(cx+0.5) > ( cw-1 ) ) 0089 cx = fmod( cx-cw-1, cw ); 0090 if ( (int)(cy+0.5) < 0 ) 0091 cy = ch-1 - fmod( -cy, ch ); 0092 else if ( (int)(cy+0.5) > ( ch-1 ) ) 0093 cy = fmod( cy-ch-1, ch ); 0094 if ( (cx != x()) || (cy != y()) ) 0095 { 0096 // printf("%5.2f %5.2f %5.2f %5.2f\n", x(), y(), cx, cy); 0097 setPos(QPointF(cx, cy)); 0098 } 0099 } 0100 0101 void MobileSprite::calculateGravity(double gravity,double mult) 0102 { 0103 double abs_2,nx,ny,ex,ey,sq,eg; 0104 0105 if(!stopped) 0106 { 0107 ex=x()-scene()->width()/2.0; 0108 ey=y()-scene()->height()/2.0; 0109 0110 abs_2=ex*ex+ey*ey; 0111 sq=sqrt(abs_2); 0112 0113 nx=ex/sq; 0114 ny=ey/sq; 0115 eg=gravity*mult; 0116 setVelocity(xVelocity()-eg*nx/abs_2, 0117 yVelocity()-eg*ny/abs_2); 0118 } 0119 } 0120 0121 int MobileSprite::spriteFieldWidth() 0122 { 0123 return (int)scene()->width(); 0124 } 0125 0126 int MobileSprite::spriteFieldHeight() 0127 { 0128 return (int)scene()->height(); 0129 } 0130 0131 void MobileSprite::setVelocity(double vx, double vy) 0132 { 0133 xvel = vx; 0134 yvel = vy; 0135 } 0136 0137 AiSprite MobileSprite::toAiSprite() 0138 { 0139 // y direction: screen: bottom to top 0140 // mathematical: top to bottom 0141 AiSprite as; 0142 as.x=x()-scene()->width()/2.0; 0143 as.y=-(y()-scene()->height()/2.0); 0144 as.dx=xVelocity(); 0145 as.dy=-yVelocity(); 0146 as.sun=false; 0147 as.border=false; 0148 return as; 0149 } 0150 0151 AnimatedSprite::AnimatedSprite(QSvgRenderer* svg, 0152 const QList<QString> &animation, 0153 int pn) 0154 : MobileSprite(svg,animation[0],pn) 0155 { 0156 frames = animation; 0157 currentFrame = 0; 0158 } 0159 0160 void AnimatedSprite::setFrame(int frame) 0161 { 0162 if (!frames.isEmpty()) { 0163 currentFrame = frame % frames.size(); 0164 setElementId(frames.at(currentFrame)); 0165 } 0166 } 0167 0168 void AnimatedSprite::advance(int phase) 0169 { 0170 if (phase == 1 && !frames.isEmpty()) { 0171 currentFrame = (currentFrame + 1) % frames.size(); 0172 setElementId(frames.at(currentFrame)); 0173 init(); 0174 } 0175 } 0176 0177 QPainterPath AnimatedSprite::shape() const 0178 { 0179 QPainterPath path; 0180 path.addRect(0, 0, 0181 boundingRect().width(), 0182 boundingRect().height()); 0183 return path; 0184 } 0185 0186 void AnimatedSprite::setAnimation(const QList<QString> &animation) 0187 { 0188 frames = animation; 0189 setFrame(0); 0190 }