1 /* This file is part of KsirK.
2    Copyright (C) 2008 Guillaume Pelouas <pelouas@hotmail.fr>
3 
4    KsirK is free software; you can redistribute it and/or
5    modify it under the terms of the GNU General Public
6    License as published by the Free Software Foundation, either version 2
7    of the License, or (at your option) any later version.
8 
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    General Public License for more details.
13 
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software
16    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17    02110-1301, USA
18 */
19 
20 /* begin                : Thu Nov 21 2007 */
21 
22 
23 #include "fightArena.h"
24 #include "GameLogic/player.h"
25 
26 namespace Ksirk
27 {
28    using namespace GameLogic;
29 
FightArena(QWidget * parent,unsigned int mapW,unsigned int mapH,QGraphicsScene * sceneArena,ONU * onuObject,GameAutomaton * automaton)30    FightArena::FightArena(QWidget* parent, unsigned int mapW, unsigned int mapH,
31       QGraphicsScene* sceneArena,ONU* onuObject, GameAutomaton* automaton):
32    QGraphicsView(parent),
33    m_scene(sceneArena),
34    m_onu(onuObject),
35    m_automaton(automaton)
36    {
37       qCDebug(KSIRK_LOG);
38 
39       setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
40       setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
41       setCacheMode(QGraphicsView::CacheBackground);
42       setMinimumSize(200,100);
43       setMaximumSize(mapW,mapH);
44       updateGeometry();
45 
46       // create the first country of the arena
47       m_countryAttack = new Country(automaton,
48                                       "",
49                                       QPointF(0,0),
50                                       QPointF(0,0),
51                                       QPointF(0,0),
52                                       QPointF(0,0),
53                                       QPointF(0,0),
54                                       QPointF(0,0));
55       // create the second country of the arena
56       m_countryDefense = new Country(automaton,
57                                       "",
58                                       QPointF(0,0),
59                                       QPointF(0,0),
60                                       QPointF(0,0),
61                                       QPointF(0,0),
62                                       QPointF(0,0),
63                                       QPointF(0,0));
64       // make the two arena countrys neighbours
65       QList<Country*> arenaAttackNeighbours;
66       arenaAttackNeighbours.insert(arenaAttackNeighbours.begin(), m_countryDefense);
67       m_countryAttack->neighbours(arenaAttackNeighbours);
68       QList<Country*> arenaDefenseNeighbours;
69       arenaDefenseNeighbours.insert(arenaDefenseNeighbours.begin(), m_countryAttack);
70       m_countryDefense->neighbours(arenaDefenseNeighbours);
71 
72 
73       // search the background image for the arena
74       KConfig config(onuObject->getConfigFileName());
75       KConfigGroup onugroup = config.group("onu");
76       QString skin = onugroup.readEntry("skinpath");
77       QString imageFileName = QStandardPaths::locate(QStandardPaths::AppDataLocation, skin + "/Images/arena.svg");
78       // create the background image
79       m_bgImage = new QPixmap(imageFileName);
80    }
81 
~FightArena()82   FightArena::~FightArena()
83   {
84     qCDebug(KSIRK_LOG);
85      delete m_countryAttack; m_countryAttack = 0;
86      delete m_countryDefense; m_countryDefense= 0;
87      delete m_bgImage; m_bgImage = 0;
88   }
89 
sizeHint() const90   QSize FightArena::sizeHint() const
91   {
92     return QSize(m_mapW, m_mapH);
93   }
94 
95    /**
96      * Init the arena with the two countries engaged
97      * @param countryA attacker country
98      * @param countryD defender country
99      */
initFightArena(Country * countryA,Country * countryD,BackGnd * bg)100    void FightArena::initFightArena (Country* countryA, Country* countryD, BackGnd* bg)
101    {
102      qCDebug(KSIRK_LOG);
103      // new size
104      int width = m_automaton->game()->centralWidget()->width();
105      int height = m_automaton->game()->centralWidget()->height();
106 
107      // resize the background image
108      QPixmap pix;
109      pix = m_bgImage->scaled(width,height);
110      // and put the image
111      bg->setPixmap(pix);
112 
113      // resize the widget
114      m_scene->setSceneRect ( 0, 0, width, height);
115      setMaximumSize(width, height);
116 
117      // new zoom
118      double newZ = height/280;
119      m_onu->setZoomArena(newZ);
120 
121      qCDebug(KSIRK_LOG) << "Hi";
122      // re-place the anchor point of the two countries
123      m_countryAttack->anchorPoint(QPointF((width/4)/newZ,(height/2)/newZ));
124      m_countryAttack->centralPoint(QPointF((width/4)/newZ,(height/2)/newZ));
125      m_countryAttack->pointFlag(QPointF((width/7)/newZ,(height/7)/newZ));
126      m_countryAttack->pointCannon(QPointF((2*width/18)/newZ,(height/2)/newZ));
127      m_countryAttack->pointCavalry(QPointF((4*width/18)/newZ,(2*height/5)/newZ));
128      m_countryAttack->pointInfantry(QPointF((6*width/18)/newZ,(3*height/5)/newZ));
129 
130      m_countryDefense->anchorPoint(QPointF((3*width/4)/newZ,(height/2)/newZ));
131      m_countryDefense->centralPoint(QPointF((3*width/4)/newZ,(height/2)/newZ));
132      m_countryDefense->pointFlag(QPointF((6*width/7)/newZ,(height/7)/newZ));
133      m_countryDefense->pointCannon(QPointF((16*width/18)/newZ,(height/2)/newZ));
134      m_countryDefense->pointCavalry(QPointF((14*width/18)/newZ,(3*height/5)/newZ));
135      m_countryDefense->pointInfantry(QPointF((12*width/18)/newZ,(2*height/5)/newZ));
136 
137      qCDebug(KSIRK_LOG) << "Ho";
138      // create the arena countries with the originals
139      m_countryAttack->copyForArena(countryA);
140      m_countryDefense->copyForArena(countryD);
141 
142      qCDebug(KSIRK_LOG) << "Done";
143    }
144 
145 }
146 
147 
148