GameView.m
资源名称:PushBox.rar [点击查看]
上传用户:qhdwjd2004
上传日期:2020-12-29
资源大小:362k
文件大小:21k
源码类别:
iPhone
开发平台:
Objective-C
- #import "GameView.h"
- #import "MainView.h"
- @implementation GameView
- //////////////////////////////////////////////////////
- // 视图函数
- //////////////////////////////////////////////////////
- // 初始化
- -(id)initWithFrame:(CGRect)rect
- {
- if ((self == [ super initWithFrame: rect ]) != nil) {
- viewRect = rect;
- // -------------------- init Fields --------------------
- direct = 0;
- orientation = 0;
- step = 0;
- isCompleted = NO;
- isPlaying = NO;
- lockOrient = NO;
- chapterCount = [Map ChapterCount];
- canUndo = false;
- // -------------------- init messageSheet --------------------
- confirmSheet = [[UIAlertSheet alloc] initWithFrame : CGRectMake(0,240,320,240)];
- [confirmSheet setDestructiveButton :[confirmSheet addButtonWithTitle:@"Yes"]];
- [confirmSheet addButtonWithTitle:@"No"];
- [confirmSheet setDelegate: self];
- // -------------------- init Image --------------------
- blockImage = [UIImage applicationImageNamed:@"images/block.png"];
- boxImage = [UIImage applicationImageNamed:@"images/box1.png"];
- destImage = [UIImage applicationImageNamed:@"images/dest.png"];
- playerImage = [UIImage applicationImageNamed:@"images/player.png"];
- playerUpImage = [UIImage applicationImageNamed:@"images/playerup.png"];
- playerDownImage = [UIImage applicationImageNamed:@"images/playerdown.png"];
- playerLeftImage = [UIImage applicationImageNamed:@"images/playerleft.png"];
- playerRightImage = [UIImage applicationImageNamed:@"images/playerright.png"];
- //playerOnDestImage = [UIImage applicationImageNamed:@"images/playerondest.png"];
- boxOnDestImage = [UIImage applicationImageNamed:@"images/box2.png"];
- backgroundImage = [UIImage applicationImageNamed:@"images/background.png"];
- // -------------------- init menu --------------------
- menuSwitch = [UIImage applicationImageNamed:@"images/menuswitch.png"];
- menuSwitchPos = CGRectMake(0, 430, 50 , 50 );
- menu = [ [UINavigationBar alloc] initWithFrame: CGRectMake(0, 430, 320, 50)];
- [ menu setDelegate: self ];
- [ menu enableAnimation ];
- [ menu setBarStyle: 0 ];
- [ self addSubview: menu ];
- CGRect btnRect = CGRectMake(0,5,60,30);
- menuItemHidden = [[UINavBarButton alloc] initWithTitle: @"Hide"];
- [menuItemHidden setFrame: btnRect];
- [menuItemHidden setDrawContentsCentered: YES];
- [menuItemHidden setNavBarButtonStyle: 2];
- [menuItemHidden addTarget:self action:@selector(menuItemClick:) forEvents:255];
- [menu addSubview:menuItemHidden];
- btnRect = CGRectMake(64,5,60,30);
- menuItemExit = [[UINavBarButton alloc] initWithTitle: @"Exit"];
- [menuItemExit setFrame: btnRect];
- [menuItemExit setDrawContentsCentered: YES];
- [menuItemExit setNavBarButtonStyle: 0];
- [menuItemExit addTarget:self action:@selector(menuItemClick:) forEvents:255];
- [menu addSubview:menuItemExit];
- btnRect = CGRectMake(128,5,60,30);
- menuItemUndo = [[UINavBarButton alloc] initWithTitle: @"Undo"];
- [menuItemUndo setFrame: btnRect];
- [menuItemUndo setDrawContentsCentered: YES];
- [menuItemUndo setNavBarButtonStyle: 0];
- [menuItemUndo addTarget:self action:@selector(menuItemClick:) forEvents:255];
- [menu addSubview:menuItemUndo];
- [menuItemUndo setEnabled:NO];
- btnRect = CGRectMake(192,5,60,30);
- menuItemPreStage = [[UINavBarButton alloc] initWithTitle: @"|<<"];
- [menuItemPreStage setFrame: btnRect];
- [menuItemPreStage setDrawContentsCentered: YES];
- [menuItemPreStage setNavBarButtonStyle: 0];
- [menuItemPreStage addTarget:self action:@selector(menuItemClick:) forEvents:255];
- [menu addSubview:menuItemPreStage];
- btnRect = CGRectMake(256,5,60,30);
- menuItemNextStage = [[UINavBarButton alloc] initWithTitle: @">>|"];
- [menuItemNextStage setFrame: btnRect];
- [menuItemNextStage setDrawContentsCentered: YES];
- [menuItemNextStage setNavBarButtonStyle: 0];
- [menuItemNextStage addTarget:self action:@selector(menuItemClick:) forEvents:255];
- [menu addSubview: menuItemNextStage];
- // 隐藏菜单
- [self hideNavBar];
- }
- return self;
- }
- // -------------- 绘图 -----------------------
- - (void)drawRect:(CGRect)rect {
- // 绘制背景
- float black[4] = { 0, 0, 0, 1 };
- CGContextRef ctx = UICurrentContext();
- CGContextSetFillColor(ctx, black);
- CGContextFillRect(ctx, rect);
- // 绘制背景图片
- [ backgroundImage draw1PartImageInRect: viewRect ];
- // 检查是否完成
- isCompleted = YES;
- int i;
- for(i=0;i<Map_LEN;i++) {
- if(mdata.mapdata[i] == BoxBlock) isCompleted = NO;
- switch(mdata.mapdata[i])
- {
- case WallBlock:
- [ blockImage draw1PartImageInRect: positions[i] ];
- break;
- case BoxBlock:
- [ boxImage draw1PartImageInRect: positions[i] ];
- break;
- case TargetBlock:
- [ destImage draw1PartImageInRect: positions[i] ];
- break;
- case PlayerOnTargetBlock:
- case PlayerBlock:
- switch(direct)
- {
- case 0:
- [ playerImage draw1PartImageInRect: positions[i] ];
- break;
- case 1:
- switch(orientation)
- {
- case 0:
- [ playerUpImage draw1PartImageInRect: positions[i] ];
- break;
- case 1:
- [ playerDownImage draw1PartImageInRect: positions[i] ];
- break;
- case 3:
- [ playerLeftImage draw1PartImageInRect: positions[i] ];
- break;
- case 2:
- [ playerRightImage draw1PartImageInRect: positions[i] ];
- break;
- }
- break;
- case 2:
- switch(orientation)
- {
- case 0:
- [ playerDownImage draw1PartImageInRect: positions[i] ];
- break;
- case 1:
- [ playerUpImage draw1PartImageInRect: positions[i] ];
- break;
- case 3:
- [ playerRightImage draw1PartImageInRect: positions[i] ];
- break;
- case 2:
- [ playerLeftImage draw1PartImageInRect: positions[i] ];
- break;
- }
- break;
- case 3:
- switch(orientation)
- {
- case 0:
- [ playerLeftImage draw1PartImageInRect: positions[i] ];
- break;
- case 1:
- [ playerRightImage draw1PartImageInRect: positions[i] ];
- break;
- case 3:
- [ playerDownImage draw1PartImageInRect: positions[i] ];
- break;
- case 2:
- [ playerUpImage draw1PartImageInRect: positions[i] ];
- break;
- }
- break;
- case 4:
- switch(orientation)
- {
- case 0:
- [ playerRightImage draw1PartImageInRect: positions[i] ];
- break;
- case 1:
- [ playerLeftImage draw1PartImageInRect: positions[i] ];
- break;
- case 3:
- [ playerUpImage draw1PartImageInRect: positions[i] ];
- break;
- case 2:
- [ playerDownImage draw1PartImageInRect: positions[i] ];
- break;
- }
- break;
- }
- break;
- case BoxOnTargetBlock:
- [ boxOnDestImage draw1PartImageInRect: positions[i] ];
- break;
- case EmptyBlock:
- default:
- break;
- }
- }
- if(menuFlag == NO)
- {
- [menuSwitch draw1PartImageInRect: menuSwitchPos];
- }
- }
- // 释放资源
- - (void)dealloc
- {
- [self dealloc];
- [super dealloc];
- }
- // 提示信息
- -(void)showConfirm:(NSString *)msg Title:(NSString *)title
- {
- [confirmSheet setTitle: title];
- [confirmSheet setBodyText: msg];
- [confirmSheet popupAlertAnimated:YES];
- }
- // 提示信息处理
- -(void)alertSheet:(UIAlertSheet*)sheet buttonClicked:(int)button
- {
- switch(button)
- {
- case 1: // YES
- [self save];
- isPlaying = NO;
- [(MainView *)parentView showCover];
- break;
- case 2: // NO
- // do nothing
- break;
- }
- [sheet dismiss];
- }
- //////////////////////////////////////////////////////
- // 加载/保存
- //////////////////////////////////////////////////////
- -(void)load:(Mess)messdata
- {
- mdata = messdata;
- int i;
- int x = 0, y = 0;
- for(i=0;i<Map_LEN;i++)
- {
- x = (i % 14) * BoxSize + BorderSize;
- y = (i / 14) * BoxSize + BorderSize;
- positions[i] = CGRectMake(x, y, BoxSize , BoxSize );
- if(mdata.mapdata[i]==PlayerBlock || mdata.mapdata[i]== PlayerOnTargetBlock)
- playerPos = i;
- }
- isPlaying = YES;
- step = 0;
- canUndo = NO;
- [menuItemUndo setEnabled:NO];
- [self setNeedsDisplayInRect:viewRect];
- [self move:0];
- }
- - (void)save
- {
- isPlaying = NO;
- [Map SaveMess:mdata];
- }
- - (void)reload
- {
- [Map LoadChapter:mdata.chapterIndex Data:mdata.mapdata];
- [self load:mdata];
- }
- //////////////////////////////////////////////////////
- // 属性
- //////////////////////////////////////////////////////
- // 是否正在游戏
- -(bool)IsPlaying
- {
- return isPlaying;
- }
- //////////////////////////////////////////////////////
- // 控制
- //////////////////////////////////////////////////////
- // 鼠标按下
- - (void)mouseDown: (struct __GSEvent *)event
- {
- CGPoint p = GSEventGetInnerMostPathPosition(event);
- mDownXY = CGPointMake(p.x,p.y);
- }
- // 鼠标弹起
- - (void)mouseUp:(struct __GSEvent *)event
- {
- CGPoint p = GSEventGetInnerMostPathPosition(event);
- if(p.y>430) // 响应菜单
- {
- if(menuFlag==NO)
- {
- if(p.x<50)
- {
- [self showNavBar];
- return;
- }
- }
- else
- {
- // 响应菜单
- return;
- }
- }
- // 检查是否完成
- if(isCompleted)
- {
- [self Pass];
- }
- else
- {
- // 响应移动
- int x = mDownXY.x - p.x;
- int y = mDownXY.y - p.y;
- bool turnx = x<0;
- bool turny = y<0;
- if(turnx) x*=-1;
- if(turny) y*=-1;
- if(x-y>5 || x-y <-5) // 精度5
- {
- if(x>y)
- {
- if(turnx) [self move:4]; // right
- else [self move:3]; // left
- }
- else
- {
- if(turny) [self move:2]; // down
- else [self move:1]; // up
- }
- step++; // 步数加一
- }
- else
- {
- //[self move:0]; // stay
- }
- }
- }
- // 玩家移动
- -(void)move:(int)dir;
- {
- direct = dir;
- switch(direct)
- {
- case 0:
- [self setNeedsDisplayInRect:positions[playerPos]];
- break;
- case 1: // 上
- if(playerPos<14 || mdata.mapdata[playerPos-14]== WallBlock)
- {
- break;
- }
- else
- {
- if((mdata.mapdata[playerPos-14]==BoxBlock || mdata.mapdata[playerPos-14]==BoxOnTargetBlock) && ([self moveBox:direct num:(playerPos-14)]==NO))
- {
- break;
- }
- else
- {
- mdata.mapdata[playerPos]-= PlayerBlock;
- mdata.mapdata[playerPos-14]+=PlayerBlock;
- [self setNeedsDisplayInRect:positions[playerPos]];
- [self setNeedsDisplayInRect:positions[playerPos-14]];
- playerPos -=14;
- }
- }
- break;
- case 2: // 下
- if(playerPos>Map_LEN-14 || mdata.mapdata[playerPos+14]==WallBlock)
- {
- break;
- }
- else
- {
- if((mdata.mapdata[playerPos+14]==BoxBlock || mdata.mapdata[playerPos+14]==BoxOnTargetBlock)&& ([self moveBox:direct num:(playerPos+14)]==NO))
- {
- break;
- }
- else
- {
- mdata.mapdata[playerPos]-=PlayerBlock;
- mdata.mapdata[playerPos+14]+=PlayerBlock;
- [self setNeedsDisplayInRect:positions[playerPos]];
- [self setNeedsDisplayInRect:positions[playerPos+14]];
- playerPos +=14;
- }
- }
- break;
- case 3: // 左
- if(playerPos%14==0 || mdata.mapdata[playerPos-1]==WallBlock)
- {
- break;
- }
- else
- {
- if((mdata.mapdata[playerPos-1]==BoxBlock || mdata.mapdata[playerPos-1]==BoxOnTargetBlock)&& ([self moveBox:direct num:(playerPos-1)]==NO))
- {
- break;
- }
- else
- {
- mdata.mapdata[playerPos]-=PlayerBlock;
- mdata.mapdata[playerPos-1]+=PlayerBlock;
- [self setNeedsDisplayInRect:positions[playerPos]];
- [self setNeedsDisplayInRect:positions[playerPos-1]];
- playerPos--;
- }
- }
- break;
- case 4: // 右
- if(playerPos%14==13 || mdata.mapdata[playerPos+1]==WallBlock)
- {
- break;
- }
- else
- {
- if((mdata.mapdata[playerPos+1]==BoxBlock || mdata.mapdata[playerPos+1]==BoxOnTargetBlock) && ([self moveBox:direct num:(playerPos+1)]==NO))
- {
- break;
- }
- else
- {
- mdata.mapdata[playerPos]-=PlayerBlock;
- mdata.mapdata[playerPos+1]+=PlayerBlock;
- [self setNeedsDisplayInRect:positions[playerPos]];
- [self setNeedsDisplayInRect:positions[playerPos+1]];
- playerPos++;
- }
- }
- break;
- }
- }
- // 箱子移动
- - (bool)moveBox:(int)dir num:(int)n
- {
- bool returnValue = NO;
- switch(dir)
- {
- case 1: // 上
- if(n<14 || mdata.mapdata[n-14]==WallBlock || mdata.mapdata[n-14]==BoxBlock || mdata.mapdata[n-14]== BoxOnTargetBlock)
- {
- returnValue = NO;
- }
- else
- {
- [self recordMess];
- mdata.mapdata[n]-=BoxBlock;
- mdata.mapdata[n-14]+=BoxBlock;
- [self setNeedsDisplayInRect:positions[n]];
- [self setNeedsDisplayInRect:positions[n-14]];
- returnValue = YES;
- }
- break;
- case 2: // 下
- if(n>Map_LEN-14 || mdata.mapdata[n+14]==WallBlock || mdata.mapdata[n+14]==BoxBlock || mdata.mapdata[n+14]== BoxOnTargetBlock)
- {
- returnValue = NO;
- }
- else
- {
- [self recordMess];
- mdata.mapdata[n]-=BoxBlock;
- mdata.mapdata[n+14]+=BoxBlock;
- [self setNeedsDisplayInRect:positions[n]];
- [self setNeedsDisplayInRect:positions[n+14]];
- returnValue = YES;
- }
- break;
- case 3: // 左
- if(n%14==0 || mdata.mapdata[n-1]== WallBlock || mdata.mapdata[n-1]==BoxBlock || mdata.mapdata[n-1]== BoxOnTargetBlock)
- {
- returnValue = NO;
- }
- else
- {
- [self recordMess];
- mdata.mapdata[n]-=BoxBlock;
- mdata.mapdata[n-1]+=BoxBlock;
- [self setNeedsDisplayInRect:positions[n]];
- [self setNeedsDisplayInRect:positions[n-1]];
- returnValue = YES;
- }
- break;
- case 4: // 右
- if(n%14==13 || mdata.mapdata[n+1]==WallBlock || mdata.mapdata[n+1]==BoxBlock || mdata.mapdata[n+1]== BoxOnTargetBlock)
- {
- returnValue = NO;
- }
- else
- {
- [self recordMess];
- mdata.mapdata[n]-=BoxBlock;
- mdata.mapdata[n+1]+=BoxBlock;
- [self setNeedsDisplayInRect:positions[n]];
- [self setNeedsDisplayInRect:positions[n+1]];
- returnValue = YES;
- }
- break;
- }
- return returnValue;
- }
- - (void)recordMess
- {
- record = [Map MessCopy:mdata];
- canUndo = YES;
- [menuItemUndo setEnabled:YES];
- }
- - (void)undo
- {
- if(canUndo == YES)
- {
- mdata = [Map MessCopy:record];
- int i;
- int x = 0, y = 0;
- for(i=0;i<Map_LEN;i++)
- {
- x = (i % 14) * BoxSize + BorderSize;
- y = (i / 14) * BoxSize + BorderSize;
- positions[i] = CGRectMake(x, y, BoxSize , BoxSize );
- if(mdata.mapdata[i]==PlayerBlock || mdata.mapdata[i]== PlayerOnTargetBlock)
- playerPos = i;
- }
- [self setNeedsDisplayInRect:viewRect];
- [menuItemUndo setEnabled:NO];
- canUndo = NO;
- }
- }
- //////////////////////////////////////////////////////
- // 设置
- //////////////////////////////////////////////////////
- // 设备翻转
- -(void)changeOrientation:(int)neworient
- {
- if(!lockOrient&&(neworient ==1||neworient ==2||neworient ==3||neworient ==4))
- {
- switch(neworient)
- {
- case 1:
- orientation = 0;
- break;
- case 2:
- orientation =1;
- break;
- case 3:
- orientation =3;
- break;
- case 4:
- orientation=2;
- break;
- }
- [playerImage setOrientation: orientation];
- [playerUpImage setOrientation: orientation];
- [playerDownImage setOrientation: orientation];
- [playerLeftImage setOrientation: orientation];
- [playerRightImage setOrientation: orientation];
- [self setNeedsDisplayInRect:positions[playerPos]];
- }
- }
- // 设置主视图
- - (void)setParentView:(UIView*)view
- {
- parentView = view;
- }
- // 检查是否过关
- - (void)Pass
- {
- if(isCompleted)
- {
- if(mdata.isCustom)
- {
- // 返回自定义地图
- [(MainView*)parentView customGame: YES];
- }
- else
- {
- // 进入下一关
- [self nextChapter:YES];
- }
- }
- }
- - (void)preChapter
- {
- if(mdata.isCustom)
- {
- // 返回自定义地图
- [(MainView*)parentView customGame: YES];
- return;
- }
- Mess nextdata;
- nextdata.chapterIndex = mdata.chapterIndex;
- nextdata.enableChapterCount = mdata.enableChapterCount;
- nextdata.isCustom=NO;
- if(nextdata.chapterIndex - 1 < 0)
- {
- nextdata.chapterIndex = nextdata.enableChapterCount - 1;
- }
- else
- {
- nextdata.chapterIndex--;
- }
- [Map LoadChapter:nextdata.chapterIndex Data:nextdata.mapdata];
- [self load:nextdata];
- [(MainView*)parentView showView: GAMEVIEW];
- }
- - (void)nextChapter:(bool)isPass
- {
- if(mdata.isCustom)
- {
- // 如果是自定义地图,返回自定义地图选择界面
- [(MainView*)parentView customGame: YES];
- return;
- }
- Mess nextdata;
- nextdata.chapterIndex = mdata.chapterIndex;
- nextdata.enableChapterCount = mdata.enableChapterCount;
- nextdata.isCustom=NO;
- // 判断是否已经是最后一关
- if(chapterCount>TOTAL_CHAPTER)
- {
- chapterCount = TOTAL_CHAPTER;
- }
- if(nextdata.chapterIndex + 1 >= chapterCount)
- {
- // 通过最后一关,进入第一关
- nextdata.chapterIndex = 0;
- nextdata.enableChapterCount = chapterCount;
- }
- else
- {
- // 是否过关
- if(isPass)
- {
- // 过关进入下一关
- nextdata.chapterIndex++;
- // 判断下一关是否曾过
- if(nextdata.chapterIndex >= nextdata.enableChapterCount)
- {
- nextdata.enableChapterCount = nextdata.chapterIndex + 1;
- }
- }
- else
- {
- if(nextdata.chapterIndex + 1 >= nextdata.enableChapterCount)
- {
- nextdata.chapterIndex = 0;
- }
- else
- {
- nextdata.chapterIndex++;
- }
- }
- }
- [Map LoadChapter:nextdata.chapterIndex Data:nextdata.mapdata];
- [self load:nextdata];
- [(MainView*)parentView showView: GAMEVIEW];
- }
- - (void)showNavBar
- {
- [menu setFrame:CGRectMake(0, 430, 320, 50)];
- menuFlag = YES;
- }
- - (void)hideNavBar
- {
- [menu setFrame:CGRectMake(0, 480, 320, 50)];
- menuFlag = NO;
- }
- - (void)menuItemClick:(UIPushButton *)button
- {
- if (button==menuItemHidden) {
- [self hideNavBar];
- [self setNeedsDisplayInRect:menuSwitchPos];
- }
- if (button==menuItemExit) {
- [self showConfirm:@"Do you realy want to EXIT?" Title:@"Confirm"];
- }
- if (button==menuItemUndo) {
- [self undo];
- }
- if (button==menuItemPreStage) {
- [self preChapter];
- }
- if (button==menuItemNextStage) {
- [self nextChapter:NO];
- }
- }
- @end