-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoin.cpp
More file actions
50 lines (42 loc) · 1.39 KB
/
coin.cpp
File metadata and controls
50 lines (42 loc) · 1.39 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
#include "coin.hpp"
#include <iostream>
// Initialize static data
sf::Texture Coin::texture;
Coin::Coin(float x, float y) : Entity(texture)
{
auto success = texture.loadFromFile("./textures/coinsheet1.png");
if (success)
{
sprite.setTexture(texture, true);
sprite.setTextureRect(sf::IntRect(
sf::Vector2i(0, 0),
sf::Vector2i(Constants::coinPixelWidth, Constants::coinPixelHeight)));
sprite.setPosition(sf::Vector2f{x, y});
}
}
void Coin::draw(sf::RenderWindow &window)
{
window.draw(sprite);
}
void Coin::update()
{
const int coinSpriteIndexStart = 0;
const float coinFramesCount = 6.f;
const float animationSpeed = 1.f;
// Scale seconds by animationspeed
float seconds = SharedResources::Instance().gameClock.getElapsedTime().asSeconds() * animationSpeed;
// Only use fractional part
float onlyFractionalPart = (seconds - (long)seconds);
// Animate through number of frames
int spriteIndex = static_cast<int>(onlyFractionalPart * coinFramesCount);
int coinSpriteIndex = spriteIndex + coinSpriteIndexStart;
sprite.setTextureRect(
sf::IntRect(
sf::Vector2i((coinSpriteIndex * Constants::coinPixelWidth), 0),
sf::Vector2i(Constants::coinPixelWidth, Constants::coinPixelHeight))
);
}
void Coin::playCoinSound() noexcept
{
SharedResources::Instance().playCoinSound();
}