From d9a4e21b3c4272ba500b92ff85d251c30227e758 Mon Sep 17 00:00:00 2001 From: onon1101 Date: Thu, 25 Apr 2024 22:23:38 +0800 Subject: [PATCH] try fix merge --- include/App.hpp | 17 ++-- include/Music/BeatHeart.h | 62 ------------- include/Music/Music.h | 84 ----------------- include/Music/Player.h | 40 +++++++++ include/Music/RhythmIndicator.h | 54 ----------- include/Music/Tempo.h | 72 --------------- src/App.cpp | 43 +++++---- src/Music/BeatHeart.cpp | 34 ------- src/Music/Music.cpp | 88 ------------------ src/Music/Player.cpp | 49 ++++++++++ src/Music/RhythmIndicator.cpp | 152 ------------------------------- src/Music/Tempo.cpp | 155 -------------------------------- 12 files changed, 126 insertions(+), 724 deletions(-) delete mode 100644 include/Music/BeatHeart.h delete mode 100644 include/Music/Music.h create mode 100644 include/Music/Player.h delete mode 100644 include/Music/RhythmIndicator.h delete mode 100644 src/Music/BeatHeart.cpp delete mode 100644 src/Music/Music.cpp create mode 100644 src/Music/Player.cpp delete mode 100644 src/Music/RhythmIndicator.cpp delete mode 100644 src/Music/Tempo.cpp diff --git a/include/App.hpp b/include/App.hpp index e7371016..f42d501e 100644 --- a/include/App.hpp +++ b/include/App.hpp @@ -4,10 +4,15 @@ #include "pch.hpp" // IWYU pragma: export #include "Dungeon/Map.h" -#include "Game/Camera.h" -#include "Music/Music.h" -#include "Player/Player.h" -#include "Settings/Background.hpp" +#include "Player.h" +#include "Player/Equipment/Shovel.h" +#include "Player/Items/Coin.h" +#include "Player/Items/Diamond.h" +#include "Player/Items/Heart.h" +#include "Player/Items/Tools.h" +#include "SpriteSheet.hpp" +#include "Util/Text.hpp" +#include "pch.hpp" // IWYU pragma: export class App { public: @@ -37,8 +42,8 @@ class App { bool m_IsMainMenu = true; // music - std::shared_ptr m_MusicSystem = - std::make_shared(); + // std::shared_ptr m_MusicSystem = + // std::make_shared(); // settings glm::vec2 m_AniPlayerDestination = {0.0f, 0.0f}; diff --git a/include/Music/BeatHeart.h b/include/Music/BeatHeart.h deleted file mode 100644 index e4ff32fc..00000000 --- a/include/Music/BeatHeart.h +++ /dev/null @@ -1,62 +0,0 @@ -// -// Created by adven on 2024/3/27. -// - -#ifndef FUCK_PTSD_BEATHEART_H -#define FUCK_PTSD_BEATHEART_H - -#include - -#include "Game/ToolBoxs.h" -#include "Settings/GameElement.h" -#include "Settings/SpriteSheet.hpp" -#include "Settings/Window.hpp" - -namespace Music { -// class Display final { -class BeatHeart final { -public: - explicit BeatHeart(); - virtual ~BeatHeart() = default; - - void Update(); - - [[nodiscard]] - std::shared_ptr getGameElement() const { - return m_BeatHeart; - }; - - void setMusicSpeed(float speed) { m_MusicSpeed = speed; }; - void setOffset(int16_t offset) { m_offset = offset; }; - void setTriggerTime(std::size_t time) { m_triggerTime = time; }; - void setCurrentMusicTime(std::size_t time) { - m_CurrentMusicTime = time + m_offset; - }; - void setTriggerRange(std::size_t range) { m_range = range; }; - void setHeartBeatTime(std::size_t time) { m_heartBeatTime = time; }; - -private: - std::string m_HeartImagePath = ASSETS_DIR "/gui/TEMP_beat_heart.png"; - - float m_MusicSpeed = 1.0f; - - std::size_t m_triggerTime = 0; - std::size_t m_CurrentMusicTime = 0; - int16_t m_offset = 0; - std::size_t m_heartBeatTime = 0; - std::size_t m_range = 100; - - glm::vec2 m_Scale = {DUNGEON_SCALE, DUNGEON_SCALE}; - glm::vec2 m_Position = { - 0, - -(static_cast(WINDOW_HEIGHT) / 2) + (52 * DUNGEON_SCALE) / 2 - + DUNGEON_TILE_WIDTH - }; - std::size_t m_ZIndex = 100; - - std::shared_ptr m_TempoHeartImage; - std::shared_ptr m_BeatHeart = std::make_shared(); -}; -} // namespace Music - -#endif // FUCK_PTSD_BEATHEART_H diff --git a/include/Music/Music.h b/include/Music/Music.h deleted file mode 100644 index 60cad421..00000000 --- a/include/Music/Music.h +++ /dev/null @@ -1,84 +0,0 @@ -// -// Created by adven on 2024/3/27. -// - -#ifndef FUCK_PTSD_MUSIC_H -#define FUCK_PTSD_MUSIC_H - -#include "soloud.h" -#include "soloud_speech.h" -#include "soloud_thread.h" -#include "soloud_wav.h" - -#include "BeatHeart.h" -#include "RhythmIndicator.h" -#include "Tempo.h" - -namespace Music { -class Player final { -public: - explicit Player(); - ~Player(); - - void setEnable(); - - void playMusic(const std::string& musicPath); - - void playMusic(const std::string& musicPath, bool setMusicLooping); - - // let music to this speed - void setSpeed(float musicSpeed); - - // plus the speed to current speed, and play. - void speedUp(float musicSpeed); - - void skipToTargetTime(float time); - - void clickEvent() { m_tempo.keyBoardClick(); }; - - void readTempoFile(const std::string& path); - - bool TempoTrigger() { return m_tempo.canBeClick(); } - - std::shared_ptr getGameObject(); - - void Update(); - - void setTempoOffset(int16_t offset) { m_TempoOffset = offset; }; - void setTempoRange(std::size_t range) { m_TempoRange = range; }; - void setDisplayOffset(int16_t offset) { m_DisplayOffset = offset; }; - void setDisplayHeartBeatDurationTime(std::size_t time) { - m_DisplayHeartBeatDurationTime = time; - }; - - std::size_t getTempoIndex(); - std::size_t getTempoTime(); - -private: - float m_currentSpeed = 1.0f; - - Tempo m_tempo; - BeatHeart m_display; - RhythmIndicator m_rhythmIndicator; - - /* ms */ - int16_t m_TempoOffset = 0; - std::size_t m_TempoRange = 500; - /* bool isShowHeartBeat = false; */ - bool isShowHeartBeat = true; - int16_t m_DisplayOffset = -30; - std::size_t m_DisplayRange = 100; - std::size_t m_DisplayHeartBeatDurationTime = 80; - - SoLoud::handle m_musicHandle; - - SoLoud::Soloud m_soloud; // SoLoud engine core - SoLoud::Speech m_speech; // A sound source (speech, in this case) - std::shared_ptr m_music; - - std::shared_ptr m_MusicObject = std::make_shared( - ); -}; -} // namespace Music - -#endif // FUCK_PTSD_MUSIC_H diff --git a/include/Music/Player.h b/include/Music/Player.h new file mode 100644 index 00000000..3a0f637c --- /dev/null +++ b/include/Music/Player.h @@ -0,0 +1,40 @@ +#ifndef MUSIC_PLAYER_H +#define MUSIC_PLAYER_H + +#include + +#include "soloud.h" +#include "soloud_speech.h" +#include "soloud_thread.h" +#include "soloud_wav.h" + +namespace Music { +class Player { +public: + static void Init(); + + static void PlayMusic(const std::string& path, bool loop = false); + static void PlayMusic( + const std::string& path, + bool loop, + float volume, + float speed + ); + + static void StopMusic(); + + static void Shutdown(); + + static void SetVolume(float volume); + + static void SetSpeed(float speed); + +private: + static SoLoud::Soloud m_MusicSystem; + static std::shared_ptr m_Wav; + static SoLoud::Speech m_Speech; + static SoLoud::handle m_MusicHandle; +}; +} // namespace Music + +#endif // MUSIC_PLAYER_H diff --git a/include/Music/RhythmIndicator.h b/include/Music/RhythmIndicator.h deleted file mode 100644 index de6be4d0..00000000 --- a/include/Music/RhythmIndicator.h +++ /dev/null @@ -1,54 +0,0 @@ -// -// Created by adven on 2024/4/19. -// - -#ifndef FUCK_PTSD_RHYTHMINDICATOR_H -#define FUCK_PTSD_RHYTHMINDICATOR_H - -#include - -#include "GameElement.h" -#include "Util/Image.hpp" -#include "Util/Logger.hpp" -#include "Util/Time.hpp" - -namespace Music { - -class RhythmIndicator final { -public: - explicit RhythmIndicator(); - - void SetTempoTriggerList(std::vector tempoTriggerList); - - std::shared_ptr GetGameElement(); - - void Update(); - -private: - // functions - void CreateGameElement(std::size_t number); - void InitIndicationPosition(); - std::size_t CountTempoNumber(std::size_t second); - - // settings - const std::size_t m_tempoRange = 2000; // 2 sec - std::size_t m_startBeatIndex = 0; - std::size_t m_tempoNumber = 0; - std::size_t m_lastTime = 0; - - std::vector m_tempoTriggerList; - - // graph - const glm::vec2 m_centerPosition = {0, -310}; - - std::shared_ptr m_rhythmIndicator; - std::vector> m_IndicatorList; - std::vector> m_IndicatorListLeft; - - std::shared_ptr m_redIndicator; - std::shared_ptr m_blueIndicator; - std::shared_ptr m_greenIndicator; -}; -} // namespace Music - -#endif // FUCK_PTSD_RHYTHMINDICATOR_H diff --git a/include/Music/Tempo.h b/include/Music/Tempo.h index 991f4f98..e69de29b 100644 --- a/include/Music/Tempo.h +++ b/include/Music/Tempo.h @@ -1,72 +0,0 @@ -// -// Created by adven on 2024/3/27. -// - -#ifndef FUCK_PTSD_TEMPO_H -#define FUCK_PTSD_TEMPO_H - -#include -#include -#include -#include -#include - -#include "Util/Logger.hpp" - -namespace Music { -class Tempo final { -public: - explicit Tempo(); - ~Tempo(); - - // must read one line in file, it not be '\n' - void readTempoFile(const std::string& txtFilePath); - - bool canBeClick(); - - void keyBoardClick(); - - std::size_t getTempo(); - std::size_t getTempoIndex() const; - - void setMusicSpeed(float speed) { m_MusicSpeed = speed; }; - void setShowBeat(bool state) { isShowHeartBeat = state; }; - void setRange(std::size_t range) { m_range = range; }; - void setOffset(int16_t offset) { m_offset = offset; }; - - void setMusicCurrentTime(std::size_t time) { - m_duringTime = time + m_offset; - }; - void Update(); - - std::vector GetTempoTriggerList(); - -protected: - void UpdateTime(); - void UpdateTempoIndex(); - -private: - std::vector txtToVector( - const std::string& line, - const char splitChar - ); - - float m_MusicSpeed = 1.0f; - - bool isShowHeartBeat = false; - bool m_isWrongTimeClick = false; - std::size_t m_punishTimes = 0; - - std::size_t m_duringTime = 0; - std::size_t m_range = 500; - int16_t m_offset = 0; // (ms) - - std::size_t m_currentTempoTime = 0; - std::size_t m_tempoIndex = 0; - std::size_t m_currentTempoIndex = 0; - std::size_t m_tempoListLength = 0; - std::vector m_tempoList; -}; -} // namespace Music - -#endif // FUCK_PTSD_TEMPO_H diff --git a/src/App.cpp b/src/App.cpp index a2d0132b..26b8324c 100644 --- a/src/App.cpp +++ b/src/App.cpp @@ -20,7 +20,16 @@ void App::Start() { m_Camera->AddChild(m_Background->GetGameElement()); // play main background music - m_MusicSystem->playMusic(ASSETS_DIR "/music/intro_onlyMusic.ogg", true); + // m_MusicSystem->playMusic(ASSETS_DIR "/music/intro_onlyMusic.ogg", + // true); + Music::Player::Init(); + Music::Player::PlayMusic( + ASSETS_DIR "/music/intro_onlyMusic.ogg", + true, + 0.5f, + 1.0f + ); + // m_MusicSystem->skipToTargetTime(118.2f); } @@ -29,7 +38,7 @@ void App::Start() { m_IsMainMenu = false; } - m_MusicSystem->Update(); + // m_MusicSystem->Update(); m_Camera->Update(); if (Util::Input::IfExit()) { @@ -39,13 +48,13 @@ void App::Start() { return; } // play lobby music - // m_MusicSystem->playMusic(ASSETS_DIR"/music/lobby.ogg", true); - // m_MusicSystem->readTempoFile(ASSETS_DIR"/music/lobby.txt"); - // m_MusicSystem->setSpeed(1.2); + Music::Player::StopMusic(); + Music::Player::PlayMusic(ASSETS_DIR "/music/zone1_1.ogg", true); + Music::Player::SetVolume(0.1f); // play zone1 leve1 - m_MusicSystem->playMusic(ASSETS_DIR "/music/zone1_1.ogg", true); - m_MusicSystem->readTempoFile(ASSETS_DIR "/music/zone1_1.txt"); + // m_MusicSystem->playMusic(ASSETS_DIR "/music/zone1_1.ogg", true); + // m_MusicSystem->readTempoFile(ASSETS_DIR "/music/zone1_1.txt"); // remove background m_Camera->RemoveChild(m_Background->GetGameElement()); @@ -71,7 +80,7 @@ void App::Start() { m_Camera->AddChild(m_DungeonMap); // display the tempo heart in music System - m_Camera->AddUIChild(m_MusicSystem->getGameObject()); + // m_Camera->AddUIChild(m_MusicSystem->getGameObject()); m_CurrentState = State::UPDATE; } @@ -84,11 +93,11 @@ void App::Update() { // m_Coin->plusCoinNumber(10); // m_Diamond->plusDiamondNumber(10); // } - auto tempoIndex = m_MusicSystem->getTempoIndex(); - if (m_BeforeTempoIndex != tempoIndex) { - m_BeforeTempoIndex = tempoIndex; - m_DungeonMap->TempoTrigger(tempoIndex); - } + // auto tempoIndex = m_MusicSystem->getTempoIndex(); + // if (m_BeforeTempoIndex != tempoIndex) { + // m_BeforeTempoIndex = tempoIndex; + // m_DungeonMap->TempoTrigger(tempoIndex); + // } if (Util::Input::IsKeyDown(Util::Keycode::N)) { m_DungeonMap->LoadLevel(m_DungeonMap->GetLevelNum() + 1); @@ -100,8 +109,8 @@ void App::Update() { if ((Util::Input::IsKeyDown(Util::Keycode::W) || Util::Input::IsKeyDown(Util::Keycode::D) || Util::Input::IsKeyDown(Util::Keycode::S) - || Util::Input::IsKeyDown(Util::Keycode::A)) - && m_MusicSystem->TempoTrigger()) { + || Util::Input::IsKeyDown(Util::Keycode::A))) { + // && m_MusicSystem->TempoTrigger()) { glm::vec2 playerDestination = m_MainCharacter->GetGamePosition(); if (m_PlayerMoveDirect != Player::NONE) { @@ -196,7 +205,7 @@ void App::Update() { || Util::Input::IsKeyDown(Util::Keycode::D) || Util::Input::IsKeyDown(Util::Keycode::S) || Util::Input::IsKeyDown(Util::Keycode::A)) { - m_MusicSystem->clickEvent(); + // m_MusicSystem->clickEvent(); } if (Util::Input::IsKeyUp(Util::Keycode::ESCAPE) || Util::Input::IfExit()) { @@ -208,7 +217,7 @@ void App::Update() { // LOG_INFO("Music's tempo index: {}", m_MusicSystem->getTempoIndex()); // LOG_INFO("Music's tempo time: {}ms", m_MusicSystem->getTempoTime()); - m_MusicSystem->Update(); + // m_MusicSystem->Update(); m_MainCharacter->Update(); m_Camera->Update(); } diff --git a/src/Music/BeatHeart.cpp b/src/Music/BeatHeart.cpp deleted file mode 100644 index 4ddc3823..00000000 --- a/src/Music/BeatHeart.cpp +++ /dev/null @@ -1,34 +0,0 @@ -// -// Created by adven on 2024/3/28. -// -#include "Music/BeatHeart.h" - -Music::BeatHeart::BeatHeart() { - const auto HeartSize = ToolBoxs::CountImagePixel(m_HeartImagePath, 2, 1); - - m_TempoHeartImage = std::make_shared( - m_HeartImagePath, - HeartSize, - std::vector{0, 1}, - true, - 10, - false, - 0 - ); - - m_BeatHeart->SetDrawable(m_TempoHeartImage); - m_BeatHeart->SetPosition(m_Position); - m_BeatHeart->SetScale(m_Scale); - m_BeatHeart->SetZIndex(m_ZIndex); -} - -// Music::BeatHeart::~BeatHeart() {} - -void Music::BeatHeart::Update() { - // update the heart beat - if (m_CurrentMusicTime + m_offset > m_triggerTime + m_heartBeatTime) { - m_TempoHeartImage->SetCurrentFrame(0); - } else if (m_CurrentMusicTime + m_offset > m_triggerTime) { - m_TempoHeartImage->SetCurrentFrame(1); - } -} diff --git a/src/Music/Music.cpp b/src/Music/Music.cpp deleted file mode 100644 index b36d2fbe..00000000 --- a/src/Music/Music.cpp +++ /dev/null @@ -1,88 +0,0 @@ -// -// Created by adven on 2024/3/27. -// -#include "Music/Music.h" - -Music::Player::Player() { - m_soloud.init(); - - m_display.setOffset(m_DisplayOffset); - m_display.setTriggerRange(m_DisplayRange); - m_display.setMusicSpeed(m_currentSpeed); - m_display.setHeartBeatTime(m_DisplayHeartBeatDurationTime); - - m_tempo.setOffset(m_TempoOffset); - m_tempo.setRange(m_TempoRange); - m_tempo.setShowBeat(isShowHeartBeat); - m_tempo.setMusicSpeed(m_currentSpeed); -} - -Music::Player::~Player() { - m_soloud.deinit(); -} - -void Music::Player::setSpeed(float musicSpeed) { - m_currentSpeed = musicSpeed; - m_soloud.setRelativePlaySpeed(m_musicHandle, musicSpeed); -} -void Music::Player::speedUp(float musicSpeed) { - m_soloud.setRelativePlaySpeed(m_musicHandle, m_currentSpeed + musicSpeed); - m_currentSpeed = m_currentSpeed + musicSpeed; -} - -std::size_t Music::Player::getTempoIndex() { - return m_tempo.getTempoIndex(); -} - -std::size_t Music::Player::getTempoTime() { - return m_tempo.getTempo(); -} - -void Music::Player::playMusic(const std::string& musicPath) { - m_music = std::make_shared(); - m_music->load(musicPath.c_str()); - m_musicHandle = m_soloud.play(*m_music); -} -void Music::Player::playMusic( - const std::string& musicPath, - bool setMusicLooping -) { - m_music = std::make_shared(); - m_music->load(musicPath.c_str()); - m_music->setLooping(setMusicLooping); - m_musicHandle = m_soloud.play(*m_music); -} - -void Music::Player::skipToTargetTime(float time) { - m_soloud.seek(m_musicHandle, time); -} - -std::shared_ptr Music::Player::getGameObject() { - m_MusicObject->AddChild(m_display.getGameElement()); - m_MusicObject->AddChild(m_rhythmIndicator.GetGameElement()); - - m_MusicObject->SetVisible(false); - return m_MusicObject; -} - -void Music::Player::Update() { - const std::size_t musicLength = m_music->getLength() * 1000; - const std::size_t CircleTime = m_soloud.getStreamTime(m_musicHandle) * 1000; - const std::size_t CurrentMusicTime = CircleTime % musicLength; - - m_tempo.setMusicCurrentTime(CurrentMusicTime * m_currentSpeed); - m_tempo.Update(); - - m_display.setTriggerTime(m_tempo.getTempo()); - m_display.setCurrentMusicTime(CurrentMusicTime * m_currentSpeed); - m_display.Update(); - - m_rhythmIndicator.Update(); -}; - -void Music::Player::readTempoFile(const std::string& path) { - m_tempo.readTempoFile(path); - - const std::vector tempoList = m_tempo.GetTempoTriggerList(); - m_rhythmIndicator.SetTempoTriggerList(tempoList); -} diff --git a/src/Music/Player.cpp b/src/Music/Player.cpp new file mode 100644 index 00000000..e49fc37c --- /dev/null +++ b/src/Music/Player.cpp @@ -0,0 +1,49 @@ +#include "Music/Player.h" + +void Music::Player::Init() { + m_MusicSystem.init(); +} + +void Music::Player::PlayMusic(const std::string& path, bool loop) { + // play main background music + + m_Wav->load(path.c_str()); + m_MusicHandle = m_MusicSystem.play(*m_Wav); +} + +void Music::Player::PlayMusic( + const std::string& path, + bool loop, + float volume, + float speed +) { + m_Wav->load(path.c_str()); + m_MusicHandle = m_MusicSystem.play(*m_Wav); + m_MusicSystem.setLooping(m_MusicHandle, loop); + m_MusicSystem.setVolume(m_MusicHandle, volume); + m_MusicSystem.setRelativePlaySpeed(m_MusicHandle, speed); +} + +void Music::Player::StopMusic() { + m_MusicSystem.stop(m_MusicHandle); +} + +void Music::Player::Shutdown() { + m_MusicSystem.stopAll(); + m_MusicSystem.deinit(); +} + +void Music::Player::SetVolume(float volume) { + m_MusicSystem.setVolume(m_MusicHandle, volume); +} + +void Music::Player::SetSpeed(float speed) { + m_MusicSystem.setRelativePlaySpeed(m_MusicHandle, speed); +} + +std::shared_ptr Music::Player::m_Wav = + std::make_shared(); + +SoLoud::Soloud Music::Player::m_MusicSystem; +SoLoud::Speech Music::Player::m_Speech; +SoLoud::handle Music::Player::m_MusicHandle; diff --git a/src/Music/RhythmIndicator.cpp b/src/Music/RhythmIndicator.cpp deleted file mode 100644 index 9d289603..00000000 --- a/src/Music/RhythmIndicator.cpp +++ /dev/null @@ -1,152 +0,0 @@ -// -// Created by adven on 2024/4/19. -// - -#include "Music/RhythmIndicator.h" - -Music::RhythmIndicator::RhythmIndicator() - : m_rhythmIndicator(std::make_shared()), - m_redIndicator(std::make_shared( - ASSETS_DIR "/gui/TEMP_beat_marker_red.png" - )), - m_blueIndicator( - std::make_shared(ASSETS_DIR "/gui/TEMP_beat_marker.png") - ), - m_greenIndicator(std::make_shared( - ASSETS_DIR "/gui/TEMP_beat_marker_green.png" - )) { - // m_rhythmIndicator->SetVisible(false); -} - -std::shared_ptr Music::RhythmIndicator::GetGameElement() { - return m_rhythmIndicator; -} - -void Music::RhythmIndicator::SetTempoTriggerList( - std::vector tempoTriggerList -) { - m_tempoTriggerList = std::move(tempoTriggerList); - m_tempoNumber = CountTempoNumber(m_tempoRange); - - CreateGameElement(m_tempoNumber); - InitIndicationPosition(); -} - -void Music::RhythmIndicator::Update() { - auto currentTime = Util::Time::GetElapsedTimeMs(); - - const auto intervalTime = currentTime - m_lastTime; - m_lastTime = currentTime; - - // LOG_INFO(intervalTime); - if (intervalTime >= 100) { - return; - } - - const auto intervalPixel = 720.0f / static_cast(m_tempoNumber); - - std::size_t index = m_startBeatIndex; - - if (index + m_tempoNumber == m_tempoTriggerList.size()) { - m_startBeatIndex = 0; - return; - } - - for (const auto& elem : m_IndicatorList) { - const std::size_t tempoIntervalTime = m_tempoTriggerList[index + 1] - - m_tempoTriggerList[index]; - const float moveSpeed = (intervalPixel - / static_cast(tempoIntervalTime)) - * static_cast(intervalTime); - - const auto position = elem->GetPosition(); - const auto movePosition = glm::vec2({position.x + moveSpeed, position.y} - ); - - index += 1; - if (movePosition.x >= 0) { - m_startBeatIndex += 1; - elem->SetPosition({-720.0f, -310.0f}); - continue; - } - elem->SetPosition(movePosition); - } - - index = m_startBeatIndex; - for (const auto& elem : m_IndicatorListLeft) { - if (index + 1 == m_tempoTriggerList.size()) { - m_startBeatIndex = 0; - return; - } - const std::size_t tempoIntervalTime = m_tempoTriggerList[index + 1] - - m_tempoTriggerList[index]; - const float moveSpeed = (intervalPixel - / static_cast(tempoIntervalTime)) - * static_cast(intervalTime); - - const auto position = elem->GetPosition(); - const auto movePosition = glm::vec2({position.x - moveSpeed, position.y} - ); - - index += 1; - if (movePosition.x <= 0) { - // m_startBeatIndex += 1; - elem->SetPosition({720.0f, -310.0f}); - continue; - } - elem->SetPosition(movePosition); - } -} - -std::size_t Music::RhythmIndicator::CountTempoNumber(std::size_t second) { - std::size_t index = 0; - for (std::size_t trigger : m_tempoTriggerList) { - if (trigger >= second) { - return index; - } - index++; - } - return index; -} - -void Music::RhythmIndicator::CreateGameElement(std::size_t number) { - for (std::size_t i = 0; i < number; i++) { - auto object = std::make_shared(); - object->SetDrawable(m_blueIndicator); - object->SetZIndex(99); - object->SetScale({3.0, 2.5}); - object->SetPosition(m_centerPosition); - m_IndicatorList.push_back(object); - - m_rhythmIndicator->AddChild(object); - } - - for (std::size_t i = 0; i < number; i++) { - auto object = std::make_shared(); - object->SetDrawable(m_blueIndicator); - object->SetZIndex(99); - object->SetScale({3.0, 2.5}); - object->SetPosition(m_centerPosition); - m_IndicatorListLeft.push_back(object); - - m_rhythmIndicator->AddChild(object); - } -} - -void Music::RhythmIndicator::InitIndicationPosition() { - auto intervalSpace = 720.0f / static_cast(m_tempoNumber); - - auto position = m_centerPosition; - for (const auto& elem : m_IndicatorList) { - elem->SetPosition(position); - - position.x -= intervalSpace; - } - - position = m_centerPosition; - for (const auto& elem : m_IndicatorListLeft) { - elem->SetPosition(position); - - position.x += intervalSpace; - } -} diff --git a/src/Music/Tempo.cpp b/src/Music/Tempo.cpp deleted file mode 100644 index f03791a3..00000000 --- a/src/Music/Tempo.cpp +++ /dev/null @@ -1,155 +0,0 @@ -// -// Created by adven on 2024/3/27. -// -#include "Music/Tempo.h" -#include "Util/Logger.hpp" - -Music::Tempo::Tempo() {} - -Music::Tempo::~Tempo() {} - -void Music::Tempo::readTempoFile(const std::string& txtFilePath) { - std::ifstream txtTempoFile(txtFilePath); - - if (!txtTempoFile.is_open()) { - LOG_ERROR("fuck it can not open this file"); - return; - } - - std::string line; - std::getline(txtTempoFile, line); - - m_tempoList = txtToVector(line, ','); - - // 关闭文件流 - txtTempoFile.close(); -} - -std::vector Music::Tempo::txtToVector( - const std::string& line, - const char splitChar -) { - std::stringstream ss(line); - std::string item; - std::size_t transform = 0; - std::vector elems; - while (std::getline(ss, item, splitChar)) { - transform = std::stoi(item); - elems.push_back(std::move(transform)); - } - - m_tempoListLength = elems.size(); - return elems; -} - -// bool Music::Tempo::canBeClick() { -// const std::size_t tempoIndex = m_tempoIndex; -// -// const std::size_t triggerLower = m_tempoList[tempoIndex] - m_range; -// const std::size_t triggerUpper = m_tempoList[tempoIndex] + m_range; -// -// // if yes = true, no = false; -// return m_duringTime >= triggerLower && m_duringTime <= triggerUpper; -// } - -bool Music::Tempo::canBeClick() { - auto tempoIndex = m_tempoIndex + m_punishTimes; - - // if (m_isWrongTimeClick == true) { - // tempoIndex -= 1; - // } - - // LOG_INFO(m_tempoList[tempoIndex]); - // LOG_DEBUG(m_duringTime); - - if (m_duringTime >= (m_tempoList[tempoIndex] - m_range) * m_MusicSpeed - && m_duringTime <= (m_tempoList[tempoIndex] + m_range) * m_MusicSpeed) { - return true; - } - return false; -} - -std::size_t Music::Tempo::getTempo() { - return m_tempoList.empty() ? 0 : m_currentTempoTime; -}; - -void Music::Tempo::Update() { - m_currentTempoTime = m_tempoList.empty() - ? 0 - : m_tempoList[m_currentTempoIndex] * m_MusicSpeed; - - // LOG_INFO(m_currentTempoIndex); - UpdateTempoIndex(); - UpdateTime(); -} - -void Music::Tempo::keyBoardClick() { - m_punishTimes = m_punishTimes < 1 ? m_punishTimes + 1 : m_punishTimes; - - LOG_INFO(m_tempoIndex); - - if (m_isWrongTimeClick == false) { - m_tempoIndex++; - } - - m_isWrongTimeClick = true; -}; - -void Music::Tempo::UpdateTime() { - if (m_tempoList.empty()) { - return; - } - - // song of looping end time, the bug - if (m_tempoIndex == 0 && m_duringTime > 5000) { - return; - } - - // LOG_INFO(m_isWrongTimeClick); - - // getTempo return value - if (m_duringTime - >= (m_tempoList[m_currentTempoIndex] - m_range) * m_MusicSpeed - && m_duringTime - <= (m_tempoList[m_currentTempoIndex] + m_range) * m_MusicSpeed) { - return; - } - - m_currentTempoIndex++; - m_isWrongTimeClick = false; - - if (m_duringTime >= (m_tempoList[m_tempoIndex] - m_range) * m_MusicSpeed - && m_duringTime - <= (m_tempoList[m_tempoIndex] + m_range) * m_MusicSpeed) { - return; - } else if (m_duringTime - <= (m_tempoList[m_tempoIndex + 1] - m_range) * m_MusicSpeed - && m_duringTime <= (m_tempoList[m_tempoIndex + 1] + m_range) - * m_MusicSpeed) { - return; - } - - m_punishTimes = m_punishTimes == 0 ? m_punishTimes : m_punishTimes - 1; - m_tempoIndex++; - - // if (isShowHeartBeat) { - // LOG_DEBUG(m_punishTimes); - // LOG_INFO(m_duringTime); - // LOG_INFO(m_currentTempoTime); - // } -} - -void Music::Tempo::UpdateTempoIndex() { - m_tempoIndex = m_tempoIndex + 1 >= m_tempoListLength ? 0 : m_tempoIndex; - m_currentTempoIndex = m_currentTempoIndex + 1 >= m_tempoListLength - ? 0 - : m_currentTempoIndex; -} - -std::size_t Music::Tempo::getTempoIndex() const { - return m_currentTempoIndex; -} - -std::vector Music::Tempo::GetTempoTriggerList() { - return m_tempoList; -}