#import "GameViewController.h"
#import "cAppDelegate.h"
@interface GameViewController ()
@end
@implementation GameViewController
@synthesize m_box1;
@synthesize m_box2;
@synthesize m_box3;
@synthesize m_box4;
@synthesize m_target;
@synthesize destinationX;
@synthesize destinationY;
@synthesize ableMoveX;
@synthesize ableMoveY;
@synthesize boxFlag;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
//初始化游戏
[self initGame];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//初始化箱子的目的地数组,人物和箱子可以移动的坐标数组
-(void)initGame
{
//初始化箱子和人物的位置
m_target.center = CGPointMake(176.5, 275.5);
m_box1.center = CGPointMake(141.5, 240.5);
m_box2.center = CGPointMake(176.5, 240.5);
m_box3.center = CGPointMake(141.5, 275.5);
m_box4.center = CGPointMake(211.5, 275.5);
destinationX = [[NSMutableArray alloc]init];
destinationY = [[NSMutableArray alloc]init];
ableMoveX = [[NSMutableArray alloc]init];
ableMoveY = [[NSMutableArray alloc]init];
//初始化箱子的目的地x坐标
[destinationX addObject:[NSNumber numberWithFloat:141.5]];
[destinationX addObject:[NSNumber numberWithFloat:71.5]];
[destinationX addObject:[NSNumber numberWithFloat:176.5]];
[destinationX addObject:[NSNumber numberWithFloat:246.5]];
//初始化箱子的目的地y坐标
[destinationY addObject:[NSNumber numberWithFloat:345.5]];
[destinationY addObject:[NSNumber numberWithFloat:240.5]];
[destinationY addObject:[NSNumber numberWithFloat:170.5]];
[destinationY addObject:[NSNumber numberWithFloat:275.5]];
//人物和箱子可以移动的x坐标
[ableMoveX addObject:[NSNumber numberWithFloat:141.5]];
[ableMoveX addObject:[NSNumber numberWithFloat:141.5]];
[ableMoveX addObject:[NSNumber numberWithFloat:141.5]];
[ableMoveX addObject:[NSNumber numberWithFloat:106.5]];
[ableMoveX addObject:[NSNumber numberWithFloat:176.5]];
[ableMoveX addObject:[NSNumber numberWithFloat:176.5]];
[ableMoveX addObject:[NSNumber numberWithFloat:176.5]];
[ableMoveX addObject:[NSNumber numberWithFloat:211.5]];
//人物和箱子可以移动的y坐标
[ableMoveY addObject:[NSNumber numberWithFloat:310.5]];
[ableMoveY addObject:[NSNumber numberWithFloat:275.5]];
[ableMoveY addObject:[NSNumber numberWithFloat:240.5]];
[ableMoveY addObject:[NSNumber numberWithFloat:240.5]];
[ableMoveY addObject:[NSNumber numberWithFloat:205.5]];
[ableMoveY addObject:[NSNumber numberWithFloat:240.5]];
[ableMoveY addObject:[NSNumber numberWithFloat:275.5]];
[ableMoveY addObject:[NSNumber numberWithFloat:275.5]];
}
- (IBAction)btnBack:(id)sender {
[cAppDelegate switchViewController:1];
}
//接收触屏事件
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//获取当前应用程序的代理对象
cAppDelegate * nowDelegate = [UIApplication sharedApplication].delegate;
CGPoint touchPoint = [[touches anyObject]locationInView:nowDelegate.m_gameVC.view];
[self moveTarget:touchPoint];
}
//人物和箱子移动
-(void)moveTarget:(CGPoint)touchPoint
{
int w = abs(touchPoint.x- m_target.center.x);
int h = abs(touchPoint.y -m_target.center.y);
if (w>h) // left or right
{
if (touchPoint.x<m_target.center.x)
{
dir = left;
}
else
{
dir = right;
}
}
else //up or down
{
if (touchPoint.y<m_target.center.y)
{
dir = up;
}
else
{
dir = down;
}
}
switch (dir) {
case up:
//判断下一个点是否可以走
if([self judge:CGPointMake( m_target.center.x,m_target.center.y-35)]==YES)
{
//判断一个点是否有箱子
if ([self judgeBoxExist:CGPointMake( m_target.center.x,m_target.center.y-35)]==YES)
{
//如果箱子存在,判断箱子是否能走到下一个点
if([self judge:CGPointMake( m_target.center.x,m_target.center.y-70)]==YES)
{
//判断当前箱子的下一个点是否有其他箱子,如果没有箱子则可以移动,反之则不能
if ([self judgeBoxExist:CGPointMake( m_target.center.x,m_target.center.y-70)]==NO)
{
//移动人物
m_target.center = CGPointMake( m_target.center.x,m_target.center.y-35);
//移动箱子
[self moveTheBox];
}
}
}
else
{
//移动人物
m_target.center = CGPointMake( m_target.center.x,m_target.center.y-35);
}
}
break;
case down:
//判断下一个点是否可以走
if([self judge:CGPointMake( m_target.center.x,m_target.center.y+35)]==YES)
{
//判断一个点是否有箱子
if ([self judgeBoxExist:CGPointMake( m_target.center.x,m_target.center.y+35)]==YES)
{
//如果箱子存在,判断箱子是否能走到下一个点
if([self judge:CGPointMake( m_target.center.x,m_target.center.y+70)]==YES)
{
//判断当前箱子的下一个点是否有其他箱子,如果没有箱子则可以移动,反之则不能
if ([self judgeBoxExist:CGPointMake( m_target.center.x,m_target.center.y+70)]==NO)
{
//移动人物
m_target.center = CGPointMake( m_target.center.x,m_target.center.y+35);
//移动箱子
[self moveTheBox];
}
}
}
else
{
//移动人物
m_target.center = CGPointMake( m_target.center.x,m_target.center.y+35);
}
}
break;
case left:
//判断下一个点是否可以走
if ([self judge:CGPointMake( m_target.center.x-35,m_target.center.y)]==YES)
{
if ([self judgeBoxExist:CGPointMake( m_target.center.x-35,m_target.center.y)]==YES)
{
//如果箱子存在,判断箱子是否能走到下一个点
if([self judge:CGPointMake( m_target.center.x-70,m_target.center.y)]==YES)
{
//判断当前箱子的下一个点是否有其他箱子,如果没有箱子则可以移动,反之则不能
if ([self judgeBoxExist:CGPointMake( m_target.center.x-70,m_target.center.y)]==NO)
{
//移动人物
m_target.center = CGPointMake( m_target.center.x-35,m_target.center.y);
//移动箱子
[self moveTheBox];
}
}
}
else
{
//移动人物
m_target.center = CGPointMake( m_target.center.x-35,m_target.center.y);
}
}
break;
case right:
//判断下一个点是否可以走
if([self judge:CGPointMake( m_target.center.x+35,m_target.center.y)]==YES)
{
if ([self judgeBoxExist:CGPointMake( m_target.center.x+35,m_target.center.y)]==YES)
{
if([self judge:CGPointMake( m_target.center.x+70,m_target.center.y)]==YES)
{
//判断当前箱子的下一个点是否有其他箱子,如果没有箱子则可以移动,反之则不能
if ([self judgeBoxExist:CGPointMake( m_target.center.x+70,m_target.center.y)]==NO)
{
//移动人物
m_target.center = CGPointMake( m_target.center.x+35,m_target.center.y);
//移动箱子
[self moveTheBox];
}
}
}
else
{
//移动人物
m_target.center = CGPointMake( m_target.center.x+35,m_target.center.y);
}
}
break;
}
}
//判断人物或箱子将要移动的坐标是否正确
-(BOOL)judge:(CGPoint)nextPoint
{
for(int i=0;i<ableMoveX.count;i++)
{
int w =abs(nextPoint.x - [[ableMoveX objectAtIndex:i]floatValue]);
int h = abs( nextPoint.y - [[ableMoveY objectAtIndex:i]floatValue]);
//误差小于10
if (w<10&&h<10)
{
return YES;
}
}
for (int i=0; i<destinationX.count; i++)
{
int w =abs( nextPoint.x - [[destinationX objectAtIndex:i]floatValue]);
int h = abs(nextPoint.y - [[destinationY objectAtIndex:i]floatValue]);
//误差小于10
if (w<10&&h<10)
{
return YES;
}
}
return NO;
}
//判断人物的下一个坐标是否有箱子
-(BOOL)judgeBoxExist:(CGPoint)nextPoint
{
int w = abs( nextPoint.x - m_box1.center.x);
int h = abs(nextPoint.y - m_box1.center.y);
//误差小于10
if (w<10&&h<10)
{
boxFlag = 1;
return YES;
}
w = abs(nextPoint.x - m_box2.center.x);
h = abs(nextPoint.y - m_box2.center.y);
//误差小于10
if (w<10&&h<10 )
{
boxFlag = 2;
return YES;
}
w = abs(nextPoint.x - m_box3.center.x);
h = abs(nextPoint.y - m_box3.center.y);
//误差小于10
if ( w<10&&h<10 )
{
boxFlag = 3;
return YES;
}
w =abs(nextPoint.x - m_box4.center.x);
h =abs(nextPoint.y - m_box4.center.y);
//误差小于10
if ( w<10&&h<10 )
{
boxFlag = 4;
return YES;
}
return NO;
}
//移动箱子
-(void)moveTheBox
{
switch (dir)
{
case up:
switch (boxFlag)
{
case 1: //移动m_box1
m_box1.center = CGPointMake(m_box1.center.x, m_box1.center.y-35);
break;
case 2: //移动m_box2
m_box2.center = CGPointMake(m_box2.center.x, m_box2.center.y-35);
break;
case 3: //移动m_box3
m_box3.center = CGPointMake(m_box3.center.x, m_box3.center.y-35);
break;
case 4: //移动m_box4
m_box4.center = CGPointMake(m_box4.center.x, m_box4.center.y-35);
break;
}
break;
case down:
switch (boxFlag)
{
case 1: //移动m_box1
m_box1.center = CGPointMake(m_box1.center.x, m_box1.center.y+35);
break;
case 2: //移动m_box2
m_box2.center = CGPointMake(m_box2.center.x, m_box2.center.y+35);
break;
case 3: //移动m_box3
m_box3.center = CGPointMake(m_box3.center.x, m_box3.center.y+35);
break;
case 4: //移动m_box4
m_box4.center = CGPointMake(m_box4.center.x, m_box4.center.y+35);
break;
}
break;
case left:
switch (boxFlag)
{
case 1: //移动m_box1
m_box1.center = CGPointMake(m_box1.center.x-35, m_box1.center.y);
break;
case 2: //移动m_box2
m_box2.center = CGPointMake(m_box2.center.x-35, m_box2.center.y);
break;
case 3: //移动m_box3
m_box3.center = CGPointMake(m_box3.center.x-35, m_box3.center.y);
break;
case 4: //移动m_box4
m_box4.center = CGPointMake(m_box4.center.x-35, m_box4.center.y);
break;
}
break;
case right:
switch (boxFlag)
{
case 1: //移动m_box1
m_box1.center = CGPointMake(m_box1.center.x+35, m_box1.center.y);
break;
case 2: //移动m_box2
m_box2.center = CGPointMake(m_box2.center.x+35, m_box2.center.y);
break;
case 3: //移动m_box3
m_box3.center = CGPointMake(m_box3.center.x+35, m_box3.center.y);
break;
case 4: //移动m_box4
m_box4.center = CGPointMake(m_box4.center.x+35, m_box4.center.y);
break;
}
break;
}
[self NextEevel];
}
//成功闯关
-(void)NextEevel
{
int num = 0;
for (int i=0; i<destinationX.count; i++)
{
int w = abs(m_box1.center.x-[[destinationX objectAtIndex:i]floatValue]);
int h = abs(m_box1.center.y-[[destinationY objectAtIndex:i]floatValue]);
//误差为10
if (w<10&&h<10)
{
num++;
}
w = abs(m_box2.center.x-[[destinationX objectAtIndex:i]floatValue]);
h = abs(m_box2.center.y-[[destinationY objectAtIndex:i]floatValue]);
//误差为10
if (w<10&&h<10)
{
num++;
}
w = abs(m_box3.center.x-[[destinationX objectAtIndex:i]floatValue]);
h = abs(m_box3.center.y-[[destinationY objectAtIndex:i]floatValue]);
//误差为10
if (w<10&&h<10)
{
num++;
}
w = abs(m_box4.center.x-[[destinationX objectAtIndex:i]floatValue]);
h = abs(m_box4.center.y-[[destinationY objectAtIndex:i]floatValue]);
//误差为10
if (w<10&&h<10)
{
num++;
}
}
if (num ==4)
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"祝贺您" message:@"成功闯关!" delegate:self cancelButtonTitle:@"再玩一次" otherButtonTitles:@"下一关", nil];
[alert show];
}
}
//处理警告结果
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
switch (buttonIndex)
{
case 0:
//再玩一次
[self initGame];
break;
case 1:
//进入第二关
[cAppDelegate switchViewController:7];
break;
}
}
@end