Skip to content

Commit

Permalink
RLDifficultyNode refactor and stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
SpaghettDev committed Mar 9, 2024
1 parent 2b7751e commit 82983b0
Show file tree
Hide file tree
Showing 8 changed files with 203 additions and 119 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Added

- `#include` directives to the `/custom_layers`
- Platform name to `RLRouletteInfoLayer`
- `RLDifficultyNode` supports epic, legendary and mythic now
- `rl::utils::getFeatureStateFromResponse`

### Changed

Expand All @@ -25,6 +28,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

- Empty list id crash on Android (probably)
- Show options sprite being set even if the info icon was never clicked
- `RLDifficultyNode::setColor` being weird
- Difficulty not being highlighted when changing the list multiple times

## [2.0.1] - 2024-03-03

Expand Down
137 changes: 83 additions & 54 deletions src/custom_nodes/RLDifficultyNode.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#include "RLDifficultyNode.hpp"
#include "../utils.hpp"

RLDifficultyNode* RLDifficultyNode::create(GJDifficulty difficulty, bool featured, bool epic)
RLDifficultyNode* RLDifficultyNode::create(const DifficultyInfo& di)
{
auto ret = new RLDifficultyNode();
auto* ret = new RLDifficultyNode();

if (ret && ret->init(difficulty, featured, epic))
if (ret && ret->init(di))
ret->autorelease();
else
{
Expand All @@ -16,15 +16,33 @@ RLDifficultyNode* RLDifficultyNode::create(GJDifficulty difficulty, bool feature
return ret;
}

bool RLDifficultyNode::init(GJDifficulty difficulty, bool featured, bool epic)
RLDifficultyNode* RLDifficultyNode::create(GJDifficulty difficulty)
{
auto* ret = new RLDifficultyNode();

if (ret && ret->init({ difficulty, RL_FEATURE_STATE::NONE }))
ret->autorelease();
else
{
delete ret;
ret = nullptr;
}

return ret;
}

bool RLDifficultyNode::init(const DifficultyInfo& di)
{
if (!CCNodeRGBA::init()) return false;

m_difficulty_info = di;

this->setCascadeColorEnabled(true);
this->setCascadeOpacityEnabled(true);

m_difficulty_info = { difficulty, featured, epic };
m_color = { 255, 255, 255 };

m_difficulty_sprite = CCSprite::createWithSpriteFrameName(
rl::constants::difficulty_to_sprite.at(difficulty).data()
rl::constants::difficulty_to_sprite.at(m_difficulty_info.difficulty).data()
);
m_difficulty_sprite->setID("difficulty-sprite");
this->addChild(m_difficulty_sprite);
Expand All @@ -34,74 +52,85 @@ bool RLDifficultyNode::init(GJDifficulty difficulty, bool featured, bool epic)
this->setContentSize(targetContentSize);
m_difficulty_sprite->setPosition(targetContentSize / 2);

if (featured)
switch (m_difficulty_info.feature_state)
{
m_featured_sprite = CCSprite::createWithSpriteFrameName("GJ_featuredCoin_001.png");
m_featured_sprite->setPosition(m_difficulty_sprite->getPosition());
m_featured_sprite->setID("featured-sprite");
this->addChild(m_featured_sprite, -1);
case RL_FEATURE_STATE::FEATURED:
case RL_FEATURE_STATE::EPIC:
case RL_FEATURE_STATE::LEGENDARY:
case RL_FEATURE_STATE::MYTHIC:
m_feature_sprite = CCSprite::createWithSpriteFrameName(
rl::constants::feature_state_to_sprite.at(m_difficulty_info.feature_state).data()
);
break;

default:
m_feature_sprite = nullptr;
break;
}

if (epic)
if (m_feature_sprite)
{
m_epic_sprite = CCSprite::createWithSpriteFrameName("GJ_epicCoin_001.png");
m_epic_sprite->setPosition(m_difficulty_sprite->getPosition());
m_epic_sprite->setID("epic-sprite");
this->addChild(m_epic_sprite, -1);
m_feature_sprite->setPosition(m_difficulty_sprite->getPosition());
m_feature_sprite->setID("feature-sprite");
this->addChild(m_feature_sprite, -1);
}

return true;
}

void RLDifficultyNode::setColor(const ccColor3B& color)
{
log::debug("called with r:{} g:{} b:{}", color.r, color.g, color.b);
m_difficulty_sprite->setColor(color);
if (m_featured_sprite)
m_featured_sprite->setColor(color);
if (m_epic_sprite)
m_epic_sprite->setColor(color);

m_color = color;
}

// basically RLDifficultyNode::init but without the call to CCNodeRGBA::init and some extra checks
void RLDifficultyNode::setDifficulty(GJDifficulty difficulty, bool featured, bool epic)
void RLDifficultyNode::setDifficulty(const DifficultyInfo& di)
{
if (m_difficulty_info == DifficultyInfo{ difficulty, featured, epic }) return;
if (m_difficulty_info == di) return;

for (auto* node : CCArrayExt<CCSprite*>(this->getChildren()))
node->removeFromParent();
if (m_difficulty_info.difficulty != di.difficulty)
{
m_difficulty_sprite->removeFromParent();

m_difficulty_sprite = CCSprite::createWithSpriteFrameName(
rl::constants::difficulty_to_sprite.at(difficulty).data()
);
m_difficulty_sprite->setColor(m_color);
m_difficulty_sprite->setID("difficulty-sprite");
this->addChild(m_difficulty_sprite);
m_difficulty_sprite = CCSprite::createWithSpriteFrameName(
rl::constants::difficulty_to_sprite.at(di.difficulty).data()
);
m_difficulty_sprite->setID("difficulty-sprite");
this->addChild(m_difficulty_sprite);
}

// man this codebase is getting worse by the minute
// TODO: find a better way :despair:
if (this->getParent() && typeinfo_cast<CCMenuItemSpriteExtra*>(this->getParent()))
m_difficulty_sprite->setPosition(this->getContentSize() / 2);

if (featured)
if (m_difficulty_info.feature_state != di.feature_state)
{
m_featured_sprite = CCSprite::createWithSpriteFrameName("GJ_featuredCoin_001.png");
m_featured_sprite->setColor(m_color);
m_featured_sprite->setPosition(m_difficulty_sprite->getPosition());
m_featured_sprite->setID("featured-sprite");
this->addChild(m_featured_sprite, -1);
if (m_feature_sprite)
m_feature_sprite->removeFromParent();

switch (di.feature_state)
{
case RL_FEATURE_STATE::FEATURED:
case RL_FEATURE_STATE::EPIC:
case RL_FEATURE_STATE::LEGENDARY:
case RL_FEATURE_STATE::MYTHIC:
m_feature_sprite = CCSprite::createWithSpriteFrameName(
rl::constants::feature_state_to_sprite.at(di.feature_state).data()
);
break;

default:
m_feature_sprite = nullptr;
break;
}

if (m_feature_sprite)
{
m_feature_sprite->setID("feature-sprite");
this->addChild(m_feature_sprite, -1);
}
}

if (epic)
{
m_epic_sprite = CCSprite::createWithSpriteFrameName("GJ_epicCoin_001.png");
m_epic_sprite->setColor(m_color);
m_epic_sprite->setPosition(m_difficulty_sprite->getPosition());
m_epic_sprite->setID("epic-sprite");
this->addChild(m_epic_sprite, -1);
}
m_difficulty_info = di;
}

m_difficulty_info = { difficulty, featured, epic };
void RLDifficultyNode::setDifficulty(GJDifficulty difficulty)
{
setDifficulty({ difficulty, RL_FEATURE_STATE::NONE });
}
26 changes: 16 additions & 10 deletions src/custom_nodes/RLDifficultyNode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,36 @@

using namespace geode::prelude;

enum class RL_FEATURE_STATE
{
NONE = -1,
FEATURED,
EPIC,
LEGENDARY,
MYTHIC
};

class RLDifficultyNode : public CCNodeRGBA
{
private:
struct DifficultyInfo
{
GJDifficulty difficulty;
bool featured;
bool epic;
RL_FEATURE_STATE feature_state = RL_FEATURE_STATE::NONE;

bool operator==(const DifficultyInfo&) const = default;
} m_difficulty_info;

CCSprite* m_difficulty_sprite;
CCSprite* m_featured_sprite;
CCSprite* m_epic_sprite;
ccColor3B m_color;
CCSprite* m_feature_sprite;

public:
static RLDifficultyNode* create(GJDifficulty, bool = false, bool = false);
bool init(GJDifficulty, bool = false, bool = false);
static RLDifficultyNode* create(const DifficultyInfo&);
static RLDifficultyNode* create(GJDifficulty);
bool init(const DifficultyInfo&);

// TODO: fix this setColor fuckery
void setColor(const ccColor3B&) override;
void setDifficulty(const DifficultyInfo&);
void setDifficulty(GJDifficulty);

void setDifficulty(GJDifficulty, bool = false, bool = false);
const DifficultyInfo& getDifficultyInfo() const { return m_difficulty_info; };
};
1 change: 1 addition & 0 deletions src/listfetcher/ListFetcher.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class ListFetcher
inline static constexpr std::string_view DEMONLIST_URL = "https://pointercrate.com/api/v2/demons/listed";
inline static constexpr std::string_view CHALLENGELIST_URL = "https://challengelist.gd/api/v1/demons";

// TODO: move to rl::constants
inline static const std::map<GJDifficulty, int> m_cDemonDiffToFilter{
{ GJDifficulty::DemonEasy, 1 },
{ GJDifficulty::DemonMedium, 2 },
Expand Down
70 changes: 50 additions & 20 deletions src/roulette/layers/RLRouletteInfoLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

using namespace geode::prelude;

GJDifficulty RLRouletteInfoLayer::m_previous_roulette_difficulty = static_cast<GJDifficulty>(-3);

RLRouletteInfoLayer* RLRouletteInfoLayer::create()
{
auto ret = new RLRouletteInfoLayer();
Expand Down Expand Up @@ -92,11 +94,13 @@ bool RLRouletteInfoLayer::init()


auto versionText = CCLabelBMFont::create(
#ifdef RWDI_MODE
("Version " + Mod::get()->getVersion().toString(true) + " dev").c_str(),
#if defined(RWDI_MODE) && defined(GEODE_PLATFORM_SHORT_IDENTIFIER)
fmt::format("Version: {} ({} dev)", Mod::get()->getVersion().toString(true), GEODE_PLATFORM_SHORT_IDENTIFIER).c_str(),
#elif defined(GEODE_PLATFORM_SHORT_IDENTIFIER)
fmt::format("Version: {} ({})", Mod::get()->getVersion().toString(true), GEODE_PLATFORM_SHORT_IDENTIFIER).c_str(),
#else
("Version " + Mod::get()->getVersion().toString(true)).c_str(),
#endif // RWDI_MODE
fmt::format("Version: {} (how)", Mod::get()->getVersion().toString(true)).c_str(),
#endif
"bigFont.fnt"
);
versionText->setPosition({ .0f, -94.f });
Expand All @@ -113,15 +117,27 @@ void RLRouletteInfoLayer::onClose(CCObject*)
this->removeFromParentAndCleanup(true);
}

// TODO: save the buttons' colors' states
void RLRouletteInfoLayer::onToggleButton(CCObject* sender)
{
auto button = static_cast<CCMenuItemToggler*>(sender);
auto prevIdx = rl::utils::getIndexOf(g_rouletteManager.getFromSaveContainer("selected-list-array").as_array(), true);
const auto demonDifficultyButton = static_cast<RLDifficultyNode*>(
g_rouletteManager.rouletteLayer->getDifficultyButton(GJDifficulty::Demon)->getNormalImage()
g_rouletteManager.rouletteLayer->getDifficultyButton(GJDifficulty::Demon)->getChildByID("sprite-node")
);

// yeah...
auto changeListWrapper = [&](const std::function<void()>& f) {
g_rouletteManager.getFromSaveContainer("selected-list-array").as_array().at(button->getTag()) = false;
g_rouletteManager.getFromSaveContainer("selected-list-array").as_array().at(prevIdx) = false;
g_rouletteManager.getFromSaveContainer("selected-list-array").as_array().at(0) = true;

f();

g_rouletteManager.getFromSaveContainer("selected-list-array").as_array().at(0) = false;
g_rouletteManager.getFromSaveContainer("selected-list-array").as_array().at(prevIdx) = false;
g_rouletteManager.getFromSaveContainer("selected-list-array").as_array().at(button->getTag()) = true;
};

g_rouletteManager.getFromSaveContainer("selected-list-array").as_array().at(prevIdx) = false;
g_rouletteManager.getFromSaveContainer("selected-list-array").as_array().at(button->getTag()) = true;

Expand All @@ -130,46 +146,60 @@ void RLRouletteInfoLayer::onToggleButton(CCObject* sender)
for (int i = 0; i < 4; i++)
static_cast<CCMenuItemToggler*>(m_buttonMenu->getChildByTag(i))->toggle(false);

if (m_previous_roulette_difficulty == static_cast<GJDifficulty>(-3))
m_previous_roulette_difficulty = g_rouletteManager.previousDifficulty;

// purely visual, "toggles" the difficulty face based on the selected list
// (demon for demon list, insane for challenge list, and previous difficulty for normal list, easy for gd list because 🔥)
if (button->getID() == "normal-list")
{
g_rouletteManager.rouletteLayer->setDifficultyButtonColor(GJDifficulty::Insane, { 125, 125, 125 });
g_rouletteManager.rouletteLayer->setDifficultyButtonColor(GJDifficulty::Demon, { 125, 125, 125 });
g_rouletteManager.rouletteLayer->setDifficultyButtonColor(g_rouletteManager.previousDifficulty, { 255, 255, 255 });
changeListWrapper([&] {
g_rouletteManager.rouletteLayer->onDifficultyButton(
g_rouletteManager.rouletteLayer->getDifficultyButton(m_previous_roulette_difficulty)
);
});

m_previous_roulette_difficulty = static_cast<GJDifficulty>(-3);
}
else if (button->getID() == "demon-list")
{
g_rouletteManager.rouletteLayer->setDifficultyButtonColor(g_rouletteManager.previousDifficulty, { 125, 125, 125 });
g_rouletteManager.rouletteLayer->setDifficultyButtonColor(GJDifficulty::Insane, { 125, 125, 125 });
g_rouletteManager.rouletteLayer->setDifficultyButtonColor(GJDifficulty::Demon, { 255, 255, 255 });
changeListWrapper([&] {
g_rouletteManager.rouletteLayer->onDifficultyButton(
g_rouletteManager.rouletteLayer->getDifficultyButton(GJDifficulty::Demon)
);
});
demonDifficultyButton->setDifficulty(GJDifficulty::DemonExtreme);
}
else if (button->getID() == "challenge-list")
{
g_rouletteManager.rouletteLayer->setDifficultyButtonColor(g_rouletteManager.previousDifficulty, { 125, 125, 125 });
g_rouletteManager.rouletteLayer->setDifficultyButtonColor(GJDifficulty::Insane, { 255, 255, 255 });
g_rouletteManager.rouletteLayer->setDifficultyButtonColor(GJDifficulty::Demon, { 125, 125, 125 });
changeListWrapper([&] {
g_rouletteManager.rouletteLayer->onDifficultyButton(
g_rouletteManager.rouletteLayer->getDifficultyButton(GJDifficulty::Insane)
);
});
demonDifficultyButton->setDifficulty(static_cast<GJDifficulty>(-2));
}
else if (button->getID() == "gd-list")
{
// TODO: set the list's difficulty ... somehow?

g_rouletteManager.rouletteLayer->setDifficultyButtonColor(g_rouletteManager.previousDifficulty, { 125, 125, 125 });
g_rouletteManager.rouletteLayer->setDifficultyButtonColor(GJDifficulty::Insane, { 125, 125, 125 });
g_rouletteManager.rouletteLayer->setDifficultyButtonColor(GJDifficulty::Demon, { 125, 125, 125 });
changeListWrapper([&] {
g_rouletteManager.rouletteLayer->onDifficultyButton(
g_rouletteManager.rouletteLayer->getDifficultyButton(GJDifficulty::Easy)
);
g_rouletteManager.rouletteLayer->getDifficultyButton(GJDifficulty::Easy)->setColor({ 125, 125, 125 });
});
demonDifficultyButton->setDifficulty(static_cast<GJDifficulty>(-2));
}

if (button->getID() != "normal-list")
g_rouletteManager.rouletteLayer->main_menu->getChildByID("demon-plus-button")->setVisible(false);
else if (g_rouletteManager.previousDifficulty == GJDifficulty::Demon)
else if (m_previous_roulette_difficulty == GJDifficulty::Demon)
g_rouletteManager.rouletteLayer->main_menu->getChildByID("demon-plus-button")->setVisible(true);

if (button->getID() == "normal-list")
demonDifficultyButton->setDifficulty(
g_rouletteManager.previousDifficulty == GJDifficulty::Demon
m_previous_roulette_difficulty == GJDifficulty::Demon
? g_rouletteManager.previousDemonDifficulty
: static_cast<GJDifficulty>(-2)
);
Expand Down
Loading

0 comments on commit 82983b0

Please sign in to comment.