Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // Digger.m
- // TestCoco.s
- //
- // Created by Developer on 18/05/09.
- // Copyright 2009 Retina Softwares All rights reserved.
- //
- #import "Digger.h"
- #import "LevelInfo.h"
- #import "GameScene.h"
- #import "MenuScene.h"
- @implementation Digger
- //@synthesize audioPlayer;
- @synthesize bCollide;
- @synthesize heroScore;
- @synthesize backgroundMusic;
- @synthesize tilemap1;
- @synthesize tileInfo;
- @synthesize hasCarrot;
- @synthesize stopMoving;
- @synthesize heroPos;
- @synthesize mDir;
- @synthesize mPreDir;
- @synthesize level;
- @synthesize bag;
- @synthesize enemyCount;
- //@synthesize bLife;
- @synthesize bagCount;
- @synthesize gemCount;
- @synthesize carrotCount;
- @synthesize heroLife;
- -(id)init
- {
- NSLog(@"digger init................");
- //heroLife=3;
- if(self != nil)
- {
- NSLog(@"digger init................");
- }
- level=1;
- self = [super init];
- return self;
- }
- -(void)startMusic
- {
- //return;
- backgroundMusic = [[Sound alloc] initWithSound:@"Dance_01" ofType:@"mp3"];
- [backgroundMusic setRepeat:YES];
- [backgroundMusic playIt];
- backgroundMusicWithCarrot = [[Sound alloc] initWithSound:@"Dance_04" ofType:@"mp3"];
- [backgroundMusicWithCarrot setRepeat:YES];
- // ` = [[Sound alloc] initWithSound:@"Bell_07" ofType:@"mp3"];
- // heroDeadSound = [[Sound alloc] initWithSound:@"hero_death" ofType:@"mp3"];
- path1=[[NSBundle mainBundle] pathForResource:@"hero_death" ofType:@"mp3"];
- AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path1], &soundID2);
- /*NSString *newAudioFile = [[NSBundle mainBundle] pathForResource:@"Dance_01" ofType:@"mp3"];
- audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:newAudioFile] error:NULL];
- [audioPlayer setDelegate:self];
- [audioPlayer prepareToPlay];
- //[audioPlayer play];
- [audioPlayer setNumberOfLoops:-1];
- */
- // Some sound initialization is also done here
- path2=[[NSBundle mainBundle] pathForResource:@"Bell_07" ofType:@"mp3"];
- AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path2], &soundID1);
- }
- -(void)startJoystick
- {
- //Coding for the joystick
- /* MenuItem *item1 = [MenuItemImage itemFromNormalImage:@"arrowUP.png" selectedImage:@"arrowUP.png"
- target:self selector:@selector(willMoveUp:)];
- item1.scale=0.4;
- item1.position = ccp(-16, -190);
- MenuItem *item2 = [MenuItemImage itemFromNormalImage:@"arrowDOWN.png" selectedImage:@"arrowDOWN.png"
- target:self selector:@selector(willMoveDown:)];
- item2.scale=0.4;
- item2.position = ccp(-16, -225);
- MenuItem *item3 = [MenuItemImage itemFromNormalImage:@"arrowRIGHT.png" selectedImage:@"arrowRIGHT.png"
- target:self selector:@selector(willMoveRight:)];
- item3.scale=0.4;
- item3.position = ccp(16, -210);
- MenuItem *item4 = [MenuItemImage itemFromNormalImage:@"arrowLEFT.png" selectedImage:@"arrowLEFT.png"
- target:self selector:@selector(willMoveLeft:)];
- item4.scale=0.4;
- item4.position = ccp(-48, -210);
- Menu *menu = [Menu menuWithItems: item1, item2, item3, item4, nil];
- [[self parent] addChild: menu z:2];
- */
- }
- //It will check if the digger can move from frompos to topos
- -(BOOL)canMoveFrom:(CGPoint)frompos to:(CGPoint)topos inMap:(TileMapAtlas*)map
- {
- /*For both cordinates divisible by 32,the position of digger is at the orgin of the tile. So we
- have to shift it to make the digger move.*/
- tileInfo = [[NSString alloc] init];
- if ([[NSNumber numberWithFloat: topos.x] intValue ]% 32==0 && [[NSNumber numberWithFloat: topos.y] intValue ]% 32==0)
- {
- if([mDir isEqualToString:@"UP"])
- {
- if([mPreDir isEqualToString:@"LEFT"])
- {
- int e = (topos.x-32)/32;
- int f = (topos.y-32)/32-2;
- topos.x=topos.x-16;
- topos.y=topos.y-16;
- ccRGBB s=[map tileAt:ccg(e,f)];
- if(s.r==15 || s.r==5 || s.r == 6)
- {
- s.r=24;
- }
- else if(s.r==21)
- {
- s.r=9;
- }
- else
- {
- s.r=3;
- }
- [map setTile:s at:ccg(e,f)];
- //[self emitParticles:[self position]];
- }
- else if([mPreDir isEqualToString:@"RIGHT"])
- {
- int e = (topos.x)/32;
- int f = (topos.y-32)/32-2;
- topos.x=topos.x+16;
- topos.y=topos.y-16;
- ccRGBB s=[map tileAt:ccg(e,f)];
- if(s.r==15 || s.r==7|| s.r==20)
- {
- s.r=25;
- }
- else if(s.r==21)
- {
- s.r=9;
- }
- else
- {
- s.r=3;
- }
- //[self emitParticles:[self position]];
- [map setTile:s at:ccg(e,f)];
- }
- //[self emitParticles:[self position]];
- [self runAction:[MoveTo actionWithDuration:0.3 position:ccp(topos.x,topos.y)]];
- [self setHeroPos:ccp(topos.x,topos.y)];
- [self checkingDiamond:topos];
- }
- else if([mDir isEqualToString:@"DOWN"])
- {
- if([mPreDir isEqualToString:@"LEFT"])
- {
- int e = (topos.x-32)/32;
- int f = (topos.y)/32-2;
- ccRGBB s=[map tileAt:ccg(e,f)];
- if(s.r==15 || s.r== 5||s.r==6)
- {
- s.r=11;
- }
- else if( s.r==21)
- {
- s.r=23;
- }
- else
- {
- s.r=3;
- }
- [map setTile:s at:ccg(e,f)];
- //[self emitParticles:[self position]];
- topos.x=topos.x-16;
- topos.y=topos.y+16;
- }
- else if([mPreDir isEqualToString:@"RIGHT"])
- {
- int e = (topos.x)/32;
- int f = (topos.y)/32-2;
- ccRGBB s=[map tileAt:ccg(e,f)];
- if(s.r==15 || s.r==7 || s.r==20)
- {
- s.r=12;
- }
- else if(s.r==21)
- {
- s.r=23;
- }
- else
- {
- s.r=3;
- }
- [map setTile:s at:ccg(e,f)];
- topos.x=topos.x+16;
- topos.y=topos.y+16;
- // [self emitParticles:[self position]];
- }
- //[self emitParticles:[self position]];
- [self runAction:[MoveTo actionWithDuration:0.3 position:ccp(topos.x,topos.y)]];
- [self setHeroPos:ccp(topos.x,topos.y)];
- [self checkingDiamond:topos];
- }
- else if([mDir isEqualToString:@"LEFT"])
- {
- if([mPreDir isEqualToString:@"UP"])
- {
- int e = (topos.x)/32;
- int f = (topos.y)/32-2;
- ccRGBB s=[map tileAt:ccg(e,f)];
- if(s.r==15 || s.r==16||s.r==17)
- {
- s.r=12;
- }
- else if(s.r==4)
- {
- s.r=9;
- }
- else
- {
- s.r=3;
- }
- [map setTile:s at:ccg(e,f)];
- topos.x=topos.x+16;
- topos.y=topos.y+16;
- [self emitParticles:[self position]];
- }
- else if([mPreDir isEqualToString:@"DOWN"])
- {
- int e = (topos.x)/32;
- int f = (topos.y-32)/32-2;
- ccRGBB s=[map tileAt:ccg(e,f)];
- if(s.r==15 || s.r==18 ||s.r==19)
- {
- s.r=25;
- }
- else if(s.r==4)
- {
- s.r=9;
- }
- else
- {
- s.r=3;
- }
- [map setTile:s at:ccg(e,f)];
- topos.x=topos.x+16;
- topos.y=topos.y-16;
- // [self emitParticles:[self position]];
- }
- [self runAction:[MoveTo actionWithDuration:0.3 position:ccp(topos.x,topos.y)]];
- [self setHeroPos:ccp(topos.x,topos.y)];
- [self checkingDiamond:topos];
- }
- else if([mDir isEqualToString:@"RIGHT"])
- {
- if([mPreDir isEqualToString:@"UP"])
- {
- int e = (topos.x-32)/32;
- int f = (topos.y)/32-2;
- ccRGBB s=[map tileAt:ccg(e,f)];
- if(s.r==15 || s.r==16 || s.r==17)
- {
- s.r=11;
- }
- else if(s.r==4)
- {
- s.r=8;
- }
- else
- {
- s.r=3;
- }
- [map setTile:s at:ccg(e,f)];
- topos.x=topos.x-16;
- topos.y=topos.y+16;
- // [self emitParticles:[self position]];
- }
- else if([mPreDir isEqualToString:@"DOWN"])
- {
- int e = (topos.x-32)/32;
- int f = (topos.y-32)/32-2;
- ccRGBB s=[map tileAt:ccg(e,f)];
- if(s.r==15 || s.r==18 || s.r==19)
- {
- s.r=24;
- }
- else if(s.r==4)
- {
- s.r=8;
- }
- else
- {
- s.r=3;
- }
- [map setTile:s at:ccg(e,f)];
- topos.x=topos.x-16;
- topos.y=topos.y-16;
- // [self emitParticles:[self position]];
- }
- [self runAction:[MoveTo actionWithDuration:0.3 position:ccp(topos.x,topos.y)]];
- [self setHeroPos:ccp(topos.x,topos.y)];
- [self checkingDiamond:topos];
- }
- return NO;
- }
- // This is the case when the topos of digger is in the middle of the two tiles.
- else if ([[NSNumber numberWithFloat: topos.x] intValue ]% 32==0 || [[NSNumber numberWithFloat: topos.y] intValue ]% 32==0 )
- {
- ccRGBB t;
- //checking for not moving on bags
- for(int h=0;h<bagCount;h++)
- {
- Bagbomb *b1=(Bagbomb*)[[self parent] getChildByTag:200+h];
- if(b1!=nil)
- {
- if([mDir isEqualToString:@"LEFT"]==YES)
- {
- if((topos.x-16)==b1.bagPos.x && (topos.y)==b1.bagPos.y)
- {
- return NO;
- }
- }
- else if([mDir isEqualToString:@"RIGHT"]==YES)
- {
- if((topos.x+16)==b1.bagPos.x && (topos.y)==b1.bagPos.y)
- {
- return NO;
- }
- }
- else if([mDir isEqualToString:@"UP"]==YES)
- {
- if((topos.x)==b1.bagPos.x && (topos.y+16)==b1.bagPos.y)
- {
- return NO;
- }
- }
- else if([mDir isEqualToString:@"DOWN"]==YES)
- {
- if((topos.x)==b1.bagPos.x && (topos.y-16)==b1.bagPos.y)
- {
- return NO;
- }
- }
- }
- }
- //For digger movement.
- if([mDir isEqualToString:@"LEFT"]==YES)
- {
- int a=((topos.x-32)/32);
- int b1=((topos.y-16)/32)-2;
- ccRGBB s= [map tileAt:ccg(a,b1)];
- ccRGBB s1= [map tileAt:ccg(a+1,b1)];
- if(s1.r==4 || s1.r==19 || s1.r==17)
- {
- s1.r=9;
- NSLog(@"Tile changed");
- [map setTile:s1 at:ccg(a+1,b1)];
- }
- if(s.r==14)
- {
- [self unschedule:@selector(animateAnt)];
- return NO;
- }
- else if(s.r==15 )
- {
- t.r=5;
- int x=(topos.x -32)/32;
- int y=((topos.y-16)/32)-2 ;
- [map setTile:t at:ccg(x,y)];
- [self emitParticles:topos];
- }
- else if( s.r==7 || s.r==16 || s.r==17 || s.r==18 || s.r==19 || s.r==20)
- {
- t.r=21;
- //t.r=3;
- int x=(topos.x -32)/32;
- int y=((topos.y-16)/32)-2 ;
- [map setTile:t at:ccg(x,y)];
- [self emitParticles:topos];
- }
- else if(s.r==4)
- {
- t.r=8;
- int x=(topos.x -32)/32;
- int y=((topos.y-16)/32)-2 ;
- [map setTile:t at:ccg(x,y)];
- [self emitParticles:topos];
- }
- else if(s.r==9)
- {
- t.r=3;
- int x=(topos.x -32)/32;
- int y=((topos.y-16)/32)-2 ;
- [map setTile:t at:ccg(x,y)];
- [self emitParticles:topos];
- }
- else if(s.r==11 || s.r==12)
- {
- t.r=23;
- int x=(topos.x-32)/32;
- int y=((topos.y-16)/32)-2 ;
- [map setTile:t at:ccg(x,y)];
- [self emitParticles:topos];
- }
- else if(s.r==24 || s.r==25)
- {
- t.r=22;
- int x=(topos.x-32)/32;
- int y=((topos.y-16)/32)-2 ;
- [map setTile:t at:ccg(x,y)];
- [self emitParticles:topos];
- }
- }
- if([mDir isEqualToString:@"RIGHT"]==YES )
- {
- int a=(topos.x/32);
- int b1=((topos.y-16)/32)-2;
- ccRGBB s= [map tileAt:ccg(a,b1)];
- ccRGBB s1= [map tileAt:ccg(a-1,b1)];
- if(s1.r==4 || s1.r==19 ||s1.r==17)
- {
- s1.r=8;
- NSLog(@"Tile chaned");
- [map setTile:s1 at:ccg(a-1,b1)];
- }
- if(s.r==14)
- {
- [self unschedule:@selector(animateAnt)];
- return NO;
- }
- else if(s.r==15)
- {
- t.r=7;
- int x=(topos.x)/32;
- int y=((topos.y-16)/32)-2 ;
- [map setTile:t at:ccg(x,y)];
- [self emitParticles:topos];
- }
- else if( s.r==5 || s.r==6 || s.r==16 || s.r==17 || s.r==18 || s.r==19)
- {
- t.r=21;
- //t.r=3;
- int x=(topos.x)/32;
- int y=((topos.y-16)/32)-2 ;
- [map setTile:t at:ccg(x,y)];
- [self emitParticles:topos];
- }
- else if(s.r==4)
- {
- t.r=9;
- int x=(topos.x)/32;
- int y=((topos.y-16)/32)-2 ;
- [map setTile:t at:ccg(x,y)];
- [self emitParticles:topos];
- }
- else if(s.r==8)
- {
- t.r=3;
- int x=(topos.x)/32;
- int y=((topos.y-16)/32)-2 ;
- [map setTile:t at:ccg(x,y)];
- [self emitParticles:topos];
- }
- else if(s.r==11 || s.r==12)
- {
- t.r=23;
- int x=(topos.x)/32;
- int y=((topos.y-16)/32)-2 ;
- [map setTile:t at:ccg(x,y)];
- [self emitParticles:topos];
- }
- else if(s.r==24 || s.r==25)
- {
- t.r=22;
- int x=(topos.x)/32;
- int y=((topos.y-16)/32)-2 ;
- [map setTile:t at:ccg(x,y)];
- [self emitParticles:topos];
- }
- }
- if([mDir isEqualToString:@"UP"]==YES)
- {
- int a=((topos.x-16)/32);
- int b1=(topos.y/32)-2;
- ccRGBB s= [map tileAt:ccg(a,b1)];
- ccRGBB s1= [map tileAt:ccg(a,b1-1)];
- if(s1.r==21 || s1.r==6 || s1.r==20)
- {
- s1.r=22;
- NSLog(@"Tile chaned");
- [map setTile:s1 at:ccg(a,b1-1)];
- }
- if(s.r==14)
- {
- [self unschedule:@selector(animateAnt)];
- return NO;
- }
- else if(s.r==15)
- {
- t.r=16;
- int x=(topos.x -16)/32;
- int y=(topos.y)/32-2 ;
- [map setTile:t at:ccg(x,y)];
- [self emitParticles:topos];
- }
- else if( s.r==5 || s.r==7 || s.r==18 || s.r==19)
- {
- t.r=4;
- int x=(topos.x -16)/32;
- int y=(topos.y)/32-2 ;
- [map setTile:t at:ccg(x,y)];
- [self emitParticles:topos];
- }
- else if(s.r==6)
- {
- t.r=8;
- int x=(topos.x -16)/32;
- int y=(topos.y)/32-2 ;
- [map setTile:t at:ccg(x,y)];
- [self emitParticles:topos];
- }
- else if(s.r==20)
- {
- t.r=9;
- int x=(topos.x -16)/32;
- int y=(topos.y)/32-2 ;
- [map setTile:t at:ccg(x,y)];
- [self emitParticles:topos];
- }
- else if(s.r==21)
- {
- t.r=23;
- int x=(topos.x -16)/32;
- int y=(topos.y)/32-2 ;
- [map setTile:t at:ccg(x,y)];
- [self emitParticles:topos];
- }
- else if(s.r==22)
- {
- t.r=3;
- int x=(topos.x -16)/32;
- int y=(topos.y)/32-2 ;
- [map setTile:t at:ccg(x,y)];
- [self emitParticles:topos];
- }
- else if(s.r==11 || s.r==24)
- {
- t.r=8;
- int x=(topos.x -16)/32;
- int y=(topos.y/32)-2 ;
- [map setTile:t at:ccg(x,y)];
- [self emitParticles:topos];
- }
- else if(s.r==12 || s.r==25)
- {
- t.r=9;
- int x=(topos.x -16)/32;
- int y=(topos.y/32)-2 ;
- [map setTile:t at:ccg(x,y)];
- [self emitParticles:topos];
- }
- else if(s.r==3)
- {
- NSLog(@"10 no tiillllle");
- }
- }
- if([mDir isEqualToString:@"DOWN"]==YES )
- {
- int a=(topos.x-16)/32;
- int b1=((topos.y-32)/32)-2;
- ccRGBB s= [map tileAt:ccg(a,b1)];
- ccRGBB s1= [map tileAt:ccg(a,b1+1)];
- if(s1.r==21 || s1.r==6 || s1.r==20)
- {
- s1.r=23;
- NSLog(@"Tile chaned");
- [map setTile:s1 at:ccg(a,b1+1)];
- }
- if(s.r==14)
- {
- [self unschedule:@selector(animateAnt)];
- return NO;
- }
- else if(s.r==15)
- {
- t.r=18;
- int x=(topos.x- 16)/32;
- int y=((topos.y-32)/32)-2 ;
- [map setTile:t at:ccg(x,y)];
- [self emitParticles:topos];
- }
- else if( s.r==5 || s.r==6 || s.r==7 || s.r==16 || s.r==17 || s.r==20)
- {
- t.r=4;
- int x=(topos.x- 16)/32;
- int y=((topos.y-32)/32)-2 ;
- [map setTile:t at:ccg(x,y)];
- [self emitParticles:topos];
- }
- else if(s.r==21)
- {
- t.r=22;
- int x=(topos.x- 16)/32;
- int y=((topos.y-32)/32)-2 ;
- [map setTile:t at:ccg(x,y)];
- [self emitParticles:topos];
- }
- else if(s.r==23)
- {
- t.r=3;
- int x=(topos.x- 16)/32;
- int y=((topos.y-32)/32)-2 ;
- [map setTile:t at:ccg(x,y)];
- [self emitParticles:topos];
- }
- else if(s.r==11 || s.r==24)
- {
- t.r=8;
- int x=(topos.x-16)/32;
- int y=((topos.y-32)/32)-2 ;
- [map setTile:t at:ccg(x,y)];
- [self emitParticles:topos];
- }
- else if(s.r==12 || s.r==25)
- {
- t.r=9;
- int x=(topos.x-16)/32;
- int y=((topos.y-32)/32)-2 ;
- [map setTile:t at:ccg(x,y)];
- [self emitParticles:topos];
- }
- }
- return YES;
- }
- // Position is on the center of the tile.
- else
- {
- if([mDir isEqualToString:@"LEFT"]==YES )
- {
- ccRGBB t=[map tileAt:ccg((topos.x-16)/32,((topos.y-16)/32)-2)];
- if(t.r==5)
- {
- t.r=6;
- int x=(topos.x -16)/32;
- int y=(topos.y-16)/32-2;
- [map setTile:t at:ccg(x,y)];
- myX=x;
- myY=y;
- //tileInfo = [NSString stringWithFormat:@"#%d#%d#",x,y];
- //[self performSelector:@selector(lastTile:) withObject:tileInfo afterDelay:0.3];
- [self schedule:@selector(lastTile) interval:0.3];
- }
- else if(t.r==8)
- {
- t.r=3;
- int x=(topos.x -16)/32;
- int y=(topos.y-16)/32-2;
- [map setTile:t at:ccg(x,y)];
- }
- else if(t.r==6)
- {
- t.r=21;
- int x=(topos.x -16)/32;
- int y=(topos.y-16)/32-2;
- [map setTile:t at:ccg(x,y)];
- }
- }
- if([mDir isEqualToString:@"RIGHT"]==YES )
- {
- ccRGBB t=[map tileAt:ccg((topos.x-16)/32,((topos.y-16)/32)-2)];
- if(t.r==7)
- {
- t.r=20;
- int x=(topos.x -16)/32;
- int y=(topos.y-16)/32-2;
- [map setTile:t at:ccg(x,y)];
- myX=x;
- myY=y;
- //tileInfo = [NSString stringWithFormat:@"#%d#%d#",x,y];
- //[self performSelector:@selector(lastTile:) withObject:tileInfo afterDelay:0.3];
- [self schedule:@selector(lastTile) interval:0.3];
- }
- else if(t.r==9)
- {
- t.r=3;
- int x=(topos.x -16)/32;
- int y=(topos.y-16)/32-2;
- [map setTile:t at:ccg(x,y)];
- }
- }
- if([mDir isEqualToString:@"UP"]==YES )
- {
- ccRGBB t=[map tileAt:ccg((topos.x-16)/32,((topos.y-16)/32)-2)];
- // ccRGBB t1=[map tileAt:ccg((topos.x-16)/32,((topos.y-16)/32)-3)];
- // if(t1.r==21)
- // {
- // t1.r=22;
- // NSLog(@"Tile changed");
- // [map setTile:t1 at:ccg((topos.x-16)/32,((topos.y-16)/32)-3)];
- // }
- if(t.r==16)
- {
- t.r=17;
- int x=(topos.x -16)/32;
- int y=(topos.y-16)/32-2;
- [map setTile:t at:ccg(x,y)];
- myX=x;
- myY=y;
- //tileInfo = [NSString stringWithFormat:@"#%d#%d#",x,y];
- //[self performSelector:@selector(lastTile:) withObject:tileInfo afterDelay:0.3];
- [self schedule:@selector(lastTile) interval:0.3];
- }
- else if(t.r==23)
- {
- t.r=3;
- int x=(topos.x -16)/32;
- int y=(topos.y-16)/32-2;
- [map setTile:t at:ccg(x,y)];
- }
- }
- if([mDir isEqualToString:@"DOWN"]==YES )
- {
- ccRGBB t=[map tileAt:ccg((topos.x-16)/32,((topos.y-16)/32)-2)];
- if(t.r==18)
- {
- t.r=19;
- int x=(topos.x -16)/32;
- int y=(topos.y-16)/32-2;
- [map setTile:t at:ccg(x,y)];
- myX=x;
- myY=y;
- //tileInfo = [NSString stringWithFormat:@"#%d#%d#",x,y];
- //[self performSelector:@selector(lastTile:) withObject:tileInfo afterDelay:0.3];
- [self schedule:@selector(lastTile) interval:0.3];
- }
- else if(t.r==22)
- {
- t.r=3;
- int x=(topos.x -16)/32;
- int y=(topos.y-16)/32-2;
- [map setTile:t at:ccg(x,y)];
- }
- }
- return YES;
- }
- }
- -(void)lastTile
- {
- [self emitParticles:[self position]];
- [self unschedule:@selector(lastTile)];
- //NSLog(@"%@",tileInfo);
- //int j=[[(NSArray*)[tileInfo componentsSeparatedByString:@"#"] objectAtIndex:0] intValue];
- //int k=[[(NSArray*)[tileInfo componentsSeparatedByString:@"#"] objectAtIndex:1] intValue];
- int j = myX;
- int k = myY;
- ccRGBB t=[tilemap1 tileAt:ccg(j,k)];
- if(t.r==3)
- {;}
- else
- {
- if([mDir isEqualToString:@"LEFT"])
- {
- if([mPreDir isEqualToString:@"LEFT"] || [mPreDir isEqualToString:@"RIGHT"])
- {
- if(t.r==9)
- {}
- else
- {
- t.r=21;
- }
- }
- else if([mPreDir isEqualToString:@"UP"] || [mPreDir isEqualToString:@"DOWN"])
- {
- if(t.r==4)
- {
- t.r=9;
- }
- //t.r=3;
- }
- [tilemap1 setTile:t at:ccg(j,k)];
- }
- else if([mDir isEqualToString:@"RIGHT"])
- {
- if([mPreDir isEqualToString:@"LEFT"] || [mPreDir isEqualToString:@"RIGHT"])
- {
- if(t.r==8)
- {}
- else
- {
- t.r=21;
- }
- }
- else if([mPreDir isEqualToString:@"UP"] || [mPreDir isEqualToString:@"DOWN"])
- {
- if(t.r==4)
- {
- t.r=8;
- }
- //t.r=3;
- }
- }
- else if([mDir isEqualToString:@"UP"])
- {
- if([mPreDir isEqualToString:@"LEFT"] || [mPreDir isEqualToString:@"RIGHT"])
- {
- if(t.r==21)
- {
- t.r=22;
- }
- else
- {
- //t.r=3;
- }
- //t.r=3;
- }
- else if([mPreDir isEqualToString:@"UP"] || [mPreDir isEqualToString:@"DOWN"])
- {
- //t.r=4;
- //t.r=3;
- if(t.r==22)
- {}
- else
- {
- t.r=4;
- }
- }
- }
- else if( [mDir isEqualToString:@"DOWN"])
- {
- if([mPreDir isEqualToString:@"LEFT"] || [mPreDir isEqualToString:@"RIGHT"])
- {
- if(t.r==21)
- {
- t.r=23;
- }
- //t.r=3;
- }
- else if([mPreDir isEqualToString:@"UP"] || [mPreDir isEqualToString:@"DOWN"])
- {
- if(t.r==23)
- {}
- else
- {
- t.r=4;
- }
- }
- }
- else if([mDir isEqualToString:@""]==YES)
- //t.r=3;
- {
- if([mPreDir isEqualToString:@"LEFT"] || [mPreDir isEqualToString:@"RIGHT"])
- {
- t.r=21;
- }
- else if([mPreDir isEqualToString:@"DOWN"] || [mPreDir isEqualToString:@"UP"])
- {
- t.r=4;
- }
- }
- [tilemap1 setTile:t at:ccg(j,k)];
- }
- }
- -(void)moveLeft
- {
- CGPoint toPoint=CGPointMake(heroPos.x-16 ,heroPos.y);
- //CGPoint goAt=CGPointMake(heroPos.x-16.heroPos.y);
- NSLog(@"%f %f",toPoint.x,toPoint.y);
- if(toPoint.x>=16 && stopMoving==NO && [mDir isEqualToString:@"LEFT"]==YES)
- {
- if([self checkIfBag:toPoint]==NO)
- {
- BOOL canMove=[self canMoveFrom:heroPos to:toPoint inMap:tilemap1];
- if (canMove==YES)
- {
- [self runAction:[MoveTo actionWithDuration:0.4 position:ccp(toPoint.x,toPoint.y)]];
- [self setHeroPos:toPoint];
- // NSLog(@"%f ***** %f ",heroPos.x,heroPos.y);
- [self checkingDiamond:toPoint];
- mPreDir=@"LEFT";
- //[self schedule:@selector(moveLeft) interval:0.5];
- }
- }
- }
- }
- -(void)moveRight
- {
- CGPoint toPoint=CGPointMake(heroPos.x+16 ,heroPos.y);
- //NSLog(@"%f %f",toPoint.x,toPoint.y);
- if(toPoint.x<=304 && stopMoving==NO && [mDir isEqualToString:@"RIGHT"]==YES)
- {
- if([self checkIfBag:toPoint]==NO)
- {
- BOOL canMove=[self canMoveFrom:heroPos to:toPoint inMap:tilemap1];
- if (canMove==YES)
- {
- [self runAction:[MoveTo actionWithDuration:0.4 position:ccp(toPoint.x,toPoint.y)]];
- [self setHeroPos:toPoint];
- //NSLog(@"%f ***** %f ",heroPos.x,heroPos.y);
- [self checkingDiamond:toPoint];
- mPreDir=@"RIGHT";
- //[self schedule:@selector(moveRight) interval:0.5];
- }
- }
- }
- }
- -(void)moveUP
- {
- NSLog(@"In UP");
- CGPoint toPoint=CGPointMake(heroPos.x ,heroPos.y+16);
- if(toPoint.y<=432 && stopMoving==NO && [mDir isEqualToString:@"UP"]==YES)
- {
- if([self checkIfBag:toPoint]==NO)
- {
- BOOL canMove=[self canMoveFrom:heroPos to:toPoint inMap:tilemap1];
- if (canMove==YES)
- {
- [self runAction:[MoveTo actionWithDuration:0.4 position:ccp(toPoint.x,toPoint.y)]];
- [self setHeroPos:toPoint];
- //NSLog(@"%f ***** %f ",heroPos.x,heroPos.y);
- [self checkingDiamond:toPoint];
- mPreDir=@"UP";
- //[self schedule:@selector(moveUP) interval:0.5];
- }
- }
- }
- }
- -(void)moveDown
- {
- CGPoint toPoint=CGPointMake(heroPos.x ,heroPos.y-16);
- if(toPoint.y>=80 && stopMoving==NO && [mDir isEqualToString:@"DOWN"]==YES)
- {
- if([self checkIfBag:toPoint]==NO)
- {
- BOOL canMove=[self canMoveFrom:heroPos to:toPoint inMap:tilemap1];
- if (canMove==YES)
- {
- [self runAction:[MoveTo actionWithDuration:0.4 position:ccp(toPoint.x,toPoint.y)]];
- [self setHeroPos:toPoint];
- //NSLog(@"%f ***** %f ",heroPos.x,heroPos.y);
- [self checkingDiamond:toPoint];
- mPreDir=@"DOWN";
- //[self schedule:@selector(moveDown) interval:0.5];
- }
- }
- }
- }
- -(void)willMoveUp:(TileMapAtlas*)myTileMap
- {
- if([(GameLayer*)[self parent] canTouch])
- {
- [self unschedule:@selector(waitingAnimation)];
- self.mDir=@"UP";
- if([self.mPreDir isEqualToString:@"UP"]==NO)
- {
- //self.mPreDir = @"UP";
- if ([[NSNumber numberWithFloat: self.heroPos.x] intValue ]% 32!=0 && [[NSNumber numberWithFloat: self.heroPos.y] intValue ]% 32!=0)
- {
- //int e = (self.heroPos.x-16)/32;
- //int f = (self.heroPos.y-16)/32-2;
- //ccRGBB s=[tilemap1 tileAt:ccg(e,f)];
- //s.r=3;
- //[tilemap1 setTile:s at:ccg(e,f)];
- }
- //self.stopMoving=NO;
- [self turnLeftAnimation:0.5];
- //[self schedule:@selector(animateAnt) interval:0.4];
- [self schedule:@selector(moveUP) interval:0.4];
- }
- //[self setRotation:90];
- self.stopMoving=NO;
- //self.mPreDir=@"UP";
- }
- }
- -(void)willMoveDown:(TileMapAtlas*)myTileMap
- {
- if([(GameLayer*)[self parent] canTouch])
- {
- [self unschedule:@selector(waitingAnimation)];
- self.mDir=@"DOWN";
- if([self.mPreDir isEqualToString:@"DOWN"]==NO)
- {
- //self.mPreDir = @"DOWN";
- if ([[NSNumber numberWithFloat: self.heroPos.x] intValue ]% 32!=0 && [[NSNumber numberWithFloat: self.heroPos.y] intValue ]% 32!=0)
- {
- /* int e = (self.heroPos.x-16)/32;
- int f = (self.heroPos.y-16)/32-2;
- ccRGBB s=[tilemap1 tileAt:ccg(e,f)];
- s.r=3;
- [tilemap1 setTile:s at:ccg(e,f)];
- */
- }
- [self setRotation:90];
- [self turnRightAnimation:0.5];
- //[self schedule:@selector(animateAnt) interval:0.4];
- [self schedule:@selector(moveDown) interval:0.4];
- }
- self.stopMoving=NO;
- }
- }
- -(void)willMoveRight:(TileMapAtlas*)myTileMap
- {
- if([(GameLayer*)[self parent] canTouch])
- {
- [self unschedule:@selector(waitingAnimation)];
- self.mDir=@"RIGHT";
- if([self.mPreDir isEqualToString:@"RIGHT"]==NO)
- {
- //self.mPreDir = @"RIGHT";
- if ([[NSNumber numberWithFloat: self.heroPos.x] intValue ]% 32!=0 && [[NSNumber numberWithFloat: self.heroPos.y] intValue ]% 32!=0)
- {
- /*int e = (self.heroPos.x-16)/32;
- int f = (self.heroPos.y-16)/32-2;
- ccRGBB s=[tilemap1 tileAt:ccg(e,f)];
- s.r=3;
- [tilemap1 setTile:s at:ccg(e,f)];
- */
- }
- //
- //self.mPreDir=@"RIGHT";
- [self turnRightAnimation:0.5];
- //[self schedule:@selector(animateAnt) interval:0.4];
- [self schedule:@selector(moveRight) interval:0.4];
- }
- self.stopMoving=NO;
- }
- }
- -(void)willMoveLeft:(TileMapAtlas*)myTileMap
- {
- if([(GameLayer*)[self parent] canTouch])
- {
- [self unschedule:@selector(waitingAnimation)];
- self.mDir=@"LEFT";
- if([self.mPreDir isEqualToString:@"LEFT"]==NO)
- {
- //self.mPreDir = @"LEFT";
- if ([[NSNumber numberWithFloat: self.heroPos.x] intValue ]% 32!=0 && [[NSNumber numberWithFloat: self.heroPos.y] intValue ]% 32!=0)
- {
- /*int e = (self.heroPos.x-16)/32;
- int f = (self.heroPos.y-16)/32-2;
- ccRGBB s=[tilemap1 tileAt:ccg(e,f)];
- s.r=3;
- [tilemap1 setTile:s at:ccg(e,f)];
- */
- }
- [self setRotation:0];
- //self.stopMoving=NO;
- [self turnLeftAnimation:0.5];
- //[self schedule:@selector(animateAnt) interval:0.4];
- [self schedule:@selector(moveLeft) interval:0.4];
- }
- self.stopMoving=NO;
- }
- }
- //Mehod in a scheduler for checking bag fall.
- -(void)checkBagFall
- {
- for(int i=0;i<bagCount;i++)
- {
- bag=(Bagbomb*)[[self parent] getChildByTag:200+i];
- if(bag!=nil)
- {
- if(roundf(bag.bagPos.y) != 80)
- {
- posx=roundf(bag.bagPos.x);
- posy=roundf(bag.bagPos.y)-32.0f;
- float p=(posx -16)/32;
- float q=(posy-16)/32-2 ;
- ccRGBB black=[tilemap1 tileAt:ccg(p,q)];
- if (black.r==3 || black.r==4 || black.r==21 || black.r==23 || black.r==22 || black.r==9 || black.r==8|| black.r==11|| black.r==12|| black.r==24||black.r==25||black.r==3)
- {
- [self unschedule:@selector(checkBagFall)];
- NSLog(@"Bag tag %d",[bag tag]);
- [self performSelector:@selector(bagMovement) withObject:bag afterDelay:1.5];
- break;
- }
- }
- }
- }
- }
- -(void)bagMovement
- {
- if(roundf(bag.bagPos.y) != 48)
- {
- NSLog(@"Bag Position %f %f",bag.bagPos.x,bag.bagPos.y);
- posx=roundf(bag.bagPos.x);
- posy=roundf(bag.bagPos.y)-32.0f;
- int p=(posx-16)/32;
- int q=(posy-16)/32-2;
- ccRGBB black=[tilemap1 tileAt:ccg(p,q)];
- if (black.r==3 || black.r==4 || black.r==21 || black.r==23 || black.r==22 || black.r==9 || black.r==8|| black.r==11|| black.r==12|| black.r==24||black.r==25 || black.r==3)
- {
- [self schedule:@selector(bagMovement) interval:0.2];
- [bag runAction:[MoveTo actionWithDuration:0.5 position:ccp(posx,posy)]];
- [bag setBagPos:ccp(posx,posy)];
- int j=(posx -16)/32;
- int k=(posy-16)/32-2;
- if(black.r==21)
- {
- ccRGBB t;
- t.r=22;
- [tilemap1 setTile:t at:ccg(j,k)];
- NSString *str=[[NSString alloc] initWithFormat:@"%d#%d#",j,k];
- [self performSelector:@selector(bagTileRemove:) withObject:str afterDelay:0.1];
- [str release];
- }
- if(black.r==11 || black.r==24)
- {
- ccRGBB t;
- t.r=8;
- [tilemap1 setTile:t at:ccg(j,k)];
- }
- if(black.r==12 || black.r==25)
- {
- ccRGBB t;
- t.r=9;
- [tilemap1 setTile:t at:ccg(j,k)];
- }
- //[bag setBagPos:ccp(posx,posy)];
- CGRect bagFrame = CGRectMake(roundf([bag bagPos].x - 16+6), roundf([bag bagPos].y - 16+6),25,25);
- NSLog(@"bag frame %f %f",[bag bagPos].x,[bag bagPos].y);
- for(int i=0;i<enemyCount ;i++)
- {
- Enemy *c = (Enemy*)[[self parent] getChildByTag:(300+i)];
- CGRect enemyFrame = CGRectMake(roundf([c position].x - 16+6), roundf([c position].y - 16+6),25,25);
- NSLog(@"enemy frame %f %f",enemyFrame.origin.x,enemyFrame.origin.y);
- if(CGRectIntersectsRect(enemyFrame, bagFrame)==YES)
- {
- [c setReady:YES];
- //[c setTransformAnchor:ccp([c transformAnchor].x,[c transformAnchor].y-)];
- [c stopAllActions];
- /*if([c rotation]==90 || [c rotation]==-90)
- {
- [c setTransformAnchor:ccp([c transformAnchor].x+30,[c transformAnchor].y)];
- [c runAction:[ScaleTo actionWithDuration:2 scaleX:0 scaleY:[c scaleY]+1]];
- }
- else
- {
- [c setTransformAnchor:ccp([c transformAnchor].x,[c transformAnchor].y+15)];
- [c runAction:[ScaleTo actionWithDuration:2 scaleX:[c scaleX]+1 scaleY:0]];
- }*/
- // [c runAction:[ScaleBy actionWithDuration:2 scaleX:0 scaleY:[c scaleY]+3]];
- // [c runAction:[FadeTo actionWithDuration:1.5 opacity:0]];
- // [(Enem)]
- [c runAction:[Spawn actions:[JumpTo actionWithDuration:1.5 position:[c position] height:50 jumps:1],[RotateBy actionWithDuration:1.5 angle:360],[ScaleTo actionWithDuration:1.5 scale:0],nil]];
- [self performSelector:@selector(removeEnemy:) withObject:c afterDelay:1.3];
- heroScore=heroScore+150;
- //[[c parent] removeChildByTag:[c tag] cleanup:YES];
- }
- Digger *c1 = (Digger*)[[self parent] getChildByTag:007];
- CGRect heroFrame = CGRectMake(roundf([c1 position].x - 16+6), roundf([c1 position].y - 16+6),25,25);
- if(!bCollide)
- {
- if(CGRectIntersectsRect(heroFrame, bagFrame)==YES)
- {
- AudioServicesPlaySystemSound(soundID2);
- bCollide=YES;
- [(GameLayer*)[self parent] setCanTouch:NO];
- [self unschedule:@selector(checkingCoins)];
- //[c stopAllActions];
- //[c1 stopAllActions];
- stopMoving=YES;
- [self unschedule:@selector(animateAnt)];
- [self unschedule:@selector(waitingAnimation)];
- [Digger cancelPreviousPerformRequestsWithTarget:self selector:@selector(waitingAnimation) object:nil];
- [self stopAllActions];
- [self heroDeathAnimation];
- //[(GameLayer*)[self parent] setCanTouch:YES];
- stopMoving=YES;
- mPreDir=@"";
- [self performSelector:@selector(heroDied) withObject:nil afterDelay:1.7 ];
- // [self heroDied];
- }
- }
- }
- }
- else
- {
- [self performSelector:@selector(bagDrop:) withObject:bag afterDelay:0.6];
- [self unschedule:@selector(bagMovement)];
- //[self schedule:@selector(checkBagFall) interval:0.2];
- }
- }
- else
- {
- [self performSelector:@selector(bagDrop:) withObject:bag afterDelay:0.6];
- [self unschedule:@selector(bagMovement)];
- //[self schedule:@selector(checkBagFall) interval:0.2];
- }
- }
- -(void)removeEnemy:(Enemy*)c
- {
- [[c parent] removeChildByTag:[c tag] cleanup:YES];
- }
- -(void)bagTileRemove:(NSString*)pos
- {
- int xx=[[[pos componentsSeparatedByString:@"#"] objectAtIndex:0] intValue];
- int yy=[[[pos componentsSeparatedByString:@"#"] objectAtIndex:1] intValue];
- ccRGBB s=[tilemap1 tileAt:ccg(xx,yy)];
- if(s.r==22)
- {
- s.r=3;
- [tilemap1 setTile:s at:ccg(xx,yy)];
- }
- }
- //After the bag is droped, a coin is generated, which can be taken by digger or enemy.
- -(void)bagDrop:(Bagbomb*)bag1
- {
- NSLog(@"bag drop %d",[bag1 tag]);
- coinCount++;
- Coins *coin = [Coins spriteWithFile:@"money-t5.png"];
- [coin setCoinPos:ccp([bag1 bagPos].x,[bag1 bagPos].y)];
- [coin setPosition:ccp([bag1 bagPos].x,[bag1 bagPos].y)];
- //[coin setScale:0.4];
- [coin setIsUsed:YES];
- [[bag1 parent] removeChildByTag:[bag1 tag] cleanup:YES];
- [[self parent] addChild:coin z:0 tag:500 + coinCount];
- [coin animateCoins];
- //[coin schedule:@selector(animateCoins) interval:1.6];
- [self schedule:@selector(checkBagFall) interval:0.2];
- }
- //In a scheduler for checking coins.
- -(void)checkingCoins
- {
- for(int h=1;h <= coinCount;h++)
- {
- Coins *myCoin = (Coins*)[[self parent] getChildByTag:(500+h)];
- if(myCoin!=nil)
- {
- //NSLog(@"Coin position %f %f",[myCoin coinPos].x,[myCoin coinPos].y);
- CGRect coinFrame = CGRectMake([myCoin coinPos].x - 16,[myCoin coinPos].y - 16,20,20);
- CGRect heroFrame = CGRectMake(heroPos.x-16, heroPos.y-16,20,20);
- if(CGRectIntersectsRect(heroFrame,coinFrame)==YES && bCollide==NO)// && [myCoin isUsed]==YES)
- {
- AudioServicesPlaySystemSound(soundID1);
- //[myCoin setIsUsed:NO];
- [(Coins*)myCoin flashCoins];
- heroScore=heroScore+100;
- Label *fs = (Label*)[[self parent] getChildByTag:79];
- [fs setString:[NSString stringWithFormat:@"%d",100]];
- [fs runAction:[FadeIn actionWithDuration:0.05]];
- [fs setPosition:CGPointMake([myCoin coinPos].x,[myCoin coinPos].y)];
- [fs runAction:[Sequence actions:[MoveBy actionWithDuration:0.4 position:ccp(0,15)],[FadeOut actionWithDuration:0.05],nil]];
- //[[self parent] removeChildByTag:[myCoin tag] cleanup:YES];
- }
- for(int i=0;i<enemyCount ;i++)
- {
- Enemy *c = (Enemy*)[[self parent] getChildByTag:(300+i)];
- if(c!=nil)
- {
- CGRect enemyFrame = CGRectMake(roundf([c position].x-16), roundf([c position].y-16),20,20);
- if(CGRectIntersectsRect(enemyFrame, coinFrame)==YES)
- {
- [[self parent] removeChildByTag:[myCoin tag] cleanup:YES];
- }
- }
- }
- }
- }
- }
- //In a scheduler for checking diamonds.
- -(void)checkingDiamond:(CGPoint)toPoint
- {
- for(int i=0;i<=gemCount ;i++)
- {
- CocosNode *c = [[self parent] getChildByTag:(100+i)];
- if(toPoint.x == roundf([c position].x) && toPoint.y == roundf([c position].y))
- {
- [(Diamond*)c flashGem];
- AudioServicesPlaySystemSound(soundID1);
- heroScore=heroScore+100;
- //nod=number of diamonds
- nod=nod+1;
- gemTaken=gemTaken+1;
- [(Label*)[[self parent] getChildByTag:75] setString:[NSString stringWithFormat:@"%d",heroScore]];
- [(Label*)[[self parent] getChildByTag:81] setString:[NSString stringWithFormat:@"%d",nod]];
- id myAction = [Sequence actions:[MoveBy actionWithDuration:0.4 position:ccp(0,15)],[FadeOut actionWithDuration:0.05],nil];
- if(nod==gemCount)
- {
- NSLog(@"Gems complete");
- [self stopAllActions];
- [self levelCompletion];
- }
- //[backgroundMusic stopIt];
- //[diamondTaking playIt];
- //[backgroundMusic playIt];
- //[[self parent] removeChildByTag:(100+i) cleanup:YES];
- //fading score
- if(!flag)
- {
- Label *fs = (Label*)[[self parent] getChildByTag:79];
- [fs setString:[NSString stringWithFormat:@"%d",100]];
- [fs runAction:[FadeIn actionWithDuration:0.05]];
- [fs setPosition:CGPointMake([c position].x,[c position].y)];
- [fs runAction:myAction];
- flag=YES;
- }
- else
- {
- Label *fs1 = (Label*)[[self parent] getChildByTag:80];
- [fs1 setString:[NSString stringWithFormat:@"%d",100]];
- [fs1 runAction:[FadeIn actionWithDuration:0.05]];
- [fs1 setPosition:CGPointMake([c position].x,[c position].y)];
- [fs1 runAction:myAction];
- flag=NO;
- }
- }
- }
- //[levelData release];
- }
- -(void)levelCompletion
- {
- //[self unschedule:@selector(levelCompletion)];
- [self unschedule:@selector(checkingCollision)];
- //[self stopAllActions];
- [backgroundMusic stopIt];
- [backgroundMusicWithCarrot stopIt];
- UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:@"Level Finished" message:@"Congratulation" delegate:self cancelButtonTitle:@"Next Level" otherButtonTitles:@"Quit",nil];
- [myAlert performSelector:@selector(show) withObject:nil afterDelay:0.5];
- }
- -(void)didPresentAlertView:(UIAlertView *)alertView
- {
- [[Director sharedDirector] pause];
- }
- -(BOOL)checkIfBag:(CGPoint)toPoint
- {
- for(int i=0;i<=bagCount;i++)
- {
- CocosNode *c = [[self parent] getChildByTag:(200+i)];
- if(toPoint.x == roundf([c position].x) && toPoint.y == roundf([c position].y))
- {
- return YES;
- }
- }
- return NO;
- }
- -(void)soundInitialization
- {
- /* NSString *path1=[[NSBundle mainBundle] pathForResource:@"Bell_07" ofType:@"mp3"];
- AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path1], &soundID1);
- NSString *path2=[[NSBundle mainBundle] pathForResource:@"Bell_09" ofType:@"mp3"];
- AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path2], &soundID2);
- */
- }
- -(void)heroDied
- {
- [(GameLayer*)[self parent] setCanTouch:NO];
- //[heroDeadSound playIt];
- if(heroLife==0 && !flag5)
- {
- //[audioPlayer stop];
- [backgroundMusic stopIt];
- flag5=YES;
- int minHighScore1;
- //code for checking high score
- NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- NSString *documentsDirectory = [paths objectAtIndex:0];
- NSString *writablePath = [documentsDirectory stringByAppendingPathComponent:@"HighScores.dat"];
- NSLog(@"%@",writablePath);
- NSData *patternData=[NSData dataWithContentsOfFile:writablePath];
- if(patternData == nil)
- {
- NSMutableArray *patternArray = [[NSMutableArray alloc] init];
- NSDictionary *score1 = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"Player 1",[NSNumber numberWithInt:0],nil] forKeys:[NSArray arrayWithObjects:@"Name",@"Score",nil]];
- NSDictionary *score2 = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"Player 2",[NSNumber numberWithInt:0],nil] forKeys:[NSArray arrayWithObjects:@"Name",@"Score",nil]];
- NSDictionary *score3 = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"Player 3",[NSNumber numberWithInt:0],nil] forKeys:[NSArray arrayWithObjects:@"Name",@"Score",nil]];
- [patternArray addObject:score1];
- [patternArray addObject:score2];
- [patternArray addObject:score3];
- minHighScore1 = [[[patternArray objectAtIndex:2] valueForKey:@"Score"] intValue];
- [score1 release];
- [score2 release];
- [score3 release];
- NSData *patternData1 = [NSKeyedArchiver archivedDataWithRootObject:patternArray];
- [patternData1 writeToFile:writablePath atomically:YES];
- [patternArray release];
- [patternData release];
- }
- else
- {
- NSMutableArray *patternArray = [[NSMutableArray arrayWithArray:[NSKeyedUnarchiver unarchiveObjectWithData:patternData]] retain];
- minHighScore1 = [[[patternArray objectAtIndex:2] valueForKey:@"Score"] intValue];
- }
- //[hs setHighScore:score];
- //[hs displayHighScores];
- NSLog(@"heroScore %d",heroScore);
- [self setHeroScore:heroScore];
- if(heroScore>minHighScore1)
- {
- NSLog(@"hero score %d",heroScore);
- PlayerName *pn = [PlayerName node];
- [[self parent] addChild:pn z:2 tag:90];
- [[Director sharedDirector] pause];
- }
- //[[Director sharedDirector] replaceScene:hs];
- else
- {
- UIAlertView *myAlert1 = [[UIAlertView alloc] initWithTitle:@"Game Over" message:@"You are dead" delegate:self cancelButtonTitle:@"Play Again" otherButtonTitles:@"Quit",nil];
- [[Director sharedDirector] pause];
- [myAlert1 show];
- }
- }
- if(heroLife > 0)
- {
- heroLife--;
- [self setHeroPos:CGPointMake(144,80)];
- [self setPosition:ccp(144,80)];
- [self setRotation:0.0f];
- [self waitingAnimation];
- [self runAction:[Blink actionWithDuration:1 blinks:3]];
- [[[self parent] getChildByTag:(76+heroLife)] setVisible:NO];
- [(GameLayer*)[self parent] setCanTouch:YES];
- [self schedule:@selector(checkingCoins) interval:0.5];
- [(GameLayer*)[self parent] enemyMovementAfterHeroDeath];
- }
- }
- -(void)checkingCollision
- {
- if(!bCollide){
- for(int i=0;i<enemyCount ;i++)
- {
- CGRect heroFrame = CGRectMake([[[self parent] getChildByTag:007] position].x-16+3, [[[self parent] getChildByTag:007] position].y-16+3,26,26);
- //CGRect heroFrame = CGRectMake(heroPos.x - 16 + 3,heroPos.y)
- Enemy *c = (Enemy*)[[self parent] getChildByTag:(300+i)];
- //Here ready is opposite
- if(c!=nil && c.ready==NO)
- {
- CGRect enemyFrame;
- if([c isFiring]==YES)
- {
- NSLog(@"Enemy is firing");
- if([c rotation]==0)
- {
- enemyFrame = CGRectMake(roundf([c position].x-50), roundf([c position].y-16),100,20);
- }
- else
- {
- enemyFrame = CGRectMake(roundf([c position].x-16), roundf([c position].y-50),20,100);
- }
- }
- else
- {
- if([c rotation]==0)
- {
- enemyFrame = CGRectMake(roundf([c position].x-30+6), roundf([c position].y-16+6),48,20);
- }else
- {
- enemyFrame = CGRectMake(roundf([c position].x-16+6), roundf([c position].y-30+6),20,48);
- }
- }
- if(CGRectIntersectsRect(enemyFrame, heroFrame)==YES)
- {
- if([(Digger*)[[self parent] getChildByTag:007] hasCarrot]==NO)
- {
- AudioServicesPlaySystemSound(soundID2);
- bCollide=YES;
- [(GameLayer*)[self parent] setCanTouch:NO];
- [c stopAllActions];
- [c unschedule:@selector(enemyMovement)];
- [self stopAllActions];
- stopMoving=YES;
- [self unschedule:@selector(animateAnt)];
- [self unschedule:@selector(waitingAnimation)];
- [Digger cancelPreviousPerformRequestsWithTarget:self selector:@selector(waitingAnimation) object:nil];
- [self stopAllActions];
- if([c type] == 1)
- {
- [c setRotation:0];
- c.texture = [[TextureMgr sharedTextureMgr] addImage:@"monster1_waiting-1.png"];
- }
- else
- {
- [c setRotation:0];
- c.texture = [[TextureMgr sharedTextureMgr] addImage:@"monster2_rotation-1.png"];
- }
- [self heroDeathAnimation];
- stopMoving=YES;
- //mPreDir=@"";
- [self setMPreDir:@""];
- //[(GameLayer*)[self parent] setCanTouch:YES];
- [self performSelector:@selector(heroDied) withObject:nil afterDelay:1.7];
- }
- else
- {
- [c setReady:YES];
- heroScore=heroScore+150;
- [[c parent] removeChildByTag:[c tag] cleanup:YES];
- }
- }
- }
- }
- }
- }
- - (void)alertView:(UIAlertView *)action clickedButtonAtIndex:(NSInteger)buttonIndex
- {
- if([[action buttonTitleAtIndex:buttonIndex] isEqualToString:@"Next Level"]==YES)
- {
- [[NSUserDefaults standardUserDefaults] setValue:[NSNumber numberWithInt:[[[NSUserDefaults standardUserDefaults] valueForKey:@"current_stage"] intValue] +1] forKey:@"current_stage"];
- if([[[NSUserDefaults standardUserDefaults] valueForKey:@"stage"] intValue]<[[[NSUserDefaults standardUserDefaults] valueForKey:@"current_stage"] intValue])
- {
- [[NSUserDefaults standardUserDefaults] setValue:[NSNumber numberWithInt:[[[NSUserDefaults standardUserDefaults] valueForKey:@"stage"] intValue] +1] forKey:@"stage"];
- }
- [[NSUserDefaults standardUserDefaults] setValue:[NSNumber numberWithInt:heroScore] forKey:@"score"];
- [[NSUserDefaults standardUserDefaults] setValue:[NSNumber numberWithInt:heroLife] forKey:@"life"];
- //NSLog(@"alert view stage == %d",[[[NSUserDefaults standardUserDefaults] valueForKey:@"stage"]intValue]);
- GameScene *gs=[GameScene node];
- [[Director sharedDirector] resume];
- [[Director sharedDirector] replaceScene:gs];
- }
- if([[action buttonTitleAtIndex:buttonIndex] isEqualToString:@"Play Again"]==YES)
- {
- GameScene *gs=[GameScene node];
- [[Director sharedDirector] resume];
- [[Director sharedDirector] replaceScene:gs];
- }
- if(buttonIndex==1)
- {
- /*MenuScene *ms=[MenuScene node];
- [[Director sharedDirector] resume];
- [[Director sharedDirector] replaceScene:ms];
- */
- if([[action message] isEqualToString:@"Congratulation"])
- {
- [[NSUserDefaults standardUserDefaults] setValue:[NSNumber numberWithInt:[[[NSUserDefaults standardUserDefaults] valueForKey:@"stage"] intValue] +1] forKey:@"stage"];
- }
- [[Director sharedDirector] resume];
- NSLog(@"Hero score %d",heroScore);
- NSDictionary *score = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"Prateek",[NSNumber numberWithInt:heroScore],nil] forKeys:[NSArray arrayWithObjects:@"Name",@"Score",nil]];
- HighScores *hs= [HighScores node];
- [hs setHighScore:score];
- [hs displayHighScores];
- [[Director sharedDirector] replaceScene:hs];
- }
- }
- -(void)checkingCarrots
- {
- for(int h=0;h < carrotCount;h++)
- {
- Carrot *myCarrot = (Carrot*)[[self parent] getChildByTag:600+h];
- CGRect carrotFrame = CGRectMake([myCarrot carrotPos].x,[myCarrot carrotPos].y,20,20);
- CGRect heroFrame = CGRectMake(heroPos.x, heroPos.y,20,20);
- if(CGRectIntersectsRect(heroFrame,carrotFrame)==YES && !entered)
- {
- //id carrotAction=[Blink actionWithDuration:15 blinks:40];
- [(Digger*)[[self parent] getChildByTag:007] setHasCarrot:YES];
- [[self parent] removeChildByTag:600+h cleanup:YES];
- //[[[self parent] getChildByTag:007] runAction:carrotAction];
- [backgroundMusic stopIt];
- //backgroundMusicWithCarrot = [[Sound alloc] initWithSound:@"Dance_04" ofType:@"mp3"];
- //[backgroundMusicWithCarrot setRepeat:YES];
- [backgroundMusicWithCarrot playIt];
- [self schedule:@selector(powerOver) interval:10];
- for(int i=0;i<enemyCount ;i++)
- {
- Enemy *c = (Enemy*)[[self parent] getChildByTag:(300+i)];
- if(c!=nil)
- {
- [c setR:[c r]+30];
- }
- }
- }
- }
- }
- -(void)powerOver
- {
- [self unschedule:@selector(powerOver)];
- [backgroundMusicWithCarrot stopIt];
- [backgroundMusic playIt];
- [(Digger*)[[self parent] getChildByTag:007] setHasCarrot:NO];
- for(int i=0;i<enemyCount ;i++)
- {
- Enemy *c = (Enemy*)[[self parent] getChildByTag:(300+i)];
- if(c!=nil)
- {
- [c setR:[c r]-30];
- }
- }
- }
- -(void)animateAnt
- {
- id action1;
- if([[self mDir] isEqualToString:@"LEFT"]==YES)
- {
- if(heroPos.x > 32)
- {
- [self turnLeftAnimation:1];
- //[self unschedule:@selector(waitingAnimation)];
- //[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(waitingAnimation) object:nil];
- [self setRotation:0];
- id animation = [Animation animationWithName:@"antAnimate" delay:0.15];
- for( int i=1;i<=10;i++)
- {
- [animation addFrame: [NSString stringWithFormat:@"rabbit-go_left%d.png", i]];
- }
- action1 = [Animate actionWithAnimation: animation];
- [self runAction:action1];
- //[self runAction:[Sequence]]
- }
- }
- else if([[self mDir] isEqualToString:@"RIGHT"]==YES)
- {
- if(heroPos.x < 288)
- {
- [self unschedule:@selector(waitingAnimation)];
- //[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(waitingAnimation) object:nil];
- [self setRotation:0];
- id animation1 = [Animation animationWithName:@"antAnimate1" delay:0.15];
- for( int i=1;i<=10;i++)
- {
- [animation1 addFrame: [NSString stringWithFormat:@"rabbit-go_right%d.png", i]];
- }
- action1 = [Animate actionWithAnimation: animation1];
- [self runAction:action1];
- }
- }
- else if([[self mDir] isEqualToString:@"DOWN"]==YES )
- {
- if(heroPos.y > 80)
- {
- [self unschedule:@selector(waitingAnimation)];
- //[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(waitingAnimation) object:nil];
- [self setRotation:90];
- id animation1 = [Animation animationWithName:@"antAnimate1" delay:0.15];
- for( int i=1;i<=10;i++)
- {
- [animation1 addFrame: [NSString stringWithFormat:@"rabbit-go_right%d.png", i]];
- }
- action1 = [Animate actionWithAnimation: animation1];
- [self runAction:action1];
- }
- }
- else if([[self mDir] isEqualToString:@"UP"]==YES)
- {
- if(heroPos.y < 432)
- {
- [self unschedule:@selector(waitingAnimation)];
- //[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(waitingAnimation) object:nil];
- [self setRotation:90];
- id animation1 = [Animation animationWithName:@"antAnimate1" delay:0.15];
- for( int i=1;i<=10;i++)
- {
- [animation1 addFrame: [NSString stringWithFormat:@"rabbit-go_left%d.png", i]];
- }
- action1 = [Animate actionWithAnimation: animation1];
- [self runAction:action1];
- }
- //}
- }
- else if([[self mDir] isEqualToString:@""]==YES)
- {
- [self unschedule:@selector(waitingAnimation)];
- //[action1 step:1];
- //[self stopAction:action1];
- //[self stopAllActions];
- //[self setRotation:0];
- //[self performSelector:@selector(waitingAnimation) withObject:nil afterDelay:1];
- /*id animation = [Animation animationWithName:@"antAnimate" delay:0.1];
- for( int i=1;i<=10;i++)
- {
- [animation addFrame: [NSString stringWithFormat:@"rabbit-go_left%d.png", i]];
- }
- id action = [Animate actionWithAnimation: animation];
- [self runAction:action];
- */
- }
- }
- -(void) heroDeathAnimation
- {
- [self setRotation:0];
- id animation = [Animation animationWithName:@"deathAnimate" delay:0.2];
- for( int i=1;i<=9;i++)
- {
- [animation addFrame: [NSString stringWithFormat:@"rabbit-death%d.png", i]];
- }
- // id action = [Animate actionWithAnimation: animation];
- // bCollide=NO;
- // [self runAction:[Sequence actions:action,[Blink actionWithDuration:0.1 blinks:1],nil]];
- [self runAction:[Animate actionWithAnimation: animation]];
- }
- -(void)waitingAnimation
- {
- id animation = [Animation animationWithName:@"antAnimate" delay:0.2];
- for( int i=1;i<=4;i++)
- {
- [animation addFrame: [NSString stringWithFormat:@"rabbit-wait%d.png", i]];
- }
- id action = [Animate actionWithAnimation: animation];
- [self runAction:action];
- [self schedule:@selector(waitingAnimation) interval:0.7];
- }
- -(void)turnLeftAnimation:(float)duration
- {
- [self unschedule:@selector(animateAnt)];
- id turnLeft = [Animation animationWithName:@"turnLeft" delay:duration/8];
- for(int i = 1 ;i<=8; i++)
- {
- [turnLeft addFrame:[NSString stringWithFormat:@"rabbit-turn_left%d.png",i]];
- }
- [self runAction:[Animate actionWithAnimation:turnLeft]];
- [self schedule:@selector(animateAnt) interval:0.4];
- }
- -(void)turnRightAnimation:(float)duration
- {
- id turnRight = [Animation animationWithName:@"turnRight" delay:duration/7];
- for(int i = 1; i<=7; i++)
- {
- [turnRight addFrame:[NSString stringWithFormat:@"rabbit-turn_right%d.png",i]];
- }
- [self runAction:[Animate actionWithAnimation:turnRight]];
- [self schedule:@selector(animateAnt) interval:0.4];
- }
- -(void)emitParticles:(CGPoint)point
- {
- ParticleFireworks *emitter = [ParticleFireworks node];
- emitter.angle=0;
- emitter.totalParticles =15;
- [emitter setDuration:0.5];
- [emitter setAngleVar:20];
- [emitter setRadialAccel:10];
- [emitter setSpeed:15];
- [emitter setSpeedVar:10];
- [emitter setGravity:ccp(0,-90)];
- [emitter setSize:5];
- [emitter setLife:0.5];
- [emitter setPosition:point];
- [[self parent] addChild:emitter z:3];
- }
- @end
- @implementation PlayerName
- @synthesize textField;
- -(id)init
- {
- self=[super init];
- if(self != nil)
- {
- /*Sprite *background =[Sprite spriteWithFile:@"Black_Granite_Tile_320.png"];
- [background setPosition:ccp(160,240)];
- [self addChild:background];
- */
- isTouchEnabled=YES;
- Label *congs = [Label labelWithString:@"Congratulations" dimensions:CGSizeMake(250, 50) alignment:UITextAlignmentCenter fontName:@"Helvetica" fontSize:35];
- Label *mess = [Label labelWithString:@"You create the high score" dimensions:CGSizeMake(300, 50) alignment:UITextAlignmentCenter fontName:@"Helvetica" fontSize:20];
- Label *en = [Label labelWithString:@"Enter your name" dimensions:CGSizeMake(250, 50) alignment:UITextAlignmentCenter fontName:@"Helvetica" fontSize:20];
- Label *submit = [Label labelWithString:@"Subsmit Scores" dimensions:CGSizeMake(250, 50) alignment:UITextAlignmentCenter fontName:@"Helvetica" fontSize:30];
- Label *yes = [Label labelWithString:@"YES" dimensions:CGSizeMake(150, 50) alignment:UITextAlignmentCenter fontName:@"Helvetica" fontSize:30];
- Label *no = [Label labelWithString:@"NO" dimensions:CGSizeMake(150, 50) alignment:UITextAlignmentCenter fontName:@"Helvetica" fontSize:30];
- [congs setPosition:ccp(160,380)];
- [congs setRGB:0 :255 :0];
- [mess setPosition:ccp(160,320)];
- [mess setRGB:255 :0 :0];
- [en setPosition:ccp(160,280)];
- [en setRGB:255 :0 :0];
- [submit setPosition:ccp(160,120)];
- [submit setRGB:255 :0 :0];
- [yes setPosition:ccp(130,70)];
- [yes setRGB:255 :0 :0];
- [no setPosition:ccp(210,70)];
- [no setRGB:255 :0 :0];
- [self addChild:congs];
- [self addChild:mess];
- [self addChild:en];
- [self addChild:submit];
- [self addChild:yes];
- [self addChild:no];
- // Putting text field
- textField=[[UITextField alloc] initWithFrame:CGRectMake(80,220,150,30)];
- [[[Director sharedDirector] openGLView] addSubview:textField];
- [[[Director sharedDirector] openGLView] bringSubviewToFront:textField];
- [textField becomeFirstResponder];
- [textField setDelegate:self];
- [textField setBorderStyle:UITextBorderStyleRoundedRect];
- }
- return self;
- }
- - (BOOL)textFieldShouldReturn:(UITextField *)textField1 {
- [textField1 resignFirstResponder];
- return NO;
- }
- - (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
- {
- UITouch *touch = [touches anyObject];
- CGPoint gestureStartPoint1 = [touch locationInView:[touch view]];
- NSLog(@"gestuere1 %f %f",gestureStartPoint1.x,gestureStartPoint1.y);
- if(gestureStartPoint1.x >= 106 && gestureStartPoint1.y >= 391 && gestureStartPoint1.x <= 160 && gestureStartPoint1.y <= 413 )
- {
- NSDictionary *score1 = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:[textField text],[NSNumber numberWithInt:[(Digger*)[(GameLayer*)[self parent] getChildByTag:007] heroScore]],nil] forKeys:[NSArray arrayWithObjects:@"Name",@"Score",nil]];
- HighScores *hs = [HighScores node];
- [hs setHighScore:score1];
- [hs displayHighScores];
- [textField removeFromSuperview];
- [[Director sharedDirector] replaceScene:hs];
- }
- else if(gestureStartPoint1.x >= 190 && gestureStartPoint1.y >= 391 && gestureStartPoint1.x <= 230 && gestureStartPoint1.y <= 413 )
- {
- HighScores *hs = [HighScores node];
- [hs displayHighScores];
- [textField removeFromSuperview];
- [[Director sharedDirector] replaceScene:hs];
- }
- return;
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement