remove old files add new sources
[mancala] / src / Stone.cpp
1 /*
2 Mancala - A Historical Board Game
3 Copyright (C) 2009-2010 A.H.M.Mahfuzur Rahman 65mahfuz90@gmail.com
4 Copyright (c) 2010 Reto Zingg g.d0b3rm4n@gmail.com
5
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License as
8 published by the Free Software Foundation; either version 2 of
9 the License, or (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "Stone.h"
21 #include <math.h>
22
23 #include <QSvgRenderer>
24 #include <QGraphicsItemAnimation>
25 #include <QTimeLine>
26
27 // #include "kdebug.h"
28 // #include "kstandarddirs.h"
29
30 #include "Board.h"
31
32
33 //Stone::Stone(int index , QSvgRenderer *render,  GameInfo* gameInfo, ThemeManager* theme)
34 Stone::Stone(int index , GameInfo* gameInfo, ThemeManager* theme,QGraphicsSvgItem *parent)
35     : QGraphicsSvgItem(parent)
36
37 {
38     m_themeManager = theme;
39     m_gameInfo = gameInfo;
40     m_board = qgraphicsitem_cast<Board*>(parent);
41
42     setSharedRenderer((QSvgRenderer*)theme);
43     setElementId("stone");
44
45     m_index = index;
46     setZValue(3);
47
48 }
49
50 void Stone::setPosition(qreal x, qreal y){
51
52     m_pos.setX(x);
53     m_pos.setY(y);
54
55     //kDebug() << "Pos Here: " << x << " " << y;
56     setPos(x,y);
57
58 }
59
60 void Stone::animateStones(QPointF newPos){
61
62     //kDebug() << "Stone Pos: New: "<< newPos << " Old: " << m_pos;
63     int steps = abs(m_pos.x() - newPos.x()) + abs(m_pos.y() - newPos.y()),diff;
64
65     QTimeLine *timer = new QTimeLine(1000);
66     timer->setFrameRange(0, 100);
67
68     QGraphicsItemAnimation *animation = new QGraphicsItemAnimation;
69     animation->setItem(this);
70     animation->setTimeLine(timer);
71
72     for (int i = 0; i < (int)steps; ++i){
73
74         if(i < abs(m_pos.x() - newPos.x()) ){
75             if( m_pos.x() < newPos.x() ){
76                 animation->setPosAt(i / (qreal)steps, QPointF( i + m_pos.x() , m_pos.y()));
77                 //kDebug() << "Position: " << QPointF( i + m_pos.x() , m_pos.y());
78             }
79             else animation->setPosAt(i / (qreal)steps, QPointF( m_pos.x() - i , m_pos.y()));
80             diff = i;
81         }
82         else{
83             //if( diff+1 == i) kDebug() <<"Diff is:" << diff;
84
85             if(m_pos.y() < newPos.y()){
86                 animation->setPosAt( i/ (qreal) steps, QPointF(newPos.x() , i-diff+m_pos.y() ));
87                 //kDebug() << "Position: " << QPointF(newPos.x() , i-diff+m_pos.y() );
88             }
89             else{
90                 animation->setPosAt( i/ (qreal) steps, QPointF(newPos.x() , m_pos.y()-(i-diff) ));
91                 //kDebug() << "Position: " << QPointF(newPos.x() , m_pos.y()-(i-diff));
92             }
93         }
94     }
95
96     timer->start();
97
98     m_pos.setX(newPos.x());
99     m_pos.setY(newPos.y());
100
101 }