// GameView.m
// (C) Copyright 1992 Jim Patterson

#import <appkit/appkit.h>
#import <objc/objc.h>
#import "GameView.h"
#import "LessonPlan.h"
#import "ArcadeApp.h"
#import "explosion1.h"
#import "PerformanceEvaluator.h"

@implementation GameView:View



+ standardNewFrame
{
	NXRect aRect,viewFrame;
	float gray,strX,strY,strW,strH;
	int i;

	NXSetRect(&viewFrame, TA_GAMEVIEW_X,TA_GAMEVIEW_Y,
		TA_GAMEVIEW_W,TA_GAMEVIEW_H);

	self = [super newFrame:&viewFrame];
	// create cacheWindow
	cacheWindow = [Window newContent:&viewFrame
			style:NX_PLAINSTYLE
			backing:NX_RETAINED
			buttonMask:0
			defer:NO];
			
	// setup rectangle for erasing characters
	NXSetRect(&clearingRect,0,0,TA_MAX_LETTER_WIDTH,TA_LINE_HEIGHT);


	[[cacheWindow contentView] lockFocus];	

	// ADD BACKGROUND TO PART WITH TEXT
	PSsetgray(NX_BLACK);
	NXSetRect(&aRect,TA_GAMEVIEW_X, TA_GAMEVIEW_Y + TA_GAMEVIEW_HEADER_H,
		TA_GAMEVIEW_W, TA_GAMEVIEW_TEXTPART_H);
	NXRectFill(&aRect);

	// ADD HEADER
	for (i=0; i< TA_GAMEVIEW_HEADER_H; i++) {
		gray =  (1 - ((float) i) / ((float) TA_GAMEVIEW_HEADER_H)) * NX_LTGRAY;
		PSsetgray(gray);
		PSrectstroke(TA_GAMEVIEW_X,
				TA_GAMEVIEW_Y+i,
				TA_GAMEVIEW_W,1);
		}		
		
	// SET TRAILER BACKGROUND
	PSsetgray(NX_LTGRAY);
	NXSetRect(&aRect,
		TA_GAMEVIEW_X,
		TA_GAMEVIEW_Y + TA_GAMEVIEW_TEXTPART_H +
			 TA_GAMEVIEW_FADEPART_H + TA_GAMEVIEW_HEADER_H,
		TA_GAMEVIEW_W,
		TA_GAMEVIEW_TRAILER_H);
	NXRectFill(&aRect);

	// ADD FADING PART TO TRAILER
	for (i=0; i< TA_GAMEVIEW_FADEPART_H; i++) {
		gray =  ((float) i) / ((float) TA_GAMEVIEW_FADEPART_H) * NX_LTGRAY;
		PSsetgray(gray);
		PSrectstroke(TA_GAMEVIEW_X,
				TA_GAMEVIEW_Y+ TA_GAMEVIEW_HEADER_H + TA_GAMEVIEW_TEXTPART_H+i,
				TA_GAMEVIEW_W,1);
		}		

	// ADD GAME-OVER BOX TO TRAILER
	PSsetgray(0.5);
	NXSetRect(&aRect,
		TA_GAMEVIEW_X+TA_GAMEVIEW_TRAILER_XMARGIN,
		TA_GAMEVIEW_Y+TA_GAMEVIEW_TRAILER_YMARGIN + TA_GAMEVIEW_HEADER_H +
		 	TA_GAMEVIEW_TEXTPART_H + TA_GAMEVIEW_FADEPART_H,
		TA_GAMEVIEW_TRAILER_BOX_W,
		TA_GAMEVIEW_TRAILER_BOX_H);
	NXDrawGroove(&aRect,&aRect);
	PSstringwidth("Game Over",&strW,&strH);
	strX = aRect.origin.x + (aRect.size.width - strW)/2;
	strY = aRect.origin.y + (aRect.size.height - strH)/2;
	PSsetgray(NX_BLACK);
	PSmoveto(strX,strY);
	PSscale(1,-1);
	PSshow("GameOver");	

	// add letters
	PSsetgray(NX_WHITE);
	[self fillWithLetters];
	lowestNonemptyRow = 0;

	[[cacheWindow contentView] unlockFocus];

	return self;
}


- drawSelf:(NXRect *) tR: (int) count
{
	int i;
	// Assumes:   (1) Count is 1, which will always be true as it is used in TypingArcade
	//                      since we are only scrolling in one dimension.
	//                      (2) We are drawing (i.e. not printing, etc.).  This is reasonable
	//                      since we will never be printing this object  
	PScomposite(tR->origin.x, tR->origin.y,tR->size.width,tR->size.height,
		[cacheWindow gState],
		tR->origin.x,tR->origin.y,NX_COPY);

	return self;
}

- free
{
	[cacheWindow free];
	return self;
}


- fillWithLetters;
{
	int lineNum,i;
	float xLoc,yLoc;
	char str[2];

	PSscale(1,-1);

	// load font information
	gameFont = [Font newFont:"Times-Roman" size:18];
	if (gameFont == nil) printf("ERROR: could not load font.\n");
		else [gameFont set];

	[[NXApp performanceEvaluator] reset];	// clear all statistics
	[[NXApp lessonPlan] compactKeyList];
	
	for (lineNum=0; lineNum<TA_NUM_GAMEVIEW_LINES; lineNum++) {
		xLoc = [[NXApp lessonPlan] randomBetween:TA_SIDE_MARGIN
				:TA_GAMEVIEW_W - TA_SIDE_MARGIN];
		yLoc = TA_GAMEVIEW_Y + TA_GAMEVIEW_HEADER_H + lineNum*TA_LINE_HEIGHT;
		gameLetters[lineNum] = [[NXApp lessonPlan] chooseChar];
		[[NXApp performanceEvaluator] logOccurrence:gameLetters[lineNum]];
		gameLetterXLocs[lineNum] = xLoc;
		str[0] = gameLetters[lineNum]; str[1] = '\0';
		PSmoveto(xLoc,yLoc);
		PSscale(1,-1);
		PSsetgray(NX_WHITE);
		PSshow(str);
		PSscale(1,-1);
		if (gameLetters[lineNum] == '0') {
			PSmoveto(xLoc+1,yLoc);
			PSscale(1,-1);
			PSsetgray(NX_WHITE);
			PSshow("/");
			PSscale(1,-1);
			}
		}
}

- lowestLetterWasHit:(int) gProgress
{
	NXRect aRect;

	[self lockFocus];
	clearingRect.origin.x = gameLetterXLocs[lowestNonemptyRow] - 8;
	clearingRect.origin.y = TA_GAMEVIEW_Y + TA_GAMEVIEW_HEADER_H - 4 +
		lowestNonemptyRow * TA_LINE_HEIGHT;

	PSsetgray(NX_BLACK);
	NXRectFill(&clearingRect);
		 lowestNonemptyRow*TA_LINE_HEIGHT;
	PSsetgray(NX_WHITE);
	PSWexplosion(clearingRect.origin.x + 0.5 *TA_MAX_LETTER_WIDTH,
		clearingRect.origin.y + 0.5*TA_LINE_HEIGHT);

	[[cacheWindow contentView] lockFocus];
	PSsetgray(NX_BLACK);
	NXRectFill(&clearingRect);
		 lowestNonemptyRow*TA_LINE_HEIGHT;	
	[[cacheWindow contentView] unlockFocus];

	PSsetgray(NX_BLACK);
	PSWexplosion(clearingRect.origin.x + 0.5 *TA_MAX_LETTER_WIDTH,
		clearingRect.origin.y + 0.5*TA_LINE_HEIGHT);

	[self unlockFocus];

	lowestNonemptyRow++;
	return self;
}


- (char) lowestLetter { return gameLetters[lowestNonemptyRow];}
- (int) lowestNonemptyRow {return lowestNonemptyRow; }

	
@end
