Add audio
parent
bd622d1873
commit
b74b0dbb9d
|
|
@ -12,6 +12,12 @@ add_subdirectory(../raylib-cpp raylib)
|
||||||
|
|
||||||
include(../assets/includeable.cmake)
|
include(../assets/includeable.cmake)
|
||||||
|
|
||||||
|
file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/assets/audio)
|
||||||
|
configure_file(
|
||||||
|
${CMAKE_CURRENT_SOURCE_DIR}/../assets/audio/ping.wav
|
||||||
|
${CMAKE_CURRENT_BINARY_DIR}/assets/audio/ping.wav
|
||||||
|
COPYONLY)
|
||||||
|
|
||||||
add_executable(as6-gravity-surfing
|
add_executable(as6-gravity-surfing
|
||||||
main.cpp
|
main.cpp
|
||||||
raygui.cpp
|
raygui.cpp
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "components/AudioComponent.hpp"
|
||||||
#include "components/CollectibleComponent.hpp"
|
#include "components/CollectibleComponent.hpp"
|
||||||
#include "components/ColliderComponent.hpp"
|
#include "components/ColliderComponent.hpp"
|
||||||
#include "components/GravityReceiverComponent.hpp"
|
#include "components/GravityReceiverComponent.hpp"
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@
|
||||||
#include "Components.hpp"
|
#include "Components.hpp"
|
||||||
|
|
||||||
#include "EnergyBarRaygui.hpp"
|
#include "EnergyBarRaygui.hpp"
|
||||||
|
#include "raylib-cpp.hpp"
|
||||||
#include "raylib.h"
|
#include "raylib.h"
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
|
||||||
|
|
@ -257,6 +258,9 @@ std::shared_ptr<Entity> CreateWorld() {
|
||||||
std::shared_ptr<Entity> CreateStats() {
|
std::shared_ptr<Entity> CreateStats() {
|
||||||
auto e = std::make_shared<Entity>();
|
auto e = std::make_shared<Entity>();
|
||||||
e->AddComponent<StatsComponent>();
|
e->AddComponent<StatsComponent>();
|
||||||
|
auto &audio = e->AddComponent<AudioComponent>();
|
||||||
|
audio.volume = 0.6f;
|
||||||
|
audio.Load("assets/audio/ping.wav");
|
||||||
auto &render = e->AddComponent<RenderComponent>();
|
auto &render = e->AddComponent<RenderComponent>();
|
||||||
render.draw = [e]() {
|
render.draw = [e]() {
|
||||||
auto stats = e->GetComponent<StatsComponent>();
|
auto stats = e->GetComponent<StatsComponent>();
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,73 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "Component.hpp"
|
||||||
|
#include "raylib-cpp.hpp"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Lightweight component that owns a raylib::Sound instance.
|
||||||
|
* The component exposes a simple API so other gameplay systems can trigger the
|
||||||
|
* sound without having to track the resource themselves.
|
||||||
|
*/
|
||||||
|
struct AudioComponent : public Component {
|
||||||
|
std::string filePath;
|
||||||
|
bool playOnStart = false;
|
||||||
|
float volume = 1.0f;
|
||||||
|
|
||||||
|
void Setup() override {
|
||||||
|
if (!filePath.empty()) {
|
||||||
|
Load(filePath);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Update(float) override {}
|
||||||
|
|
||||||
|
void Cleanup() override {
|
||||||
|
if (sound.IsValid()) {
|
||||||
|
sound.Unload();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Load(const std::string &path) {
|
||||||
|
if (sound.IsValid()) {
|
||||||
|
sound.Unload();
|
||||||
|
}
|
||||||
|
filePath = path;
|
||||||
|
sound.Load(filePath);
|
||||||
|
if (!sound.IsValid()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
sound.SetVolume(volume);
|
||||||
|
sound.SetPitch(1.0f);
|
||||||
|
if (playOnStart) {
|
||||||
|
Play();
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Play() {
|
||||||
|
if (!sound.IsValid()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
sound.Play();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Stop() {
|
||||||
|
if (!sound.IsValid()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
sound.Stop();
|
||||||
|
}
|
||||||
|
|
||||||
|
void SetVolume(float newVolume) {
|
||||||
|
volume = std::clamp(newVolume, 0.0f, 1.0f);
|
||||||
|
if (sound.IsValid()) {
|
||||||
|
sound.SetVolume(volume);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
raylib::Sound sound;
|
||||||
|
};
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
#include "components/CollectibleComponent.hpp"
|
#include "components/CollectibleComponent.hpp"
|
||||||
|
|
||||||
#include "Entity.hpp"
|
#include "Entity.hpp"
|
||||||
|
#include "components/AudioComponent.hpp"
|
||||||
#include "components/ColliderComponent.hpp"
|
#include "components/ColliderComponent.hpp"
|
||||||
#include "components/StatsComponent.hpp"
|
#include "components/StatsComponent.hpp"
|
||||||
|
|
||||||
|
|
@ -27,6 +28,10 @@ void CollectibleComponent::Setup() {
|
||||||
stats->get().AddValue(stats->get().gainPerStar);
|
stats->get().AddValue(stats->get().gainPerStar);
|
||||||
stats->get().AddStar();
|
stats->get().AddStar();
|
||||||
}
|
}
|
||||||
|
auto audio = context->statsEntity->GetComponent<AudioComponent>();
|
||||||
|
if (audio) {
|
||||||
|
audio->get().Play();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,9 @@ int main() {
|
||||||
window.SetState(FLAG_WINDOW_RESIZABLE);
|
window.SetState(FLAG_WINDOW_RESIZABLE);
|
||||||
window.SetTargetFPS(60);
|
window.SetTargetFPS(60);
|
||||||
|
|
||||||
|
// Initialize audio device early so we can play sound effects.
|
||||||
|
raylib::AudioDevice audioDevice;
|
||||||
|
|
||||||
SceneManager sceneManager;
|
SceneManager sceneManager;
|
||||||
sceneManager.ChangeScene<StartMenuScene>();
|
sceneManager.ChangeScene<StartMenuScene>();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
#include "scene/StartMenuScene.hpp"
|
#include "scene/StartMenuScene.hpp"
|
||||||
|
|
||||||
void DeathScene::Update(float) {
|
void DeathScene::Update(float) {
|
||||||
if (IsKeyPressed(KEY_ENTER) || IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) {
|
if (IsKeyPressed(KEY_ENTER)) {
|
||||||
manager.QueueSceneChange<GameplayScene>();
|
manager.QueueSceneChange<GameplayScene>();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue