1 #include "CreaturePanel.hpp"
5 #include "../app/Assets.hpp"
6 #include "../creature/Creature.hpp"
7 #include "../creature/Need.hpp"
8 #include "../graphics/Viewport.hpp"
10 #include <glm/gtx/transform.hpp>
16 CreaturePanel::CreaturePanel(const app::Assets &assets)
19 , name(new Label(assets.fonts.large))
22 , health_meter(new Meter)
24 Label *health_label = new Label(assets.fonts.medium);
25 health_label->Text("Health");
27 ->Size(glm::vec2(100.0f, assets.fonts.medium.Height() + assets.fonts.medium.Descent()))
28 ->Padding(glm::vec2(1.0f))
30 ->FillColor(glm::vec4(0.9f, 0.0f, 0.0f, 1.0f))
31 ->BorderColor(glm::vec4(0.0f, 0.0f, 0.0f, 1.0f));
32 Panel *health_panel = new Panel;
36 ->Direction(Panel::HORIZONTAL);
41 ->Padding(glm::vec2(10.0f))
43 ->Direction(Panel::VERTICAL)
44 ->Background(glm::vec4(0.7f, 0.7f, 0.7f, 1.0f));
47 CreaturePanel::~CreaturePanel() {
51 void CreaturePanel::Show(creature::Creature &cr) {
53 name->Text(c->Name());
57 void CreaturePanel::CreateNeeds() {
58 needs->Clear()->Reserve(c->Needs().size());
60 need_meters.reserve(c->Needs().size());
61 for (auto &need : c->Needs()) {
62 Label *label = new Label(assets.fonts.medium);
63 label->Text(need->name);
64 Meter *meter = new Meter;
66 ->Value(1.0f - need->value)
67 ->Size(glm::vec2(100.0f, assets.fonts.medium.Height() + assets.fonts.medium.Descent()))
68 ->Padding(glm::vec2(1.0f))
70 ->FillColor(glm::vec4(0.0f, 0.0f, 0.0f, 1.0f))
71 ->BorderColor(glm::vec4(0.0f, 0.0f, 0.0f, 1.0f));
72 Panel *need_panel = new Panel;
74 ->Direction(Panel::HORIZONTAL)
78 needs->Add(need_panel);
79 need_meters.push_back(meter);
84 void CreaturePanel::Hide() noexcept {
88 void CreaturePanel::Draw(app::Assets &assets, graphics::Viewport &viewport) noexcept {
91 health_meter->Value(c->Health());
93 if (need_meters.size() != c->Needs().size()) {
96 auto need = c->Needs().begin();
97 auto need_end = c->Needs().end();
98 auto meter = need_meters.begin();
99 for (; need != need_end; ++need, ++meter) {
100 (*meter)->Value(1.0f - (*need)->value);
101 if ((*need)->IsSatisfied()) {
102 (*meter)->FillColor(glm::vec4(0.0f, 0.7f, 0.0f, 1.0f));
103 } else if ((*need)->IsInconvenient()) {
104 (*meter)->FillColor(glm::vec4(0.7f, 0.5f, 0.0f, 1.0f));
106 (*meter)->FillColor(glm::vec4(0.9f, 0.0f, 0.0f, 1.0f));
111 const glm::vec2 margin(20.0f);
113 panel.Position(glm::vec2(viewport.Width() - margin.x - panel.Size().x, margin.y));
114 panel.Draw(assets, viewport);