bubble_field.h

Go to the documentation of this file.
00001 
00014 #ifndef _BUBBLE_FIELD_H_
00015 #define _BUBBLE_FIELD_H_
00016 
00017 #include <cmath>
00018 #include <list>
00019 #include <vector>
00020 
00021 #include "bubble.h"
00022 #include "bubble_generator.h"
00023 #include "time_updateable.h"
00024 #include "templates/callable_back.h"
00025 
00033 class BubbleField: public TimeUpdateable, public GraphicalNode,
00034         public Templates::CallableBack<Bubble>
00035 {
00036         private:
00037 
00041                 class Coordinates
00042                 {
00043                         public:
00044 
00046                                 unsigned plate;
00048                                 unsigned x;
00050                                 unsigned y;
00051 
00052                         public:
00053 
00055                                 Coordinates(unsigned _plate, unsigned _x, unsigned _y):
00056                                         plate(_plate), x(_x), y(_y)
00057                                 {
00058                                 }
00059 
00071                                 bool operator < (const Coordinates& rhs) const
00072                                 {
00073                                         if (plate == rhs.plate) {
00074                                                 if (x == rhs.x) {
00075                                                         return y < rhs.y;
00076                                                 } else {
00077                                                         return x < rhs.x;
00078                                                 }
00079                                         } else {
00080                                                 return plate < rhs.plate;
00081                                         }
00082                                 }
00083 
00089                                 bool operator ==  (const Coordinates& rhs) const
00090                                 {
00091                                         return plate == rhs.plate && x == rhs.x && y == rhs.y;
00092                                 }
00093 
00099                                 bool operator !=  (const Coordinates& rhs) const
00100                                 {
00101                                         return !(*this == rhs);
00102                                 }
00103                 };
00104 
00108                 class isInvalidPosition
00109                 {
00110                         private:
00111 
00113                                 const BubbleField& _bubbleField;
00114 
00115                         public:
00116 
00118                                 isInvalidPosition(const BubbleField& bubbleField):
00119                                         _bubbleField(bubbleField)
00120                                 {
00121                                 }
00122 
00124                                 bool operator() (const Coordinates& coords)
00125                                 {
00126                                         return !_bubbleField.isValidPosition(coords.plate, coords.x, coords.y);
00127                                 }
00128 
00129                 };
00130 
00135                 class FallingBubblesStorage: public TimeUpdateable, public GraphicalNode
00136                 {
00137                         private:
00138 
00140                                 typedef std::list<Bubble *> StorageType;
00141 
00143                                 StorageType _storage;
00144 
00145                         public:
00146 
00147                                 FallingBubblesStorage();
00148 
00149                                 ~FallingBubblesStorage();
00150 
00151                                 // Overrides
00152                                 virtual void timeUpdate(float dt);
00153 
00159                                 void addBubble(Bubble* bubble);
00160 
00161                 };
00162 
00163         private:
00164 
00166                 static const unsigned BUBBLES_IN_ROW;
00167 
00169                 typedef Bubble*** BubblePlate;
00170                 typedef std::vector<BubblePlate> FieldContent;
00171 
00173                 FieldContent _content;
00174 
00177                 Templates::CallableBack<BubbleField>* _addBubbleDone;
00178 
00180                 std::list<Bubble *> _explodingBubbles;
00181 
00183                 int _explodingBubblesCount;
00184 
00186                 FallingBubblesStorage _fallingBubbles;
00187 
00189                 void (*_successGameEndFunc)();
00190 
00191         private:
00192 
00193                 // Disable copy constructor and assignment operator
00194                 BubbleField(const BubbleField &);
00195                 BubbleField & operator=(const BubbleField &);
00196 
00202                 unsigned bubblesInPlateRow(unsigned index) const;
00203 
00211                 bool isValidPosition(unsigned plate, unsigned x, unsigned y) const;
00212 
00218                 static bool isInCollisionWithWall(const SbVec3f& position);
00219 
00226                 bool bubbleCollidesInPlate(unsigned plate, const SbVec3f& position) const;
00227 
00235                 void addCollidingBubblesInPlateToVector(unsigned plate, const Bubble* bubble,
00236                                 std::vector<const Bubble*>& collidingBubbles) const;
00237 
00243                 void solveCollisionWithWall(Bubble* bubble);
00244 
00250                 void solveCollisionWithBubble(Bubble* bubble);
00251 
00257                 void destroyBubblesInCluster(const Coordinates& position);
00258 
00262                 void removeBubblesNotConnectedWithWall();
00263 
00269                 Coordinates searchForPosition(Bubble* bubble) const;
00270 
00278                 std::vector<const Bubble*> getCollidingBubbles(Bubble* bubble) const;
00279 
00286                 std::list<Coordinates> getNeighboursCoords(const Coordinates& coords) const;
00287 
00298                 void addBubble(Bubble* bubble, unsigned plate, unsigned x, unsigned y);
00299 
00307                 void addNeighboursTowardsWall(const BubbleGenerator* bubbleGen,
00308                         const Coordinates& coords);
00309 
00319                 bool isConnectedWithWall(const Coordinates& coords,
00320                                 std::vector<Coordinates> processedCoords,
00321                                 Coordinates& nearestToWallCoords) const;
00322 
00331                 bool isConnectedWithWall(const Coordinates& coords) const;
00332 
00343                 static unsigned oiXYCoordinateToLocal(float oiCoord, unsigned plate,
00344                                 float (*roundFunc)(float) = roundf);
00345 
00353                 static unsigned oiZCoordinateToLocal(float oiCoord, float (*roundFunc)(float) = roundf);
00354 
00363                 static SbVec3f localCoordinatesToOI(unsigned plate, unsigned x, unsigned y);
00364 
00374                 static SbVec3f localCoordinatesToOI(Coordinates local);
00375 
00376         public:
00377 
00381                 BubbleField();
00382 
00386                 ~BubbleField();
00387 
00397                 void setOnSuccessfullGameEndCallback(void (*callback)());
00398 
00410                 void generateStartingLayout(const BubbleGenerator* bubbleGen,
00411                         unsigned numOfInitiallyFilledPlates, float probCellContainBubble);
00412 
00425                 void addBubble(Bubble* bubble, Templates::CallableBack<BubbleField>* addBubbleDone);
00426 
00436                 bool isInCollision(const SbVec3f& position);
00437 
00441                 static unsigned getNumberOfBubblesInRow() { return BUBBLES_IN_ROW; }
00442 
00443                 // Override
00444                 virtual void timeUpdate(float dt);
00445 
00446                 // Override
00447                 virtual void callableBackNotify(Bubble *subject);
00448 };
00449 
00450 #endif

Generated on Sat Dec 20 19:21:24 2008 for PGR2008 by  doxygen 1.5.6