[prev in list] [next in list] [prev in thread] [next in thread]
List: kde-commits
Subject: KDE/kdegames/kbreakout/src
From: Julian Helfferich <julian.helfferich () googlemail ! com>
Date: 2011-04-02 20:23:15
Message-ID: 20110402202315.F0CB9AC8CE () svn ! kde ! org
[Download RAW message or body]
SVN commit 1226892 by helfferich:
Including the possibility to place a gift at a certain position in a levelset. The \
position is specified as x,y where x is the column number (leftmost column is 1) and \
y the line number (topmost line is 1) of the brick. It can be specified in one of the \
following ways in a levelset file: <Gift Position="x,y">GiftType</Gift>
<Gift Type="GiftType">x,y</Gift> or
<Gift>
<Type>GiftType</Type>
<Position>x,y</Position>
</Gift>
M +1 -0 brick.h
M +1 -1 gameengine.cpp
M +60 -2 levelloader.cpp
M +3 -0 levelloader.h
--- trunk/KDE/kdegames/kbreakout/src/brick.h #1226891:1226892
@@ -41,6 +41,7 @@
bool isDeleted() {return m_deleted;}
bool hasGift() const;
void setGift(Gift *);
+ Gift *gift() {return m_gift;}
// bricks to the left, right top and bottom
QList<Brick *> nearbyBricks();
--- trunk/KDE/kdegames/kbreakout/src/gameengine.cpp #1226891:1226892
@@ -51,6 +51,7 @@
m_bar_ptr = &m_bar;
+ qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
m_levelLoader = new LevelLoader( this );
}
@@ -78,7 +79,6 @@
setScore(0);
loadLevel();
- qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
m_elapsedTimeTimer.start();
m_gameTimer.start();
--- trunk/KDE/kdegames/kbreakout/src/levelloader.cpp #1226891:1226892
@@ -224,6 +224,7 @@
giftType = attribute.value();
} else if( !attributeNode.isNull() ){
giftType = attributeNode.text();
+ nodeTextRead = true;
} else {
giftType = giftNode.text();
nodeTextRead = true;
@@ -238,15 +239,51 @@
times = attribute.value().toInt( &ok );
} else if( !attributeNode.isNull() ){
times = attributeNode.text().toInt( &ok );
+ nodeTextRead = true;
} else if( !nodeTextRead ){
times = giftNode.text().toInt( &ok );
if( !ok ){ times = 1; }
}
+
if( bricksLeft.count() < times ){
kError() << "Invalid levelset " << m_levelname << ": In Level " << m_level
<< " are too many gifts of type " << giftType << endl;
}
- // Distribute gifts
+
+ // If only one brick to be placed: see if position is given
+ QPoint position;
+ if( times == 1 ){
+ attribute = giftNode.attributeNode("Position");
+ attributeNode = giftNode.firstChildElement("Position");
+ if( !attribute.isNull() ){
+ position = positionFromString( attribute.value() );
+ } else if( !attributeNode.isNull() ){
+ position = positionFromString( attributeNode.text() );
+ nodeTextRead = true;
+ } else if( !nodeTextRead && giftNode.text().contains(',') ){
+ position = positionFromString( giftNode.text() );
+ nodeTextRead = true;
+ }
+ }
+
+ if( !position.isNull() ){
+ // Put gift at given position
+ Brick *giftBrick = brickAt( position, bricks );
+ if( giftBrick == 0 ){
+ kError() << "Invalid levelset " << m_levelname << ": Can't place gift at \
position (" + << position.x() << "," << position.y() << "). There \
is no brick.\n"; + } else {
+ if( giftBrick->hasGift() ){
+ // Brick already has a gift -> move this gift to a random position
+ int index = qrand() % bricksLeft.count();
+ bricksLeft.at(index)->setGift( giftBrick->gift() );
+ }
+ Gift *newgift = new Gift(giftType);
+ newgift->hide();
+ giftBrick->setGift(newgift);
+ }
+ } else {
+ // Distribute gifts randomly
for( int i = 0; i < times; i++ ){
Gift *gift = new Gift( giftType );
gift->hide();
@@ -256,6 +293,7 @@
bricksLeft.removeAt(index);
}
}
+}
void LevelLoader::loadOldStyleLevel ( QList< Brick* >& m_bricks )
{
@@ -367,4 +405,24 @@
<< type << "'\n";
return "PlainBrick1";
}
-}
\ No newline at end of file
+}
+
+Brick *LevelLoader::brickAt( const QPoint& position, QList<Brick *> &bricks )
+{
+ Brick *result = 0;
+ foreach( Brick *testbrick, bricks ){
+ if( testbrick->position().x() / BRICK_WIDTH + 1 == position.x()
+ && testbrick->position().y() / BRICK_HEIGHT + 1 == position.y() ){
+ result = testbrick;
+ break;
+ }
+ }
+ return result;
+}
+
+QPoint LevelLoader::positionFromString(const QString& posString)
+{
+ int seperatorPosition = posString.indexOf(',');
+ int length = posString.length();
+ return QPoint( posString.left(seperatorPosition).toInt(), \
posString.right(length-seperatorPosition-1).toInt() ); +}
--- trunk/KDE/kdegames/kbreakout/src/levelloader.h #1226891:1226892
@@ -23,6 +23,7 @@
class QDomDocument;
class QDomElement;
+class QPoint;
class Brick;
class GameEngine;
@@ -44,6 +45,8 @@
void loadOldStyleLevel( QList<Brick *> &m_bricks );
static QString getTypeFromChar(char type);
+ Brick *brickAt( const QPoint& position, QList<Brick *> &bricks );
+ QPoint positionFromString( const QString& posString );
QDomDocument *m_levelset;
bool m_oldstyle;
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic