unit uGmData;

interface

uses
  SysUtils, zglHeader, uGmSprite;

var
  Font        : zglPFont;
  Font2       : zglPFont;

  CursorTex   : zglPTexture;
  CWalkTex    : zglPTexture;
  CSwordTex   : zglPTexture;
  CArrowTex   : zglPTexture;
  GrassTex    : zglPTexture;
  NoiseTex    : zglPTexture;
  HexTex      : zglPTexture;
  BlackhexTex : zglPTexture;
  SelCrHexTex : zglPTexture;
  ShadowTex   : zglPTexture;
  CoinTex     : zglPTexture;
  GoldTex     : zglPTexture;
  TitleTex    : zglPTexture;
  FightBtnTex : zglPTexture;
  FScrBtnTex  : zglPTexture;
  FScrBtnTex2 : zglPTexture;
  HourglassTex: zglPTexture;
  WinTex      : zglPTexture;
  DefeatTex   : zglPTexture;

type
  PCrPat = ^TCrPat;
  TCrPat = record
    FileName    : string;
    Name        : string;
    StandSpr    : TSprite;
    WalkSpr     : TSprite;
    AtackSpr    : TSprite;
    DamageSpr   : TSprite;

    SprOffsetX  : integer;
    SprOffsetY  : integer;
    MoveSpeed   : single;
    AtackFrame  : integer;
    DamageFrame : integer;
    BckDAnim    : boolean;

    Health      : integer;
    Damage      : integer;
    Speed       : integer;
    Cost        : integer;
    SdachaCnt   : integer;
    CanShoot    : boolean;
    CanFly      : boolean;
    Bullet      : string;
    Ability     : string;
  end;

var
  CrPat     : array of TCrPat;
  CrPatCnt  : integer;

type
  PDecorPat = ^TDecorPat;
  TDecorPat = record
    FileName  : string;
    Tex       : zglPTexture;
    OffsetX   : integer;
    OffsetY   : integer;
  end;

var
  DecorPat     : array of TDecorPat;
  DecorPatCnt  : integer;

type
  PBulletPat = ^TBulletPat;
  TBulletPat = record
    FileName  : string;
    Tex       : zglPTexture;
    OffsetX   : integer;
    OffsetY   : integer;
    Speed     : single;
    NoRotate  : boolean;
  end;

var
  BulletPat    : array of TBulletPat;
  BulletPatCnt : integer;

procedure Data_Load;
procedure CrPat_Load( FileName : string );
function CrPat_Get( FileName : string ): PCrPat;
procedure DecorPat_Load( FileName : string );
function DecorPat_Get( FileName : string ): PDecorPat;
procedure BulletPat_Load( FileName : string );
function BulletPat_Get( FileName : string ): PBulletPat;

implementation

//==============================================================================
procedure Data_Load;
var
  fs : TSearchRec;
begin
  Font        := font_LoadFromFile( '.\Data\GUI\font.zfi' );
  Font2       := font_LoadFromFile( '.\Data\GUI\font2.zfi' );  

  CursorTex   := tex_LoadFromFile( '.\Data\GUI\cursor.png' );
  CWalkTex    := tex_LoadFromFile( '.\Data\GUI\walk.png' );
  CSwordTex   := tex_LoadFromFile( '.\Data\GUI\sword.png' );
  CArrowTex   := tex_LoadFromFile( '.\Data\GUI\arrow.png' );

  GrassTex    := tex_LoadFromFile( '.\Data\Textures\grass.png' );
  NoiseTex    := tex_LoadFromFile( '.\Data\Textures\noise.jpg' );
  HexTex      := tex_LoadFromFile( '.\Data\Textures\hex.png' );
  BlackhexTex := tex_LoadFromFile( '.\Data\Textures\blackhex.png' );
  SelCrHexTex := tex_LoadFromFile( '.\Data\Textures\selcrhex.png' );
  ShadowTex   := tex_LoadFromFile( '.\Data\Textures\Shadow.png' );

  CoinTex     := tex_LoadFromFile( '.\Data\GUI\coin.png' );
  GoldTex     := tex_LoadFromFile( '.\Data\GUI\gold.png' );
  TitleTex    := tex_LoadFromFile( '.\Data\GUI\title.png' );
  FightBtnTex := tex_LoadFromFile( '.\Data\GUI\fight.png' );
  FScrBtnTex  := tex_LoadFromFile( '.\Data\GUI\fscreen.png' );
  FScrBtnTex2 := tex_LoadFromFile( '.\Data\GUI\fscreen2.png' );
  HourglassTex:= tex_LoadFromFile( '.\Data\GUI\Hourglass.png' );
  WinTex      := tex_LoadFromFile( '.\Data\GUI\Win.png' );
  DefeatTex   := tex_LoadFromFile( '.\Data\GUI\Defeat.png' );

  if FindFirst( '.\Data\Creatures\*.*', faDirectory, fs ) = 0 then
    repeat
      if ( fs.Name = '.' ) or ( fs.Name = '..' ) then Continue;
      CrPat_Load( '.\Data\Creatures\' + fs.Name +'\' + fs.Name + '.dat' );
    until FindNext( fs ) <> 0;
  FindClose( fs );

  if FindFirst( '.\Data\Decor\*.dat', faAnyFile, fs ) = 0 then
    repeat
      if ( fs.Name = '.' ) or ( fs.Name = '..' ) then Continue;
      DecorPat_Load( '.\Data\Decor\' + fs.Name );
    until FindNext( fs ) <> 0;
  FindClose( fs );

  if FindFirst( '.\Data\Bullets\*.dat', faAnyFile, fs ) = 0 then
    repeat
      if ( fs.Name = '.' ) or ( fs.Name = '..' ) then Continue;
      BulletPat_Load( '.\Data\Bullets\' + fs.Name );
    until FindNext( fs ) <> 0;
  FindClose( fs );
end;

//==============================================================================
function GetParamName( str : string ) : string;
var
  len, i : integer;
begin
  Result := '';
  len := Length( str );
  for i := 1 to len do
  begin
    if str[ i ] = '=' then Exit;
    Result := Result + str[ i ];
  end;
end;

//==============================================================================
function GetParamValue( str : string ) : string;
var
  len, i : integer;
  bool   : boolean;
begin
  Result := '';
  len    := Length( str );
  bool   := False;
  for i := 1 to len do
  begin
    if str[ i ] = '=' then
    begin
      bool := True;
      Continue;
    end;
    if bool = True then Result := Result + str[ i ];
  end;
end;

//==============================================================================
procedure CrPat_Load( FileName : string );
var
  F       : TextFile;
  str     : string;
  fname   : string;
  fdir    : string;
  k       : integer;
  param   : string;
  value   : string;
begin
  if file_Exists( FileName ) = False then Exit;
   
  INC( CrPatCnt );
  SetLength( CrPat, CrPatCnt );
  k := CrPatCnt - 1;

  fname := file_GetName( FileName );
  fdir  := ExtractFileDir( FileName );

  CrPat[ k ].FileName := fname;

  if file_Exists( fdir + '\' + fname + '_s.spr' ) then
    Sprite_Load( @CrPat[ k ].StandSpr, fdir + '\' + fname + '_s.spr' );
  if file_Exists( fdir + '\' + fname + '_w.spr' ) then
    Sprite_Load( @CrPat[ k ].WalkSpr, fdir + '\' + fname + '_w.spr' );
  if file_Exists( fdir + '\' + fname + '_a.spr' ) then
    Sprite_Load( @CrPat[ k ].AtackSpr, fdir + '\' + fname + '_a.spr' );
  if file_Exists( fdir + '\' + fname + '_d.spr' ) then
    Sprite_Load( @CrPat[ k ].DamageSpr, fdir + '\' + fname + '_d.spr' );

  AssignFile( F, FileName );
  FileMode := $0000;
  Reset( F );

  while not EOF( F ) do
  begin
    ReadLn( F, str );

    param := GetParamName( str );
    value := GetParamValue( str );

    if param = 'Name'         then CrPat[ k ].Name        := value;
    if param = 'SprOffsetX'   then CrPat[ k ].SprOffsetX  := StrToInt( value );
    if param = 'SprOffsetY'   then CrPat[ k ].SprOffsetY  := StrToInt( value );
    if param = 'MoveSpeed'    then CrPat[ k ].MoveSpeed   := u_StrToFloat( value );
    if param = 'AtackFrame'   then CrPat[ k ].AtackFrame  := StrToInt( value );
    if param = 'DamageFrame'  then CrPat[ k ].DamageFrame := StrToInt( value );
    if param = 'BckDAnim'     then CrPat[ k ].BckDAnim    := True;
    if param = 'Health'       then CrPat[ k ].Health      := StrToInt( value );
    if param = 'Damage'       then CrPat[ k ].Damage      := StrToInt( value );
    if param = 'Speed'        then CrPat[ k ].Speed       := StrToInt( value ) + 1;
    if param = 'Cost'         then CrPat[ k ].Cost        := StrToInt( value );
    if param = 'SdachaCnt'    then CrPat[ k ].SdachaCnt   := StrToInt( value );
    if param = 'CanShoot'     then CrPat[ k ].CanShoot    := True;
    if param = 'CanFly'       then CrPat[ k ].CanFly      := True;       
    if param = 'Bullet'       then CrPat[ k ].Bullet      := value;
    if param = 'Ability'      then CrPat[ k ].Ability     := value;    
  end;

  CloseFile( F );
end;

//==============================================================================
function CrPat_Get( FileName : string ): PCrPat;
var
  i : integer;
begin
  Result := nil;
  for i := 0 to CrPatCnt - 1 do
    if CrPat[ i ].FileName = FileName then
    begin
      Result := @CrPat[ i ];
      Exit;
    end;
end;

//==============================================================================
procedure DecorPat_Load( FileName : string );
var
  F : TextFile;
  k : integer;
  str, param, value : string;
begin
  if file_Exists( FileName ) = False then Exit;

  INC( DecorPatCnt );
  SetLength( DecorPat, DecorPatCnt );
  k := DecorPatCnt - 1;

  DecorPat[ k ].FileName := file_GetName( FileName );

  AssignFile( F, FileName );
  FileMode := $0000;
  Reset( F );

  while not EOF( F ) do
  begin
    ReadLn( F, str );

    param := GetParamName( str );
    value := GetParamValue( str );

    if param = 'OffsetX' then DecorPat[ k ].OffsetX := StrToInt( value );
    if param = 'OffsetY' then DecorPat[ k ].OffsetY := StrToInt( value );
    if param = 'Texture' then DecorPat[ k ].Tex     := tex_LoadFromFile( '.\Data\Decor\' + value );
  end;

  CloseFile( F );
end;

//==============================================================================
function DecorPat_Get( FileName : string ): PDecorPat;
var
  i : integer;
begin
  Result := nil;
  for i := 0 to DecorPatCnt - 1 do
    if DecorPat[ i ].FileName = FileName then
    begin
      Result := @DecorPat[ i ];
      Exit;
    end;
end;

//==============================================================================
procedure BulletPat_Load( FileName : string );
var
  F : TextFile;
  k : integer;
  str, param, value : string;
begin
  if file_Exists( FileName ) = False then Exit;

  INC( BulletPatCnt );
  SetLength( BulletPat, BulletPatCnt );
  k := BulletPatCnt - 1;

  BulletPat[ k ].FileName := file_GetName( FileName );

  AssignFile( F, FileName );
  FileMode := $0000;
  Reset( F );

  while not EOF( F ) do
  begin
    ReadLn( F, str );

    param := GetParamName( str );
    value := GetParamValue( str );

    if param = 'OffsetX'  then BulletPat[ k ].OffsetX  := StrToInt( value );
    if param = 'OffsetY'  then BulletPat[ k ].OffsetY  := StrToInt( value );
    if param = 'Texture'  then BulletPat[ k ].Tex      := tex_LoadFromFile( '.\Data\Bullets\' + value );
    if param = 'Speed'    then BulletPat[ k ].Speed    := u_StrToFloat( value );
    if param = 'NoRotate' then BulletPat[ k ].NoRotate := True;
  end;

  CloseFile( F );
end;

//==============================================================================
function BulletPat_Get( FileName : string ): PBulletPat;
var
  i : integer;
begin
  Result := nil;
  for i := 0 to BulletPatCnt - 1 do
    if BulletPat[ i ].FileName = FileName then
    begin
      Result := @BulletPat[ i ];
      Exit;
    end;
end;


end.
