Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
Merge pull request #268 from Broken-Gem-Studio/Audio-Scripting
Browse files Browse the repository at this point in the history
Added Audio scripting Funcitons
  • Loading branch information
t3m1X authored Apr 22, 2020
2 parents e8c330d + 9230a3d commit cb9e010
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 31 deletions.
2 changes: 1 addition & 1 deletion Broken Engine/Source/ModulePhysics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion Broken Engine/Source/ModulePhysics.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions Broken Engine/Source/ModuleScripting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()

// ----------------------------------------------------------------------------------
Expand Down
3 changes: 2 additions & 1 deletion Broken Engine/Source/ModuleTimeManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ class BROKEN_API ModuleTimeManager : public Module {
private:

Timer Realtime_clock;
Timer Gametime_clock;
float Time_scale = 1.0f;


Expand All @@ -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
139 changes: 112 additions & 27 deletions Broken Engine/Source/ScriptingAudio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "ModuleScripting.h"
#include "ModuleRenderer3D.h"
#include "ModuleAudio.h"
#include "ModuleSceneManager.h"

// -- Components --
#include "GameObject.h"
Expand All @@ -15,6 +16,8 @@
#include "ComponentAnimation.h"
#include "ComponentCamera.h"
#include "ComponentAudioSource.h"
#include "ResourceScene.h"



#include "../Game/Assets/Sounds/Wwise_IDs.h"
Expand All @@ -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<ComponentAudioSource>();
GameObject* GO = App->scene_manager->currentScene->GetGOWithUID(UID);
if (GO) {
ComponentAudioSource* sound = GO->GetComponent<ComponentAudioSource>();

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<ComponentAudioSource>();

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<ComponentAudioSource>();
GameObject* GO = App->scene_manager->currentScene->GetGOWithUID(UID);
if (GO)
{
ComponentAudioSource* sound = GO->GetComponent<ComponentAudioSource>();

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<ComponentAudioSource>();

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<ComponentAudioSource>();

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<ComponentAudioSource>();

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<ComponentAudioSource>();

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<ComponentAudioSource>();

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<ComponentAudioSource>();

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");
}
ENGINE_CONSOLE_LOG("![Script]: (ResumeAudioEvent) Sound Emmiter component is NULL");
}

7 changes: 6 additions & 1 deletion Broken Engine/Source/ScriptingAudio.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions Broken Engine/Source/ScriptingScenes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include "ResourcePrefab.h"
#include "ResourceModel.h"
#include "ComponentTransform.h"
#include "ModuleTimeManager.h"
#include "ModulePhysics.h"

using namespace Broken;
ScriptingScenes::ScriptingScenes() {}
Expand All @@ -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()
Expand Down
6 changes: 6 additions & 0 deletions Broken Engine/Source/Timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions Broken Engine/Source/Timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class BROKEN_API Timer {

void Start();
void Stop();
void Resume();

Uint32 Read();

Expand Down

0 comments on commit cb9e010

Please sign in to comment.