|
| 1 | +#include <algorithm> |
1 | 2 | #include <filesystem> |
2 | 3 | #include <iostream> |
3 | 4 | #include <string> |
4 | 5 | #include <vector> |
5 | | -#include "str_format.hpp" |
| 6 | +#include "models.hpp" |
6 | 7 | #include "utils.hpp" |
| 8 | +#include <str_format/str_format.hpp> |
7 | 9 |
|
8 | | -// TODO: update args to Pokemon object |
9 | | -std::vector<std::string> gen_healthbar(std::string name, int level, int hp, int max_hp) |
| 10 | +std::vector<std::string> gen_healthbar(Pokemon& pkmn) |
10 | 11 | { |
11 | | - /* |
12 | | - * Bulbasaur :L30 // label |
13 | | - * HP [********* ] // progressbar |
14 | | - * 58 / 60 // hitpoints |
15 | | - */ |
16 | | - std::string label = kt::format_str("{} :L{}\n", name, level); |
17 | | - |
18 | | - double percentage = ceil(static_cast<double>(hp) / max_hp * 100); |
19 | | - int digits = static_cast<int>(log10(percentage)); |
20 | | - int first_digit = static_cast<int>(percentage / pow(10, digits)); |
21 | | - |
22 | | - std::string stars = std::string(first_digit, '*'); |
23 | | - stars = stars.append(std::string(10LL - first_digit, ' ')); |
24 | | - std::string progressbar = kt::format_str("HP [{}]\n", style(stars, Color::GREEN)); |
25 | | - |
26 | | - std::string hitpoints = kt::format_str("{} / {}\n", hp, max_hp); |
27 | | - hitpoints = std::string(16 - hitpoints.length(), ' ').append(hitpoints); |
| 12 | + /* |
| 13 | + * bulbasaur :L30 // label |
| 14 | + * HP [********* ] // progressbar |
| 15 | + * 58 / 60 // hitpoints |
| 16 | + */ |
| 17 | + std::string label = kt::format_str("{} :L{}", pkmn.name, pkmn.level); |
| 18 | + std::size_t max_width = pkmn.name.length() > 10 ? label.length() : 15; |
| 19 | + |
| 20 | + label = std::string(max_width - label.length(), ' ').append(label); |
| 21 | + |
| 22 | + double linear_map = (10.0 / pkmn.max_hp) * pkmn.hp; |
| 23 | + int hp_scaled = (0 < linear_map && linear_map < 1) ? 1 : std::floor(linear_map); |
| 24 | + |
| 25 | + std::string stars = std::string(hp_scaled, '*'); |
| 26 | + stars = stars.append(std::string(10 - hp_scaled, ' ')); |
| 27 | + Color star_color = (hp_scaled >= 5) ? Color::GREEN : (3 < hp_scaled && hp_scaled < 5) ? Color::YELLOW : Color::RED; |
| 28 | + std::string progressbar = kt::format_str("HP [{}]", style(stars, star_color)); |
| 29 | + progressbar = std::string(max_width - 15, ' ').append(progressbar); |
| 30 | + |
| 31 | + std::string hitpoints = kt::format_str("{} / {}", pkmn.hp, pkmn.max_hp); |
| 32 | + hitpoints = std::string(max_width - hitpoints.length(), ' ').append(hitpoints); |
28 | 33 |
|
29 | 34 | return {label, progressbar, hitpoints}; |
30 | 35 | } |
31 | 36 |
|
32 | | -std::vector<std::string> load_sprite(int id, const std::filesystem::path& assets) |
| 37 | +void print_frame(Pokemon& pkmn1, Pokemon& pkmn2) |
33 | 38 | { |
34 | | - for (const auto& file : std::filesystem::directory_iterator(assets)) |
| 39 | + std::string healthbars{}; |
| 40 | + std::string sprites{}; |
| 41 | + |
| 42 | + std::vector<std::string> healthbar1 = gen_healthbar(pkmn1); |
| 43 | + std::vector<std::string> healthbar2 = gen_healthbar(pkmn2); |
| 44 | + |
| 45 | + std::size_t padding = healthbar1[0].length() + healthbar2[0].length(); |
| 46 | + |
| 47 | + if (padding > 30) |
| 48 | + padding -= 2 * (padding - 30); |
| 49 | + |
| 50 | + for (std::size_t i = 0; i < 3; i++) |
| 51 | + healthbars.append(healthbar1[i]).append(std::string(padding, ' ')).append(healthbar2[i]).append("\n"); |
| 52 | + |
| 53 | + std::cout << healthbars; |
| 54 | + |
| 55 | + for (std::size_t i = 0; i < 20; i++) |
35 | 56 | { |
36 | | - if (file.path().extension() == ".txt" && std::stoi(file.path().stem().string()) == id) |
37 | | - { |
38 | | - return read_file(file); |
39 | | - } |
| 57 | + std::string tmp = pkmn1.sprite[i]; // strip new line |
| 58 | + tmp.erase(std::remove(tmp.begin(), tmp.end(), '\n'), tmp.end()); |
| 59 | + sprites.append(tmp).append(std::string(20, ' ')).append(pkmn2.sprite[i]); |
40 | 60 | } |
| 61 | + |
| 62 | + std::cout << sprites; |
| 63 | + |
| 64 | + return; |
41 | 65 | } |
0 commit comments