GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/itemsoundmanager.cpp Lines: 1 24 4.2 %
Date: 2018-12-09 Branches: 0 18 0.0 %

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