feat: Adding world::buildScene() method

This commit is contained in:
PolishPigeon 2022-01-24 11:59:08 +01:00
parent 15b5ad346d
commit f886ba36b8
10 changed files with 72 additions and 5 deletions

View File

@ -1,6 +1,9 @@
#ifndef WORLD_CPP // ZA WARUDO
#define WORLD_CPP
#include <cstddef> // std::size_t
#include "../SceneNodeDerrivatives/SpriteNode.hpp"
World::World(sf::RenderWindow& window)
: mWindow(window)
, mWorldView(window.getDefaultView())
@ -20,8 +23,8 @@ World::World(sf::RenderWindow& window)
, mPlayerAircraft(nullptr)
{
loadTextures();
// buildScene();
// mWorldView.setCenter(mSpawnPosition);
buildScene();
mWorldView.setCenter(mSpawnPosition);
}
void World::loadTextures()
@ -31,4 +34,39 @@ void World::loadTextures()
mTextures.load(Textures::Desert, PATH_TO_DESERT_TEXTURE);
}
void World::buildScene()
{
for (std::size_t i = 0; i < LayerCount; i++) // initialization of scene layers, iterate through array of layer node pointers
{
SceneNode::ScenePointer layer(new SceneNode());
mSceneLayers[i] = layer.get(); // initialize elments of this arry
mSceneGraph.attachChild(std::move(layer)); // attach new node to the scene graph's root node
}
sf::Texture& texture = mTextures.get(Textures::Desert);
sf::IntRect textureRect(mWorldBounds);
texture.setRepeated(true); // make desert texture repeat itself
std::unique_ptr<SpriteNode> backgroundSprite(new SpriteNode(texture, textureRect)); // SpriteNode class that links to the desrt texture, our sprite will be as big as the whole world because we passed the texture rectangle
backgroundSprite -> setPosition(mWorldBounds.left, mWorldBounds.top);
mSceneLayers[Background] -> attachChild(std::move(backgroundSprite));
// Adding airplanes
std::unique_ptr<Aircraft> leader(new Aircraft(Aircraft::Eagle, mTextures)); // we create the player's airplane
mPlayerAircraft = leader.get();
mPlayerAircraft -> setPosition(mSpawnPosition); // Set player position
mPlayerAircraft -> setVelocity(PLAYER_SIDEWARD_VELOCITY, mScrollSpeed); // forward velocity equals scroll speed, sideward velocity equals PLAYER_SIDEWARD_VELOCITY
mSceneLayers[Air] -> attachChild(std::move(leader)); // we attach the plane to the Air scene layer
std::unique_ptr<Aircraft> leftEscort(new Aircraft(Aircraft::Raptor, mTextures)); // create new airplane
leftEscort -> setPosition(LEFT_ESCORT_X_POSITION, LEFT_ESCORT_Y_POSITION); // Set new airplane position
mPlayerAircraft -> attachChild(std::move(leftEscort)); // leftEscort is now a child of player aircraft and it will folow it!
std::unique_ptr<Aircraft> rightEscort(new Aircraft(Aircraft::Raptor, mTextures)); // create new airplane
rightEscort -> setPosition(RIGHT_ESCORT_X_POSITION, RIGHT_ESCORT_Y_POSITION); // Set new airplane position
mPlayerAircraft -> attachChild(std::move(rightEscort)); // leftEscort is now a child of player aircraft and it will folow it!
}
#endif // WORLD_CPP

View File

@ -1,5 +1,19 @@
#ifndef SPRITE_NODE_CPP
#define SPRITE_NODE_CPP
SpriteNode::SpriteNode(const sf::Texture& texture)
: mSprite(texture)
{
}
SpriteNode::SpriteNode(const sf::Texture& texture, const sf::IntRect& textureRect)
: mSprite(texture, textureRect)
{
}
void SpriteNode::drawCurrent(sf::RenderTarget& target, sf::RenderStates states) const
{
target.draw(mSprite, states);
}
#endif // SPRITE_NODE_CPP

Binary file not shown.

After

Width:  |  Height:  |  Size: 152 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

Binary file not shown.

View File

@ -7,16 +7,24 @@
const std::string PATH_TO_PLAYER_TEXTURE = "Textures/Player.png";
const std::string PATH_TO_EAGLE_TEXTURE = "Textures/Eagle.png";
const std::string PATH_TO_RAPTOR_TEXTURE = "Textures/Raptor.png";
const std::string PATH_TO_DESERT_TEXTURE = "Textures/Desert.png";
const std::string PATH_TO_DESERT_TEXTURE = "Textures/Desert.jpg";
// Player constants
const float PLAYER_RADIUS = 40;
const float PLAYER_X_POSITION = 100;
const float PLAYER_Y_POSITION = 100;
const sf::Color PLAYER_COLOR = sf::Color::Cyan;
const float PLAYER_SIDEWARD_VELOCITY = 40;
// Other sprites constants
const float LEFT_ESCORT_X_POSITION = -80;
const float LEFT_ESCORT_Y_POSITION = 50;
const float RIGHT_ESCORT_X_POSITION = -LEFT_ESCORT_X_POSITION;
const float RIGHT_ESCORT_Y_POSITION = LEFT_ESCORT_Y_POSITION;
// Movement constants
// const sf::Vector2f INITIAL_MOVEMENT (0.f, 0.f);
const float MOVING_UP_SPEED = -100;
const float MOVING_DOWN_SPEED = -MOVING_UP_SPEED;
const float MOVING_RIGHT_SPEED = 100;

View File

@ -132,8 +132,15 @@ void Game::render()
int main()
{
Game game;
game.run();
try
{
Game game;
game.run();
}
catch (std::exception& e)
{
std::cout << "\nEXCEPTION: " << e.what() << std::endl;
}
}
#endif // GAME_CPP

Binary file not shown.