unit uGMPObject;

{$mode delphi}

interface

uses
  zglHeader, uGMPLib;

type
  { TGMPObject }
  TGMPObject = class

    private
    DSize, DPos, D2Pos : TPos;

    public
    Rect : zglTRect;
    Pos, Size : ZPos;
    Enabled : Boolean;
    Color : Integer;
    Property DrawPos : TPos read DPos;
    Property DrawSize : TPos read DSize;

    procedure Draw; virtual;
    procedure Update; virtual;
    procedure drawUpdate;
    procedure Z2Pos;
    function RectColission(R : zglTRect) : Boolean;
    function LineColission(A: ZPos; var B : ZPos) : Boolean;
    {function PointColission(P : ZPos) : Boolean;
    function PointColissionXY(X, Y : Single) : Boolean;  }
  end;

implementation

{ uGMPObject }

procedure TGMPObject.Draw;
begin
  pr2d_Rect(DPos.X, DPos.Y, DSize.X, DSize.Y, Color, $FF, PR2D_FILL);
end;

procedure TGMPObject.Update;
begin
  DPos := Pos - Size / 2;
  DSize := Size;

  D2Pos := DPos + DSize;

  Rect.X := Pos.x - Size.x / 2;
  Rect.Y := Pos.y - Size.y / 2;
  Rect.W := Size.x;
  Rect.H := Size.y;

end;

procedure TGMPObject.drawUpdate;
begin
  DPos := Pos - Size / 2;
  DSize := Size;
end;

procedure TGMPObject.Z2Pos;
begin
  DPos.x := trunc(Pos.x - DSize.X div 2);
  DPos.y := trunc(Pos.y - DSize.Y div 2);
end;

function TGMPObject.RectColission(R: zglTRect): Boolean;
begin
  result := col2d_Rect(R, Rect);
end;

function TGMPObject.LineColission(A: ZPos; var B: ZPos): Boolean;
var
  vL : zglTLine;
  vP : zglTPoint2D;
begin
  vL.x0 := A.x;
  vL.y0 := A.y;
  vL.x1 := B.x;
  vL.y1 := B.y;

  if col2d_LineVsRect(vL, Rect, @vP) then
  begin
    B.SetValue(vP.X, vP.Y);
    Result := true;
  end
  else
    Result := false;
end;
     {
function TGMPObject.PointColission(P : ZPos): Boolean;
begin
  result := (P.x > DPos.x) and (P.y > DPos.y) and (P.x < D2Pos.x) and (P.y < D2Pos.y);
end;

function TGMPObject.PointColissionXY(X, Y: Single): Boolean;
begin
  result := (X > DPos.x) and (Y > DPos.y) and (X < D2Pos.x) and (Y < D2Pos.y);
end;  }

end.

