Capycore Engine  0.1.0
A simple and lightweight game engine written in C++ based on the Unity API architecture.
Loading...
Searching...
No Matches
AudioSource Class Reference

Component that plays audio using the AudioService. More...

#include <audio_source.h>

Inheritance diagram for AudioSource:
[legend]

Public Member Functions

 AudioSource (std::string audio_file_path, bool play_on_awake=true, bool loop=false, float volume=1.0f)
 
 ~AudioSource () override=default
 
void update (float dt) override
 
void on_attach () override
 Called when the component is attached to a GameObject.
 
void on_detach () override
 Called when the component is detached from a GameObject.
 
void play (bool loop=false)
 
void stop ()
 
const std::string & audio_file_path () const noexcept
 
void audio_file_path (const std::string &path) noexcept
 
float volume () const noexcept
 
void volume (float value) noexcept
 
bool loop () const noexcept
 
void loop (bool value) noexcept
 
bool play_on_awake () const noexcept
 
void play_on_awake (bool value) noexcept
 
std::string audio_name () const noexcept
 
std::optional< std::reference_wrapper< SoundInstance > > instance () const noexcept
 
std::string type_name () const override
 Provides a consistent type name for the component.
 
- Public Member Functions inherited from Component
 Component ()
 
virtual ~Component ()=default
 
bool active () const noexcept
 Checks if the component is active.
 
Componentactive (bool value) noexcept
 
bool marked_for_deletion () const noexcept
 Activates the component.
 
Componentmark_for_deletion () noexcept
 
virtual void on_serialize (std::vector< uint8_t > &) const
 Serializes the component's state into a byte array.
 
virtual void on_deserialize (const std::vector< uint8_t > &, size_t &)
 Deserializes the component's state from a byte array.
 
const std::optional< std::reference_wrapper< GameObject > > & parent () const noexcept
 Gets the parent GameObject of this component.
 
std::optional< std::reference_wrapper< GameObject > > & parent () noexcept
 Gets the parent GameObject of this component.
 
Componentparent (GameObject &parent)
 
Componentparent (std::nullopt_t nullopt)
 
size_t add_on_attach (const std::function< void(Component &)> &action)
 Retrieves the type information of the component.
 
void remove_on_attach (size_t index)
 
size_t add_on_detach (const std::function< void(Component &)> &action)
 Adds an action to be performed when the component is attached.
 
void remove_on_detach (size_t index)
 

Detailed Description

Component that plays audio using the AudioService.

AudioSource allows a GameObject to play sounds, with options for looping, volume control, and play-on-awake functionality.

Parameters
audio_file_pathThe file path to the audio clip.
play_on_awakeWhether the audio should play automatically when the component is attached.
loopWhether the audio should loop after finishing.
volumeThe volume level of the audio (0.0 to 1.0).

Usage:

  • Create an AudioSource with the desired audio file and settings.
  • Call play() to start playback, and stop() to halt it.
  • Adjust volume and looping settings as needed.
    Note
    Ensure the audio file exists and is in a supported format.

Constructor & Destructor Documentation

◆ AudioSource()

AudioSource::AudioSource ( std::string  audio_file_path,
bool  play_on_awake = true,
bool  loop = false,
float  volume = 1.0f 
)
explicit

◆ ~AudioSource()

AudioSource::~AudioSource ( )
overridedefault

Member Function Documentation

◆ audio_file_path() [1/2]

const std::string & AudioSource::audio_file_path ( ) const
noexcept

◆ audio_file_path() [2/2]

void AudioSource::audio_file_path ( const std::string &  path)
noexcept

◆ audio_name()

std::string AudioSource::audio_name ( ) const
noexcept

◆ instance()

std::optional< std::reference_wrapper< SoundInstance > > AudioSource::instance ( ) const
noexcept

◆ loop() [1/2]

bool AudioSource::loop ( ) const
noexcept

◆ loop() [2/2]

void AudioSource::loop ( bool  value)
noexcept

◆ on_attach()

void AudioSource::on_attach ( )
overridevirtual

Called when the component is attached to a GameObject.

Reimplemented from Component.

◆ on_detach()

void AudioSource::on_detach ( )
overridevirtual

Called when the component is detached from a GameObject.

Reimplemented from Component.

◆ play()

void AudioSource::play ( bool  loop = false)

◆ play_on_awake() [1/2]

bool AudioSource::play_on_awake ( ) const
noexcept

◆ play_on_awake() [2/2]

void AudioSource::play_on_awake ( bool  value)
noexcept

◆ stop()

void AudioSource::stop ( )

◆ type_name()

std::string AudioSource::type_name ( ) const
overridevirtual

Provides a consistent type name for the component.

Returns
A string representing the type name of the component. @CAUTION: All derived components must implement this method to ensure consistent serialization across different compilers and platforms.

Implements Component.

◆ update()

void AudioSource::update ( float  dt)
overridevirtual

Implements Component.

◆ volume() [1/2]

float AudioSource::volume ( ) const
noexcept

◆ volume() [2/2]

void AudioSource::volume ( float  value)
noexcept

The documentation for this class was generated from the following file: