1 // ambient_sound.cpp basti_
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23 #include "ambient_sound.h"
24 #include "object_factory.h"
25 #include "lisp/lisp.h"
28 AmbientSound::AmbientSound(const lisp::Lisp& lisp)
37 if (!(lisp.get("x", position.x)&&lisp.get("y", position.y))) {
38 std::cerr << "No Position in ambient_sound" << std::endl;
40 lisp.get("distance_factor",distance_factor);
41 lisp.get("distance_bias" ,distance_bias );
42 lisp.get("sample" ,sample );
44 if (distance_factor == 0)
45 silence_distance = 10e99;
47 silence_distance = distance_factor*10e2;
55 AmbientSound::hit(Player& )
60 AmbientSound::startPlaying()
62 playing=sound_manager->play_sound(sample,-1);
63 Mix_Volume(playing,0);
64 currentvolume=targetvolume=1e-20;
68 AmbientSound::action(float)
70 float dx=Sector::current()->player->get_pos().x-position.x;
71 float dy=Sector::current()->player->get_pos().y-position.y;
72 float distance=sqrt(dx*dx+dy*dy);
74 distance-=distance_bias;
79 targetvolume=1/(1+distance*distance_factor);
80 float rise=targetvolume/currentvolume;
81 currentvolume*=pow(rise,.05);
82 currentvolume += 1e-30;
83 // std::cout << currentvolume << " " << targetvolume << std::endl;
84 Mix_Volume(playing,(int)(currentvolume*MIX_MAX_VOLUME));
89 AmbientSound::draw(DrawingContext &)
93 IMPLEMENT_FACTORY(AmbientSound, "ambient_sound");