
#import "NXIGameView.h"
#import "NXIGameBrain.h"
#import <libc.h>	// event stuff, misc.
#import <daymisckit/daymisckit.h>
#import "NXIBullet.h"
#import "NXISaucer.h"
#import "NXIAlien.h"
#import "NXIAlienOverseer.h"
#import "NXIPlayer.h"
//#import "NXIPlayerBullet.h"

// comment out the next line to disable the cheat modes.  Cheaters can't
// get net high scores, but they can get local high scores.
#define CHEATMODES YES

@implementation NXIGameView

- initFrame:(const NXRect *)frm    // designated initializer for a view
{    
	[super initFrame:frm];
	return self;
}

// Called by appDidInit to load up images, etc. that we need.
- loadPix
{
	[super loadPix];	
	return self;
}

- autoUpdate:sender
{			// ALL animation is controlled from here!!!
	BOOL updateFlag = NO;

    // keep track of how many time we've been called; we can use it do
	// decide when to do certain things...
	if ([NXApp isHidden]) return self; // don't suck cycles if we're hidden
	cycles++;
	
	if (state == GAMEOVER) {
	// when demowait counter hits WAITFORDEMO, we start up demo mode.
	// it basically restarts the game--but with "demoMode" turned on.
		if (demoWait++ == WAITFORDEMO) {
			state = NORMALSTATE;
			[self restartGameForDemo:YES];
			[controller unpause];
			[[self window] setTitle:"NXInvaders Demo"]; // ***** localize!
			[scoreKeeper resetScore];
	}	}
    if (state == NORMALSTATE) {
	}
	// draw all the changes, if applicable.
	if (updateFlag) [self update];
    [self updateSelf:&bounds :1];
    return self;
}

- drawSelf:(const NXRect *)rects :(int)rectCount	// redraws the screen.
{
	return self;
}

- updateSelf:(NXRect *)rects :(int)rectCount	// redraws the screen.
{
	return self;
}

// Handle player movement.  We respond to the arrow keys.  If we don't
// recognize the key, we pass it on.  This method just shunts the key
// off to the player object, which is what really deals with it. 
- keyDown:(NXEvent *)myevent
{
	unsigned short charCode;
	unsigned short charSet;
	
	if (!myevent) return self; // if no event when coalescing, go away
	PSobscurecursor();
	// check for arrow keys or space bar
	if (!(myevent->flags&(NX_CONTROLMASK|NX_ALTERNATEMASK|NX_COMMANDMASK))) {
		charCode = myevent->data.key.charCode;
		charSet = myevent->data.key.charSet;
		
		if (charSet == NX_SYMBOLSET) { // symbol set contains the arrows
			if		  (charCode == 0xAD) { // Up Arrow
				[player setDirection:GK_UP_DIRECTION];
				if (paused) [controller unpause];
				return self;
			} else if (charCode == 0xAF) { // Down Arrow
				[player setDirection:GK_DOWN_DIRECTION];
				if (paused) [controller unpause];
				return self;
			} else if (charCode == 0xAC) { // Left Arrow
				[player setDirection:GK_LEFT_DIRECTION];
				if (paused) [controller unpause];
				return self;
			} else if (charCode == 0xAE) { // Right Arrow
				[player setDirection:GK_RIGHT_DIRECTION];
				if (paused) [controller unpause];
				return self;
			}
		} else if (myevent->data.key.charCode == ' ') { // Space Bar
			[player setDirection:GK_NO_DIRECTION];
			if (paused) [controller unpause];
			return self;
		} else if ((myevent->data.key.charCode == 'a') && cheatMode) {
		} else {
			[super keyDown:myevent];
		}
	} else [super keyDown:myevent];
	[self keyDown:[NXApp peekAndGetNextEvent:NX_KEYDOWN]]; // coalesce keydowns
	// this is done recursively...primitive, but easy to implement :-)
    return self;
}

- setUpScreen
{	    
    [super setUpScreen];
    return self;
}

- restartGame
{
	return [self restartGameForDemo:NO];
}

- restartGameForDemo:(BOOL)doingDemo
{
    if (demoMode) {
		demoMode = NO;
		[[self window] setTitle:"NXInvaders"]; // ***** localize
    }
    [scoreKeeper resetScore];	// clear the score
    [scoreKeeper resetBonus:(-1)];	// clear all bonuses
    [self getPreferences];	// make sure we're up to date
	[player newPlayer];		// get a new base to play with
	if (doingDemo) demoMode = YES;
	[self update]; [self updateSelf:&bounds :1]; NXPing(); // show screen
	// start up the sound player object with the "start game" music
    return self;
}

@end
