ManaPlus
itemsoundmanager.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2013-2019 The ManaPlus Developers
4  * Copyright (C) 2019-2021 Andrei Karas
5  *
6  * This file is part of The ManaPlus Client.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #include "itemsoundmanager.h"
23 
24 #include "soundmanager.h"
25 
26 #include "being/flooritem.h"
27 #include "being/localplayer.h"
28 
29 #include "resources/iteminfo.h"
30 
31 #include "resources/item/item.h"
32 
33 #include "debug.h"
34 
35 void ItemSoundManager::playSfx(const Item *const item,
36  const ItemSoundEvent::Type sound)
37 {
38  if (item == nullptr)
39  return;
40  playSfx(item->getId(), sound);
41 }
42 
43 void ItemSoundManager::playSfx(const int itemId,
44  const ItemSoundEvent::Type sound)
45 {
46  playSfx(ItemDB::get(itemId), sound);
47 }
48 
49 void ItemSoundManager::playSfx(const FloorItem *const item,
50  const ItemSoundEvent::Type sound)
51 {
52  if (item == nullptr)
53  return;
54  playSfx(ItemDB::get(item->getItemId()), sound);
55 }
56 
57 std::string ItemSoundManager::getSoundEffect(const Being *const being,
58  const ItemInfo &info,
59  const ItemSoundEvent::Type sound)
60 {
61  std::string sfx = info.getSound(sound).sound;
62  if (sfx.empty())
63  {
64  if (being == nullptr)
65  return std::string();
66 
67  // fallback to player race sound if no item sound.
68  const int id = -100 - toInt(being->getSubType(), int);
69  const ItemInfo &info2 = ItemDB::get(id);
70  sfx = info2.getSound(sound).sound;
71  }
72  return sfx;
73 }
74 
76  const ItemSoundEvent::Type sound)
77 {
79  0,
80  0);
81 }
82 
83 void ItemSoundManager::playSfx(const Being *const being,
84  const int itemId,
85  const ItemSoundEvent::Type sound)
86 {
87  if (being == nullptr)
88  return;
89 
90  soundManager.playSfx(getSoundEffect(being, ItemDB::get(itemId), sound),
91  being->getTileX(),
92  being->getTileY());
93 }
Definition: being.h:96
int getTileX() const
Definition: being.h:168
BeingTypeId getSubType() const
Definition: being.h:400
int getTileY() const
Definition: being.h:174
int getItemId() const
Definition: flooritem.h:82
const SoundInfo & getSound(const ItemSoundEvent::Type event) const
Definition: iteminfo.cpp:161
static std::string getSoundEffect(const Being *const being, const ItemInfo &info, const ItemSoundEvent::Type sound)
static void playSfx(const int item, const ItemSoundEvent::Type sound)
Definition: item.h:50
int getId() const
Definition: item.h:81
void playSfx(const std::string &path, const int x, const int y) const
#define toInt(val, name)
Definition: intdefines.h:47
LocalPlayer * localPlayer
bool info(InputEvent &event)
Definition: commands.cpp:57
const ItemInfo & get(const int id)
Definition: itemdb.cpp:792
SoundManager soundManager
std::string sound
Definition: soundinfo.h:45