1 // ==============================================================
2 //	This file is part of Glest (www.glest.org)
3 //
4 //	Copyright (C) 2001-2008 Marti�o Figueroa
5 //
6 //	You can redistribute this code and/or modify it under
7 //	the terms of the GNU General Public License as published
8 //	by the Free Software Foundation; either version 2 of the
9 //	License, or (at your option) any later version
10 // ==============================================================
11 
12 #include "time_flow.h"
13 
14 #include "sound_renderer.h"
15 #include "config.h"
16 #include "game_constants.h"
17 #include "leak_dumper.h"
18 
19 namespace Glest{ namespace Game{
20 
21 // =====================================================
22 // 	class TimeFlow
23 // =====================================================
24 
25 const float TimeFlow::dusk= 18.f;
26 const float TimeFlow::dawn= 6.f;
27 
init(Tileset * tileset)28 void TimeFlow::init(Tileset *tileset){
29 	firstTime= true;
30 	this->tileset= tileset;
31 	time= dawn+1.5f;
32 	lastTime= time;
33 	Config &config= Config::getInstance();
34 	timeInc= 24.f*(1.f/config.getFloat("DayTime"))/GameConstants::updateFps;
35 }
36 
update()37 void TimeFlow::update(){
38 
39 	//update time
40 	time+= isDay()? timeInc: timeInc*2;
41 	if(time>24.f){
42 		time-= 24.f;
43 	}
44 
45 	//sounds
46 	SoundRenderer &soundRenderer= SoundRenderer::getInstance();
47 	AmbientSounds *ambientSounds= tileset->getAmbientSounds();
48 
49 	//day
50 	if(lastTime<dawn && time>=dawn){
51 		soundRenderer.stopAmbient(ambientSounds->getNight());
52 	}
53 
54 	if((lastTime<dawn && time>=dawn) || firstTime){
55 
56 		//day sound
57 		if(ambientSounds->isEnabledDayStart() && !firstTime){
58 			soundRenderer.playFx(ambientSounds->getDayStart());
59 		}
60 		if(ambientSounds->isEnabledDay()){
61 			if(ambientSounds->getAlwaysPlayDay() || tileset->getWeather()==wSunny){
62 				soundRenderer.playAmbient(ambientSounds->getDay());
63 			}
64 		}
65 		firstTime= false;
66 	}
67 
68 	//night
69 	if(lastTime<dusk && time>=dusk){
70 		soundRenderer.stopAmbient(ambientSounds->getDay());
71 	}
72 
73 	if(lastTime<dusk && time>=dusk){
74 		//night
75 		if(ambientSounds->isEnabledNightStart()){
76 			soundRenderer.playFx(ambientSounds->getNightStart());
77 		}
78 		if(ambientSounds->isEnabledNight()){
79 			if(ambientSounds->getAlwaysPlayNight() || tileset->getWeather()==wSunny){
80 				soundRenderer.playAmbient(ambientSounds->getNight());
81 			}
82 		}
83 	}
84 	lastTime= time;
85 }
86 
isAproxTime(float time)87 bool TimeFlow::isAproxTime(float time){
88 	return (this->time>=time) && (this->time<time+timeInc);
89 }
90 
91 }}//end namespace
92