unit g_Character;

interface
uses
  g_Data,g_Types,g_Utils,g_Land_Load,g_Game,
  {$INCLUDE uses.inc};

procedure Character_Draw(self : zCharacter);
procedure Character_Update(self : zCharacter; time : float);
procedure Character_See(self : zCharacter; pos1 : izPosition_f);
procedure Character_Move(self : zCharacter; time : float);
function Character_Check_to_Dead(self : zCharacter) : bool;
implementation

procedure Character_Draw(self : zCharacter);
begin
  Gfx.TexMan.Bind(self^.Tex);
  case self^.State of
    csMove : begin
      Gfx.SprMan.Draw_Anim_Alpha(self^.Spr,self^.CHARACTER_ANIM_BODY_MOVE,self^.Pos,self^.SeeAngle);
      gfx.FontMan.Write(Game_font,zRound(self^.pos),inttostr(self^.life));
    end;
    csAtack : begin
      Gfx.SprMan.Draw_Anim_Alpha(self^.Spr, self^.CHARACTER_ANIM_BODY_MOVE,self^.Pos,self^.SeeAngle);
   end;
   csDeath : begin
     Gfx.SprMan.Draw_Anim_Alpha(self^.Spr,self^.CHARACTER_ANIM_DEAD,self^.Pos,self^.SeeAngle);
   end;
   csDead : begin
     Gfx.SprMan.Draw_Alpha(self^.Spr,self^.AniDeadEnd,self^.Pos,self^.SeeAngle);
   end;
 end;
end;

procedure Character_Update(self : zCharacter; time : float);
begin
  case self^.State of
   csMove : begin
     Gfx.SprMan.Anim_Update(self^.Spr,self^.CHARACTER_ANIM_BODY_MOVE,time*50);
     Character_Move(self,time);
   end;
   csAtack : begin
     Gfx.SprMan.Anim_Update(self^.Spr,self^.CHARACTER_ANIM_BODY_MOVE,time*50);
     Character_Move(self,time);
   end;
   csDeath: begin
     Gfx.SprMan.Anim_Update(self^.Spr,self^.CHARACTER_ANIM_DEAD,time*50);
     if gfx.SprMan.Items[self^.Spr].Animation[self^.CHARACTER_ANIM_DEAD].cID = gfx.SprMan.Items[self^.Spr].Animation[self^.CHARACTER_ANIM_DEAD].eID then
     begin
       self^.State := csDead;
       gfx.SprMan.Items[self^.Spr].Animation[self^.CHARACTER_ANIM_DEAD].cID := self^.AniDeadStart;
     end;
   end;
 end;
end;

procedure Character_See(self : zCharacter; pos1 : izPosition_f);
var pos : izPosition_f;
begin
   pos[0] := self^.pos[0] + self^.Deviation[0];
   pos[1] := self^.pos[1] + self^.Deviation[1];
   self^.SeeAngle := -GetAngle(pos,pos1)-90;
end;

procedure Character_Move(self : zCharacter; time : float);
var
  dt,a : float;
  r : word;
begin
 case self^.State of
   csMove : begin
     if Character_Check_to_Dead(self) then Exit;
     dt := Time*self^.Speed;
     if dt > 0 then
      begin
        a := PI/180*(-self^.SeeAngle+270);
        self^.Pos[0] := self^.Pos[0]-Sin(a)*dt;
        self^.Pos[1] := self^.Pos[1]-Cos(a)*dt;
        if (LineWayPoints[self^.LineWP].WPoints[self^.nextWP].pos[0] < self^.Pos[0]+80) and
           (LineWayPoints[self^.LineWP].WPoints[self^.nextWP].pos[0] > self^.Pos[0]-80) and
           (LineWayPoints[self^.LineWP].WPoints[self^.nextWP].pos[1] < self^.Pos[1]+80) and
           (LineWayPoints[self^.LineWP].WPoints[self^.nextWP].pos[1] > self^.Pos[1]-80) then
           if self^.NextWP < LineWayPoints[self^.LineWP].WPointCount-1 then
           begin
             self^.NextWP := self^.NextWP + 1;
             Character_See(self,LineWayPoints[self^.LineWP].WPoints[self^.nextWP].pos);
           end else
             self^.State := csAtack;
        end;
     end;
   csAtack : begin
     if Character_Check_to_Dead(self) then Exit;
     dt := Time*self^.Speed;
     if dt > 0 then
      begin
       Character_See(self,World.Base.Pos);
       r := 0;
       if not self^.Move_end then r := Distance_fast2Di(zRound(self^.pos),zRound(World.Base.pos));
       if r > World.Base.Rad then
       begin
         a := PI/180*(-self^.SeeAngle+270);
         self^.Pos[0] := self^.Pos[0]-Sin(a)*dt;
         self^.Pos[1] := self^.Pos[1]-Cos(a)*dt;
       end else
       begin
         Self^.Move_end := true;
         World.Base.Life := World.Base.Life - 0.1*time;
       end;
     end
   end;
 end;
end;

function Character_Check_to_Dead(self : zCharacter) : bool;
begin
  Result := false;
  if self^.Life <= 0 then
  begin
    self^.State := csDeath;
    gfx.SprMan.Items[self^.Spr].Animation[self^.CHARACTER_ANIM_DEAD].cID := self^.AniDeadStart;
    Add_Money_and_Mobs(@Game);
    Result := true;
  end;
end;

end.
