
// Handles moving and rendering the base, whether under player or demo control.

#import <libc.h>
#import "NXIPlayer.h"

@implementation NXIPlayer

- init						// initialize the player
{
	[super init];
	playerStopped = NO;
	renderedImage = [NXImage findImageNamed:"Base.tiff"];
	NX_HEIGHT(&boundingBox) = BASE_WIDTH;
	NX_WIDTH(&boundingBox) = BASE_HEIGHT;
	maxFrames[0] = BASE_FRAMES; xdirection = 0.0;
	// ***** need to set up keys for movement!
	return self;
}

- (BOOL)newPlayer			// get and set up a new base.  Returns NO if can't
{
	if (![basesLeft decNumUp:self]) return NO;		// no bases left
	[self resetPlayer];
	return YES;
}

- resetPlayer				// reset all player info
{
	state = BASE_ALIVE;
	xdirection = 0.0;
	GK_SET_VECTOR(&nextLocation, 0.0, 0.0); // start in lower left corner
	return self;
}

- setStage:aStage
{
	id ret = [super setStage:aStage];
	NXRect bounds;
	[[aStage bufferType:GK_SCREEN_BUFFER] getBounds:&bounds];
	// figure out how far to the right we can go
	screenWidth = NX_WIDTH(&bounds) - BASE_WIDTH - BASE_STEPS;
	return ret;
}

- demoMove:sender	// handles base movement during the demo sequence
{
	return self;
}

- move:sender				// Move the base one animation frame
{
	GK_VECTOR_X(&nextLocation) += xdirection;
	GK_VECTOR_X(&nextLocation) = GK_CLAMP(GK_VECTOR_X(&nextLocation),
			BASE_STEPS, (screenWidth - BASE_WIDTH - BASE_STEPS));
	return self;
}

- collidedWith:anActor	// called when it is detected that we hit something
{	// we need to see if it was a vader's bullet that hit us (we ignore
	// anything else...) and then act accordingly.
	// *****
	return self;
}

- keyChanged:(NXEvent *)myevent :(int)kind
{	// gameview will tell us one of these if a key was hit (or released)
	if (kind == GK_ALL_KEYUP) {
		xdirection = 0.0;
		return self;
	}
	if ((kind == GK_KEYUP) &&
			(GK_KEYS_ARE_SAME(myevent, &keys[NXI_LEFTKEY]) ||
			 GK_KEYS_ARE_SAME(myevent, &keys[NXI_RIGHTKEY]))) {
		xdirection = 0.0;
	} else if (kind == GK_KEYDOWN) {
		if (GK_KEYS_ARE_SAME(myevent, &keys[NXI_LEFTKEY]))
			xdirection = -BASE_STEPS;
		if (GK_KEYS_ARE_SAME(myevent, &keys[NXI_RIGHTKEY]))
			xdirection = BASE_STEPS;
		if (GK_KEYS_ARE_SAME(myevent, &keys[NXI_FIREKEY])) {
			// ***** fire a shot
		}
	}
	return self;
}

- updateDrawingState	// change the internal state machine (ie. advance
{
	id ret;
	ret = [super updateDrawingState];
	// wrap frames early for living base (there is only one frame)
	if (state == BASE_ALIVE) frame = 0;
	// if we've finished stepping through the death frames, then we need
	// to get off the stage since the death is complete.
	if ((state == BASE_DYING) && !frame) { // wraparound occurred
		state = BASE_DEAD;
		// ***** get a new base and end game if cannot
	}
	return ret;
}

- setDirection:(int)newDirection;	// sent by GameView at right time
{	// ***** not yet done
	return self;
}

@end
