//
//  Ship.m
//  StarFighter
//
//  Created by Lynden Rosier on 8/30/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "MovableObject.h"


@implementation MovableObject

- (id) init
{
	return [self initWithX: DEFAULT_X withY: DEFAULT_Y];
}

- (id) initWithX: (float) x  withY: (float) y
{
	return [self initWithX:x withY:y withWidth:DEFAULT_WIDTH withHeight:DEFAULT_HEIGHT];
}

- (id) initWithX: (float) x  withY: (float) y   withWidth: (float) width  withHeight: (float) height
{
	return [self initWithX:x withY:y withWidth:width withHeight:height withSpeed:3];
}

- (id) initWithX: (float) x  withY: (float) y   withWidth: (float) width  withHeight: (float) height withSpeed: (int) spd
{
	id result = [super initWithFrame:CGRectMake(x,y,width,height)];
	// do any initializing here
	pos.x = x;
	pos.y = y;
	size.width = width;
	size.height = height;
	speed = spd;
	time = 0;
	touchedStartPos.x = -1;
	touchedStartPos.y = -1;
	touchedEndPos.x = -1;
	touchedEndPos.y = -1;
	touchedState = -1;  // no touch = -1, touch down or dragged = 0; touch up = 1
	return result;
}

- (void) setWithX: (float) x  withY: (float) y   withWidth: (float) width  withHeight: (float) height withSpeed: (int) spd
{
	// do any initializing here
	pos.x = x;
	pos.y = y;
	size.width = width;
	size.height = height;
	speed = spd;
	time = 0;
	touchedStartPos.x = -1;
	touchedStartPos.y = -1;
	touchedEndPos.x = -1;
	touchedEndPos.y = -1;
	touchedState = -1;  // no touch = -1, touch down or dragged = 0; touch up = 1;
	[self setFrame:CGRectMake([self getX],[self getY],[self getWidth],[self getHeight])];
}


// get and set methods
- (void) setX: (float) x
{
	pos.x = x;
	[self setFrame:CGRectMake([self getX],[self getY],[self getWidth],[self getHeight])];
}

- (float) getX
{
	return pos.x;
}

- (void) setY: (float) y
{
	pos.y = y;
	[self setFrame:CGRectMake([self getX],[self getY],[self getWidth],[self getHeight])];
}

- (float) getY
{
	return pos.y;
}

- (void) setWidth: (float) width
{
	size.width = width;
	[self setFrame:CGRectMake([self getX],[self getY],[self getWidth],[self getHeight])];
}

- (float) getWidth
{
	return size.width;
	[self setFrame:CGRectMake([self getX],[self getY],[self getWidth],[self getHeight])];
}

- (void) setHeight: (float) height
{
	size.height = height;
	[self setFrame:CGRectMake([self getX],[self getY],[self getWidth],[self getHeight])];
}

- (float) getHeight
{
	return size.height;
}

- (void) setSpeed: (float) spd
{
	speed = spd;
}

- (float)  getSpeed
{
	return speed;
}

- (void) setSpeedX: (float) spd
{
	speedX = spd;
}

- (float)  getSpeedX
{
	return speedX;
}

- (void) setSpeedY: (float) spd
{
	speedY = spd;
}

- (float)  getSpeedY
{
	return speedY;
}


- (void) setTime: (float) t
{
	time = t;
}

- (float)  getTime
{
	return time;
}

- (CGRect) getRect
{
	return CGRectMake([self getX], [self getY], [self getWidth], [self getHeight]);
}

-(int) getDirection
{
	return direction;
}

-(void) setDirection:(int) dir
{
	direction = dir;
}

// move methods
- (void) moveUp
{
	pos.y = pos.y - speed;
	[self setFrame:CGRectMake([self getX],[self getY],[self getWidth],[self getHeight])];
}

- (void) moveDown
{
	pos.y = pos.y + speed;
	[self setFrame:CGRectMake([self getX],[self getY],[self getWidth],[self getHeight])];
}

- (void) moveLeft
{
	pos.x = pos.x - speed;
	[self setFrame:CGRectMake([self getX],[self getY],[self getWidth],[self getHeight])];
}

- (void) moveRight
{
	pos.x = pos.x +speed;
	[self setFrame:CGRectMake([self getX],[self getY],[self getWidth],[self getHeight])];
}

- (void) print 
{
    // notice that point.x and point.y are stored as floats!!!!
    printf("Object is located at (%f,%f)\n",[self getX],pos.y);
}


-(BOOL) intersects:(MovableObject *) other
{
	if (CGRectIntersectsRect([self getRect],[other getRect]))
		return YES;
	return NO;
}


-(int) getTouchedState
{
	return touchedState;
}

-(void) setTouchedState:(int) state
{
	touchedState = state;
}


// don't forget to call [ship setUserInteractionEnabled:YES];

// UITouch objects
-(void) touchesBegan:(NSSet*) touches withEvent:(UIEvent*) event
{
	CGPoint pt = [[touches anyObject] locationInView:self];
	touchedState = 0; 
	touchedStartPos = pt;
	//[[self superview] bringSubviewToFront:self];
}

-(void) touchesMoved:(NSSet*) touches withEvent:(UIEvent*) event
{
	CGPoint pt = [[touches anyObject] locationInView:self];
	touchedEndPos = pt;
	//[[self superview] bringSubviewToFront:self];
	if (touchedEndPos.x + 2 < touchedStartPos.x)
	{ // move to the left
		float moveByAmount = touchedStartPos.x - touchedEndPos.x;
		[self setX:[self getX]-moveByAmount];
	}
	else if (touchedEndPos.x  >  touchedStartPos.x + 2)
	{ // move to the right
		float moveByAmount = touchedEndPos.x - touchedStartPos.x;
		[self setX:[self getX]+moveByAmount];
	}
	touchedState = 0; 
}

-(void) touchesEnded:(NSSet*) touches withEvent:(UIEvent*) event
{
	CGPoint pt = [[touches anyObject] locationInView:self];
	touchedEndPos = pt;
	//[[self superview] bringSubviewToFront:self];
	if (touchedEndPos.x + 2 < touchedStartPos.x)
	{ // move to the left
		float moveByAmount = touchedStartPos.x - touchedEndPos.x;
		[self setX:[self getX]-moveByAmount];
	}
	else if (touchedEndPos.x  >  touchedStartPos.x + 2)
	{ // move to the right
		float moveByAmount = touchedEndPos.x - touchedStartPos.x;
		[self setX:[self getX]+moveByAmount];
	}
	
	// fire a bullet
	touchedState = 1; 
}


@end












