#include "soundnode.h"
#include "resourcemanager.h"

SoundNode::SoundNode(std::string soundFileName):
    m_sound_file(soundFileName)
{

}

void SoundNode::init()
{
    if ((m_valid = RESOURCE_CHECK(sf::SoundBuffer,m_sound_file)))
        m_sound.setBuffer(*RESOURCE_GET(sf::SoundBuffer, m_sound_file));
    else{
        sf::SoundBuffer* buffer;
        m_valid = buffer->loadFromFile(m_sound_file);
        if(m_valid){
            RESOURCE_ADD(buffer,sf::SoundBuffer,m_sound_file);
            m_sound.setBuffer(*buffer);
        }
    }
}

void SoundNode::play()
{
    if (m_valid)
        m_sound.play();
}

void SoundNode::pause()
{
    if(m_valid)
        m_sound.pause();
}

void SoundNode::stop()
{
    if(m_valid)
        m_sound.stop();
}

void SoundNode::setPosition(glm::vec3 pos,bool relative)
{
    m_sound.setPosition(pos.x,pos.y,pos.z);
    m_sound.setRelativeToListener(relative);
}

