/*																										*/
/* Copyright (c) 1991																			*/
/* Pages / KVM  Banzai Research Inst.														*/
/* 3914 Murphy Cyn Rd.																			*/
/* San Diego, CA 92123																			*/
/* (619) 492-9050																					*/
/*																										*/

/* RCS History Section-------------------------------------------------------	*/

#ifdef RCSID
static char _rcsid[] = "$Header$";
#endif

/*
 * $Log$
 */


/* RCS History Section-------------------------------------------------------	*/


/* Description --------------------------------------------------------------	*/
/*  																									*/
/*		This object is the programatic representation of a toaster (or piece		*/
/*		of toast). This is where most of the work for toasters gets done.			*/
/*  																									*/
/*  	Nov 14, 1991: Bruce Henderson (bruce@pages.com)									*/
/*  																									*/
/* Description --------------------------------------------------------------	*/

#import "FlyingImage.h"
#import "ToastersViewClass.h"
#import <appkit/NXImage.h>
#import <appkit/Application.h>
#import <dpsclient/dpsNeXT.h>
#import <dpsclient/wraps.h>
#import <libc.h>
#import <math.h>

@implementation FlyingImage

static id	frames[4] = {NULL,NULL,NULL,NULL};
static id	toast = NULL;

static NXPoint	 maxPt = {0.0, 0.0};

- initAtPoint:(NXPoint*)start andSpeed:(float)moveSpeed isToast:(BOOL)toastFlag for:owningView;
{
	NXSize	screen;
	
	if(!frames[0])
	{
		frames[0] = [NXImage findImageNamed:"toaster1.tiff"];
		frames[1] = [NXImage findImageNamed:"toaster2.tiff"];
		frames[2] = [NXImage findImageNamed:"toaster3.tiff"];
		frames[3] = [NXImage findImageNamed:"toaster4.tiff"];
				
		toast = [NXImage findImageNamed:"toast.tiff"];
		
		[NXApp getScreenSize:&screen];
		maxPt.x = screen.width;
		maxPt.y = screen.height;
	}
	
	owner = owningView;
	
	isToast = toastFlag;
	increment = YES;
	
	if(isToast)
	{
		isToast = YES;
		image = toast;
	}
	else
		{
			image = frames[0];
		}
	
	frame = MIN_FRAME;
	speed = moveSpeed;
	NXSetRect(&target,0.0, 0.0, 64.0, 64.0);
	target.origin = *start;

	return self;
}	

- erase
{
	
	PSsetgray(NX_BLACK);
	NXRectFill(&target);

	return self;
}

- move;
{
	NXRect	newTarget;
	
	if(!isToast)
	{
		if(increment)
		{
			frame++;
			if(frame > MAX_FRAME)
				frame = MAX_FRAME;
			if(frame == MAX_FRAME)
				increment = NO;
			image = frames[frame];
		}
		else
		{
			frame--;
			if(frame < MIN_FRAME)
				frame = MIN_FRAME;
			if(frame == MIN_FRAME)
				increment = YES;
			image = frames[frame];
		}
	}
	
	newTarget = target;
	newTarget.origin.x -= speed;
	newTarget.origin.y -= speed;
	
	if(newTarget.origin.x < 0.0)
		newTarget.origin.x = maxPt.x;
	
	if(newTarget.origin.y < 0.0)
		newTarget.origin.y = maxPt.y;
		
	if(!isToast)
		[owner verifyNewRect:&newTarget for:self];
		
	[self erase];	
	target = newTarget;
	[self display];
				
	return self;
}

- display
{
	[image composite:NX_COPY toPoint:&(target.origin)];
	return self;
}

- setPostition:(NXPoint)startPoint;
{
	target.origin = startPoint;
	return self;
}

- (NXRect)targetRect
{
	return target;
}

- setFrame:(int)startFrame;
{
	frame = startFrame;
	return self;
}

- (float)speed;
{
	return speed;
}

- setSpeed:(float)newSpeed;
{
	speed = newSpeed;
	//printf("\nsetting speed %f\n", newSpeed);fflush(stdout);
	return self;
}
@end