unit Main;

interface

uses
  SysUtils,
  Squall,
  JEN_Header,
  JEN_Math,
  UDirector,
  UPlayer,
  UWeapon,
  UEnemy;

type
  TPerk = (PR_Heal, PR_Power, PR_Speed);

  TGame = class(TInterfacedObject, IGame)
  constructor Create(const Name: string);
  public
    procedure LoadContent; stdcall;
    procedure OnUpdate(dt: LongInt); stdcall;
    procedure OnRender; stdcall;
    procedure Close; stdcall;
    procedure AddScore(Count:LongInt);
    procedure SaveHighscore;
  var
    Name      : string;
    Score     : LongInt;
    Cash      : LongInt;
    Perk      : array[TPerk] of LongInt;
    Player    : TPlayer;
    WeaponEng : TWeaponEng;
    EnemyEng  : TEnemyEng;
    Font      : IFont;
    GameTime  : Cardinal;
    SceneDirector  : TDirector;
    ScreenWidth: Single;
    ScreenHeight: Single;
    OffSet      : TVec2i;
    GameScene : TScene;
  end;

const
  WorldSize = 1024;

var
  Engine    : IJenEngine;
  Display   : IDisplay;
  Render    : IRender;
  Render2d  : IRender2d;
  Helpers   : IHelpers;
  Game      : TGame;
  Input     : IInput;
  ResMan    : IResourceManager;

implementation

uses
  UIntro, UMainMenu, UGameScene;

constructor TGame.Create(const Name: string);
begin
  Self.Name := Name;
end;

procedure TGame.LoadContent;
begin
  ResMan.Load('Media\ArialFont.jfi', Font);
  SceneDirector := TDirector.Create;
  SceneDirector.AddScene(TIntroScene.Create);
  SceneDirector.AddScene(TMenuScene.Create);
  GameScene := SceneDirector.AddScene(TGameScene.Create);
  SceneDirector.LoadScene('Menu');

  ScreenWidth:= Render2d.RCWidth + Render2d.RCRect.Left *2;
  ScreenHeight:= Render2d.RCHeight + Render2d.RCRect.Top *2;
  OffSet := Render2d.RCRect.Location;
end;

procedure TGame.OnUpdate(dt: LongInt);
var
  s : String;
begin
  SceneDirector.Update(dt);
  s := IntToStr(Render.FPS) + ':' + IntToStr(Render.FrameDipCount);
  Display.Caption := PWideChar(s);
end;

procedure TGame.OnRender;
begin
  Render.Clear(True, False, False);
  SceneDirector.Draw;
end;

procedure TGame.AddScore(Count:LongInt);
begin
  Inc(Score, Count);
end;

procedure TGame.SaveHighscore;
var
  Xml : IXML;
  Stream : IStream;
  node: IXML;
  i, j : Integer;
  str : AnsiString;
  Table: array[0..10] of record
    Name: String;
    Count : LongInt;
  end;
begin
  Helpers.CreateXMLReader(xml, 'scores.xml');

  for i := 0 to 9 do
  begin
      node := xml.NodeI[i];
      Table[i].Name := Node.Tag;
      Table[i].Count := StrToInt(Node.Params['score'].Value);
  end;

  for i := 0 to 9 do
    if Table[i].Count <= Score then
    begin

      for j := 9 downto i do
      begin
        Table[j+1].Name := Table[j].Name;
        Table[j+1].Count := Table[j].Count;
      end;

      Table[i].Name := Name;
      Table[i].Count := Score;
      Break;
    end;
  node := nil;
  xml := nil;


  Helpers.CreateStream(Stream, 'scores.xml');
  Str := '<scores>' + #10#13;
  Stream.Write(Str[1], Length(Str) );

  for i := 0 to 9 do
  begin
    Str:= '<'+ Table[i].Name +' score="'+IntToStr(Table[i].Count)+'"/>' + #10#13;
    Stream.Write(Str[1], Length(Str) );
  end;

  Str:= '</scores>';
  Stream.Write(Str[1], Length(Str) );


end;

procedure TGame.Close;
begin

  SceneDirector.Free;
  Display  := nil;
  Input    := nil;
  Render   := nil;
  Render2d := nil;
  ResMan   := nil;
  Helpers  := nil;
end;

end.
