1 /* This file is part of KsirK.
2    Copyright (C) 2001-2007 Gael de Chalendar <kleag@free.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                : Wed Jul 18 2001  */
21 
22 #ifndef ANIMSPRITESLIST_H
23 #define ANIMSPRITESLIST_H
24 
25 #include "animsprite.h"
26 #include "armysprite.h"
27 
28 #include <QTextStream>
29 #include <QPoint>
30 #include <QList>
31 #include <QObject>
32 
33 #include "ksirk_debug.h"
34 
35 namespace Ksirk
36 {
37 
38 
39 /**
40  * the AnimSpritesList is a list of AnimSprite-s with useful methods to
41  * do some actions on each the elements. It is templatized to allow its
42  * instanciation with the various kind of sprites
43  */
44 template < typename SpriteType >
45 class AnimSpritesList : public QList< SpriteType* >
46 {
47 public:
48 
49   /** Default constructor */
50   AnimSpritesList();
51 
52   /** Default destructor */
53   virtual ~AnimSpritesList();
54 
55 
56   /**
57     * hide the sprites of the list, remove them from the list  and call their
58     * destructors iff the liste is in auto-delete mode
59     */
60   void hideAndRemoveAll();
61 
62  /**
63     * hide the sprites of the list, remove them from the list  and call their
64     * destructors iff the liste is in auto-delete mode
65     */
66   void hideAndRemoveFirst();
67 
68   /**
69     * return the first AnimSprite of the list that currently displays its last
70     * frame
71     */
72   const SpriteType* firstThatIsLastFrame();
73 
74   /**
75     * Makes all sprites of this list to move one step toward their destination
76     */
77   void moveAll();
78 
79   /**
80     * Makes all sprites of this list to move up to their destination
81     * @param clear if true removes the sprites from the list and add them to
82     * their destination country
83     */
84   void moveAllToDestinationNow(bool clear = false);
85 
86   /**
87     * Saves all elements of this list with a XML format
88     * @param xmlStream the stream on which to write the XML
89     */
90   void saveXmlAll(QTextStream& xmlStream);
91 } ;
92 
93 template < typename SpriteType >
AnimSpritesList()94 AnimSpritesList< SpriteType >::AnimSpritesList() : QList< SpriteType* >()
95 {
96 }
97 
98 template < typename SpriteType >
~AnimSpritesList()99   AnimSpritesList< SpriteType >::~AnimSpritesList()
100 {
101 }
102 
103 template < typename SpriteType >
hideAndRemoveAll()104 void AnimSpritesList< SpriteType >::hideAndRemoveAll()
105 {
106 //   qCDebug(KSIRK_LOG);
107 
108   while (!QList< SpriteType* >::empty())
109   {
110     hideAndRemoveFirst();
111   }
112 }
113 
114 template < typename SpriteType >
hideAndRemoveFirst()115 void AnimSpritesList< SpriteType >::hideAndRemoveFirst()
116 {
117     SpriteType* sprite = QList< SpriteType* >::front();
118     QList< SpriteType* >::pop_front();
119     sprite-> hide();
120     sprite->deleteLater();
121 }
122 
123 template < typename SpriteType >
firstThatIsLastFrame()124 const SpriteType* AnimSpritesList< SpriteType >::firstThatIsLastFrame()
125 {
126 //    qDebug("AnimSpritesList< SpriteType >::firstThatIsLastFrame");
127   typename AnimSpritesList< SpriteType >::iterator it;
128   typename AnimSpritesList< SpriteType >::iterator it_end = QList< SpriteType* >::end();
129   for ( it = QList< SpriteType* >::begin(); it != it_end; it++ )
130   {
131     if ((*it)-> isLastFrame()) return (*it);
132   }
133   return 0;
134 }
135 
136 template < typename SpriteType >
moveAll()137 void AnimSpritesList< SpriteType >::moveAll()
138 {
139   typename AnimSpritesList< SpriteType >::iterator it, it_end;
140   it = QList< SpriteType* >::begin();
141   it_end = QList< SpriteType* >::end();
142   while (it != it_end)
143   {
144     SpriteType* sp = (*it);
145 
146     const QPointF& destinationPoint = sp-> getDestination()-> pointFor(sp);
147 
148     if (((sp->x()) == (destinationPoint.x())) && ((sp-> y()) == (destinationPoint.y())))
149     {
150       sp-> hide();
151       it = QList< SpriteType* >::erase(it);
152       sp-> getDestination()-> incrNbArmies((*it)-> nbArmies());
153       sp-> getDestination()-> createArmiesSprites();
154       sp->deleteLater();
155     }
156     else it++;
157   }
158 }
159 
160 template < typename SpriteType >
moveAllToDestinationNow(bool clear)161 void AnimSpritesList< SpriteType >::moveAllToDestinationNow(bool clear)
162 {
163   qCDebug(KSIRK_LOG) << clear;
164   typename AnimSpritesList< SpriteType >::iterator it, it_end;
165   it = QList< SpriteType* >::begin();
166   it_end = QList< SpriteType* >::end();
167   while (it != it_end)
168   {
169     SpriteType* sp = (*it);
170 
171     const QPointF& destinationPoint = sp-> getDestinationPoint();
172     sp->setPos(destinationPoint);
173     sp->moveIt();
174 
175     if (clear)
176     {
177       sp-> hide();
178       it = QList< SpriteType* >::erase(it);
179       sp-> getDestination()-> incrNbArmies(((ArmySprite*)(*it))-> nbArmies());
180       sp-> getDestination()-> createArmiesSprites();
181     }
182     else
183     {
184       it++;
185     }
186   }
187 }
188 
189 template < typename SpriteType >
saveXmlAll(QTextStream & xmlStream)190 void AnimSpritesList< SpriteType >::saveXmlAll(QTextStream& xmlStream)
191 {
192   foreach (SpriteType* sp, *this)
193   {
194     sp->saveXml(xmlStream);
195   }
196 }
197 
198 } // closing namespace Ksirk
199 
200 #endif // ANIMSPRITESLIST_H
201 
202