diff --git a/Broken Engine/Source/ModulePhysics.cpp b/Broken Engine/Source/ModulePhysics.cpp index c25d15dcd..b950c4818 100644 --- a/Broken Engine/Source/ModulePhysics.cpp +++ b/Broken Engine/Source/ModulePhysics.cpp @@ -226,7 +226,7 @@ update_status ModulePhysics::Update(float dt) if (App->GetAppState() == AppState::PLAY && !App->time->gamePaused) { // --- Step physics simulation --- - physAccumulatedTime += App->time->GetRealTimeDt(); + physAccumulatedTime += App->time->GetGameDt(); // --- If enough time has elapsed, update --- if (physAccumulatedTime >= physx::fixed_dt) diff --git a/Broken Engine/Source/ModulePhysics.h b/Broken Engine/Source/ModulePhysics.h index b0e374202..b44d54f2e 100644 --- a/Broken Engine/Source/ModulePhysics.h +++ b/Broken Engine/Source/ModulePhysics.h @@ -167,11 +167,11 @@ class BROKEN_API ModulePhysics : public Broken::Module physx::PxVolumeCache* cache; UserIterator iter; + float physAccumulatedTime = 0.0f; FilterCallback filterCallback; private: PhysxSimulationEvents* simulationEventsCallback = nullptr; - float physAccumulatedTime = 0.0f; bool loaded = false; float3 materialDesc = float3(1.0f, 1.0f, 0.0f); float gravity = 9.8; diff --git a/Broken Engine/Source/ModuleScripting.cpp b/Broken Engine/Source/ModuleScripting.cpp index 7700201df..09e7ae097 100644 --- a/Broken Engine/Source/ModuleScripting.cpp +++ b/Broken Engine/Source/ModuleScripting.cpp @@ -334,6 +334,11 @@ void ModuleScripting::CompileScriptTableClass(ScriptInstance* script) .addFunction("StopAudioEvent", &ScriptingAudio::StopAudioEvent) .addFunction("PauseAudioEvent", &ScriptingAudio::PauseAudioEvent) .addFunction("ResumeAudioEvent", &ScriptingAudio::ResumeAudioEvent) + .addFunction("SetVolume", &ScriptingAudio::SetVolume) + .addFunction("PlayAudioEventGO", &ScriptingAudio::PlayAudioEventGO) + .addFunction("StopAudioEventGO", &ScriptingAudio::StopAudioEventGO) + .addFunction("PauseAudioEventGO", &ScriptingAudio::PauseAudioEventGO) + .addFunction("ResumeAudioEventGO", &ScriptingAudio::ResumeAudioEventGO) .endClass() // ---------------------------------------------------------------------------------- diff --git a/Broken Engine/Source/ModuleTimeManager.h b/Broken Engine/Source/ModuleTimeManager.h index 64647aa15..4431a8b84 100644 --- a/Broken Engine/Source/ModuleTimeManager.h +++ b/Broken Engine/Source/ModuleTimeManager.h @@ -35,7 +35,6 @@ class BROKEN_API ModuleTimeManager : public Module { private: Timer Realtime_clock; - Timer Gametime_clock; float Time_scale = 1.0f; @@ -48,6 +47,8 @@ class BROKEN_API ModuleTimeManager : public Module { int last_fps; uint capped_ms; uint last_frame_ms; +public: + Timer Gametime_clock; }; BE_END_NAMESPACE #endif \ No newline at end of file diff --git a/Broken Engine/Source/ScriptingAudio.cpp b/Broken Engine/Source/ScriptingAudio.cpp index d7d737e31..5a80689c1 100644 --- a/Broken Engine/Source/ScriptingAudio.cpp +++ b/Broken Engine/Source/ScriptingAudio.cpp @@ -6,6 +6,7 @@ #include "ModuleScripting.h" #include "ModuleRenderer3D.h" #include "ModuleAudio.h" +#include "ModuleSceneManager.h" // -- Components -- #include "GameObject.h" @@ -15,6 +16,8 @@ #include "ComponentAnimation.h" #include "ComponentCamera.h" #include "ComponentAudioSource.h" +#include "ResourceScene.h" + #include "../Game/Assets/Sounds/Wwise_IDs.h" @@ -26,84 +29,166 @@ ScriptingAudio::ScriptingAudio() {} ScriptingAudio::~ScriptingAudio() {} -void ScriptingAudio::SetVolume(float volume) +void ScriptingAudio::SetVolume(float volume, uint UID) { - ComponentAudioSource* sound = App->scripting->current_script->my_component->GetContainerGameObject()->GetComponent(); + GameObject* GO = App->scene_manager->currentScene->GetGOWithUID(UID); + if (GO) { + ComponentAudioSource* sound = GO->GetComponent(); + + if (sound) + sound->SetVolume(volume); + else + ENGINE_CONSOLE_LOG("![Script]: (SetVolume) Sound Emmiter component is NULL"); + } + else + ENGINE_CONSOLE_LOG("![Script]: (SetVolume) GameObject with UID %d does not exist", UID); +} - if (sound) - sound->SetVolume(volume); +void ScriptingAudio::PlayAudioEventGO(std::string event, uint UID) +{ + GameObject* GO = App->scene_manager->currentScene->GetGOWithUID(UID); + if (GO) + { + ComponentAudioSource* sound = GO->GetComponent(); + + if (sound) { + uint EventId = App->audio->EventMap[event]; + sound->SetID(EventId); + sound->wwiseGO->PlayEvent(EventId); + sound->isPlaying = true; + } + else + ENGINE_CONSOLE_LOG("![Script]: (PlayAudioEventGO) Component AudioSource does not exist"); + } else - ENGINE_CONSOLE_LOG("[Script]: Sound Emmiter component is NULL"); + ENGINE_CONSOLE_LOG("![Script]: (StopAudioEventGO) GameObject with UID %d does not exist", UID); } -void ScriptingAudio::PlayAudioEvent(std::string event) +void ScriptingAudio::StopAudioEventGO(std::string event, uint UID) { - ComponentAudioSource* sound = nullptr; - sound = App->scripting->current_script->my_component->GetContainerGameObject()->GetComponent(); + GameObject* GO = App->scene_manager->currentScene->GetGOWithUID(UID); + if (GO) + { + ComponentAudioSource* sound = GO->GetComponent(); + + if (sound) + { + uint EventId = App->audio->EventMap[event]; + sound->SetID(EventId); + sound->wwiseGO->StopEvent(EventId); + sound->isPlaying = false; + } + else + ENGINE_CONSOLE_LOG("![Script]: (StopAudioEventGO) Component AudioSource does not exist"); + } + else + ENGINE_CONSOLE_LOG("![Script]: (StopAudioEventGO) GameObject with UID %d does not exist", UID); +} - uint EventId = App->audio->EventMap[event]; +void ScriptingAudio::PauseAudioEventGO(std::string event, uint UID) +{ + GameObject* GO = App->scene_manager->currentScene->GetGOWithUID(UID); + + if (GO) + { + ComponentAudioSource* sound = GO->GetComponent(); + + if (sound) { + uint EventId = App->audio->EventMap[event]; + sound->SetID(EventId); + sound->wwiseGO->PauseEvent(EventId); + sound->isPlaying = false; + sound->isPaused = true; + } + else + ENGINE_CONSOLE_LOG("![Script]: (PauseAudioEventGO) Component AudioSource does not exist"); + } + else + ENGINE_CONSOLE_LOG("![Script]: (PauseAudioEventGO) GameObject with UID %d does not exist", UID); +} +void ScriptingAudio::ResumeAudioEventGO(std::string event,uint UID) +{ + GameObject* GO = App->scene_manager->currentScene->GetGOWithUID(UID); + ComponentAudioSource* sound = nullptr; + if (GO) { + sound = GO->GetComponent(); + + if (sound) { + uint EventId = App->audio->EventMap[event]; + sound->SetID(EventId); + sound->wwiseGO->ResumeEvent(EventId); + sound->isPlaying = true; + sound->isPaused = false; + } + ENGINE_CONSOLE_LOG("![Script]: (ResumeAudioEventGO) Component AudioSource does not exist"); + } + else + ENGINE_CONSOLE_LOG("![Script]: (ResumeAudioEventGO) GameObject with UID %d does not exist", UID); +} - sound->SetID(EventId); +void ScriptingAudio::PlayAudioEvent(std::string event) +{ + ComponentAudioSource* sound = App->scripting->current_script->my_component->GetContainerGameObject()->GetComponent(); if (sound) { + uint EventId = App->audio->EventMap[event]; + sound->SetID(EventId); sound->wwiseGO->PlayEvent(EventId); sound->isPlaying = true; } else - ENGINE_CONSOLE_LOG("[Script]: Sound Emmiter component is NULL"); + ENGINE_CONSOLE_LOG("![Script]: (PlayAudioEvent) Sound Emmiter component is NULL"); } void ScriptingAudio::StopAudioEvent(std::string event) { ComponentAudioSource* sound = App->scripting->current_script->my_component->GetContainerGameObject()->GetComponent(); - uint EventId = App->audio->EventMap[event]; - - sound->SetID(EventId); - if (sound) { + uint EventId = App->audio->EventMap[event]; + + sound->SetID(EventId); sound->wwiseGO->StopEvent(EventId); sound->isPlaying = false; } else - ENGINE_CONSOLE_LOG("[Script]: Sound Emmiter component is NULL"); + ENGINE_CONSOLE_LOG("![Script]: (StopAudioEvent) Sound Emmiter component is NULL"); } void ScriptingAudio::PauseAudioEvent(std::string event) { ComponentAudioSource* sound = App->scripting->current_script->my_component->GetContainerGameObject()->GetComponent(); - uint EventId = App->audio->EventMap[event]; - - sound->SetID(EventId); - if (sound) { + uint EventId = App->audio->EventMap[event]; + + sound->SetID(EventId); sound->wwiseGO->PauseEvent(EventId); sound->isPlaying = false; sound->isPaused = true; } else - ENGINE_CONSOLE_LOG("[Script]: Sound Emmiter component is NULL"); + ENGINE_CONSOLE_LOG("![Script]: (PauseAudioEvent) Sound Emmiter component is NULL"); } void ScriptingAudio::ResumeAudioEvent(std::string event) { ComponentAudioSource* sound = App->scripting->current_script->my_component->GetContainerGameObject()->GetComponent(); - uint EventId = App->audio->EventMap[event]; - - sound->SetID(EventId); - if (sound) { + uint EventId = App->audio->EventMap[event]; + + sound->SetID(EventId); sound->wwiseGO->ResumeEvent(EventId); sound->isPlaying = true; sound->isPaused = false; } else - ENGINE_CONSOLE_LOG("[Script]: Sound Emmiter component is NULL"); -} \ No newline at end of file + ENGINE_CONSOLE_LOG("![Script]: (ResumeAudioEvent) Sound Emmiter component is NULL"); +} + diff --git a/Broken Engine/Source/ScriptingAudio.h b/Broken Engine/Source/ScriptingAudio.h index da483bd39..26911b425 100644 --- a/Broken Engine/Source/ScriptingAudio.h +++ b/Broken Engine/Source/ScriptingAudio.h @@ -10,7 +10,12 @@ class BROKEN_API ScriptingAudio { ~ScriptingAudio(); public: - void SetVolume(float volume); + void SetVolume(float volume, uint UID); + void PlayAudioEventGO(std::string event, uint UID); + void StopAudioEventGO(std::string event, uint UID); + void PauseAudioEventGO(std::string event, uint UID); + void ResumeAudioEventGO(std::string event,uint UID); + void PlayAudioEvent(std::string event); void StopAudioEvent(std::string event); void PauseAudioEvent(std::string event); diff --git a/Broken Engine/Source/ScriptingScenes.cpp b/Broken Engine/Source/ScriptingScenes.cpp index 01692137d..283367552 100644 --- a/Broken Engine/Source/ScriptingScenes.cpp +++ b/Broken Engine/Source/ScriptingScenes.cpp @@ -10,6 +10,8 @@ #include "ResourcePrefab.h" #include "ResourceModel.h" #include "ComponentTransform.h" +#include "ModuleTimeManager.h" +#include "ModulePhysics.h" using namespace Broken; ScriptingScenes::ScriptingScenes() {} @@ -19,7 +21,10 @@ ScriptingScenes::~ScriptingScenes() {} void ScriptingScenes::LoadSceneFromScript(uint scene_UUID) { ResourceScene* scene = (ResourceScene*)App->resources->GetResource(scene_UUID, false); + App->time->Gametime_clock.Stop(); App->scene_manager->SetActiveScene(scene); + App->physics->physAccumulatedTime = 0.0f;//Reset Physics + App->time->Gametime_clock.Start(); } void ScriptingScenes::QuitGame() diff --git a/Broken Engine/Source/Timer.cpp b/Broken Engine/Source/Timer.cpp index 3e24848b8..bca0d6252 100644 --- a/Broken Engine/Source/Timer.cpp +++ b/Broken Engine/Source/Timer.cpp @@ -23,6 +23,12 @@ void Timer::Stop() { stopped_at = SDL_GetTicks(); } +void Timer::Resume() +{ + running = true; + started_at += SDL_GetTicks() - stopped_at; +} + // --------------------------------------------- Uint32 Timer::Read() { if (running == true) { diff --git a/Broken Engine/Source/Timer.h b/Broken Engine/Source/Timer.h index 3c4949ecc..2a19d6016 100644 --- a/Broken Engine/Source/Timer.h +++ b/Broken Engine/Source/Timer.h @@ -13,6 +13,7 @@ class BROKEN_API Timer { void Start(); void Stop(); + void Resume(); Uint32 Read();