unit uStar;

interface

uses  uSMath,
      QuadEngine;

type
  pStar = ^rStar;
  rStar = packed record
      Pos   : rVec2f;
      StarSize  : Single;
      Frame     : Word;
    private
      prev  : pStar;
      next  : pStar;
    public
      procedure AddToTail( p: rVec2f; fr: Word; sz: Single; var head, tail : pStar );
      procedure Delete( var head, tail : pStar );

      procedure Update( speed: rVec2f; dt: Double );
      procedure FrameUpdate( maxFrame: Word );

      procedure Draw( sprite: IQuadTexture ); overload;
      procedure Draw( sprite: IQuadTexture; rect: rRect ); overload;

      procedure Shift( dpos: rVec2f );

      property PrevStar : pStar read prev;
      property NextStar : pStar read next;
  end;

implementation

procedure rStar.AddToTail(p: rVec2f; fr: Word; sz: Single; var head: pStar; var tail: pStar);
var buf : pStar;
begin
  buf := New( pStar );

  buf.Pos := p;
  buf.Frame := fr;
  buf.StarSize := sz;

  buf.prev := nil;
  buf.next := nil;
  if head = nil then begin
    head := buf;
    tail := buf;
  end else begin
    buf.prev  := tail;
    tail.next := buf;
    tail      := buf;
  end;
end;

procedure rStar.Delete(var head: pStar; var tail: pStar);
begin
  if Self.prev <> nil then Self.prev.next := Self.next else head := Self.next;
  if Self.next <> nil then Self.next.prev := Self.prev else tail := Self.prev;
  Dispose( @Self );
end;

procedure rStar.Update( speed: rVec2f; dt: Double );
begin
  Pos := Pos + speed * dt;
end;

procedure rStar.FrameUpdate( maxFrame: Word );
begin
  Inc( Frame );
  if Frame > maxFrame then Frame := 0;
end;

procedure rStar.Draw( sprite: IQuadTexture );
begin
  sprite.DrawRot( Pos.X, Pos.Y, 0, StarSize, $FFFFFFFF, Frame );
end;

procedure rStar.Draw( sprite: IQuadTexture; rect: rRect );
begin
  if ( Pos.X >= rect.X ) and ( Pos.X <= rect.X + rect.Width ) and
     ( Pos.Y >= rect.Y ) and ( Pos.Y <= rect.Y + rect.Height ) then
  begin
    sprite.DrawRot( Pos.X, Pos.Y, 0, 1.0, $FFFFFFFF, Frame );
  end;
end;

procedure rStar.Shift(dpos: rVec2f);
begin
  Pos := Pos + dpos;
end;

end.
