void Game::addLevel( int yPos )
{
Level * level = new Level( this );
// add enemy to scene
for(int lol = 0; lol < 10; lol++)
{
Spaceship * spaceship = new Spaceship(x, yPos );
scene->addItem(spaceship);
x = x + 100;
// add the spaceship to the level
level->addSpaceship( spaceship );
}
connect( level, SIGNAL( endOfLine( int ) ), this, SLOT( onEndOfLine( int ) ) );
levels.push_back( level ); // "levels" is QVector< Level * >, a member of the Game class
}
void Game::onSpaceshipTimeout()
{
int nLevels = levels.size();
for ( int nLevel = 0; nLevel < nLevels; ++nLevel )
{
Level * level = levels[ nLevel ];
level->step();
}
}
void Level::step()
{
// Note that "direction" has the values 1 or -1 to indicate movement to the right or left, respectively
int nShips = spaceships.size();
bool bChangedDirection = false;
if ( direction > 0 )
{
if ( spaceships[ nShips - 1 ]->x() >= 950 )
{
direction = -1;
bChangedDirection = true;
}
}
else
{
if ( spaceships[ 0 ]->x() <= 50 )
{
direction = 1;
bChangedDirection = true;
}
}
int yPos = spaceships[ 0 ]->y();
if ( bChangedDirection ) // have to decrease y also now
yPos -= 10;
int distance = 10 * direction;
for ( nShip = 0; nShip < nShips; ++nShip )
{
Spaceship * spaceship = spaceships[ nShip ];
spaceship->setPos( spaceship->x() + distance, yPos );
}
if ( bChangedDirection )
emit endOfLine( yPos );
}