Beispiel #1
0
void ts::scene::Engine_sound_controller::update(std::size_t ticks)
{
    auto frame_duration = ticks * 0.001f;

    if (sorting_required_ && started_)
    {
        sort_sounds_by_locality();
        play_prioritized_sounds();
    }

    for (auto& entry : engine_sounds_)
    {
        auto car = entry.first;

        if (auto& engine_sound = entry.second)
        {  
            auto position = car->position();
            auto speed = car->speed(), top_speed = car->top_speed();
           
            if (top_speed == 0.0) continue;

            const float pitch_factor = std::min(static_cast<float>(speed / top_speed), 1.1f);
            float new_pitch = base_pitch + (1.0f - base_pitch) * pitch_factor;

            if (!car->control_state(controls::Control::Accelerate))
            {
                new_pitch = std::max(base_pitch + (new_pitch - base_pitch) * 0.67f, static_cast<float>(engine_sound->getPitch()) - frame_duration);
            }

            engine_sound->setPitch(new_pitch);
            engine_sound->setVolume(new_pitch * new_pitch * 100.0f);
            engine_sound->setPosition(static_cast<float>(position.x), static_cast<float>(position.y), 0.0f);
        }
    }
}
Beispiel #2
0
 /*
  * Get a string representation of this Spaceship's specifications.
  * The string will be formatted as
  * "NAME\n
  *  Top Speed:     Warp TOP_SPEED\n
  *  Fuel Source:   FUEL_SOURCE\n
  *  Crew Capacity: CREW_CAPACITY"
  * where NAME, TOP_SPEED (to two decimal places), FUEL_SOURCE, and
  * CREW_CAPACITY contain the values of the associated member variables.
  * @uses stringstream
  * @return string - A representation of this Spaceship's specifications
  */
 string Spaceship::ToString() const
 {
     
     stringstream this_ship;
     this_ship.setf(std::ios::fixed|std::ios::showpoint); // floatfield not set
     
     this_ship.precision(2);
     this_ship << name() << "\n" << "Top Speed:     Warp " << top_speed() << "\n"
     << "Fuel Source:   " << fuel_source() << "\n" << "Crew Capacity: " << crew_capacity();
     return this_ship.str();
 }