Skip to content

Commit 5b16aa4

Browse files
committed
separate hook for mock
1 parent 378df0a commit 5b16aa4

2 files changed

Lines changed: 105 additions & 87 deletions

File tree

src/hooks/Mock.cpp

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#include <Horrible.hpp>
2+
3+
#include <Geode/Geode.hpp>
4+
5+
#include <Geode/modify/MenuLayer.hpp>
6+
7+
using namespace geode::prelude;
8+
using namespace geode::utils;
9+
using namespace matjson;
10+
using namespace horrible;
11+
12+
static RandomSeeder _randomSeeder;
13+
14+
class $modify(MockMenuLayer, MenuLayer) {
15+
bool init() {
16+
if (!MenuLayer::init()) return false;
17+
18+
auto rnd = rand() % 101;
19+
log::debug("chance {}", rnd);
20+
21+
// Show a LazySprite for the first PNG found in the save directory
22+
if (horribleMod && horribleMod->getSavedValue<bool>("mock", false)) {
23+
log::debug("mock feature enabled in MainMenu layer");
24+
25+
namespace fs = std::filesystem;
26+
27+
if (rnd <= static_cast<int>(horribleMod->getSettingValue<int64_t>("mock-chance"))) {
28+
auto mockConfigPath = fmt::format("{}\\mock.json", horribleMod->getSaveDir());
29+
auto mockConfig = file::readJson(fs::path(mockConfigPath));
30+
31+
log::debug("Reading path {}...", mockConfigPath);
32+
33+
if (mockConfig.isOk()) {
34+
log::debug("Read mocking config file");
35+
36+
auto mockConfigUnwr = mockConfig.unwrapOr(Value());
37+
38+
auto lvlUnwr = mockConfigUnwr.begin();
39+
std::advance(lvlUnwr, rnd % mockConfigUnwr.size());
40+
41+
auto id = lvlUnwr->getKey().value_or("");
42+
auto percent = lvlUnwr->asInt().unwrapOr(99);
43+
44+
if (!id.empty()) {
45+
log::debug("ID {} with percentage {} is valid", id, percent);
46+
47+
std::string pngPath = fmt::format("{}\\{}.png", horribleMod->getSaveDir(), id);
48+
49+
log::info("Displaying {}", pngPath);
50+
51+
auto ss = LazySprite::create({ 192.f, 108.f });
52+
ss->setID("mock"_spr);
53+
ss->setScale(0.25);
54+
ss->setAnchorPoint({ 0.5, 0.5 });
55+
ss->setPosition({ -192.f, getScaledContentHeight() / 2.f });
56+
ss->setZOrder(1000);
57+
58+
ss->setLoadCallback([this, ss, percent, rnd](Result<> res) {
59+
if (res.isOk()) {
60+
log::info("Sprite loaded successfully from save dir PNG");
61+
62+
auto percLabelText = fmt::format("{}%", percent);
63+
64+
auto percLabel = CCLabelBMFont::create(percLabelText.c_str(), "bigFont.fnt");
65+
percLabel->setID("percentage");
66+
percLabel->setPosition({ ss->getScaledContentWidth() / 2.f, ss->getScaledContentHeight() / 2.f });
67+
percLabel->setAlignment(CCTextAlignment::kCCTextAlignmentLeft);
68+
percLabel->ignoreAnchorPointForPosition(false);
69+
percLabel->setAnchorPoint({ 0, 0 });
70+
percLabel->setScale(2.5);
71+
72+
ss->addChild(percLabel);
73+
74+
float yA = static_cast<float>(rnd) / 100.f; // starting height pos
75+
float yB = static_cast<float>(rnd) / 100.f; // ending height pos
76+
77+
ss->setPositionY(getScaledContentHeight() * yA);
78+
ss->setRotation(360.f * (yA * yB)); // random rotation
79+
80+
auto move = CCEaseIn::create(CCMoveTo::create(10.f, { getScaledContentWidth() + 192.f, getScaledContentHeight() * yB }), 1.f);
81+
auto rotate = CCEaseOut::create(CCRotateBy::create(12.5f, 45.f), 1.f);
82+
83+
auto action = CCSpawn::createWithTwoActions(move, rotate);
84+
ss->runAction(action);
85+
86+
log::info("Animated sprite successfully");
87+
} else {
88+
log::error("Sprite failed to load: {}", res.unwrapErr());
89+
ss->removeMeAndCleanup();
90+
}; });
91+
92+
ss->loadFromFile(fs::path(pngPath));
93+
addChild(ss);
94+
} else {
95+
log::error("ID is invalid");
96+
};
97+
} else {
98+
log::error("Mocking data file not found");
99+
};
100+
};
101+
};
102+
103+
return true;
104+
};
105+
};

src/modified/MenuLayer.cpp

Lines changed: 0 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
#include <Geode/modify/MenuLayer.hpp>
1010

1111
using namespace geode::prelude;
12-
using namespace geode::utils;
13-
using namespace matjson;
1412
using namespace horrible;
1513

1614
class $modify(HorribleMenuLayer, MenuLayer) {
@@ -25,9 +23,6 @@ class $modify(HorribleMenuLayer, MenuLayer) {
2523

2624
log::debug("Store Current FPS: {}", storedFPS);
2725

28-
auto rnd = rand() % 101;
29-
log::debug("chance {}", rnd);
30-
3126
if (auto bottomMenu = getChildByID("bottom-menu")) {
3227
auto btnSprite = CircleButtonSprite::createWithSpriteFrameName(
3328
"GJ_moonsIcon_001.png",
@@ -49,88 +44,6 @@ class $modify(HorribleMenuLayer, MenuLayer) {
4944
};
5045
};
5146

52-
// Show a LazySprite for the first PNG found in the save directory
53-
if (horribleMod && horribleMod->getSavedValue<bool>("mock", false)) {
54-
log::debug("mock feature enabled in MainMenu layer");
55-
56-
namespace fs = std::filesystem;
57-
58-
if (rnd <= static_cast<int>(horribleMod->getSettingValue<int64_t>("mock-chance"))) {
59-
auto mockConfigPath = fmt::format("{}\\mock.json", horribleMod->getSaveDir());
60-
auto mockConfig = file::readJson(fs::path(mockConfigPath));
61-
62-
log::debug("Reading path {}...", mockConfigPath);
63-
64-
if (mockConfig.isOk()) {
65-
log::debug("Read mocking config file");
66-
67-
auto mockConfigUnwr = mockConfig.unwrapOr(Value());
68-
69-
auto lvlUnwr = mockConfigUnwr.begin();
70-
std::advance(lvlUnwr, rnd % mockConfigUnwr.size());
71-
72-
auto id = lvlUnwr->getKey().value_or("");
73-
auto percent = lvlUnwr->asInt().unwrapOr(99);
74-
75-
if (!id.empty()) {
76-
log::debug("ID {} with percentage {} is valid", id, percent);
77-
78-
std::string pngPath = fmt::format("{}\\{}.png", horribleMod->getSaveDir(), id);
79-
80-
log::info("Displaying {}", pngPath);
81-
82-
auto ss = LazySprite::create({ 192.f, 108.f });
83-
ss->setID("mock"_spr);
84-
ss->setScale(0.25);
85-
ss->setAnchorPoint({ 0.5, 0.5 });
86-
ss->setPosition({ -192.f, getScaledContentHeight() / 2.f });
87-
ss->setZOrder(1000);
88-
89-
ss->setLoadCallback([this, ss, percent, rnd](Result<> res) {
90-
if (res.isOk()) {
91-
log::info("Sprite loaded successfully from save dir PNG");
92-
93-
auto percLabelText = fmt::format("{}%", percent);
94-
95-
auto percLabel = CCLabelBMFont::create(percLabelText.c_str(), "bigFont.fnt");
96-
percLabel->setID("percentage");
97-
percLabel->setPosition({ ss->getScaledContentWidth() / 2.f, ss->getScaledContentHeight() / 2.f });
98-
percLabel->setAlignment(CCTextAlignment::kCCTextAlignmentLeft);
99-
percLabel->ignoreAnchorPointForPosition(false);
100-
percLabel->setAnchorPoint({ 0, 0 });
101-
percLabel->setScale(2.5);
102-
103-
ss->addChild(percLabel);
104-
105-
float yA = static_cast<float>(rnd) / 100.f;
106-
float yB = static_cast<float>(rnd) / 100.f;
107-
108-
ss->setPositionY(getScaledContentHeight() * yA);
109-
ss->setRotation(360.f * (yA * yB));
110-
111-
auto move = CCEaseIn::create(CCMoveTo::create(10.f, { getScaledContentWidth() + 192.f, getScaledContentHeight() * yB }), 1.f);
112-
auto rotate = CCEaseOut::create(CCRotateBy::create(12.5f, 45.f), 1.f);
113-
114-
auto action = CCSpawn::createWithTwoActions(move, rotate);
115-
ss->runAction(action);
116-
117-
log::info("Animated sprite successfully");
118-
} else {
119-
log::error("Sprite failed to load: {}", res.unwrapErr());
120-
ss->removeMeAndCleanup();
121-
}; });
122-
123-
ss->loadFromFile(fs::path(pngPath));
124-
addChild(ss);
125-
} else {
126-
log::error("ID is invalid");
127-
};
128-
} else {
129-
log::error("Mocking data file not found");
130-
};
131-
};
132-
};
133-
13447
return true;
13548
};
13649

0 commit comments

Comments
 (0)