00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <math.h>
00023
00024
00025 #include <QSizeF>
00026
00027
00028 #include <kdebug.h>
00029 #include <kconfig.h>
00030
00031
00032 #include "piecesprite.h"
00033
00034
00035 PieceSprite::PieceSprite(QString id, ThemeManager* theme, int advancePeriod, int no, QGraphicsScene* canvas)
00036 : Themable(id, theme), PixmapSprite(advancePeriod, no, canvas)
00037 {
00038 mNotify = new SpriteNotify(this);
00039 if (theme) theme->updateTheme(this);
00040 }
00041
00042
00043 PieceSprite::~PieceSprite()
00044 {
00045 delete mNotify;
00046 }
00047
00048
00049 void PieceSprite::changeTheme()
00050 {
00051 PixmapSprite::changeTheme();
00052 }
00053
00054
00055
00056 void PieceSprite::startLinear(QPointF start, QPointF end, double velocity)
00057 {
00058 mStart = start;
00059 mEnd = end;
00060 QPointF p = mEnd-mStart;
00061 double dist = sqrt(p.x()*p.x()+p.y()*p.y());
00062 if (dist > 0.0) mDuration = dist/velocity*1000.0;
00063 else mDuration = 0.0;
00064
00065 mMovementState = LinearMove;
00066 mTime = 0;
00067 setPos(mStart.x()*getScale(), mStart.y()*getScale());
00068 show();
00069 }
00070
00071
00072
00073 void PieceSprite::startLinear(QPointF end, double velocity)
00074 {
00075 mStart = QPointF(x()/getScale(), y()/getScale());
00076 mEnd = end;
00077 QPointF p = mEnd-mStart;
00078 double dist = sqrt(p.x()*p.x()+p.y()*p.y());
00079 if (dist > 0.0) mDuration = dist/velocity*1000.0;
00080 else mDuration = 0.0;
00081 mMovementState = LinearMove;
00082 mTime = 0;
00083 show();
00084 }
00085
00086
00087
00088 void PieceSprite::advance(int phase)
00089 {
00090
00091 PixmapSprite::advance(phase);
00092
00093
00094 if (phase == 0)
00095 {
00096 return ;
00097 }
00098
00099
00100 double scale = this->getScale();
00101
00102
00103 if (mMovementState == LinearMove)
00104 {
00105
00106 if (mTime >= mDuration)
00107 {
00108 mMovementState = Idle;
00109 setPos(mEnd.x()*scale, mEnd.y()*scale);
00110
00111 mNotify->emitSignal(0);
00112 }
00113 else
00114 {
00115
00116 double t = mTime/mDuration;
00117 qreal x = mStart.x() + t*(mEnd.x()-mStart.x());
00118 qreal y = mStart.y() + t*(mEnd.y()-mStart.y());
00119 setPos(x*scale, y*scale);
00120 }
00121 }
00122
00123 }
00124