-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPowerUpDisplay.cpp
94 lines (73 loc) · 2.62 KB
/
PowerUpDisplay.cpp
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
/*
Copyright (c) 2017 InversePalindrome
Nihil - PowerUpDisplay.cpp
InversePalindrome.com
*/
#include "PowerUpDisplay.hpp"
#include "FilePaths.hpp"
#include "AnimationParser.hpp"
#include <fstream>
#include <sstream>
PowerUpDisplay::PowerUpDisplay(ResourceManager& resourceManager) :
resourceManager(resourceManager)
{
std::ifstream inFile(Path::miscellaneous / "PowerUpsDisplay.txt");
std::string line;
while (std::getline(inFile, line))
{
std::istringstream iStream(line);
std::size_t itemID = 0u;
int left = 0, top = 0, width = 0, height = 0;
std::string animationFile;
float scale = 0.f;
iStream >> itemID >> animationFile >> left >> top >> width >> height >> scale;
powerUpData.emplace(Item{ itemID }, PowerUpData{ animationFile, { left, top, width, height }, scale });
}
}
void PowerUpDisplay::update(float deltaTime)
{
for (auto& powerUpGraphic : this->powerUps)
{
powerUpGraphic.second.animator.update(sf::seconds(deltaTime));
powerUpGraphic.second.animator.animate(powerUpGraphic.second.sprite);
}
}
void PowerUpDisplay::addPowerUp(Item powerUp)
{
this->powerUps.emplace(powerUp, PowerUpGraphics(this->resourceManager, powerUp, this->powerUpData));
this->powerUps.at(powerUp).setPosition(
{ this->powerUps.size() * this->powerUps.at(powerUp).sprite.getGlobalBounds().width, 0.f });
}
void PowerUpDisplay::removePowerUp(Item powerUp)
{
this->powerUps.erase(powerUp);
}
void PowerUpDisplay::clearPowerUps()
{
this->powerUps.clear();
}
void PowerUpDisplay::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
states.transform = this->getTransform();
for (const auto& powerUpGraphic : this->powerUps)
{
target.draw(powerUpGraphic.second, states);
}
}
PowerUpGraphics::PowerUpGraphics(ResourceManager& resourceManager, Item item, const std::unordered_map<Item, PowerUpData>& powerUpData)
{
const auto& graphicData = powerUpData.at(item);
sprite.setTexture(resourceManager.getTexture(TexturesID::PowerUps));
sprite.setTextureRect(graphicData.textureRect);
sprite.setScale({ graphicData.scale, graphicData.scale });
const auto& animations = Parsers::parseAnimations("CoinAnimations.txt", animator);
if (!animations.empty())
{
animator.playAnimation(animations.cbegin()->first, true);
}
}
void PowerUpGraphics::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
states.transform *= this->getTransform();
target.draw(this->sprite, states);
}