World::World(sf::RenderTarget& target, FontHolder& fonts, SoundPlayer& sounds) : mTarget(target) , mFonts(fonts) , mSounds(sounds) , mWorldView(target.getDefaultView()) , mImages() , mTextures() , mSceneGraph() , mSceneLayers() , mWorldBounds(mWorldView.getCenter() - mWorldView.getSize() / 2.f, mWorldView.getSize()) , mSpawnPosition(mWorldView.getSize().x / 2.f, mWorldView.getSize().y / 2.f) , mBossFactory(mTextures, mWorldBounds) , mPlayerFactory(mTextures) , mPlayer(nullptr) , mBoss(nullptr) , mLife(nullptr) , mScore(nullptr) , mQuadTreePrimary(1, mWorldBounds) , mQuadTreeSecondary(1, mWorldBounds) , mPlayerBulletNodes() , mEnemyBulletNodes() , mInvaders() , mLivesCount(3) , mChangeSpeed(false) , mDeadLine(getBattlefieldBounds().height + getBattlefieldBounds().top - Padding / 2.f) , mIsGameEnded(false) { loadTextures(); buildScene(); // Prepare the view mWorldView.setCenter(mSpawnPosition); }
void World::destroyEntitiesOutsideView() { Command command; command.category = Category::Planet | Category::Sattelite; command.action = derivedAction<Entity>([this](Entity& e, sf::Time) { sf::FloatRect boundingRect = e.getBoundingRect()[0]; bool isNeedDelete = true; if (!getBattlefieldBounds().intersects(boundingRect) && e.getHitpoints() > 0) { const std::vector<SceneNode::Ptr>& sattelites = e.getChilds(); for (int satteliteCounter = 0; satteliteCounter < sattelites.size(); ++satteliteCounter) { if (getBattlefieldBounds().intersects(sattelites[satteliteCounter]->getBoundingRect()[0])) { isNeedDelete = false; } } if (isNeedDelete) { e.destroy(); } } }); mCommandQueue.push(command); }
void World::destroyEntitiesOutsideView() { Command command; command.category = Category::Projectile | Category::EnemyAircraft; command.action = derivedAction<Entity>([this] (Entity& e, sf::Time) { if (!getBattlefieldBounds().intersects(e.getBoundingRect())) e.remove(); }); mCommandQueue.push(command); }
void World::destroyEntitiesOutsideView() { Command command; command.category = Category::Projectile; command.action = derivedAction<Entity>([this](auto& entity) { if (!getBattlefieldBounds().intersects(entity.getBoundingRect())) entity.remove(); }); mCommandQueue.push(command); }
void World::spawnEnemies() { while (!mPlanetSpawnPoints.empty() && mPlanetSpawnPoints.back().y > getBattlefieldBounds().top + 300) { SpawnPoint spawn = mPlanetSpawnPoints.back(); std::unique_ptr<Planet> planet(new Planet(spawn.type, mTextures)); planet->setPosition(spawn.x, spawn.y); mSceneLayers[Space]->attachChild(std::move(planet)); mPlanetSpawnPoints.pop_back(); } }
void World::buildScene() { // Initialize the different layers for (std::size_t i = 0; i < LayerCount; ++i) { SceneNode::Ptr layer(new SceneNode()); mSceneLayers[i] = layer.get(); mSceneGraph.attachChild(std::move(layer)); } // Prepare the tiled background sf::Texture& texture = mTextures.get(Textures::Background); sf::IntRect textureRect(mWorldBounds); texture.setRepeated(true); // Add the background sprite to the scene std::unique_ptr<SpriteNode> backgroundSprite(new SpriteNode(texture, textureRect)); backgroundSprite->setPosition(mWorldBounds.left, mWorldBounds.top); mBackgroundSprite = backgroundSprite.get(); mSceneLayers[Background]->attachChild(std::move(backgroundSprite)); // Add player's spaceship std::unique_ptr<Spaceship> leader(new Spaceship(Spaceship::Rocket, mTextures)); mPlayerSpaceship = leader.get(); mPlayerSpaceship->setPosition(mSpawnPosition); mSceneLayers[Space]->attachChild(std::move(leader)); mBattlifieldBounds.setSize(sf::Vector2f(getBattlefieldBounds().width, getBattlefieldBounds().height)); mBattlifieldBounds.setPosition(getBattlefieldBounds().left, getBattlefieldBounds().top); mBattlifieldBounds.setFillColor(sf::Color::Transparent); mBattlifieldBounds.setOutlineThickness(2.f); mBattlifieldBounds.setOutlineColor(sf::Color::Green); initialText(); }
void World::update(sf::Time dt) { // Scroll the world, reset player velocity mWorldView.move(0.f, mScrollSpeed * dt.asSeconds()); mPlayerSpaceship->setVelocity(0.f, 0.f); // Check for delete enemies destroyEntitiesOutsideView(); // Forward commands to scene graph, adapt velocity (scrolling, diagonal correction) while (!mCommandQueue.isEmpty()) mSceneGraph.onCommand(mCommandQueue.pop(), dt); adaptPlayerVelocity(); // Handle our collision. handleCollisions(); // Remove all destroyed entities, and create new. mSceneGraph.removeWrecks(); // Templates for add new Enemies. addNewEnemies(dt); spawnEnemies(); // Check, for edding new world texture. updateWorldBounds(); // Regular update step, adapt position (correct if outside view) mSceneGraph.update(dt, mCommandQueue); adaptPlayerPosition(); // Update Points and Health. updateText(dt); mBattlifieldBounds.setPosition(getBattlefieldBounds().left, getBattlefieldBounds().top); // Update ScrollSpeed mScrollSpeed -= ScrollSpeedAccelerate * dt.asSeconds(); }
void World::spawnEnemies() { // Spawn all enemies entering the view area (including distance) this frame while (!mEnemySpawnPoints.empty() && mEnemySpawnPoints.back().y > getBattlefieldBounds().top) { SpawnPoint spawn = mEnemySpawnPoints.back(); std::unique_ptr<Aircraft> enemy(new Aircraft(spawn.type, mTextures, mFonts)); enemy->setPosition(spawn.x, spawn.y); enemy->setRotation(180.f); if (mNetworkedWorld) enemy->disablePickups(); mSceneLayers[UpperAir]->attachChild(std::move(enemy)); // Enemy is spawned, remove from the list to spawn mEnemySpawnPoints.pop_back(); } }
void World::spawnEnemies() { // TODO: wrap up condition into a bool method while ( !mEnemySpawnPoints.empty() && mEnemySpawnPoints.back().y > getBattlefieldBounds().top ) { SpawnPoint spawn = mEnemySpawnPoints.back(); std::unique_ptr<Aircraft> enemy( new Aircraft( spawn.type, mTextures, mFonts ) ); enemy->setPosition( spawn.x, spawn.y ); enemy->setRotation( 180.0f ); if ( mNetworkedWorld ) { enemy->DisablePickups(); } mSceneLayers[UpperAir]->AttachChild( std::move( enemy ) ); mEnemySpawnPoints.pop_back(); } }