unit g_World;

interface
uses
  g_Data,g_Types,g_Game,g_Land_load,g_Monster_Load,g_Tower,g_Camera,g_Land,g_Monster,g_Base,
  {$INCLUDE uses.inc};

procedure World_Load(self : zWorld);
procedure World_Update(self : zWorld);
procedure World_Draw(self : zWorld);
procedure World_Destroy(self : zWorld);
implementation

procedure World_Load(self : zWorld);
var
  dir : string;
  i : byte;
begin
  dir := iz_dir_local_data +'world.cfg';
  self^.MaxMonster := 100;
  self^.MonAddCoef := self^.MaxMonster div 3;
  self^.MonsterTypeCount := Cfg_ReadInt(dir,'MONSTERTYPE',1);
  self^.MonsterCreateLTC := GetTickCount;
  self^.MonsterCreateDelay := 1500;
  self^.CharacterCount := self^.MonsterTypeCount;
  self^.CharacterActiveCount := self^.MonsterTypeCount;
  SetLength(self^.Characters,self^.MonsterTypeCount);
  SetLength(self^.Monsters,self^.MonsterTypeCount);
  for i := 0 to self^.MonsterTypeCount-1 do
  begin
    self^.Monsters[i].Character := i;
    self^.Characters[self^.Monsters[i].Character].name := Cfg_ReadStr(dir,'CHAR_TYPE_'+inttostr(i+1),'ZOMBIE');
    Monster_Load(@self^.Monsters[i]);
  end;
  self^.TowerTypeCount := Cfg_ReadInt(dir,'TOWERTYPE',2);
  self^.TowerCount := self^.TowerTypeCount;
  SetLength(self^.Tower,self^.TowerCount);
  self^.ActiveTower := 1;
  for i := 0 to self^.TowerTypeCount-1 do
  begin
    self^.Tower[i].name := Cfg_ReadStr(dir,'TOWER_TYPE_' + inttostr(i+1),'FIRST');
    Tower_Load(@self^.Tower[i]);
  end;
end;

procedure World_Update(self : zWorld);
var i : word;
begin
  if not Game.Finish then begin
    Monster_Add_Update;

    i:=World.MonsterTypeCount;
    while i<World.EndCharAC do
    begin
      Monster_Update(@World.Monsters[i],DeltaTime);
      inc(i);
    end;

    i:=World.TowerTypeCount;
    while i<World.TowerCount do begin
      Tower_Update(@World.Tower[i],DeltaTime);
      inc(i);
    end;

    Base_Update(@World.Base);
    Check_for_complte(@Game);
  end;
end;

procedure World_Draw(self : zWorld);
var i,j : word;
begin
  glTranslatef(round(Camera.pos[0]),round(Camera.pos[1]),0);
  for i:=Camera.left[0] to Camera.right[0] do
  for j:=Camera.left[1] to Camera.right[1] do LandDraw(i,j);

  //WayPointsDraw();

  i:=World.MonsterTypeCount;
  while i<World.EndCharAC do
  begin
    if ((World.Characters[World.Monsters[i].Character].pos[0]+World.Characters[World.Monsters[i].Character].rad > -Camera.pos[0])and
    (World.Characters[World.Monsters[i].Character].pos[1]+World.Characters[World.Monsters[i].Character].rad > -Camera.pos[1])and
    (World.Characters[World.Monsters[i].Character].pos[0]-World.Characters[World.Monsters[i].Character].rad < -Camera.Pos_2[0])and
    (World.Characters[World.Monsters[i].Character].pos[1]-World.Characters[World.Monsters[i].Character].rad < -Camera.Pos_2[1]))
    then Draw_Monster(@World.Monsters[i]);
    inc(i);
  end;

  i:=World.TowerTypeCount;
  while i<World.TowerCount do begin
    Tower_Draw(@World.Tower[i]);
    inc(i);
  end;

  i:=0;
  while i<ObjCount do
  begin
    if ((Obj[i].pos[0]+Obj[i].rad > -Camera.pos[0])and
    (Obj[i].pos[1]+Obj[i].rad > -Camera.pos[1])and
    (Obj[i].pos[0]-Obj[i].rad < -Camera.pos_2[0])and
    (Obj[i].pos[1]-Obj[i].Rad < -Camera.pos_2[1]) and (i <> 0)) then ObjDraw(i);
    inc(i);
  end;

  Base_Draw(@World.Base);
end;

procedure World_Destroy(self : zWorld);
begin
  self^.CharacterCount := 0;
  self^.TowerCount := 0;
  SetLength(self^.Monsters,0);
  SetLength(self^.Characters,0);
  SetLength(self^.Tower,0);
  Game.GUI.Destroy;
end;

end.
