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);
      procedure Update(dt: LongInt);
      procedure Draw;
  end;

implementation

uses
  Main,
  UIntro,
  UShopScene,
  UGameScene;

constructor TDirector.Create;
begin
  Helpers.CreateList(SceneList);
  SceneList.Add(TIntroScene.Create);
//  SceneList.Add(TShopScene.Create);
  SceneList.Add(TGameScene.Create);

  LoadScene('Game');
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;

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.
