-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWorld.cpp
More file actions
106 lines (84 loc) · 2.95 KB
/
World.cpp
File metadata and controls
106 lines (84 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include "World.hpp"
#include <iostream>
#include <string>
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include "TextureHolder.hpp"
#include "SpriteNode.hpp"
using namespace std;
using namespace sf;
World::World(RenderWindow& window)
: mWindow(window)
, mWorldView(window.getDefaultView())
, mTextures()
, mSceneGraph()
, mSceneLayers()
, mWorldBounds(
0.f, //Left
0.f, //Top
mWorldView.getSize().x, //Width
2000.f //Height
)
, mSpawPosition(mWorldView.getSize().x/2.f,mWorldBounds.height-mWorldView.getSize().y/2.f)
, mScrollSpeed(-50.f) //Here the speed is -ve because y axis is facing in downward direction
, mPlayerAircraft(nullptr)
{
loadTexture();
buildScene();
mWorldView.setCenter(mSpawPosition);
}
void World::loadTexture()
{
mTextures.load(Textures::Eagle ,"C:/Users/Siddhant/Desktop/Eagle.png");
mTextures.load(Textures::Raptor ,"C:/Users/Siddhant/Desktop/Raptor.png");
mTextures.load(Textures::Desert ,"C:/Users/Siddhant/Desktop/Desert.png");
}
void World::buildScene()
{
// Initialize the different layers
for (int i = 0; i < Layercount; ++i)
{
unique_ptr<SceneNode> layer(new SceneNode());
mSceneLayers[i] = layer.get();
mSceneGraph.attachChild(std::move(layer));
}
Texture& textures=mTextures.get(Textures::Desert);
textures.setRepeated(true);
IntRect textureRect(mWorldBounds);
unique_ptr<SpriteNode> BackGroundSprite(new SpriteNode(textures,textureRect));
BackGroundSprite->setPosition(mWorldBounds.left,mWorldBounds.top);
mSceneLayers[Background]->attachChild(std::move(BackGroundSprite));
unique_ptr<Aircraft> leader(new Aircraft(Aircraft::Eagle,mTextures));
mPlayerAircraft=leader.get();
mPlayerAircraft->setPosition(mSpawPosition);
mPlayerAircraft->SetVelocity(40.f,mScrollSpeed);
mSceneLayers[Air]->attachChild(std::move(leader));
unique_ptr<Aircraft> leftEscort(new Aircraft(Aircraft::Raptor,mTextures));
leftEscort->setPosition(-80.f,50.f);
mPlayerAircraft->attachChild(std::move(leftEscort));
std::unique_ptr<Aircraft> rightEscort(new Aircraft(Aircraft::Raptor, mTextures));
rightEscort->setPosition(80.f, 50.f);
mPlayerAircraft->attachChild(std::move(rightEscort));
}
void World::draw()
{
mWindow.setView(mWorldView);
mWindow.draw(mSceneGraph);
}
void World::update(sf::Time dt)
{
// Scroll the world
mWorldView.move(0.f, mScrollSpeed * dt.asSeconds());
// Move the player sidewards (plane scouts follow the main aircraft)
sf::Vector2f position = mPlayerAircraft->getPosition();
sf::Vector2f velocity = mPlayerAircraft->getVelocity();
// If player touches borders, flip its X velocity
if (position.x <= mWorldBounds.left + 150.f
|| position.x >= mWorldBounds.left + mWorldBounds.width - 150.f)
{
velocity.x = -velocity.x;
mPlayerAircraft->SetVelocity(velocity);
}
// Apply movements
mSceneGraph.update(dt);
}