File indexing completed on 2024-12-08 03:48:02

0001 /* This file is part of KsirK.
0002    Copyright (C) 2001-2007 Gael de Chalendar <kleag@free.fr>
0003 
0004    KsirK is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU General Public
0006    License as published by the Free Software Foundation, either version 2
0007    of the License, or (at your option) any later version.
0008 
0009    This program is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    General Public License for more details.
0013 
0014    You should have received a copy of the GNU General Public License
0015    along with this program; if not, write to the Free Software
0016    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
0017    02110-1301, USA
0018 */
0019 
0020 /*  begin                : Wed Jul 18 2001  */
0021 
0022 #ifndef ANIMSPRITESLIST_H
0023 #define ANIMSPRITESLIST_H
0024 
0025 #include "animsprite.h"
0026 #include "armysprite.h"
0027 
0028 #include <QTextStream>
0029 #include <QPoint>
0030 #include <QList>
0031 #include <QObject>
0032 
0033 #include "ksirk_debug.h"
0034 
0035 namespace Ksirk
0036 {
0037 
0038 
0039 /**
0040  * the AnimSpritesList is a list of AnimSprite-s with useful methods to
0041  * do some actions on each the elements. It is templatized to allow its
0042  * instanciation with the various kind of sprites
0043  */
0044 template < typename SpriteType >
0045 class AnimSpritesList : public QList< SpriteType* >
0046 {
0047 public:
0048 
0049   /** Default constructor */
0050   AnimSpritesList();
0051 
0052   /** Default destructor */
0053   virtual ~AnimSpritesList();
0054 
0055   
0056   /**
0057     * hide the sprites of the list, remove them from the list  and call their
0058     * destructors iff the liste is in auto-delete mode
0059     */
0060   void hideAndRemoveAll();
0061 
0062  /**
0063     * hide the sprites of the list, remove them from the list  and call their
0064     * destructors iff the liste is in auto-delete mode
0065     */
0066   void hideAndRemoveFirst();
0067 
0068   /**
0069     * return the first AnimSprite of the list that currently displays its last
0070     * frame
0071     */
0072   const SpriteType* firstThatIsLastFrame();
0073 
0074   /**
0075     * Makes all sprites of this list to move one step toward their destination
0076     */
0077   void moveAll();
0078 
0079   /**
0080     * Makes all sprites of this list to move up to their destination
0081     * @param clear if true removes the sprites from the list and add them to
0082     * their destination country
0083     */
0084   void moveAllToDestinationNow(bool clear = false);
0085 
0086   /**
0087     * Saves all elements of this list with a XML format
0088     * @param xmlStream the stream on which to write the XML
0089     */
0090   void saveXmlAll(QTextStream& xmlStream);
0091 } ;
0092 
0093 template < typename SpriteType >
0094 AnimSpritesList< SpriteType >::AnimSpritesList() : QList< SpriteType* >()
0095 {
0096 }
0097 
0098 template < typename SpriteType > 
0099   AnimSpritesList< SpriteType >::~AnimSpritesList()
0100 {
0101 }
0102 
0103 template < typename SpriteType >
0104 void AnimSpritesList< SpriteType >::hideAndRemoveAll()
0105 {
0106 //   qCDebug(KSIRK_LOG);
0107 
0108   while (!QList< SpriteType* >::empty())
0109   {
0110     hideAndRemoveFirst();
0111   }
0112 }
0113 
0114 template < typename SpriteType >
0115 void AnimSpritesList< SpriteType >::hideAndRemoveFirst()
0116 {   
0117     SpriteType* sprite = QList< SpriteType* >::front();
0118     QList< SpriteType* >::pop_front();
0119     sprite-> hide();
0120     sprite->deleteLater();
0121 }
0122 
0123 template < typename SpriteType >
0124 const SpriteType* AnimSpritesList< SpriteType >::firstThatIsLastFrame()
0125 {
0126 //    qDebug("AnimSpritesList< SpriteType >::firstThatIsLastFrame");
0127   typename AnimSpritesList< SpriteType >::iterator it;
0128   typename AnimSpritesList< SpriteType >::iterator it_end = QList< SpriteType* >::end();
0129   for ( it = QList< SpriteType* >::begin(); it != it_end; it++ )
0130   {
0131     if ((*it)-> isLastFrame()) return (*it);
0132   }
0133   return 0;
0134 }
0135 
0136 template < typename SpriteType >
0137 void AnimSpritesList< SpriteType >::moveAll()
0138 {
0139   typename AnimSpritesList< SpriteType >::iterator it, it_end;
0140   it = QList< SpriteType* >::begin();
0141   it_end = QList< SpriteType* >::end();
0142   while (it != it_end)
0143   {
0144     SpriteType* sp = (*it);
0145 
0146     const QPointF& destinationPoint = sp-> getDestination()-> pointFor(sp);
0147 
0148     if (((sp->x()) == (destinationPoint.x())) && ((sp-> y()) == (destinationPoint.y())))
0149     {
0150       sp-> hide();
0151       it = QList< SpriteType* >::erase(it);
0152       sp-> getDestination()-> incrNbArmies((*it)-> nbArmies());
0153       sp-> getDestination()-> createArmiesSprites();
0154       sp->deleteLater();
0155     }
0156     else it++;
0157   }
0158 }
0159 
0160 template < typename SpriteType >
0161 void AnimSpritesList< SpriteType >::moveAllToDestinationNow(bool clear)
0162 {
0163   qCDebug(KSIRK_LOG) << clear;
0164   typename AnimSpritesList< SpriteType >::iterator it, it_end;
0165   it = QList< SpriteType* >::begin();
0166   it_end = QList< SpriteType* >::end();
0167   while (it != it_end)
0168   {
0169     SpriteType* sp = (*it);
0170 
0171     const QPointF& destinationPoint = sp-> getDestinationPoint();
0172     sp->setPos(destinationPoint);
0173     sp->moveIt();
0174 
0175     if (clear)
0176     {
0177       sp-> hide();
0178       it = QList< SpriteType* >::erase(it);
0179       sp-> getDestination()-> incrNbArmies(((ArmySprite*)(*it))-> nbArmies());
0180       sp-> getDestination()-> createArmiesSprites();
0181     }
0182     else
0183     {
0184       it++;
0185     }
0186   }
0187 }
0188 
0189 template < typename SpriteType >
0190 void AnimSpritesList< SpriteType >::saveXmlAll(QTextStream& xmlStream)
0191 {
0192   foreach (SpriteType* sp, *this)
0193   {
0194     sp->saveXml(xmlStream);
0195   }
0196 }
0197 
0198 } // closing namespace Ksirk
0199 
0200 #endif // ANIMSPRITESLIST_H
0201