File indexing completed on 2023-12-03 07:53:49

0001 /*
0002     SPDX-FileCopyrightText: 2007 Paolo Capriotti <p.capriotti@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "audioplayer.h"
0008 
0009 #include <KgSound>
0010 
0011 #include <QDir>
0012 #include <QStandardPaths>
0013 
0014 AudioPlayer::AudioPlayer(QObject* parent)
0015 : QObject(parent)
0016 , m_sink(nullptr)
0017 , m_shootA(nullptr)
0018 , m_shootB(nullptr)
0019 , m_shootWater(nullptr)
0020 {
0021 
0022 }
0023 
0024 void AudioPlayer::play(Sea::Player player, const HitInfo& info)
0025 {
0026     KgSound *sound;
0027     if (info.type == HitInfo::HIT) {
0028         if (info.shipDestroyed) {
0029             sound = m_sink;
0030         }
0031         else {
0032             sound = player == Sea::PLAYER_A ? m_shootA : m_shootB;
0033         }
0034     }
0035     else {
0036         sound = m_shootWater;
0037     }
0038 
0039     if (sound) {
0040         sound->start();
0041     }
0042 }
0043 
0044 void AudioPlayer::setActive(bool value)
0045 {
0046     if (value) {
0047         if (!m_sink) {
0048             const QDir dir = QStandardPaths::locate(QStandardPaths::AppDataLocation, QStringLiteral("sounds/"), QStandardPaths::LocateDirectory);
0049             m_sink = new KgSound(dir.filePath(QStringLiteral("ship-sink.ogg")), this);
0050             m_shootA = new KgSound(dir.filePath(QStringLiteral("ship-player1-shoot.ogg")), this);
0051             m_shootB = new KgSound(dir.filePath(QStringLiteral("ship-player2-shoot.ogg")), this);
0052             m_shootWater = new KgSound(dir.filePath(QStringLiteral("ship-player-shoot-water.ogg")), this);
0053         }
0054     }
0055     else {
0056         delete m_sink;
0057         delete m_shootA;
0058         delete m_shootB;
0059         delete m_shootWater;
0060         m_sink = nullptr;
0061         m_shootA = nullptr;
0062         m_shootB = nullptr;
0063         m_shootWater = nullptr;
0064     }
0065 }
0066 
0067 #include "moc_audioplayer.cpp"