-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathitem.cpp
More file actions
48 lines (40 loc) · 1.31 KB
/
item.cpp
File metadata and controls
48 lines (40 loc) · 1.31 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
#include <math.h>
#include "item.hpp"
const int SPAWN_RADIUS = 150;
unsigned int num_stationary_items(std::vector<Item*>& items) {
unsigned int res = 0;
for (Item* it : items) {
if (!it->being_carried && !it->in_box) res++;
}
return res;
}
void spawn_item(std::vector<Item*>& items, std::vector<sf::Texture*> item_textures) {
if (num_stationary_items(items) < MAX_ITEMS) {
Item* item = new Item;
sf::Vector2f center(WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2);
float random_angle = float(rand() % 180) / M_PI;
int random_radius = rand() % SPAWN_RADIUS;
sf::Vector2f offset(
random_radius * cos(random_angle),
random_radius * sin(random_angle)
);
int random_item = int(rand() % item_textures.size());
item->sprite.setTexture(*item_textures[random_item]);
item->sprite.setOrigin(10,10);
item->sprite.setPosition(center + offset);
item->being_carried = false;
item->in_box = false;
items.push_back(item);
}
}
void remove_item(std::vector<Item*>& items, Item* item) {
size_t index = 0;
for (size_t i = 0; i < items.size(); ++i) {
if (items[i] == item) {
index = i;
break;
}
}
items.erase(items.begin() + index);
delete item;
}