
#import "NXIAlien.h"
#import "NXIAlienOverseer.h"

static int seriesFrames[3] = { ALIEN_FRAMES, ALIEN_FRAMES, ALIEN_FRAMES};

@implementation NXIAlien

- init
{
	NXPoint zeroPoint = { 0.0, 0.0 };
	return [self initAlienColor:0 at:&zeroPoint];
}

- initAlienColor:(int)col at:(NXPoint *)startPos 
{
	id ret = [super init];
	overseer = [NXIAlienOverseer new];	// find the overseer object
	NX_HEIGHT(&boundingBox) = ALIEN_WIDTH;
	NX_WIDTH(&boundingBox) = ALIEN_HEIGHT;
	renderedImage = [NXImage findImageNamed:"Invaders.tiff"];
	state = ALIEN_ALIVE;
	[self setMaxFrames:seriesFrames count:3];
	return ret;
}

- setStage:aStage
{
	id ret = [super setStage:aStage];
	NXRect bounds;
	[[aStage bufferType:GK_SCREEN_BUFFER] getBounds:&bounds];
	screenWidth = NX_WIDTH(&bounds);
	return ret;
}

- move:sender		// move one frame
{	// we let the overseer handle moving all the invaders since they all
	// move in concert.
	NXPoint whichWay;
	// find out where to go:
	[overseer movementVector:&whichWay];
	// move that direction:
	GK_ADD_VECTORS(&nextLocation, &whichWay);
	if (GK_NONZERO_VECTOR(&whichWay)) movedThisFrame = YES;
	else movedThisFrame = NO;
	return self;
}

- (BOOL)hitEdge:(int)anEdge
{
	if ((anEdge == LEFT_EDGE) &&
		(GK_location.x < 2 * ALIEN_HORIZONTAL_STEP_SIZE)) return YES;
	if ((anEdge == RIGHT_EDGE) && (GK_location.x >
		screenWidth - 2 * ALIEN_HORIZONTAL_STEP_SIZE)) return YES;
	return NO;
}

- leaveTheStage	// get the actor off the stage and into the "free" list
{
	[overseer alienLeavingTheStage:self];
	// ***** play a dead sound here?  or upon collision in the method below?
	return [super leaveTheStage];
}

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

- updateDrawingState	// change the internal state machine (ie. advance
{
	id ret;
	if ((state == ALIEN_ALIVE) && !movedThisFrame) return self;
	ret = [super updateDrawingState];
	// wrap frames early for living invader (there are only two frames)
	if ((state == ALIEN_ALIVE) && (frame >= ALIEN_WALK_FRAMES)) 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 == ALIEN_DYING) && !frame) { // wraparound occurred
		state = ALIEN_DEAD;
		[self leaveTheStage];
	}
	return ret;
}

@end
