Source

iPhone / solarwolf / Classes / solarwolfViewController.m

The default branch has multiple heads

//
//  solarwolfViewController.m
//  solarwolf
//
//  Created by Tau Bernstorff Lehmann on 21/07/10.
//  Copyright __MyCompanyName__ 2010. All rights reserved.
//

#import "solarwolfViewController.h"

@implementation solarwolfViewController




- (void)viewDidLoad {
	myView=[[UIView alloc] initWithFrame:[[self view] bounds]];
	[myView setBackgroundColor:[UIColor whiteColor]];
	[[self view] addSubview:myView];
	shipView=[[UIView alloc] initWithFrame:[[self view] bounds]];
	[myView addSubview:shipView];
	[self setUpLevel];	
	platformArray=[[NSMutableArray alloc]init];
	fireballArray=[[NSMutableArray alloc]init];
	
	myShip =[[Ship alloc]initWithPos:CGPointMake (3,5)];
	UIColor *color = [[UIColor alloc] initWithRed:0.0 green:0.0 blue:0.0 alpha:0.0];
	
	[shipView setBackgroundColor: color];
	
	platformLeft=[[Platforms alloc]initWithPos:CGPointMake(0,0):@"Down":@"Left"];
	platformTop=[[Platforms alloc]initWithPos:CGPointMake(0,0):@"Right":@"Top"];
	platformBottom=[[Platforms alloc]initWithPos:CGPointMake(0,17):@"Right":@"Bottom"];
	platformRight=[[Platforms alloc]initWithPos:CGPointMake(11,17):@"Up":@"Right"];
	
	[myView addSubview:[platformLeft drawSpirint]];
	
	[platformArray addObject:platformLeft];
	[platformArray addObject:platformRight];
	[platformArray addObject:platformTop];
	[platformArray addObject:platformBottom];
	
	NSEnumerator *e=[platformArray objectEnumerator];
	id object;
	while (object=[e nextObject]) {
	UIView * platformView=[object drawSpirint];
	[myView addSubview:platformView];
	}
	
	
	[myView addSubview:[myShip drawSpirint]];
	[NSTimer scheduledTimerWithTimeInterval: 0.1
									 target: self
								   selector: @selector(gameLoop)
								   userInfo: nil
									repeats: YES];
	[NSTimer scheduledTimerWithTimeInterval: 0.5
									 target: self
								   selector: @selector(fireMoveLoop)
								   userInfo: nil
									repeats: YES];
	[ship release];
	[color release];
	[shipView release];

    [super viewDidLoad];
}

-(void)gameLoop{
	NSEnumerator* e=[fireballArray objectEnumerator];
	id object;
	while(object=[e nextObject]){		
		[self uDead:[object position]];
	}
}
	

-(void)fireMoveLoop{
	NSEnumerator* e=[fireballArray objectEnumerator];
	id object;
	while(object=[e nextObject]){
		
		if(![object objectWillLeave]){
		[object move];
		}
		else{
			[[object view] removeFromSuperview];
			
		}
	}
	NSEnumerator *j=[platformArray objectEnumerator];
	id platform;
	while (platform=[j nextObject]) {
		[platform move];
		int value=arc4random() % 10;
		if(value==0){
		Spirint* fireball=[platform launchFireball];
		UIView* fireballView = [fireball drawSpirint];
		[myView addSubview:fireballView];
	[fireballArray addObject:fireball];
		}
	}
	
}

-(void)setUpLevel{
	fatArray=[[NSMutableArray alloc] init];
	for(int i=0; i<=11; i++){
		NSMutableArray *test=[[NSMutableArray alloc] init];
		for(int i=0; i<=17; i++){
			[test insertObject:[NSNumber numberWithInt:1] atIndex:i];
		}
		[fatArray insertObject:test atIndex:i];
	}
	for(int i=0; i<=11; i++){
		NSMutableArray *rows=[fatArray objectAtIndex:i];
		for(int j=0; j<=17; j++){
			CGRect frame=CGRectMake(10+25*i,5+25*j, 25, 25);
			UIView *box=[[UIView alloc]initWithFrame:frame];
			int test=[[[rows objectAtIndex:j] retain] intValue];
			if(test==1){				
				box.backgroundColor=[UIColor greenColor];				
			}
			else if (test==2){
				box.backgroundColor=[UIColor redColor];
			}			
			else{
				box.backgroundColor=[UIColor whiteColor];				
			}
			[shipView addSubview:box];
			[box release];
		}
		[rows release];
	}	
}	
	
-(void)uDead:(CGPoint) value{
	CGPoint temp=[myShip position];
	if(temp.x==value.x&&temp.y==value.y){
		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Ur Dead" message:@"" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
		[alert show];
		[alert release];
	}
}
	

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *) event{
	UITouch *touch=[[event allTouches] anyObject];
	CGPoint move= [touch locationInView:myView];
	
	
	

	[myShip moveShip:move];
	
	
	[self destroyBox];
	
	
	}

	
-(void)colorbox{
	CGPoint temp=[myShip position];
	int tempx=temp.x;
	int tempy=temp.y;
	NSMutableArray *row=[fatArray objectAtIndex:tempx];
		CGRect frame=[myShip convertPosToSquare];
		int test=[[row objectAtIndex:tempy] intValue];
		UIView *box=[[UIView alloc]initWithFrame:frame];
		if(test==1){			
			box.backgroundColor=[UIColor greenColor];			
		}
		else if (test==2){			
			box.backgroundColor=[UIColor redColor];			
		}
		else{
			 box.backgroundColor=[UIColor whiteColor];
			}
	[shipView addSubview:box];
	[box release];
	
	}
-(void)destroyBox{
	CGPoint temp=[myShip position];
	int tempx=temp.x;
	int tempy=temp.y;
	NSMutableArray *row=[fatArray objectAtIndex:tempx];
	int k= [[row objectAtIndex:tempy]intValue];
	int a=k-1;
	[row replaceObjectAtIndex:tempy	withObject:[NSNumber numberWithInt:a]];
	[fatArray replaceObjectAtIndex:tempx withObject:row];	
	[self colorbox];	
	[self gameWon];	
	}
-(void)gameWon{
	int l=0;
	for(int i=0; i<=11; i++){
		NSMutableArray *row=[fatArray objectAtIndex:i];
		for(int j=0; j<=17; j++){
			int k=[[row	objectAtIndex:j]intValue];
			if(k>0){
				l++;
			}
		}
	}
	if(l>0)
	{
	}
	else{
		
		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"U Won" message:@"U won the game" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
		[alert show];
		[alert release];
	}
	
}




/*
// 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 {
	[myShip release];
    [super dealloc];
}

@end