/*																				*/
/* 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 <appkit/Window.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 id	eggie = NULL;

static NXPoint	 maxPt = {0.0, 0.0};

- initAtPoint:(NXPoint*)start andSpeed:(float)moveSpeed foodType:(int)foodFlag 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"];
		
		eggie = [NXImage findImageNamed:"egg.1.tiff"];
		
		[NXApp getScreenSize:&screen];
		maxPt.x = screen.width;
		maxPt.y = screen.height;
	}
	
	owner = owningView;
	
	foodType = foodFlag;
	increment = YES;
	
	switch(foodType)
	{
		case T_TOASTER:
		{
			image = frames[0];
		}
		break;
	
		case T_TOAST:
		{
			image = toast;
		}
		break;
	
		case T_EGGIE:
		{
			image = eggie;
		}
		break;
	
	}
	
	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:ownerWindow;
{
	NXRect	newTarget;
	
	if(foodType == T_TOASTER)
	{
		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 < -64.0)
		newTarget.origin.x = maxPt.x;
	
	if(newTarget.origin.y < -64.0)
		newTarget.origin.y = maxPt.y;
		
	if(foodType == T_TOASTER)
		[owner verifyNewRect:&newTarget for:self];
		
	[self erase];	
	target = newTarget;
	[self display];
	
	[ownerWindow flushWindow];
				
	return self;
}

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

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

- (NXRect)targetRect
{
	return target;
}

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

- (float)speed;
{
	return speed;
}

- setSpeed:(float)newSpeed;
{
	speed = newSpeed;
	return self;
}
@end