diff --git a/src/otclient/core/creature.cpp b/src/otclient/core/creature.cpp index 4ccfc37e..ee4b5604 100644 --- a/src/otclient/core/creature.cpp +++ b/src/otclient/core/creature.cpp @@ -26,7 +26,7 @@ #include "map.h" #include #include -#include + Creature::Creature() : Thing(Otc::Creature) { @@ -37,6 +37,7 @@ Creature::Creature() : Thing(Otc::Creature) m_walkOffsetX = 0; m_walkOffsetY = 0; + m_informationFont = g_fonts.getFont("tibia-12px-rounded"); } void Creature::draw(int x, int y) @@ -153,33 +154,8 @@ void Creature::drawInformation(int x, int y, bool useGray) { Color fillColor = Color(96, 96, 96); - if(!useGray) { - // health bar according to yatc - fillColor = Fw::black; - if(m_healthPercent > 92) { - fillColor.setGreen(188); - } - else if(m_healthPercent > 60) { - fillColor.setRed(80); - fillColor.setGreen(161); - fillColor.setBlue(80); - } - else if(m_healthPercent > 30) { - fillColor.setRed(161); - fillColor.setGreen(161); - } - else if(m_healthPercent > 8) { - fillColor.setRed(160); - fillColor.setGreen(39); - fillColor.setBlue(39); - } - else if(m_healthPercent > 3) { - fillColor.setRed(160); - } - else { - fillColor.setRed(79); - } - } + if(!useGray) + fillColor = m_informationColor; Rect backgroundRect = Rect(x-(14.5), y, 27, 4); Rect healthRect = backgroundRect.expanded(-1); @@ -192,8 +168,7 @@ void Creature::drawInformation(int x, int y, bool useGray) g_graphics.drawFilledRect(healthRect); // name - FontPtr font = g_fonts.getFont("tibia-12px-rounded"); - font->renderText(m_name, Rect(x-100, y-15, 200, 15), Fw::AlignTopCenter, fillColor); + m_informationFont->renderText(m_name, Rect(x-100, y-15, 200, 15), Fw::AlignTopCenter, fillColor); } void Creature::walk(const Position& position) @@ -246,7 +221,43 @@ void Creature::walk(const Position& position) m_lastTicks = g_platform.getTicks(); } +void Creature::setHealthPercent(uint8 healthPercent) +{ + int oldHealthPercent = m_healthPercent; + m_healthPercent = healthPercent; + onHealthPercentChange(oldHealthPercent); +} + const ThingAttributes& Creature::getAttributes() { return g_dat.getCreatureAttributes(m_outfit.type); } + +void Creature::onHealthPercentChange(int) +{ + m_informationColor = Fw::black; + + if(m_healthPercent > 92) { + m_informationColor.setGreen(188); + } + else if(m_healthPercent > 60) { + m_informationColor.setRed(80); + m_informationColor.setGreen(161); + m_informationColor.setBlue(80); + } + else if(m_healthPercent > 30) { + m_informationColor.setRed(161); + m_informationColor.setGreen(161); + } + else if(m_healthPercent > 8) { + m_informationColor.setRed(160); + m_informationColor.setGreen(39); + m_informationColor.setBlue(39); + } + else if(m_healthPercent > 3) { + m_informationColor.setRed(160); + } + else { + m_informationColor.setRed(79); + } +} diff --git a/src/otclient/core/creature.h b/src/otclient/core/creature.h index ef86e49d..1f000b1b 100644 --- a/src/otclient/core/creature.h +++ b/src/otclient/core/creature.h @@ -24,6 +24,7 @@ #define CREATURE_H #include "thing.h" +#include struct Outfit { uint16 type; @@ -44,7 +45,7 @@ public: void drawInformation(int x, int y, bool useGray); void setName(const std::string& name) { m_name = name; } - void setHealthPercent(uint8 healthPercent) { m_healthPercent = healthPercent; } + void setHealthPercent(uint8 healthPercent); void setDirection(Otc::Direction direction) { m_direction = direction; } void setOutfit(const Outfit& outfit) { m_outfit = outfit; } void setLight(const Light& light) { m_light = light; } @@ -64,13 +65,14 @@ public: uint8 getShield() { return m_shield; } uint8 getEmblem() { return m_emblem; } bool getImpassable() { return m_impassable; } + const ThingAttributes& getAttributes(); + + void onHealthPercentChange(int); void walk(const Position& position); int getWalkOffsetX() { return m_walkOffsetX; } int getWalkOffsetY() { return m_walkOffsetY; } - const ThingAttributes& getAttributes(); - CreaturePtr asCreature() { return std::static_pointer_cast(shared_from_this()); } private: @@ -85,6 +87,9 @@ private: uint8 m_emblem; bool m_impassable; + FontPtr m_informationFont; + Color m_informationColor; + int m_lastTicks; bool m_walking; float m_walkTimePerPixel;