1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
use super::event_manager::*;
use glium::glutin::{ElementState, Event, VirtualKeyCode};
use std::f32::consts;
use base::math::*;

#[derive(Debug)]
pub struct DayTime {
    time_year: u32,
    time_day: u32,
    time_on_day: f32,
    speed: f32,
}

// `DEFAULT_TIME_SPEED` is 1.0 so the time goes at normal speed
// `PLUS_TIME_SPEED` is the factor with which the time is sped up, when the
// speed-up key is pressed
const DEFAULT_TIME_SPEED: f32 = 1.0;
const PLUS_TIME_SPEED: f32 = 100.0;


impl Default for DayTime {
    fn default() -> DayTime {
        DayTime {
            time_year: 0,
            time_day: 0,
            time_on_day: DAY_LENGTH / 3.0,
            speed: DEFAULT_TIME_SPEED,
        }
    }
}

// 360 == Mittag
const DAY_LENGTH: f32 = 720.0;
const YEAR_LENGTH: u32 = 12;


// lengthens the day by a static offset
const DAY_LENGTHER: f32 = 0.0;

// Distance of the path of the sun to the player
const SUN_DISTANCE: f32 = 300.0;

impl DayTime {
    pub fn set_time(&mut self, time_year: u32, time_day: u32, time_on_day: f32) {
        self.time_year = time_year;
        self.time_day = time_day;
        self.time_on_day = time_on_day;
        self.speed = DEFAULT_TIME_SPEED;
    }

    // Bei tag sind die RGBWerte bei 3000, leicht rötlich
    // Bei nacht sind sie 0
    pub fn get_sun_color(&self) -> Vector3f {
        let mut vec = Vector3f::new(0.0, 0.0, 0.0);

        let offset_factor = 1.1;
        let max = 30.0 / offset_factor;

        let factor = if self.time_on_day <= (DAY_LENGTH / 2.0) {
            max * (self.time_on_day / (DAY_LENGTH / 2.0))
        } else {
            max - (max * ((self.time_on_day - DAY_LENGTH / 2.0) / (DAY_LENGTH / 2.0)))
        };

        vec.x = offset_factor * factor;
        vec.y = factor;
        vec.z = factor;

        vec
    }

    // Bei tag ist die Helligkeit 100, leicht bläulich
    // Bei nacht ist die Helligkeit 0
    pub fn get_sky_light(&self) -> Vector3f {
        let mut vec = Vector3f::new(0.0, 0.0, 0.0);

        let offset_factor = 1.1;
        let max = 1.0 / offset_factor;

        let mut factor = if self.time_on_day <= (DAY_LENGTH / 2.0) {
            max * (self.time_on_day / (DAY_LENGTH / 2.0))
        } else {
            max - (max * 0.5 * ((self.time_on_day - DAY_LENGTH / 2.0) / (DAY_LENGTH / 2.0)))
        };

        if factor < 0.4 {
            factor = 0.4;
        }

        vec.x = factor;
        vec.y = factor;
        vec.z = offset_factor * factor;

        vec
    }

    pub fn get_time_year(&self) -> u32 {
        self.time_year
    }

    pub fn get_time_day(&self) -> u32 {
        self.time_day
    }

    pub fn get_time_on_day(&self) -> f32 {
        self.time_on_day
    }

    /// Updates time with the use of `delta` as additionally passed time
    /// `DAY_LENGTH` defines the length of a day in real-life seconds
    /// `YEAR_LENGTH` defines the length of a year in `DAY_LENGTH`s
    pub fn update(&mut self, delta: f32) {
        // Output of Time
        debug!("Year: {} Day: {} Time: {}",
               self.time_year,
               self.time_day,
               self.time_on_day);

        // Checks if one day has passed
        self.time_on_day += delta * self.speed;
        if (self.time_on_day) >= DAY_LENGTH {
            self.time_on_day -= DAY_LENGTH; // Removes one day from time_on_day
            self.time_day += 1;
            if (self.time_day) >= YEAR_LENGTH {
                self.time_day = 0;
                self.time_year += 1;
            }
        }

    }


    /// returns the position of the sun corresponding to time
    /// only mid summer
    pub fn get_sun_position(&self) -> Point3f {

        let half_year = YEAR_LENGTH as f32 / 2.0;
        let half_day = DAY_LENGTH as f32 / 2.0;

        let theta;
        let phi;

        let mut month_diff = self.time_day as f32 - half_year;
        if month_diff < 0.0 {
            month_diff *= -1.0
        }

        if self.time_on_day < half_day {
            // pre noon
            // sun rising
            theta = consts::PI - consts::PI * (self.time_on_day / half_day);
            phi = 0.0;
        } else {
            // after noon
            // sun going down
            theta = consts::PI * ((self.time_on_day - half_day) / half_day);
            phi = consts::PI;
        }

        // for debugging
        // info!("THETA: {} PHI: {}", theta, phi);

        // returns sun position in cartesian coordinates
        // uses `YEAR_LENGTH` and the current day of the year (month) to influence the
        // path the sun moves on
        let pos =
            Vector3f::new(theta.sin() * phi.cos(),
                          theta.sin() * phi.sin() + month_diff / (0.75 * YEAR_LENGTH as f32),
                          theta.cos() - month_diff / (0.75 * YEAR_LENGTH as f32) + DAY_LENGTHER)
                .normalize() * SUN_DISTANCE;

        Point3f::new(pos.x, pos.y, pos.z)
    }

    /// returns the Vector3f for the directional sunlight
    pub fn get_sun_light_vector(&self) -> Vector3f {
        Vector3f::new(0.0, 0.0, 0.0) - self.get_sun_position().to_vec().normalize()
    }
}

/// Handler to speed up time with use of '+' key
impl EventHandler for DayTime {
    fn handle_event(&mut self, e: &Event) -> EventResponse {
        match *e {
            Event::KeyboardInput(ElementState::Pressed, _, Some(VirtualKeyCode::Add)) => {
                self.speed = PLUS_TIME_SPEED;
                EventResponse::Continue
            }
            Event::KeyboardInput(ElementState::Released, _, Some(VirtualKeyCode::Add)) => {
                self.speed = DEFAULT_TIME_SPEED;
                EventResponse::Continue
            }
            _ => EventResponse::NotHandled,
        }

    }
}