Peoplemon  0.1.0
Peoplemon 3 game source documentation
PeoplemonButton.cpp
Go to the documentation of this file.
2 
3 #include <Core/Items/Item.hpp>
5 #include <Core/Properties.hpp>
6 #include <Core/Resources.hpp>
7 
8 namespace game
9 {
10 namespace menu
11 {
12 namespace
13 {
14 constexpr float GrowRate = 100.f;
15 constexpr float HpBarWidth = 178.f;
16 constexpr float HpBarHeight = 12.f;
17 } // namespace
18 
20  return Ptr(new PeoplemonButton(ppl));
21 }
22 
23 PeoplemonButton::PeoplemonButton(const core::pplmn::OwnedPeoplemon& ppl)
24 : ppl(ppl)
25 , enginePtr(nullptr)
26 , overlay(nullptr)
27 , color(sf::Color::White) {
28  getSignal(bl::menu::Item::Selected).willAlwaysCall([this]() {
29  image.setColor(sf::Color(170, 190, 30));
30  });
31  getSignal(bl::menu::Item::Deselected).willAlwaysCall([this]() { image.setColor(color); });
32 }
33 
34 void PeoplemonButton::setHighlightColor(const sf::Color& c) {
35  color = c;
36  if (!isSelected) { image.setColor(color); }
37 }
38 
39 glm::vec2 PeoplemonButton::getSize() const { return txtr->size(); }
40 
42  const unsigned int curHp = ppl.currentHp();
43  const unsigned int maxHp = ppl.currentStats().hp;
44  const float hpPercent = static_cast<float>(curHp) / static_cast<float>(maxHp);
45  hpText.getSection().setString(std::to_string(curHp) + " / " + std::to_string(maxHp));
46  hpText.getTransform().setPosition(342.f - hpText.getLocalBounds().width - 8.f, 83.f);
47  hpBarTarget = hpPercent * HpBarWidth;
48 
49  updateAilment(ppl.currentAilment());
50 }
51 
53  return std::abs(hpBar.getGlobalSize().x - hpBarTarget) < 0.5f;
54 }
55 
56 void PeoplemonButton::doCreate(bl::engine::Engine& engine) {
57  enginePtr = &engine;
58 
59  txtr = engine.renderer().texturePool().getOrLoadTexture(
60  bl::util::FileUtil::joinPath(core::Properties::MenuImagePath(), "Peoplemon/button.png"));
61  image.create(engine, txtr);
62  image.getTransform().setDepth(-2.f);
63 
64  name.create(engine,
66  ppl.name(),
67  22,
68  sf::Color(60, 225, 200),
69  sf::Text::Bold);
70  name.getTransform().setPosition(135.f, 18.f);
71  name.setParent(image);
72 
73  const unsigned int curHp = ppl.currentHp();
74  const unsigned int maxHp = ppl.currentStats().hp;
75  const float hpPercent = static_cast<float>(curHp) / static_cast<float>(maxHp);
76  const float hpBarWidth = hpPercent * HpBarWidth;
77  const sf::Color hpColor = core::Properties::HPBarColor(curHp, maxHp);
78 
79  hpBar.create(engine, {100.f, 12.f});
80  hpBar.getTransform().setPosition(164.f, 70.f);
81  hpBar.setFillColor(hpColor);
82  hpBar.scaleToSize({hpBarWidth, HpBarHeight});
83  hpBar.setParent(image);
84  hpBar.getTransform().setDepth(1.f);
85  hpBarTarget = hpBarWidth;
86 
87  hpText.create(engine,
89  std::to_string(curHp) + " / " + std::to_string(maxHp),
90  18,
91  sf::Color::Black);
92  hpText.getTransform().setPosition(342.f - hpText.getLocalBounds().width - 8.f, 83.f);
93  hpText.setParent(image);
94 
95  item.create(engine,
98  "No hold item",
99  16,
100  sf::Color(30, 230, 160));
101  item.getTransform().setPosition(85.f, 136.f);
102  item.setParent(image);
103 
104  level.create(engine,
106  std::to_string(ppl.currentLevel()),
107  16,
108  sf::Color(50, 220, 250),
109  sf::Text::Bold);
110  level.getTransform().setPosition(304.f, 16.f);
111  level.setParent(image);
112 
113  updateAilment(ppl.currentAilment());
114 
115  const std::string faceSrc = core::pplmn::Peoplemon::thumbnailImage(ppl.id());
116  faceTxtr = engine.renderer().texturePool().getOrLoadTexture(faceSrc);
117  face.create(engine, faceTxtr);
118  face.setParent(image);
119 
120  sf::Rect<unsigned int> bounds(100000, 100000, 0, 0);
121  const auto faceRes = ImageManager::load(faceSrc);
122  const sf::Image& img = *faceRes;
123  for (unsigned int x = 0; x < img.getSize().x; ++x) {
124  for (unsigned int y = 0; y < img.getSize().y; ++y) {
125  if (img.getPixel(x, y) != sf::Color::Transparent) {
126  if (x < bounds.left) bounds.left = x;
127  if (x > bounds.width) bounds.width = x;
128  if (y < bounds.top) bounds.top = y;
129  if (y > bounds.height) bounds.height = y;
130  }
131  }
132  }
133  const sf::Vector2f size(bounds.width - bounds.left, bounds.height - bounds.top);
134  const sf::Vector2f dscale(95.f / size.x, 95.f / size.y);
135  const float scale = std::min(dscale.x, dscale.y);
136  face.getTransform().setOrigin(faceTxtr->size() * 0.5f);
137  face.getTransform().setScale(scale, scale);
138  face.getTransform().setPosition(71.f, 76.f);
139 }
140 
141 void PeoplemonButton::doSceneAdd(bl::rc::Scene* o) {
142  overlay = o;
143 
144  image.addToScene(overlay, bl::rc::UpdateSpeed::Static);
145  face.addToScene(overlay, bl::rc::UpdateSpeed::Static);
146  hpBar.addToScene(overlay, bl::rc::UpdateSpeed::Static);
147  name.addToScene(overlay, bl::rc::UpdateSpeed::Static);
148  level.addToScene(overlay, bl::rc::UpdateSpeed::Static);
149  item.addToScene(overlay, bl::rc::UpdateSpeed::Static);
150  hpText.addToScene(overlay, bl::rc::UpdateSpeed::Static);
151  if (ailment.entity() != bl::ecs::InvalidEntity) {
152  ailment.addToScene(overlay, bl::rc::UpdateSpeed::Static);
153  }
154 }
155 
156 void PeoplemonButton::draw(bl::rc::scene::CodeScene::RenderContext& ctx) {
157  image.draw(ctx);
158  face.draw(ctx);
159  hpBar.draw(ctx);
160  name.draw(ctx);
161  level.draw(ctx);
162  item.draw(ctx);
163  hpText.draw(ctx);
164  if (ailment.entity() != bl::ecs::InvalidEntity) { ailment.draw(ctx); }
165 }
166 
167 void PeoplemonButton::doSceneRemove() { image.removeFromScene(); }
168 
169 bl::ecs::Entity PeoplemonButton::getEntity() const { return image.entity(); }
170 
171 void PeoplemonButton::update(float dt) {
172  if (hpBar.getGlobalSize().x < hpBarTarget) {
173  const float nw = std::min(hpBarTarget, hpBar.getGlobalSize().x + GrowRate * dt);
174  hpBar.scaleToSize({nw, HpBarHeight});
175  hpBar.setFillColor(core::Properties::HPBarColor(nw / HpBarWidth));
176  }
177  else if (hpBar.getGlobalSize().x > hpBarTarget) {
178  const float nw = std::max(hpBarTarget, hpBar.getGlobalSize().x - GrowRate * dt);
179  hpBar.scaleToSize({nw, HpBarHeight});
180  hpBar.setFillColor(core::Properties::HPBarColor(nw / HpBarWidth));
181  }
182 }
183 
184 void PeoplemonButton::updateAilment(core::pplmn::Ailment ail) {
185  const std::string ailSrc = core::Properties::AilmentTexture(ppl.currentAilment());
186  if (!ailSrc.empty()) {
187  ailTxtr = enginePtr->renderer().texturePool().getOrLoadTexture(ailSrc);
188  if (ailment.entity() == bl::ecs::InvalidEntity) {
189  ailment.create(*enginePtr, ailTxtr);
190  ailment.getTransform().setPosition(135.f, 90.f);
191  ailment.setParent(image);
192  }
193  else if (ailment.getTexture().get() != ailTxtr.get()) { ailment.setTexture(ailTxtr, {}); }
194  if (overlay) { ailment.addToScene(overlay, bl::rc::UpdateSpeed::Static); }
195  }
196  else {
197  if (ailment.entity() != bl::ecs::InvalidEntity) { ailment.destroy(); }
198  }
199 }
200 
201 } // namespace menu
202 } // namespace game
Ailment
Represents an ailment that a Peoplemon can have.
Definition: Ailment.hpp:16
Parent namespace for all functionality unique to the game.
static const std::string & getName(item::Id item)
Returns the name of the given item.
Definition: Item.cpp:91
Represents an instance of a peoplemon. Can be a wild peoplemon, trainer, or player peoplemon....
Ailment & currentAilment()
Access the current ailment of this peoplemon.
std::uint16_t & currentHp()
Access the current HP.
unsigned int currentLevel() const
Returns the current level of this peoplemon.
Stats currentStats() const
Returns the computed stats for the peoplemon. Does not take into account changes during battle.
Id id() const
Returns the id of this peoplemon.
item::Id & holdItem()
Access the current hold item of this peoplemon.
const std::string & name() const
Returns the name of this peoplemon, custom or defualt.
static std::string thumbnailImage(Id id)
Returns the path of the image to render as the peoplemon thumbnail.
Definition: Peoplemon.cpp:211
static const sf::VulkanFont & MenuFont()
Definition: Properties.cpp:363
static std::string AilmentTexture(pplmn::Ailment ailment)
Definition: Properties.cpp:653
static const std::string & MenuImagePath()
Definition: Properties.cpp:303
static sf::Color HPBarColor(float percent)
Definition: Properties.cpp:647
Menu item for peoplemon.
virtual void doSceneAdd(bl::rc::Scene *overlay) override
Called when the item should be added to the overlay.
static Ptr create(const core::pplmn::OwnedPeoplemon &ppl)
Creates a new peoplemon button from the peoplemon.
virtual void doSceneRemove() override
Called when the item should be removed from the overlay.
void setHighlightColor(const sf::Color &color)
Set the highlight color.
bool synced() const
Returns whether or not this button is synced.
virtual void doCreate(bl::engine::Engine &engine) override
Called at least once when the item is added to a menu. Should create required graphics primitives and...
virtual void draw(bl::rc::scene::CodeScene::RenderContext &ctx) override
Renders the item.
std::shared_ptr< PeoplemonButton > Ptr
virtual glm::vec2 getSize() const override
Returns the size of the button.
void sync(const core::pplmn::OwnedPeoplemon &ppl)
Syncs the HP bar and ailment texture with the peoplemon.
void update(float dt)
Updates the HP bar.
virtual bl::ecs::Entity getEntity() const override
Returns the entity (or top level entity) of the item.