From 12057d9f6d47ae8f6616bff2dc24ae5a5a947022 Mon Sep 17 00:00:00 2001 From: Bamcane Date: Sun, 19 May 2024 20:06:07 +0800 Subject: [PATCH] Add map sound Co-authored-by: heinrich5991 --- datasrc/network.py | 8 ++++++++ src/game/client/components/mapsounds.cpp | 16 ++++++++++++++++ src/game/client/components/mapsounds.h | 3 +++ src/game/client/gameclient.cpp | 19 +++++++++++++++++++ 4 files changed, 46 insertions(+) diff --git a/datasrc/network.py b/datasrc/network.py index 1db49da01..04c02748b 100644 --- a/datasrc/network.py +++ b/datasrc/network.py @@ -374,6 +374,10 @@ Objects = [ NetIntAny("m_Layer"), NetIntAny("m_EntityClass"), ]), + + NetEventEx("MapSoundWorld:Common", "map-sound-world@netevent.ddnet.org", [ + NetIntAny("m_SoundId"), + ]), ] Messages = [ @@ -585,4 +589,8 @@ Messages = [ NetMessageEx("Sv_ChangeInfoCooldown", "change-info-cooldown@netmsg.ddnet.org", [ NetTick("m_WaitUntil") ]), + + NetMessageEx("Sv_MapSoundGlobal", "map-sound-global@netmsg.ddnet.org", [ + NetIntAny("m_SoundId"), + ]), ] diff --git a/src/game/client/components/mapsounds.cpp b/src/game/client/components/mapsounds.cpp index c15280799..0ddae91c2 100644 --- a/src/game/client/components/mapsounds.cpp +++ b/src/game/client/components/mapsounds.cpp @@ -17,6 +17,22 @@ CMapSounds::CMapSounds() m_Count = 0; } +void CMapSounds::Play(int SoundId) +{ + if(SoundId < 0 || SoundId >= m_Count) + return; + + m_pClient->m_Sounds.PlaySample(CSounds::CHN_MAPSOUND, m_aSounds[SoundId], 1.0f, 0); +} + +void CMapSounds::PlayAt(int SoundId, vec2 Pos) +{ + if(SoundId < 0 || SoundId >= m_Count) + return; + + m_pClient->m_Sounds.PlaySampleAt(CSounds::CHN_MAPSOUND, m_aSounds[SoundId], 1.0f, Pos, 0); +} + void CMapSounds::OnMapLoad() { IMap *pMap = Kernel()->RequestInterface(); diff --git a/src/game/client/components/mapsounds.h b/src/game/client/components/mapsounds.h index 455326434..2f89d402b 100644 --- a/src/game/client/components/mapsounds.h +++ b/src/game/client/components/mapsounds.h @@ -33,6 +33,9 @@ public: CMapSounds(); virtual int Sizeof() const override { return sizeof(*this); } + void Play(int SoundId); + void PlayAt(int SoundId, vec2 Pos); + virtual void OnMapLoad() override; virtual void OnRender() override; virtual void OnStateChange(int NewState, int OldState) override; diff --git a/src/game/client/gameclient.cpp b/src/game/client/gameclient.cpp index b4ab98477..7ccedb278 100644 --- a/src/game/client/gameclient.cpp +++ b/src/game/client/gameclient.cpp @@ -1067,6 +1067,17 @@ void CGameClient::OnMessage(int MsgId, CUnpacker *pUnpacker, int Conn, bool Dumm CNetMsg_Sv_ChangeInfoCooldown *pMsg = (CNetMsg_Sv_ChangeInfoCooldown *)pRawMsg; m_NextChangeInfo = pMsg->m_WaitUntil; } + else if(MsgId == NETMSGTYPE_SV_MAPSOUNDGLOBAL) + { + if(m_SuppressEvents) + return; + + if(!g_Config.m_SndGame) + return; + + CNetMsg_Sv_MapSoundGlobal *pMsg = (CNetMsg_Sv_MapSoundGlobal *)pRawMsg; + m_MapSounds.Play(pMsg->m_SoundId); + } } void CGameClient::OnStateChange(int NewState, int OldState) @@ -1240,6 +1251,14 @@ void CGameClient::ProcessEvents() m_Sounds.PlayAt(CSounds::CHN_WORLD, pEvent->m_SoundId, 1.0f, vec2(pEvent->m_X, pEvent->m_Y)); } + else if(Item.m_Type == NETEVENTTYPE_MAPSOUNDWORLD) + { + CNetEvent_MapSoundWorld *pEvent = (CNetEvent_MapSoundWorld *)Item.m_pData; + if(!Config()->m_SndGame) + continue; + + m_MapSounds.PlayAt(pEvent->m_SoundId, vec2(pEvent->m_X, pEvent->m_Y)); + } } }