// Players can play up to 4 sounds concurrently in slots 0-3. We'll use slot 0
// for saying things.
$Player::SpeechSlot = 0;

// Say a phrase defined in the voice object given.
function Player::say(%this, %phrase, %voice)
{
   if (!isObject(%voice))
      %voice = DefaultVoice;

   %this.stopTalking();

   %sound = %voice.getValue(%voice.getIndexFromKey(%phrase));
   if (isObject(%sound))
   {
      %this.playAudio($Player::SpeechSlot, %sound);
      %this.stopTalking = %this.getDataBlock().schedule(
         %sound.getSoundDuration() * 1000,
         "onFinishedTalking",
         %this);
   }
}

// Shut this Player up immediately!
function Player::stopTalking(%this)
{
   %this.stopAudio($Player::SpeechSlot);
   if (%this.stopTalking)
   {
      cancel(%this.stopTalking);
      %this.getDatablock().onFinishedTalking(%this);
   }
}

// Callback when the character finishes playing the speech sound. You can override
// this in other datablock classes, but remember to always call
// Parent::onFinishedTalking(%this, %obj);
function PlayerData::onFinishedTalking(%this, %obj)
{
   %obj.stopTalking = 0;
}

// Define the 'default' voice used.
new ArrayObject(DefaultVoice);
DefaultVoice.add("ouch", PainCrySound);
DefaultVoice.add("ouch!", DeathCrySound);