-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCItemsManager.cpp
114 lines (94 loc) · 2.55 KB
/
CItemsManager.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include "CApplication.h"
#include "CItemsManager.h"
extern CApplication Game;
/* CLASS: ITEM */
CItem::CItem(const std::string& type, sf::Texture& itemTexture)
: m_Type(type)
, m_Texture(itemTexture)
{
//std::cout << "Created item! Type: " << m_Type << std::endl;
element.setTexture(itemTexture);
if(m_Type == "Health")
b_Rotating = false;
}
/* CLASS: ITEM MANAGER */
CItemsManager::CItemsManager()
{
m_Items.reserve(MAX_ALLOWED_ITEMS);
}
CItemsManager::~CItemsManager()
{
}
std::string CItemsManager::getRandomItem() {
unsigned int i = rand() % 5 + 1;
switch(i) {
case 5:
return "Health";
break;
default:
return "Rock";
break;
}
}
void CItemsManager::generate(std::string type) {
if(m_Items.size() >= MAX_ALLOWED_ITEMS || m_LastItem.getElapsedTime().asSeconds() < MIN_GENERATE_WAIT)
return;
if(type == "none")
type = getRandomItem();
CItem item(type, Game.m_Textures[type]);
float range_y = Game.m_Window->getSize().y - item.get().getGlobalBounds().height;
item.get().setPosition((float)Game.m_Window->getSize().x, (float)(rand() % (int)range_y) + (item.get().getGlobalBounds().height));
item.get().setOrigin(item.get().getGlobalBounds().width / 2, item.get().getGlobalBounds().height / 2);
if(type == "Rock")
item.get().setRotation((float)(rand() % 180 + 0));
m_Items.push_back(item);
m_LastItem.restart();
}
void CItemsManager::update(float& dt) {
generate();
std::vector<CItem>::iterator it;
for(it = m_Items.begin(); it != m_Items.end();) {
if(it->get().getPosition().x + it->get().getGlobalBounds().width < 0.0f) {
it = m_Items.erase(it);
}
else {
move(dt, (*it));
it++;
}
}
}
void CItemsManager::move(float& dt, CItem& item) {
item.m_Velocity.x = item.m_Units * dt;
if(item.b_Rotating) {
float angle = dt * 180;
item.get().rotate(angle);
}
item.get().move(item.m_Velocity.x, 0);
//std::cout << "Moving some rock instance to X: " << (rock.getPosition().x + m_Velocity.x) << std::endl;
}
void CItemsManager::draw() {
std::vector<CItem>::iterator it;
for(it = m_Items.begin(); it != m_Items.end(); it++) {
Game.m_Window->draw(it->get());
}
}
void CItemsManager::clear() {
m_Items.clear();
m_LastItem.restart();
}
std::string CItemsManager::collide(sf::Sprite& element, bool destroy) {
std::vector<CItem>::iterator it;
for(it = m_Items.begin(); it != m_Items.end();) {
if(it->get().getGlobalBounds().intersects(element.getGlobalBounds())) {
std::string type = it->m_Type;
if(destroy) {
it = m_Items.erase(it);
} else {
it++;
}
return type;
} else
it++;
}
return "none";
}