-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
157 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
#include "MusicZoneManager.h" | ||
|
||
#include <components/Vob.h> | ||
#include <components/VobClasses.h> | ||
#include <engine/BaseEngine.h> | ||
#include <logic/PlayerController.h> | ||
#include <logic/ScriptEngine.h> | ||
|
||
using namespace Engine; | ||
|
||
static const std::array<const std::string, 6> instanceSuffixes = | ||
{ | ||
"_DAY_STD", | ||
"_DAY_THR", | ||
"_DAY_FGT", | ||
"_NGT_STD", | ||
"_NGT_THR", | ||
"_NGT_FGT", | ||
}; | ||
|
||
MusicZoneManager::MusicZoneManager(World::WorldInstance& world) | ||
: m_world(world) | ||
{ | ||
} | ||
|
||
void MusicZoneManager::addZone(const ZenLoad::zCVobData& zone) | ||
{ | ||
assert(zone.vobType == ZenLoad::zCVobData::VT_oCZoneMusic); | ||
m_zones.emplace_back(zone); | ||
} | ||
|
||
static bool isInside(const Math::float3& vec, const ZMath::float3& min, const ZMath::float3& max) | ||
{ | ||
return (vec.x >= min.x && vec.x < max.x && | ||
vec.y >= min.y && vec.y < max.y && | ||
vec.z >= min.z && vec.z < max.z); | ||
} | ||
|
||
void MusicZoneManager::playTheme(const std::string& prefix, EMusicType type, EMusicTime time) | ||
{ | ||
auto& audioWorld = m_world.getEngine()->getAudioWorld(); | ||
// First try the requested theme | ||
if (!audioWorld.playMusicTheme(prefix + instanceSuffixes[time + type])) | ||
{ | ||
// If it wasn't found, try the daytime variation | ||
if (!audioWorld.playMusicTheme(prefix + instanceSuffixes[MT_Day + type])) | ||
{ | ||
// Then try the standard daytime variation | ||
audioWorld.playMusicTheme(prefix + instanceSuffixes[MT_Day + MT_Std]); | ||
} | ||
} | ||
} | ||
|
||
void MusicZoneManager::onUpdate() | ||
{ | ||
// Get player's position | ||
const auto& playerEntity = m_world.getScriptEngine().getPlayerEntity(); | ||
auto playerVob = VobTypes::asNpcVob(m_world, playerEntity); | ||
|
||
if (!playerVob.isValid() || playerVob.playerController->getUsedMob().isValid()) | ||
return; | ||
|
||
const auto& playerPos = playerVob.position->m_WorldMatrix.Translation(); | ||
|
||
const ZenLoad::zCVobData* currentZone = nullptr; | ||
for (const auto& zone : m_zones) | ||
{ | ||
// BBoxes are expressed in centimeters, but REGoth uses meters | ||
if (isInside(playerPos, zone.bbox[0] * .01f, zone.bbox[1] * .01f)) | ||
{ | ||
if (!currentZone | ||
|| currentZone->oCZoneMusic.priority < zone.oCZoneMusic.priority) | ||
{ | ||
currentZone = &zone; | ||
} | ||
} | ||
} | ||
|
||
auto time = m_world.getEngine()->getGameClock().isDaytime() ? MT_Day : MT_Ngt; | ||
|
||
auto& audioWorld = m_world.getEngine()->getAudioWorld(); | ||
|
||
if (!currentZone) | ||
{ | ||
// If the player's not inside any zone, play the default theme | ||
playTheme(m_defaultZonePrefix, MT_Std, time); | ||
} | ||
else | ||
{ | ||
auto zoneName = currentZone->vobName; | ||
auto instancePrefix = Utils::uppered(zoneName.substr(zoneName.find('_') + 1)); | ||
|
||
// Try playing the zone's theme | ||
playTheme(instancePrefix, MT_Std, time); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#pragma once | ||
|
||
#include <vector> | ||
|
||
#include <engine/World.h> | ||
|
||
namespace Engine | ||
{ | ||
/** | ||
* @brief Handles transitions between music zones | ||
* | ||
*/ | ||
class MusicZoneManager | ||
{ | ||
public: | ||
MusicZoneManager(World::WorldInstance& world); | ||
|
||
/** | ||
* @brief Adds a zone to the zone list | ||
*/ | ||
void addZone(const ZenLoad::zCVobData& vob); | ||
|
||
/** | ||
* @brief Sets the world's default zone | ||
*/ | ||
void setDefaultZone(const std::string& zone) { m_defaultZonePrefix = zone; } | ||
void onUpdate(); | ||
|
||
private: | ||
World::WorldInstance& m_world; | ||
std::vector<ZenLoad::zCVobData> m_zones; | ||
std::string m_defaultZonePrefix; | ||
|
||
enum EMusicTime { | ||
MT_Day = 0, | ||
MT_Ngt = 3, | ||
}; | ||
|
||
enum EMusicType { | ||
MT_Std = 0, | ||
MT_Thr = 1, | ||
MT_Fgt = 2, | ||
}; | ||
|
||
void playTheme(const std::string& prefix, EMusicType type, EMusicTime time); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters