Line data Source code
1 : /* Copyright (C) 2026 Wildfire Games. 2 : * This file is part of 0 A.D. 3 : * 4 : * 0 A.D. is free software: you can redistribute it and/or modify 5 : * it under the terms of the GNU General Public License as published by 6 : * the Free Software Foundation, either version 2 of the License, or 7 : * (at your option) any later version. 8 : * 9 : * 0 A.D. is distributed in the hope that it will be useful, 10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 : * GNU General Public License for more details. 13 : * 14 : * You should have received a copy of the GNU General Public License 15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 : */ 17 : 18 : #include "precompiled.h" 19 : 20 : #include "ICmpSoundManager.h" 21 : 22 : #include "lib/path.h" 23 : #include "maths/Vector3D.h" 24 : #include "scriptinterface/Interface.h" 25 : #include "simulation2/components/ICmpOwnership.h" 26 : #include "simulation2/components/ICmpPosition.h" 27 : #include "simulation2/components/ICmpRangeManager.h" 28 : #include "simulation2/helpers/Player.h" 29 : #include "simulation2/system/Component.h" 30 : #include "simulation2/system/Entity.h" 31 : #include "soundmanager/ISoundManager.h" 32 : 33 : #include <string> 34 : 35 7 : class CCmpSoundManager final : public ICmpSoundManager 36 : { 37 : public: 38 : static void ClassInit(CComponentManager&) 39 : { 40 : } 41 : 42 14 : DEFAULT_COMPONENT_ALLOCATOR(SoundManager) 43 : 44 137 : static std::string GetSchema() 45 : { 46 274 : return "<a:component type='system'/><empty/>"; 47 : } 48 : 49 7 : void Init(const CParamNode&) override 50 : { 51 7 : } 52 : 53 7 : void Deinit() override 54 : { 55 7 : } 56 : 57 18 : void Serialize(ISerializer&) override 58 : { 59 : // Do nothing here - sounds are purely local, and don't need to be preserved across saved games etc 60 : // (If we add music support in here then we might want to save the music state, though) 61 18 : } 62 : 63 0 : void Deserialize(const CParamNode& paramNode, IDeserializer&) override 64 : { 65 0 : Init(paramNode); 66 0 : } 67 : 68 18 : void PlaySoundGroup(const std::wstring& name, entity_id_t source) override 69 : { 70 18 : if (!g_SoundManager || (source == INVALID_ENTITY)) 71 18 : return; 72 : 73 0 : int currentPlayer = GetSimContext().GetCurrentDisplayedPlayer(); 74 : 75 0 : CmpPtr<ICmpRangeManager> cmpRangeManager(GetSystemEntity()); 76 0 : if (!cmpRangeManager || (cmpRangeManager->GetLosVisibility(source, currentPlayer) != LosVisibility::VISIBLE)) 77 0 : return; 78 : 79 0 : CmpPtr<ICmpPosition> cmpPosition(GetSimContext(), source); 80 0 : if (!cmpPosition || !cmpPosition->IsInWorld()) 81 0 : return; 82 : 83 0 : bool playerOwned = false; 84 0 : CmpPtr<ICmpOwnership> cmpOwnership(GetSimContext(), source); 85 0 : if (cmpOwnership) 86 0 : playerOwned = cmpOwnership->GetOwner() == currentPlayer; 87 : 88 0 : CVector3D sourcePos = CVector3D(cmpPosition->GetPosition()); 89 0 : g_SoundManager->PlayAsGroup(name, sourcePos, source, playerOwned); 90 : } 91 : 92 114 : void PlaySoundGroupAtPosition(const std::wstring& name, const CFixedVector3D& sourcePos) override 93 : { 94 114 : if (!g_SoundManager) 95 : return; 96 0 : g_SoundManager->PlayAsGroup(name, CVector3D(sourcePos), INVALID_ENTITY, false); 97 : } 98 : 99 0 : void PlaySoundGroupForPlayer(const VfsPath& groupPath, const player_id_t playerId) const override 100 : { 101 0 : if (!g_SoundManager) 102 : return; 103 0 : g_SoundManager->PlayAsGroup(groupPath, CVector3D(0.f, 0.f, 0.f), INVALID_ENTITY, GetSimContext().GetCurrentDisplayedPlayer() == playerId); 104 : } 105 : 106 0 : void StopMusic() override 107 : { 108 0 : if (!g_SoundManager) 109 : return; 110 0 : g_SoundManager->Pause(true); 111 : } 112 : 113 : }; 114 : 115 274 : REGISTER_COMPONENT_TYPE(SoundManager) 116 : 117 :