Palladion Software
user icon Guest

Created by zope. Last modified 2004-07-15 05:38:33.

File Properties

Filename GoofFish.pas
Size 3924
Content-type text/plain

Download File

Download

//==============================================================================
// Unit:       GoofFish
//
// Purpose:    Declare TGoofFish, a concrete fish which animates its swimming
//			   by cycling through a set of bitmaps.
//
// Copyright:  1997, Palladion Software
//==============================================================================
unit GoofFish;

interface

uses
	SysUtils,
    Classes,
    Graphics,
    Fish;

type

  TGoofFish = class( TFish )

  private

  	FCycle				: integer;
    FOffset				: integer;
    FHorizontalSpeed	: integer;
    FVerticalSpeed		: integer;

  protected

  	// Implement inherited abstract methods.
  	function GetCommonName : string;						override;
  	function GetScientificName : string;					override;
    function GetWidth : integer;							override;
    function GetHeight : integer;							override;
    function GetBitmap : TBitmap;							override;
    function GetAsString : string;							override;

  public

    constructor Create;

    procedure Swim( var X : integer; var Y : integer;
    				maxX, maxY : integer );					override;

    function Clone : TFish;									override;

  end;                                                               

  function	GoofFish_Builder( aPersistentState : string ) : TFish;

implementation

uses
	Controls;

var
	TheImageList : array[ 'a'..'b', 0..2 ] of TBitmap;


constructor TGoofFish.Create;
begin
    FCycle := 0;
    FHorizontalSpeed := Random( 11 ) - 5;
    FVerticalSpeed := Random( 5 ) - 2;

    if FHorizontalSpeed > 0 then
    	FOffset := 1
    else
    	FOffset := 0;
end;

function TGoofFish.GetCommonName : string;
begin
	Result := 'Gooffish';
end;

function TGoofFish.GetScientificName : string;
begin
	Result := 'Goofus fubarii';
end;

function TGoofFish.GetWidth : integer;
begin
	Result := 50;
end;

function TGoofFish.GetHeight : integer;
begin
	Result := 50;
end;

function TGoofFish.GetBitmap : TBitmap;

var
	iBmp	: integer;
	bmp		: TBitMap;
    cOffset	: char;

begin
	cOffset := Chr( Ord( 'a' ) + FOffset );
    Result := TheImageList[ cOffset, FCycle ];
end;


function TGoofFish.GetAsString : string;
begin
	Result := IntToStr( FHorizontalSpeed ) + ','
            + IntToStr( FVerticalSpeed );
end;



function GoofFish_Builder( aPersistentState : string ) : TFish;

var
	comma		: integer;
    goofFish	: TGoofFish;

begin
	comma := Pos( ',', aPersistentState );

    if ( comma = 0 ) then
    	raise EFishError.Create( 'Invalid GoofFish entry.' );

    goofFish := TGoofFish.Create;

    with goofFish do
    begin
    	FHorizontalSpeed := StrToInt( Copy( aPersistentState, 1, comma-1 ) );
    	FVerticalSpeed := StrToInt( Copy( aPersistentState, comma+1, 999 ) );
    end;

    Result := goofFish;
end;

procedure TGoofFish.Swim( var X : integer; var Y : integer;
                			maxX, maxY : integer );
begin
    FCycle := ( FCycle + 1 ) mod 3;

	X := X + FHorizontalSpeed;

    if ( X <= 0 ) or ( X + Width >= maxX ) or ( Random( 100 ) < 5 ) then
    begin
    	FHorizontalSpeed := - FHorizontalSpeed;
        FOffset := 1 - FOffset;
    end;

    Y := Y + FVerticalSpeed;

    if ( Y <= 0 ) or ( Y + Height >= maxY ) or ( Random( 100 ) < 5 ) then
    	FVerticalSpeed := - FVerticalSpeed;
end;

function TGoofFish.Clone : TFish;
begin
	Result := TGoofFish.Create;
end;


procedure Setup;

const
	fileNameFmt = 'goof_0%d%s.bmp';

var
	cImage		: char;
	iImage		: integer;
    fileName	: string;
    bmp			: TBitmap;

begin
	for cImage := 'a' to 'b' do
    begin
        for iImage := 1 to 3 do
        begin
            fileName := Format( fileNameFmt, [ iImage, cImage ] );
            bmp := TBitmap.Create;
            bmp.LoadFromFile( fileName );
        	TheImageList[ cImage, iImage-1 ] := bmp;
        end;
    end;
end;

procedure Cleanup;
begin

end;

initialization

	Setup;
    TFish.RegisterBuilder( TGoofFish.ClassName, @GoofFish_Builder );

finalization

	Cleanup;

end.