unit Game;

interface

uses
  Classes, SysUtils, Scene, Graphics, Controls, Generics.Collections;

type
  TGameState = (gsStopped, gsRunning, gsPaused);
  TKeyPressedEvent = function (Key: Word): Boolean of object;

  { TGame }

  TGame = class
  private
    FOnKeyPressed: TKeyPressedEvent;
  public
    Name: string;
    Scene: TScene;
    State: TGameState;
    Time: TDateTime;
    procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); virtual;
    procedure KeyUp(var Key: Word; Shift: TShiftState); virtual;
    procedure KeyDown(var Key: Word; Shift: TShiftState); virtual;
    function KeyPressed(Key: Word): Boolean;
    procedure Run; virtual;
    procedure Stop; virtual;
    procedure Reset; virtual;
    procedure Tick; virtual;
    procedure Draw(Canvas: TCanvas); virtual;
    constructor Create; virtual;
    destructor Destroy; override;
    property OnKeyPressed: TKeyPressedEvent read FOnKeyPressed write FOnKeyPressed;
  end;

  TGames = class(TObjectList<TGame>)
  end;

const
  KeySpace = 32;
  KeyCodeLeft = 37;
  KeyCodeUp = 38;
  KeyCodeRight = 39;
  KeyCodeDown = 40;


implementation

{ TGame }

procedure TGame.MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer
  );
begin
end;

procedure TGame.KeyUp(var Key: Word; Shift: TShiftState);
begin
end;

procedure TGame.KeyDown(var Key: Word; Shift: TShiftState);
begin
end;

function TGame.KeyPressed(Key: Word): Boolean;
begin
  if Assigned(FOnKeyPressed) then Result := FOnKeyPressed(Key)
    else Result := False;
end;

procedure TGame.Run;
begin
  State := gsRunning;
  if Assigned(Scene) then Scene.Redraw;
end;

procedure TGame.Stop;
begin
  State := gsStopped;
  if Assigned(Scene) then Scene.Redraw;
end;

procedure TGame.Reset;
begin
  State := gsStopped;
  Time := 0;
end;

procedure TGame.Tick;
begin
end;

procedure TGame.Draw(Canvas: TCanvas);
begin
  if Assigned(Scene) then begin
    Scene.Canvas := Canvas;
    Scene.Draw;
  end;
end;

constructor TGame.Create;
begin
  Reset;
end;

destructor TGame.Destroy;
begin
  if Assigned(Scene) then FreeAndNil(Scene);
  inherited;
end;

end.

