-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasteroid.cpp
More file actions
124 lines (98 loc) · 3.08 KB
/
Copy pathasteroid.cpp
File metadata and controls
124 lines (98 loc) · 3.08 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include "asteroid.hpp"
#include <map>
#include <array>
#include <SFML/Graphics.hpp>
#include "entity.hpp"
#include "constants.hpp"
const float Asteroid::ASTEROID_LIFE = 1.0f;
const std::map<Asteroid::Size, float> Asteroid::ASTEROID_SPEED_MAP = Asteroid::setSpeedMap();
const std::map<Asteroid::Size, std::array<sf::Texture, 8>> Asteroid::ASTEROID_TEXTURE_MAP = Asteroid::setTextureMap();
Asteroid::Asteroid() :
Entity(sf::Vector2f(0.0f, static_cast<float>(rand() % HEIGHT)),
static_cast<float>(rand() % 360), ASTEROID_LIFE),
size_(Size::BIG)
{
speed_ = ASTEROID_SPEED_MAP.at(size_);
sprite_.setTexture(ASTEROID_TEXTURE_MAP.at(size_).at(rand() % 7));
sprite_.setPosition(position_);
sprite_.setOrigin((sprite_.getLocalBounds().width / 2.0f), (sprite_.getLocalBounds().height / 2.0f));
}
Asteroid::Asteroid(sf::Vector2f position, float angle, Asteroid::Size size) :
Entity (position, angle, 1.0f),
size_(size)
{
speed_ = ASTEROID_SPEED_MAP.at(size_);
sprite_.setTexture(ASTEROID_TEXTURE_MAP.at(size_).at(rand() % 7));
sprite_.setPosition(position_);
sprite_.setOrigin((sprite_.getLocalBounds().width / 2.0f), (sprite_.getLocalBounds().height / 2.0f));
}
Asteroid::Size Asteroid::getSize()
{
return size_;
}
void Asteroid::update(sf::Time deltaTime)
{
sf::Vector2f distance = direction_ * deltaTime.asSeconds() * speed_;
position_.x += distance.x;
position_.y += distance.y;
float radius = sprite_.getLocalBounds().width / 2.5;
if (position_.x + radius < 0.0f)
{
position_.x = static_cast<float>(WIDTH) + radius;
}
else if (position_.x - radius > static_cast<float>(WIDTH))
{
position_.x = 0.0f - radius;
}
if (position_.y + radius < 0.0f)
{
position_.y = static_cast<float>(HEIGHT) + radius;
}
else if (position_.y - radius > static_cast<float>(HEIGHT))
{
position_.y = 0.0f - radius;
}
sprite_.setPosition(position_);
sprite_.rotate(0.01f + deltaTime.asSeconds());
}
std::map<Asteroid::Size, float> Asteroid::setSpeedMap()
{
std::map<Asteroid::Size, float> map;
map[Asteroid::Size::BIG] = 40.0f;
map[Asteroid::Size::MEDIUM] = 60.0f;
map[Asteroid::Size::SMALL] = 80.0f;
return map;
}
std::map<Asteroid::Size, std::array<sf::Texture, 8>> Asteroid::setTextureMap()
{
std::string path;
std::array<sf::Texture, 8> bigArray;
for (int i = 0; i < 8; ++i)
{
path = "../images/big";
path.append(std::to_string(i));
path.append(".png");
bigArray[i] = Entity::loadTexture(path);
}
std::array<sf::Texture, 8> mediumArray;
for (int i = 0; i < 8; ++i)
{
path = "../images/medium";
path.append(std::to_string(i));
path.append(".png");
mediumArray[i] = Entity::loadTexture(path);
}
std::array<sf::Texture, 8> smallArray;
for (int i = 0; i < 8; ++i)
{
path = "../images/small";
path.append(std::to_string(i));
path.append(".png");
smallArray[i] = Entity::loadTexture(path);
}
std::map<Asteroid::Size, std::array<sf::Texture, 8>> map;
map[Asteroid::Size::BIG] = bigArray;
map[Asteroid::Size::MEDIUM] = mediumArray;
map[Asteroid::Size::SMALL] = smallArray;
return map;
}