#import "iTennisViewController.h"
#define kCompMoveSpeed 15
#define kGameStateRunning 1
#define kGameStatePaused 2
#define kBallSpeedX 10
#define kBallSpeedY 10
@implementation iTennisViewController
@synthesize ball,racquet_yellow,racquet_green,player_score,computer_score,gameState,ballVelocity,tapToBegin;
/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
// Custom initialization
}
return self;
}
*/
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
self.gameState = kGameStatePaused;
ballVelocity = CGPointMake(kBallSpeedX,kBallSpeedY);
[NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(gameLoop) userInfo:nil repeats:YES];
}
-(void) gameLoop {
if(gameState == kGameStateRunning) {
ball.center = CGPointMake(ball.center.x + ballVelocity.x, ball.center.y + ballVelocity.y);
if(ball.center.x > self.view.bounds.size.width || ball.center.x < 0) {
ballVelocity.x = -ballVelocity.x;
}
if(ball.center.y > self.view.bounds.size.width || ball.center.y < 0) {
ballVelocity.y = -ballVelocity.y;
}
} else {
if (tapToBegin.hidden) {
tapToBegin.hidden = NO;
}
}
if(CGRectIntersectsRect(ball.frame,racquet_yellow.frame)) {
if(ball.center.y <racquet_yellow.center.y) {
ballVelocity.y = -ballVelocity.y;
NSLog(@"%f %f",ball.center, racquet_green.center);
}
}
if(CGRectIntersectsRect(ball.frame,racquet_green.frame)) {
if(ball.center.y >racquet_green.center.y) {
ballVelocity.y = -ballVelocity.y;
}
}
//Begin Simple AI
if(ball.center.y <= self.view.center.y) {
if(ball.center.x < racquet_green.center.x) {
CGPoint compLocation = CGPointMake(racquet_green.center.x - kCompMoveSpeed, racquet_green.center.y);
racquet_green.center = compLocation;
}
if(ball.center.x > racquet_green.center.x) {
CGPoint compLocation = CGPointMake(racquet_green.center.x + kCompMoveSpeed, racquet_green.center.y);
racquet_green.center = compLocation;
}
}
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if(gameState == kGameStatePaused) {
tapToBegin.hidden = YES;
gameState = kGameStateRunning;
} else if(gameState == kGameStateRunning){
[self touchesMoved:touches withEvent:event];
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
CGPoint xLocation = CGPointMake(location.x,racquet_yellow.center.y);
racquet_yellow.center = xLocation;
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
[ball release];
[racquet_green release];
[racquet_yellow release];
[player_score release];
[computer_score release];
[tapToBegin release];
}
@end