Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
SFML Game Development.pdf
Скачиваний:
194
Добавлен:
28.03.2016
Размер:
4.19 Mб
Скачать

Forge of the Gods – Shaping Our World

But if we handle exceptions nowhere, the program will crash. We put an exception handler into the main() function, giving the user a meaningful error description:

int main()

{

try

{

Game game; game.run();

}

catch (std::exception& e)

{

std::cout << "\nEXCEPTION: " << e.what() << std::endl;

}

}

Building the scene

Now comes the interesting part: building up the scene, which is done in the World::buildScene() method. First, we initialize the different scene layers. We iterate through our array of layer node pointers and initialize each element. std::unique_ptr::get() returns a raw pointer to the stored object, we do not transfer ownership to the array. Finally, we attach the new node to the scene graph's root node as shown in the following code:

for (std::size_t i = 0; i < LayerCount; ++i)

{

SceneNode::Ptr layer(new SceneNode()); mSceneLayers[i] = layer.get();

mSceneGraph.attachChild(std::move(layer));

}

After the background texture for the desert is loaded, we configure it to repeat itself. We also create a texture rect (with int coordinates) using the conversion from the world bounds (which have the type sf::FloatRect and thus store float coordinates).

sf::Texture& texture = mTextures.get(Textures::Desert); sf::IntRect textureRect(mWorldBounds); texture.setRepeated(true);

[ 78 ]

www.it-ebooks.info

Chapter 3

Then, we create our SpriteNode class that links to the desert texture. We pass to its constructor the texture rectangle. This grants that our sprite is as big as the whole world. While the desert image is only one screen wide, it is tiled along the whole level in -y direction.

std::unique_ptr<SpriteNode> backgroundSprite( new SpriteNode(texture, textureRect));

backgroundSprite->setPosition( mWorldBounds.left, mWorldBounds.top);

mSceneLayers[Background] ->attachChild(std::move(backgroundSprite));

Finally, we have come to the point where we add the airplanes! First, we create the player's airplane. We set the world's pointer mPlayerAircraft to the newly created scene node. Then, we specify an initial position (the spawn position) and velocity.

The forward velocity equals the scroll speed. We also introduce a sideward velocity with value 40, to show a more interesting movement pattern. At last, we attach the plane to the Air scene layer.

std::unique_ptr<Aircraft> leader(

new Aircraft(Aircraft::Eagle, mTextures)); mPlayerAircraft = leader.get(); mPlayerAircraft->setPosition(mSpawnPosition); mPlayerAircraft->setVelocity(40.f, mScrollSpeed); mSceneLayers[Air]->attachChild(std::move(leader));

Since single planes are boring, we give our leader an escort of two other airplanes, one on each side. After construction, we set a position and attach the escort to the leader's scene node. The specified position is thus interpreted relative to the leader. In the following example, the first escort plane is located 80 units to the left and 50 units behind the leader:

std::unique_ptr<Aircraft> leftEscort(

new Aircraft(Aircraft::Raptor, mTextures)); leftEscort->setPosition(-80.f, 50.f); mPlayerAircraft->attachChild(std::move(leftEscort));

For the second escort, the code is almost the same, except that the relative x position is now positive, meaning that this plane is located to the right.

std::unique_ptr<Aircraft> rightEscort(

new Aircraft(Aircraft::Raptor, mTextures)); rightEscort->setPosition(80.f, 50.f); mPlayerAircraft->attachChild(std::move(rightEscort));

[ 79 ]

www.it-ebooks.info

Forge of the Gods – Shaping Our World

That was it, now our scene is ready! Now, our scene graph looks as shown in the following diagram. Below the description of each node, you see the most derived class type.

Scene Graph Root

 

[SceneNode]

 

 

Layer: Background

Layer: Air

[SceneNode]

[SceneNode]

Desert

 

 

[SpriteNode]

Leader Aircraft

 

 

[Aircraft]

Escort Aircraft 1

Escort Aircraft 2

[Aircraft]

 

[Aircraft]

Update and draw

The update() and draw() methods bring the encapsulated scene graph functionality to the API of the World class.

The function to draw sets the current view and delegates the work to the scene graph:

void World::draw()

{

mWindow.setView(mWorldView);

mWindow.draw(mSceneGraph);

}

[ 80 ]

www.it-ebooks.info

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]