added music
This commit is contained in:
parent
f8b4174eea
commit
c9e9755e28
69
src/Game.cpp
69
src/Game.cpp
@ -222,7 +222,11 @@ namespace ZL
|
|||||||
std::cout << "Audio initialization failed" << std::endl;
|
std::cout << "Audio initialization failed" << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
menuManager.setupMainMenu();
|
menuManager.onGameStateChanged = [this](GameState newState) {
|
||||||
|
this->updateMusicForGameState(newState);
|
||||||
|
};
|
||||||
|
|
||||||
|
menuManager.setupMainMenu();
|
||||||
|
|
||||||
SDL_ShowCursor(SDL_ENABLE);
|
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::string musicTrack;
|
||||||
std::cout << "[GAME] No current location" << std::endl;
|
|
||||||
|
switch (newState) {
|
||||||
|
case GameState::MainMenu:
|
||||||
|
case GameState::AboutMenu:
|
||||||
|
case GameState::HelpScreen:
|
||||||
|
musicTrack = "audio/background.wav";
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (currentMusicTrack == musicTrack) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
std::cout << "[GAME] Changing location to: " << newLocationId << std::endl;
|
|
||||||
currentLocation->unload();
|
currentMusicTrack = musicTrack;
|
||||||
currentLocation = std::make_shared<Location>(renderer, inventory, newLocationId);
|
if (audioPlayer) {
|
||||||
currentLocation->setup();
|
audioPlayer->stopMusicAsync();
|
||||||
std::cout << "[GAME] Location changed" << std::endl;
|
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
|
} // namespace ZL
|
||||||
|
|||||||
@ -21,7 +21,8 @@
|
|||||||
#include <unordered_set>
|
#include <unordered_set>
|
||||||
#include "Location.h"
|
#include "Location.h"
|
||||||
#include "AudioPlayerAsync.h"
|
#include "AudioPlayerAsync.h"
|
||||||
|
#include <cstdint>
|
||||||
|
#include <chrono>
|
||||||
namespace ZL {
|
namespace ZL {
|
||||||
|
|
||||||
class Game {
|
class Game {
|
||||||
@ -54,14 +55,16 @@ namespace ZL {
|
|||||||
MenuManager menuManager;
|
MenuManager menuManager;
|
||||||
|
|
||||||
void changeLocation(const std::string& locId);
|
void changeLocation(const std::string& locId);
|
||||||
|
void updateMusicForGameState(GameState newState);
|
||||||
|
void updateMusicForLocation(const std::string& locationId);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool rightMouseDown = false;
|
bool rightMouseDown = false;
|
||||||
int lastMouseX = 0;
|
int lastMouseX = 0;
|
||||||
int lastMouseY = 0;
|
int lastMouseY = 0;
|
||||||
std::unique_ptr<AudioPlayerAsync> audioPlayer;
|
|
||||||
|
|
||||||
int64_t getSyncTimeMs();
|
int64_t getSyncTimeMs();
|
||||||
|
std::unique_ptr<AudioPlayerAsync> audioPlayer;
|
||||||
|
std::string currentMusicTrack;
|
||||||
void processTickCount();
|
void processTickCount();
|
||||||
void drawScene();
|
void drawScene();
|
||||||
void drawUI();
|
void drawUI();
|
||||||
|
|||||||
@ -10,6 +10,8 @@
|
|||||||
|
|
||||||
#include "ScriptEngine.h"
|
#include "ScriptEngine.h"
|
||||||
#include "dialogue/DialogueSystem.h"
|
#include "dialogue/DialogueSystem.h"
|
||||||
|
#include <string>
|
||||||
|
|
||||||
namespace ZL
|
namespace ZL
|
||||||
{
|
{
|
||||||
|
|
||||||
@ -45,6 +47,7 @@ namespace ZL
|
|||||||
public:
|
public:
|
||||||
Location(Renderer& iRenderer, Inventory& iInventory, const std::string& locId = "default");
|
Location(Renderer& iRenderer, Inventory& iInventory, const std::string& locId = "default");
|
||||||
|
|
||||||
|
std::string getLocationId() const { return locationId; }
|
||||||
|
|
||||||
std::shared_ptr<Texture> roomTexture;
|
std::shared_ptr<Texture> roomTexture;
|
||||||
VertexRenderStruct roomMesh;
|
VertexRenderStruct roomMesh;
|
||||||
|
|||||||
@ -17,9 +17,14 @@ namespace ZL {
|
|||||||
|
|
||||||
void MenuManager::setupMainMenu()
|
void MenuManager::setupMainMenu()
|
||||||
{
|
{
|
||||||
|
previousState = currentState;
|
||||||
currentState = GameState::MainMenu;
|
currentState = GameState::MainMenu;
|
||||||
uiManager.loadFromFile("resources/config2/main_menu.json", renderer, CONST_ZIP_FILE);
|
uiManager.loadFromFile("resources/config2/main_menu.json", renderer, CONST_ZIP_FILE);
|
||||||
|
|
||||||
|
if (onGameStateChanged) {
|
||||||
|
onGameStateChanged(GameState::MainMenu);
|
||||||
|
}
|
||||||
|
|
||||||
uiManager.setButtonCallback("startButton", [this](const std::string&) {
|
uiManager.setButtonCallback("startButton", [this](const std::string&) {
|
||||||
startGame();
|
startGame();
|
||||||
});
|
});
|
||||||
@ -35,9 +40,14 @@ namespace ZL {
|
|||||||
|
|
||||||
void MenuManager::setupHelpMenu()
|
void MenuManager::setupHelpMenu()
|
||||||
{
|
{
|
||||||
|
previousState = currentState;
|
||||||
currentState = GameState::HelpScreen;
|
currentState = GameState::HelpScreen;
|
||||||
uiManager.loadFromFile("resources/config2/help.json", renderer, CONST_ZIP_FILE);
|
uiManager.loadFromFile("resources/config2/help.json", renderer, CONST_ZIP_FILE);
|
||||||
|
|
||||||
|
if (onGameStateChanged) {
|
||||||
|
onGameStateChanged(GameState::HelpScreen);
|
||||||
|
}
|
||||||
|
|
||||||
uiManager.setButtonCallback("backButton", [this](const std::string&) {
|
uiManager.setButtonCallback("backButton", [this](const std::string&) {
|
||||||
enterMainMenu();
|
enterMainMenu();
|
||||||
});
|
});
|
||||||
@ -45,9 +55,14 @@ namespace ZL {
|
|||||||
|
|
||||||
void MenuManager::setupAboutMenu()
|
void MenuManager::setupAboutMenu()
|
||||||
{
|
{
|
||||||
|
previousState = currentState;
|
||||||
currentState = GameState::AboutMenu;
|
currentState = GameState::AboutMenu;
|
||||||
uiManager.loadFromFile("resources/config2/about.json", renderer, CONST_ZIP_FILE);
|
uiManager.loadFromFile("resources/config2/about.json", renderer, CONST_ZIP_FILE);
|
||||||
|
|
||||||
|
if (onGameStateChanged) {
|
||||||
|
onGameStateChanged(GameState::AboutMenu);
|
||||||
|
}
|
||||||
|
|
||||||
uiManager.setButtonCallback("backButton", [this](const std::string&) {
|
uiManager.setButtonCallback("backButton", [this](const std::string&) {
|
||||||
enterMainMenu();
|
enterMainMenu();
|
||||||
});
|
});
|
||||||
@ -70,7 +85,12 @@ namespace ZL {
|
|||||||
|
|
||||||
void MenuManager::startGame()
|
void MenuManager::startGame()
|
||||||
{
|
{
|
||||||
|
previousState = currentState;
|
||||||
currentState = GameState::Gameplay;
|
currentState = GameState::Gameplay;
|
||||||
|
|
||||||
|
if (onGameStateChanged) {
|
||||||
|
onGameStateChanged(GameState::Gameplay);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|||||||
@ -3,6 +3,7 @@
|
|||||||
#include "Environment.h"
|
#include "Environment.h"
|
||||||
#include "render/TextureManager.h"
|
#include "render/TextureManager.h"
|
||||||
#include "UiManager.h"
|
#include "UiManager.h"
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
namespace ZL {
|
namespace ZL {
|
||||||
|
|
||||||
@ -57,6 +58,7 @@ namespace ZL {
|
|||||||
public:
|
public:
|
||||||
UiManager uiManager;
|
UiManager uiManager;
|
||||||
GameState currentState = GameState::MainMenu;
|
GameState currentState = GameState::MainMenu;
|
||||||
|
GameState previousState = GameState::MainMenu;
|
||||||
|
|
||||||
MenuManager(Renderer& iRenderer);
|
MenuManager(Renderer& iRenderer);
|
||||||
|
|
||||||
@ -69,6 +71,8 @@ namespace ZL {
|
|||||||
void startGame();
|
void startGame();
|
||||||
|
|
||||||
GameState getState() const { return currentState; }
|
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)
|
// Returns true for states where Space should render and run (Gameplay, GameOver, ConnectionLost)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user