unit time;

interface

uses variables, OpenGL, particles, creation;

procedure CountTime;
procedure CountDeltas;
procedure SetSkyColors;
procedure SetTimescale(new_time_scale : integer);

implementation

var
  ts, dr, dg, db, da : single;

procedure ChangeTime;
begin
with sky_color do
  begin
    r := r + dr;
    g := g + dg;
    b := b + db;
    fog_destination_density := fog_destination_density + da;
    fog_height_density := fog_height_density + da;
    glClearColor(r, g, b, 1);
  end;
end;

procedure CountDeltas;
var
nh : integer;
begin
ts := time_scale / 60;
nh := hours + 1; if nh > 23 then nh := 0;
dr := (sky_colors[nh].r - sky_colors[hours].r) * ts;
dg := (sky_colors[nh].g - sky_colors[hours].g) * ts;
db := (sky_colors[nh].b - sky_colors[hours].b) * ts;
da := (sky_colors[nh].a - sky_colors[hours].a) * ts;
end;

procedure SetTimescale(new_time_scale : integer);
begin
time_scale := new_time_scale;
if time_scale < 5 - time_control then time_scale := 5 - time_control;
if time_scale > 5 + time_control then time_scale := 5 + time_control;
if time_scale < 0 then time_scale := 0;
if time_scale > 30 then time_scale := 30;

CountDeltas;
rain.fade_in_speed := time_scale / 10;
rain.fade_out_speed := rain.fade_in_speed;
end;

procedure ChangeWeather;
begin
if random(2) = random(3) then
  if rain.active then rain.fade_out := true
  else rain.fade_in := true;
end;

procedure CountTime;
begin
minutes := minutes + time_scale;

if (days >= next_wave_day) and (hours >= next_wave_hour) and
 (minutes >= next_wave_minute) then CreateEnemys;

credits := credits + credits_for_minute*time_scale;
score := score + score_for_minute*time_scale;

ChangeTime;
if minutes < 60 then exit;
minutes := 0;
hours := hours + 1;

credits := credits + credits_for_hour;
score := score + score_for_hour;

if hours > 23 then
  begin
    days := days + 1; hours := 0;
    credits := credits + credits_for_day;
    score := score + score_for_day;
  end;
if rain_aviable then if hours mod 4 = 0 then ChangeWeather;
CountDeltas;
end;

procedure SetSkyColors;
begin
with sky_colors[0] do begin r := 0.05; g := 0.05; b := 0.05; a := 0.0011; end;
with sky_colors[1] do begin r := 0.05; g := 0.05; b := 0.05; a := 0.00111; end;
with sky_colors[2] do begin r := 0.05; g := 0.05; b := 0.05; a := 0.00112; end;
with sky_colors[3] do begin r := 0.05; g := 0.05; b := 0.05; a := 0.00112; end;
with sky_colors[4] do begin r := 0.1; g := 0.1; b := 0.13; a := 0.00111; end;
with sky_colors[5] do begin r := 0.15; g := 0.15; b := 0.18; a := 0.0011; end;
with sky_colors[6] do begin r := 0.24; g := 0.2; b := 0.22; a := 0.0011; end;
with sky_colors[7] do begin r := 0.28; g := 0.23; b := 0.25; a := 0.0011; end;
with sky_colors[8] do begin r := 0.37; g := 0.35; b := 0.35; a := 0.00099; end;
with sky_colors[9] do begin r := 0.45; g := 0.45; b := 0.45; a := 0.00097; end;
with sky_colors[10] do begin r := 0.6; g := 0.6; b := 0.6; a := 0.00095; end;
with sky_colors[11] do begin r := 0.75; g := 0.75; b := 0.75; a := 0.0009; end;
with sky_colors[12] do begin r := 0.85; g := 0.85; b := 0.85; a := 0.0008; end;
with sky_colors[13] do begin r := 0.9; g := 0.9; b := 0.95; a := 0.0008; end;
with sky_colors[14] do begin r := 0.9; g := 0.9; b := 0.95; a := 0.0008; end;
with sky_colors[15] do begin r := 0.9; g := 0.9; b := 0.95; a := 0.0008; end;
with sky_colors[16] do begin r := 0.85; g := 0.85; b := 0.85; a := 0.0008; end;
with sky_colors[17] do begin r := 0.75; g := 0.75; b := 0.8; a := 0.0008; end;
with sky_colors[18] do begin r := 0.7; g := 0.7; b := 0.8; a := 0.0008; end;
with sky_colors[19] do begin r := 0.65; g := 0.65; b := 0.75; a := 0.00085; end;
with sky_colors[20] do begin r := 0.56; g := 0.55; b := 0.6; a := 0.0009; end;
with sky_colors[21] do begin r := 0.42; g := 0.42; b := 0.45; a := 0.00092; end;
with sky_colors[22] do begin r := 0.25; g := 0.25; b := 0.28; a := 0.00095; end;
with sky_colors[23] do begin r := 0.15; g := 0.15; b := 0.18; a := 0.001; end;

sky_color := sky_colors[hours];
fog_destination_density := sky_colors[hours].a;
fog_height_density := sky_colors[hours].a;
end;

end.
