Line data Source code
1 : /* Copyright (C) 2022 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 "simulation2/system/Component.h"
21 : #include "ICmpSoundManager.h"
22 :
23 : #include "simulation2/MessageTypes.h"
24 : #include "simulation2/components/ICmpPosition.h"
25 : #include "simulation2/components/ICmpRangeManager.h"
26 : #include "simulation2/components/ICmpOwnership.h"
27 :
28 : #include "soundmanager/ISoundManager.h"
29 :
30 9 : class CCmpSoundManager final : public ICmpSoundManager
31 : {
32 : public:
33 116 : static void ClassInit(CComponentManager& UNUSED(componentManager) )
34 : {
35 116 : }
36 :
37 6 : DEFAULT_COMPONENT_ALLOCATOR(SoundManager)
38 :
39 116 : static std::string GetSchema()
40 : {
41 116 : return "<a:component type='system'/><empty/>";
42 : }
43 :
44 3 : void Init(const CParamNode& UNUSED(paramNode)) override
45 : {
46 3 : }
47 :
48 3 : void Deinit() override
49 : {
50 3 : }
51 :
52 0 : void Serialize(ISerializer& UNUSED(serialize)) override
53 : {
54 : // Do nothing here - sounds are purely local, and don't need to be preserved across saved games etc
55 : // (If we add music support in here then we might want to save the music state, though)
56 0 : }
57 :
58 0 : void Deserialize(const CParamNode& paramNode, IDeserializer& UNUSED(deserialize)) override
59 : {
60 0 : Init(paramNode);
61 0 : }
62 :
63 0 : void PlaySoundGroup(const std::wstring& name, entity_id_t source) override
64 : {
65 0 : if (!g_SoundManager || (source == INVALID_ENTITY))
66 0 : return;
67 :
68 0 : int currentPlayer = GetSimContext().GetCurrentDisplayedPlayer();
69 :
70 0 : CmpPtr<ICmpRangeManager> cmpRangeManager(GetSystemEntity());
71 0 : if (!cmpRangeManager || (cmpRangeManager->GetLosVisibility(source, currentPlayer) != LosVisibility::VISIBLE))
72 0 : return;
73 :
74 0 : CmpPtr<ICmpPosition> cmpPosition(GetSimContext(), source);
75 0 : if (!cmpPosition || !cmpPosition->IsInWorld())
76 0 : return;
77 :
78 0 : bool playerOwned = false;
79 0 : CmpPtr<ICmpOwnership> cmpOwnership(GetSimContext(), source);
80 0 : if (cmpOwnership)
81 0 : playerOwned = cmpOwnership->GetOwner() == currentPlayer;
82 :
83 0 : CVector3D sourcePos = CVector3D(cmpPosition->GetPosition());
84 0 : g_SoundManager->PlayAsGroup(name, sourcePos, source, playerOwned);
85 : }
86 :
87 0 : void PlaySoundGroupAtPosition(const std::wstring& name, const CFixedVector3D& sourcePos) override
88 : {
89 0 : if (!g_SoundManager)
90 0 : return;
91 0 : g_SoundManager->PlayAsGroup(name, CVector3D(sourcePos), INVALID_ENTITY, false);
92 : }
93 :
94 0 : void PlaySoundGroupForPlayer(const VfsPath& groupPath, const player_id_t playerId) const override
95 : {
96 0 : if (!g_SoundManager)
97 0 : return;
98 0 : g_SoundManager->PlayAsGroup(groupPath, CVector3D(0.f, 0.f, 0.f), INVALID_ENTITY, GetSimContext().GetCurrentDisplayedPlayer() == playerId);
99 : }
100 :
101 0 : void StopMusic() override
102 : {
103 0 : if (!g_SoundManager)
104 0 : return;
105 0 : g_SoundManager->Pause(true);
106 : }
107 :
108 : };
109 :
110 119 : REGISTER_COMPONENT_TYPE(SoundManager)
111 :
112 :
|