unit UDirector;

interface

uses
  JEN_Header,
  JEN_Math;

type
  TScene = class
    function GetName: String; virtual; abstract;
    procedure Load; virtual; abstract;
    procedure UnLoad; virtual; abstract;
    procedure Update(dt: LongInt); virtual; abstract;
    procedure Draw; virtual; abstract;

    property Name :string read GetName;
  end;

  TDirector = class
    constructor Create;
    destructor Destroy; override;
    var
      CurrectScene: TScene;
      NextScene   : TScene;
      SceneList: IList;
    public
      procedure LoadScene(SceneName: String);
      function AddScene(Scene: TScene): TScene;
      procedure Update(dt: LongInt);
      procedure Draw;
  end;

implementation

uses
  Main,
  UIntro,
  UShopScene,
  UMainMenu,
  UGameScene;

constructor TDirector.Create;
begin
  Helpers.CreateList(SceneList);
end;

destructor TDirector.Destroy;
var
  i : Integer;
begin
  if Assigned(CurrectScene) then
    CurrectScene.UnLoad;

  for I := 0 to SceneList.Count - 1 do
    TScene(SceneList[i]).Free;

end;

function TDirector.AddScene(Scene: TScene): TScene;
begin
  Result := SceneList.Add(Scene);
end;

procedure TDirector.LoadScene(SceneName: String);
var
  i : Integer;
begin
  for I := 0 to SceneList.Count - 1 do
    if (TScene(SceneList[i]).Name = SceneName) then
    begin
      NextScene := TScene(SceneList[i]);
      Exit;
    end;

  Engine.Log('Register scene first');
end;

procedure TDirector.Update;
begin
  if Assigned(NextScene) then
  begin
    if Assigned(CurrectScene) then
      CurrectScene.UnLoad;

    CurrectScene := NextScene;
    CurrectScene.Load;
    NextScene := nil;
  end;

  if Assigned(CurrectScene) then
    CurrectScene.Update(dt);
end;

procedure TDirector.Draw;
begin
  if Assigned(CurrectScene) then
    CurrectScene.Draw;
end;

end.
