added music

This commit is contained in:
Vlad 2026-04-19 14:13:27 +06:00
parent f8b4174eea
commit c9e9755e28
5 changed files with 93 additions and 12 deletions

View File

@ -222,7 +222,11 @@ namespace ZL
std::cout << "Audio initialization failed" << std::endl;
}
menuManager.setupMainMenu();
menuManager.onGameStateChanged = [this](GameState newState) {
this->updateMusicForGameState(newState);
};
menuManager.setupMainMenu();
SDL_ShowCursor(SDL_ENABLE);
}
@ -656,17 +660,64 @@ namespace ZL
}
void Game::changeLocation(const std::string& newLocationId)
void Game::updateMusicForGameState(GameState newState)
{
if (!currentLocation) {
std::cout << "[GAME] No current location" << std::endl;
std::string musicTrack;
switch (newState) {
case GameState::MainMenu:
case GameState::AboutMenu:
case GameState::HelpScreen:
musicTrack = "audio/background.wav";
break;
default:
return;
}
if (currentMusicTrack == musicTrack) {
return;
}
std::cout << "[GAME] Changing location to: " << newLocationId << std::endl;
currentLocation->unload();
currentLocation = std::make_shared<Location>(renderer, inventory, newLocationId);
currentLocation->setup();
std::cout << "[GAME] Location changed" << std::endl;
currentMusicTrack = musicTrack;
if (audioPlayer) {
audioPlayer->stopMusicAsync();
audioPlayer->playMusicAsync(musicTrack, -1);
}
}
void Game::updateMusicForLocation(const std::string& locationId)
{
std::string musicTrack;
if (locationId == "default") {
musicTrack = "audio/lullaby-music-vol20-186394--online-audio-convert.com.ogg";
}
else if (locationId == "forest") {
musicTrack = "audio/background.wav";
}
else {
musicTrack = "audio/lullaby-music-vol20-186394--online-audio-convert.com.ogg";
}
if (currentMusicTrack == musicTrack) {
return;
}
currentMusicTrack = musicTrack;
if (audioPlayer) {
audioPlayer->stopMusicAsync();
audioPlayer->playMusicAsync(musicTrack, -1);
}
}
void Game::changeLocation(const std::string& locId)
{
currentLocation = std::make_unique<Location>(renderer, inventory, locId);
currentLocation->setup();
if (menuManager.getState() == GameState::Gameplay) {
updateMusicForLocation(locId);
}
}
} // namespace ZL

View File

@ -21,7 +21,8 @@
#include <unordered_set>
#include "Location.h"
#include "AudioPlayerAsync.h"
#include <cstdint>
#include <chrono>
namespace ZL {
class Game {
@ -54,14 +55,16 @@ namespace ZL {
MenuManager menuManager;
void changeLocation(const std::string& locId);
void updateMusicForGameState(GameState newState);
void updateMusicForLocation(const std::string& locationId);
private:
bool rightMouseDown = false;
int lastMouseX = 0;
int lastMouseY = 0;
std::unique_ptr<AudioPlayerAsync> audioPlayer;
int64_t getSyncTimeMs();
std::unique_ptr<AudioPlayerAsync> audioPlayer;
std::string currentMusicTrack;
void processTickCount();
void drawScene();
void drawUI();

View File

@ -10,6 +10,8 @@
#include "ScriptEngine.h"
#include "dialogue/DialogueSystem.h"
#include <string>
namespace ZL
{
@ -45,6 +47,7 @@ namespace ZL
public:
Location(Renderer& iRenderer, Inventory& iInventory, const std::string& locId = "default");
std::string getLocationId() const { return locationId; }
std::shared_ptr<Texture> roomTexture;
VertexRenderStruct roomMesh;

View File

@ -17,9 +17,14 @@ namespace ZL {
void MenuManager::setupMainMenu()
{
previousState = currentState;
currentState = GameState::MainMenu;
uiManager.loadFromFile("resources/config2/main_menu.json", renderer, CONST_ZIP_FILE);
if (onGameStateChanged) {
onGameStateChanged(GameState::MainMenu);
}
uiManager.setButtonCallback("startButton", [this](const std::string&) {
startGame();
});
@ -35,9 +40,14 @@ namespace ZL {
void MenuManager::setupHelpMenu()
{
previousState = currentState;
currentState = GameState::HelpScreen;
uiManager.loadFromFile("resources/config2/help.json", renderer, CONST_ZIP_FILE);
if (onGameStateChanged) {
onGameStateChanged(GameState::HelpScreen);
}
uiManager.setButtonCallback("backButton", [this](const std::string&) {
enterMainMenu();
});
@ -45,9 +55,14 @@ namespace ZL {
void MenuManager::setupAboutMenu()
{
previousState = currentState;
currentState = GameState::AboutMenu;
uiManager.loadFromFile("resources/config2/about.json", renderer, CONST_ZIP_FILE);
if (onGameStateChanged) {
onGameStateChanged(GameState::AboutMenu);
}
uiManager.setButtonCallback("backButton", [this](const std::string&) {
enterMainMenu();
});
@ -70,7 +85,12 @@ namespace ZL {
void MenuManager::startGame()
{
previousState = currentState;
currentState = GameState::Gameplay;
if (onGameStateChanged) {
onGameStateChanged(GameState::Gameplay);
}
}
/*

View File

@ -3,6 +3,7 @@
#include "Environment.h"
#include "render/TextureManager.h"
#include "UiManager.h"
#include <functional>
namespace ZL {
@ -57,6 +58,7 @@ namespace ZL {
public:
UiManager uiManager;
GameState currentState = GameState::MainMenu;
GameState previousState = GameState::MainMenu;
MenuManager(Renderer& iRenderer);
@ -69,6 +71,8 @@ namespace ZL {
void startGame();
GameState getState() const { return currentState; }
GameState getPreviousState() const { return previousState; }
std::function<void(GameState)> onGameStateChanged;
/*
// Returns true for states where Space should render and run (Gameplay, GameOver, ConnectionLost)