bump up
[mancala] / src / Kalah.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 "Kalah.h"
21
22 #include <math.h>
23
24 #include <QGraphicsScene>
25 #include <QGraphicsSvgItem>
26 // #include <kdebug.h>
27
28 #include "GameInfo.h"
29 #include "Board.h"
30 #include "Stone.h"
31
32
33 Kalah::Kalah(GameInfo* info, Kalah::side p, QGraphicsSvgItem *parent)
34 : QGraphicsSvgItem(parent){
35
36     m_side = p;
37     m_gameInfo = info;
38     m_board = qgraphicsitem_cast<Board*>(parent);
39
40     setSharedRenderer(m_board->renderer());
41     setElementId("kalah");
42     setZValue(2);
43     initialSetup();
44     setupText();
45
46 }
47
48
49 //The Drawing of Kalah Items
50 void Kalah::initialSetup(){
51
52     qreal gapWidth = m_board->boundingRect().width() * ( 0.2 / ( m_gameInfo->numCupsPerRow() + m_gameInfo->numKalahs() + 1 ) );
53
54     m_size.setWidth( m_board->boundingRect().width() * 0.8 / ( m_gameInfo->numCupsPerRow() + m_gameInfo->numKalahs() ) );
55     m_size.setHeight( m_board->boundingRect().height() * 0.8 );
56
57     //kalahWidth and cupWidth are same
58     if(m_side == Left)
59         m_pos.setX( m_board->boundingRect().x() + gapWidth );
60     else m_pos.setX( m_board->boundingRect().x() +  gapWidth + ( gapWidth + m_size.width() ) * ( m_gameInfo->numCupsPerRow() + 1 ) );
61     m_pos.setY( m_board->boundingRect().y() + m_board->boundingRect().height() * 0.1 );
62
63     //scaling the kalah object
64     qreal kalahScaleX = m_size.width() / boundingRect().width();
65     qreal kalahScaleY = m_size.height()/ boundingRect().height();
66
67     m_mid_point.setX( m_pos.x() + m_size.width() / 2 );
68     m_mid_point.setY( m_pos.y() + m_size.height() / 2 );
69
70     setPos(m_pos);
71     scale(kalahScaleX,kalahScaleY);
72 }
73
74 void Kalah::setupText(){
75
76     m_text = new QGraphicsSimpleTextItem("0",m_board);
77     m_text->setFont( QFont("Comic Sans MS", 24, QFont::Bold) );
78     m_text->setPen(QPen(QColor("darkslateblue"), 1.5, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
79     QLinearGradient gradient(0, 0, 0, 100);
80     gradient.setColorAt(0.0, "mediumslateblue");
81     gradient.setColorAt(1.0, "cornsilk");
82     m_text->setBrush(gradient);
83
84     int textX = m_pos.x() + m_size.width();
85     textX -= m_text->boundingRect().size().width() + 15;
86
87     int textY =  m_pos.y() + m_size.height();
88     textY -= m_text->boundingRect().size().height() - 15;
89
90     m_text->scale(0.8,0.8);
91     m_text->setPos(textX,textY);
92     m_text->setZValue(4);
93     m_text->setVisible(false);
94 }
95
96 void Kalah::updateStoneList(Stone* stoneItem){
97
98     //kDebug() << "In Kalah" ;
99     QPointF *stonePos;
100
101     m_text->setVisible(false);
102     m_stone.append(stoneItem);
103
104     //calculate stone postion and place it
105     stonePos = calculateStonePosition(stoneItem);
106     stoneItem->animateStones(QPointF(stonePos->x(),stonePos->y()));
107
108     //we want to change the text showing ball number
109     if( m_stone.count() ){
110
111         m_text->setText( QString("%1").arg(m_stone.count() ) );
112         m_text->setVisible(true);
113
114         update();
115     }
116
117     //kDebug() << "Returning";
118 }
119
120
121 //calculate stonePostion in Kalah
122 QPointF* Kalah::calculateStonePosition(Stone* stone){
123
124     QPointF *returnPoint = new QPointF();
125
126     qreal diameterCup = qMin(m_size.width(),m_size.height());
127
128     int initialLength = qrand() % (int) diameterCup / 3;
129     int angle = qrand() % 360;
130
131     qreal posX = middlePoint().x() + initialLength * cos(angle * M_PI / 180);
132     qreal posY = middlePoint().y() + initialLength * sin(angle * M_PI / 180);
133
134     if( ( posX + stone->size().width()) > ( m_pos.x() + 0.7 * m_size.width() ) )
135         posX -= stone->size().width();
136     else if( posX < ( m_pos.x()+ 0.3 * m_size.width() ) )
137         posX += stone->size().width();
138
139     if( ( posY + stone->size().height() ) > ( m_pos.y() + 0.7 * m_size.height() ) )
140         posY -= stone->size().height();
141     else if( posY < (m_pos.y() + 0.3 < m_size.height() ) )
142         posY += stone->size().height();
143
144     returnPoint->setX(posX);
145     returnPoint->setY(posY);
146
147     return returnPoint;
148 }