Skip to content

Commit

Permalink
stuff, i don't know what the hell i made, please fix yourself
Browse files Browse the repository at this point in the history
  • Loading branch information
pundang committed Jul 7, 2024
1 parent 6a12459 commit 737e0da
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 24 deletions.
1 change: 0 additions & 1 deletion about.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ This mod allows you to change the menu song every time you open the game, pickin

# Notes

- The song changes every time you restart the game, so you will not get another song every time you go back to the menu.
- In the future i'll add a setting to toggle between using NG songs or your own songs.

# Thanks to!
Expand Down
2 changes: 1 addition & 1 deletion changelog.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# v1.1.0

- Updated Geode target version to `v3.1.1`
- Fixed crash when installing the mod (missing textures crash)
- Fixed crash when opening the game (missing textures crash)
- Fixed random song picker being on a loop for every song downloaded.
- Moved song class to its own file.
- The mod should now check for the songs path when loaded, not when executed.
Expand Down
10 changes: 8 additions & 2 deletions mod.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"geode": "3.1.1",
"geode": "3.2.0",
"gd": {
"win": "2.206",
"android": "2.206"
Expand All @@ -8,7 +8,7 @@
"id": "elnexreal.menuloop_randomizer",
"name": "Menu Loop Randomizer",
"developer": "elnexreal",
"description": "Randomize your menu loop when opening the game.",
"description": "Menu Loop randomizer for GD.",
"tags": [
"music",
"interface",
Expand All @@ -25,6 +25,12 @@
"default": true,
"description": "Shows a notification with the name of the currently playing song.",
"name": "Show notification"
},
"randomizeWhenExiting": {
"type": "bool",
"default": true,
"description": "Randomize the song when you exit a level (for example when exiting the editor)",
"name": "Randomize on level exit"
}
},
"resources": {
Expand Down
12 changes: 12 additions & 0 deletions src/Utils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include "Utils.hpp"
#include <random>

int Utils::randomIndex(int size) {
// select a random item from the vector and return the path
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dist(0, size - 1);
int randomIndex = dist(gen);

return randomIndex;
}
6 changes: 6 additions & 0 deletions src/Utils.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#pragma once

class Utils {
public:
static int randomIndex(int size);
};
46 changes: 26 additions & 20 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
#include "PlayingCard.hpp"
#include "Song.hpp"
#include "Utils.hpp"
#include <Geode/Geode.hpp>
#include <Geode/modify/GameManager.hpp>
#include <Geode/modify/MenuLayer.hpp>
#include <random>
#include <Geode/modify/PauseLayer.hpp>
#include <vector>

using namespace geode::prelude;

// global variables
std::vector<Song> songs;
Song *selectedSong;
Song selectedSong;

$on_mod(Loaded) {
// get the path for the songs
Expand All @@ -29,17 +30,22 @@ Song *selectedSong;
}
}

// select a random item from the vector and return the path
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dist(0, songs.size() - 1);
int randomIndex = dist(gen);
selectedSong = std::move(&songs[randomIndex]);
selectedSong = std::move(songs[Utils::randomIndex(songs.size())]);
}

struct GameManagerHook : Modify<GameManagerHook, GameManager> {
gd::string getMenuMusicFile() {
return selectedSong->path;
return selectedSong.path;
}
};

struct PauseLayerHook : Modify<PauseLayerHook, PauseLayer> {
void onQuit(CCObject *sender) {
if (Mod::get()->getSettingValue<bool>("randomizeWhenExiting")) {
selectedSong = std::move(songs[Utils::randomIndex(songs.size())]);
}

PauseLayer::onQuit(sender);
}
};

Expand All @@ -54,13 +60,13 @@ struct MenuLayerHook : Modify<MenuLayerHook, MenuLayer> {
auto cardSettingValue = Mod::get()->getSettingValue<bool>("nowPlayingCard");

if (cardSettingValue) {
if (auto songObject = downloadManager->getSongInfoObject(stoi(selectedSong->id))) {
selectedSong->name = songObject->m_songName;
if (auto songObject = downloadManager->getSongInfoObject(stoi(selectedSong.id))) {
selectedSong.name = songObject->m_songName;
} else {
selectedSong->name = "Unknown";
selectedSong.name = "Unknown";
}

auto card = PlayingCard::create(selectedSong->name, selectedSong->id);
auto card = PlayingCard::create(selectedSong.name, selectedSong.id);
card->position.x = screenSize.width / 2.0f;
card->position.y = screenSize.height;

Expand All @@ -71,13 +77,13 @@ struct MenuLayerHook : Modify<MenuLayerHook, MenuLayer> {
card->setPosition(defaultPos);
this->addChild(card);

// auto sequence = CCSequence::create(
// CCEaseInOut::create(CCMoveTo::create(1.5f, {posx, posy - 25.0f}), 2.0f),
// CCDelayTime::create(0.5f),
// CCEaseInOut::create(CCMoveTo::create(1.5f, {posx, posy}), 2.0f),
// nullptr
// );
// card->runAction(sequence);
auto sequence = CCSequence::create(
CCEaseInOut::create(CCMoveTo::create(1.5f, {posx, posy - 25.0f}), 2.0f),
CCDelayTime::create(0.5f),
CCEaseInOut::create(CCMoveTo::create(1.5f, {posx, posy}), 2.0f),
nullptr
);
card->runAction(sequence);
}

return true;
Expand Down

0 comments on commit 737e0da

Please sign in to comment.