Repository navigation
Expand file tree
/
Copy pathinventory.cpp
More file actions
204 lines (167 loc) · 6.17 KB
/
Copy pathinventory.cpp
File metadata and controls
204 lines (167 loc) · 6.17 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include "inventory.hpp"
namespace tol {
void Inventory::draw(sf::RenderTarget& target, sf::RenderStates state) const {
sf::Vector2f target_size(
{ std::min(static_cast<float>(target.getSize().x), 1000.f), static_cast<float>(target.getSize().y) });
sf::FloatRect inventory_dims(
(static_cast<float>(target.getSize().x) - target_size.x) / 2, target_size.y * 0.1f, target_size.x,
target_size.y * 0.85f);
sf::FloatRect detail(inventory_dims.left, inventory_dims.top, inventory_dims.width * 0.29f, inventory_dims.height);
sf::FloatRect objects(
detail.left + inventory_dims.width * 0.3f, inventory_dims.top, inventory_dims.width * 0.7f, inventory_dims.height);
const auto font = *asset_cache->load_font("fonts/Gaegu-Regular.ttf");
sf::Text header;
header.setFont(font);
header.setStyle(sf::Text::Style::Bold);
header.setFillColor(sf::Color::White);
header.setCharacterSize(60);
header.setOutlineColor(sf::Color::Black);
header.setOutlineThickness(4.f);
header.setString("Inventory");
header.setPosition({ (static_cast<float>(target.getSize().x) - header.getGlobalBounds().width) / 2.f, 0 });
target.draw(header);
sf::RectangleShape detail_box;
detail_box.setFillColor(sf::Color(0, 0, 0, 175));
detail_box.setOutlineColor(sf::Color::Blue);
detail_box.setOutlineThickness(2.f);
detail_box.setSize({ detail.width, detail.height });
detail_box.setPosition({ detail.left, detail.top });
sf::RectangleShape objects_box;
objects_box.setFillColor(sf::Color(0, 0, 0, 175));
objects_box.setOutlineColor(sf::Color::Blue);
objects_box.setOutlineThickness(2.f);
objects_box.setSize({ objects.width, objects.height });
objects_box.setPosition({ objects.left, objects.top });
target.draw(detail_box);
target.draw(objects_box);
sf::Vector2f margin({ 30, 30 });
sf::Vector2f scale({ 3.f, 3.f });
auto i = 0;
// Offsets for placement.
auto h = 0;
auto w = 0;
for (auto [id, item]: _items) {
item.setScale(scale);
auto rect = item.bounds();
sf::Vector2f bounding_size({ rect.width * scale.x, rect.height * scale.y });
sf::RectangleShape bounding_box(bounding_size);
if (margin.x + w + bounding_size.x + margin.x > objects.width) {
h += margin.y + bounding_size.y;
w = 0;
}
bounding_box.setPosition({ objects.left + margin.x + w, objects.top + margin.y + h });
if (bounding_box.getGlobalBounds().contains(mouse_location) && mouse_pressed) {
selected = i;
}
bounding_box.setFillColor(sf::Color(0, 0, 0, 220));
bounding_box.setOutlineColor(i == selected ? sf::Color::White : sf::Color::Black);
bounding_box.setOutlineThickness(2.f);
target.draw(bounding_box);
item.setPosition({ (objects.left + margin.x + w) / scale.x, (objects.top + margin.y + h) / scale.y });
target.draw(item);
w += margin.x + bounding_size.x;
i++;
}
const auto display_text = [&target, &font](const std::string& text_, sf::Vector2f pos) {
sf::Text text;
text.setFont(font);
text.setFillColor(sf::Color::White);
text.setString(text_);
text.setPosition(pos);
target.draw(text);
};
if (_items.size() > 0 && selected) {
const auto& [id, item] = _items.at(*selected);
sf::Vector2f info_pos({ detail.left + margin.x / 2, detail.top + margin.y / 2 });
auto max_line_width = detail.width - margin.x;
if (item.usable()) {
display_text("<C> use item", { info_pos.x, detail.top + detail.height - 4 * margin.y });
}
display_text("<X> drop item", { info_pos.x, detail.top + detail.height - 2 * margin.y });
std::istringstream iss(item.description());
const std::vector<std::string> words(
{ std::istream_iterator<std::string>{ iss }, std::istream_iterator<std::string>{} });
sf::Text text;
text.setFont(font);
text.setFillColor(sf::Color::White);
text.setPosition(info_pos);
std::vector<sf::Text> lines;
lines.push_back(text);
for (auto& word: words) {
if (lines.back().getGlobalBounds().width + word.size() * 14 > max_line_width) {
lines.push_back(text);
lines.back().setPosition({ info_pos.x, info_pos.y + text.getCharacterSize() * (lines.size() - 1) });
}
std::string current = lines.back().getString();
lines.back().setString(current + " " + word);
}
for (auto& line: lines) {
target.draw(line);
}
}
}
Inventory::Inventory(size_t max_size_, const std::shared_ptr<AssetCache> asset_cache_):
asset_cache(asset_cache_), max_size(max_size_), mouse_pressed(false) {}
void Inventory::clear() {
_items.clear();
}
[[nodiscard]] bool Inventory::empty() const {
return _items.empty();
}
[[nodiscard]] size_t Inventory::size() const {
return _items.size();
}
[[nodiscard]] const std::vector<std::pair<int, Object>>& Inventory::items() const {
return _items;
}
bool Inventory::add(std::pair<int, Object> item) {
if (size() < max_size) {
_items.push_back(item);
return true;
}
return false;
}
std::pair<int, Object> Inventory::remove(size_t index) {
auto item = _items[index];
_items.erase(_items.cbegin() + index);
return item;
}
void Inventory::mouse(sf::Vector2f location, bool pressed) {
mouse_location = location;
mouse_pressed = pressed;
}
void Inventory::drop_selected(Protagonist& player, Map& map) {
if (selected) {
auto [id, collectible] = remove(*selected);
auto new_position = player.getPosition();
new_position.x -= collectible.bounds().width / 2.f;
new_position.y -= collectible.bounds().height / 2.f;
collectible.setPosition(new_position);
map.collectibles().emplace(std::make_pair(id, collectible));
select_next();
player.drop_item();
}
}
std::optional<std::string> Inventory::use_selected(Protagonist& player, std::function<void(int)> callback) {
if (selected) {
if (_items[*selected].second.usable()) {
auto pair = remove(*selected);
const auto& add_to_used = callback;
add_to_used(pair.first);
const auto message = player.use_item(pair);
select_next();
return message;
}
}
return std::nullopt;
}
void Inventory::select_next() {
if (*selected < size()) {
return;
} else if (*selected > 0) {
(*selected)--;
} else {
selected = std::nullopt;
}
}
} // namespace tol