www.pudn.com > Simple-shooting-game.zip > GameScene.m, change:2012-02-07,size:13261b


//
//  GameScene.m
//  Cocos2DGame
//
//  Created by iii on 12/31/11.
//  Copyright (c) 2011 千锋3G www.mobiletrain.org. All rights reserved.
//

#import "GameScene.h"
#import "StartScene.h"
#define kill_x 23
#define kill_y 40

@implementation GameScene
@synthesize player = _player;
@synthesize targetArray = _targetArray;
@synthesize bulletArray = _bulletArray;
@synthesize killMi;
@synthesize lostMi;
@synthesize killAllcount;

static int nextGame = 0;
static float i = 0;
static int censorship = 1;

+(id)scene{
    CCScene * sc = [CCScene node];
    GameScene *ss = [GameScene node];
    [sc addChild:ss];
    return sc;
}

+(id)node{
    return [[[self alloc] init] autorelease];
}


-(id)init{
    
    if ((self = [super init])) {
        self.isTouchEnabled = YES;
        [self startGame];
    }

 
    
    return self;
}

-(void)startGame{
    [self unscheduleAllSelectors];
    flag = 0;
    killCounts = 0;
    lostCounts = 0;
    killAllTargetcounts = 3;
    [self setSpeed];
    self.targetArray = [NSMutableArray arrayWithCapacity:0];
    self.bulletArray = [NSMutableArray arrayWithCapacity:0];
    CGSize winSize = [[CCDirector sharedDirector]winSize];
    CCSprite * bg = [CCSprite spriteWithFile:@"bg.jpg"];
    bg.position = CGPointMake(winSize.width/2, winSize.height/2);
    [self addChild:bg];
    self.player = [CCSprite spriteWithFile:@"Player.png"
                                      rect:CGRectMake(0, 0, 27, 40)];
    self.player.position = CGPointMake(self.player.contentSize.width/2, winSize.height/2);
    [self addChild:self.player];
    
    [CCMenuItemFont setFontName:@"Arial"];
    [CCMenuItemFont setFontSize:20];
    
    CCLabelTTF * censorshipLable = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"第 %d 关",censorship]
                                                      fontName:@"Arial" fontSize:25];
    censorshipLable.color = ccBLACK;
    censorshipLable.position = ccp(240, 290);
    [self addChild:censorshipLable];
    
    CCMenuItem* killAll = [CCMenuItemFont itemFromString:@"必杀技" target:self selector:@selector(killAllTarget:)];
    
    CCMenu * menu = [CCMenu menuWithItems:killAll, nil];
    menu.position = ccp(40, 50);
    
    killAllcount = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"%d", killAllTargetcounts] fontName:@"Arial" fontSize:20];
    killAllcount.position = ccp(90, 50);
    [self addChild:menu];
    [self addChild:killAllcount];
    
    [self showScore];
    [self schedule:@selector(gameLogic:) interval:(1.1 - speedNow)];
    [self schedule:@selector(update:)];
    
}

-(void)addTarget{
    CCSprite * target = [CCSprite spriteWithFile:@"Target.png"
                                            rect:CGRectMake(0, 0, 27, 40)];
    target.tag = 1;
    CGSize winSize = [[CCDirector sharedDirector]winSize];
    int minY = target.contentSize.height/2;
    int maxY = winSize.height - target.contentSize.height/2;
    int rangeY = maxY - minY;
    int actualY = (arc4random()% rangeY) + minY;
    
    target.position = CGPointMake(winSize.width+(target.contentSize.width)/2 + 20, actualY);
    [self addChild:target];
    
    float minDuration = 2.0;
    float maxDuration = 4.0;
    
    float rangeDuration = maxDuration - minDuration;
    float actualDuration = (arc4random()%(int)rangeDuration) +minDuration;
    id actionMove = [CCMoveTo actionWithDuration:actualDuration position:
                     CGPointMake(-target.contentSize.width/2, actualY)];
    
    id actionMoveDone = [CCCallFuncN actionWithTarget:self selector:
                         @selector(spriteMoveFinished:)];
    
    [target runAction:[CCSequence actions:actionMove, actionMoveDone, nil]];

    [self.targetArray addObject:target];
}

-(void) spriteMoveFinished:(id)sender{

    CCSprite * sprite = (CCSprite *)sender;
    [self removeChild:sprite cleanup:YES];
    if (sprite.tag == 1 && flag == 0) {
        lostCounts++;
        [self.targetArray removeObject:sprite];
        [self.lostMi setString:[NSString stringWithFormat:@"lost:%d/5", lostCounts]];
        if (lostCounts == 5) {
            [self restartMenu];
            lostCounts = 0;
            killCounts = 0;
        }
    }else if (sprite.tag == 2){
        [self.bulletArray removeObject:sprite];
    }
    return;
}

-(void)gameLogic:(ccTime)dt{
    [self addTarget];
}

-(void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch * touch = [touches anyObject];
    CGPoint location = [touch locationInView:[touch view]];
    location = [[CCDirector sharedDirector] convertToGL:location];
    CGSize winSize = [[CCDirector sharedDirector] winSize];
    CCSprite * bullet = [CCSprite spriteWithFile:@"Bullet.png"
                                            rect:CGRectMake(0, 0, 20, 20)];
    bullet.position = CGPointMake(20, winSize.height/2);
    int offX = location.x - bullet.position.x;

    if(offX <= 0)return;
    

    CGFloat xxx;
    if (location.y >= 160) {
        xxx = (160* (location.x - 20) / (location.y - 160)) + 20; 
    }else{
        xxx = (160* (location.x - 20) / (160 - location.y)) + 20;
    }
    
    

    float length = sqrtf(((xxx-20)*(xxx-20)) + (160*160));
    
    float velocity = 480/1;
    
    float realMoveDuration = length/velocity ;
    
    CGPoint realDest;


    if (location.y >= 160) {
        realDest = ccp(xxx, 320);
    }else{
        realDest = ccp(xxx, 0);
    }
        
     

    [bullet runAction:[CCSequence actions:
                       [CCMoveTo actionWithDuration:realMoveDuration
                                           position:realDest],
                       [CCCallFuncN actionWithTarget:self
                                            selector:@selector(spriteMoveFinished:)],
                       nil]];
            
    [self addChild:bullet];
    bullet.tag = 2;
    [self.bulletArray addObject:bullet];
}


-(void)dealloc{
    self.player = nil;
//    [_targetArray release];
//    [_bulletArray release];
//    [killMi release];
//    [lostMi release];
//    [killAllcount release];
    [super dealloc];
}

- (void)update:(ccTime)dt{

    for (id objt in _targetArray) {
        for (id objb in _bulletArray) {
            CCSprite * spt = (CCSprite *)objt;
            CCSprite * spb = (CCSprite *)objb;
            CGFloat realx = spt.position.x - spb.position.x;
            CGFloat realy = spt.position.y - spb.position.y;
            if ((sqrtf(realx * realx) < kill_x) && (sqrtf(realy * realy) < kill_y) && flag == 0)
            {
                killCounts++;
                [self removeChild:objt cleanup:YES];
                [self removeChild:objb cleanup:YES];
                [self.targetArray removeObject:objt];
                [self.bulletArray removeObject:objb];
                if (killCounts == 10 && speedNow < 1) {
                    [self nextMenu];
                }else if(killCounts == 10 && speedNow == 1){
                    [self theLastMenu];
                }
                
                NSString * str = [NSString stringWithFormat:@"kill:%d/50", killCounts];

                [self.killMi setString:str];
                return;
            }
            
        }
    }
    
    return;
}

- (void)showScore{
    killMi = [CCLabelTTF labelWithString:@"kill:0/50" fontName:@"Arial" fontSize:20];
    lostMi = [CCLabelTTF labelWithString:@"lost:0/5" fontName:@"Arial" fontSize:20];
    killMi.position = ccp(40, 300);
    lostMi.position = ccp(40, 280);
    killMi.color = ccGREEN;
    lostMi.color = ccGREEN;

    [self addChild:killMi];
    [self addChild:lostMi];

}

-(void)restartMenu{
    flag = 1;
    [self unscheduleAllSelectors];
    [CCMenuItemFont setFontName:@"Arial"];
    [CCMenuItemFont setFontSize:25];
    
    CCMenuItem* gameOver = [CCMenuItemFont itemFromString:@"你输了"];
    CCMenuItem* restartMi = [CCMenuItemFont itemFromString:@"重新游戏" target:self selector:@selector(restart:)];
    CCMenuItem* returnMi = [CCMenuItemFont itemFromString:@"返回主菜单" target:self selector:@selector(mainMenu:)];
    CCMenu * menu = [CCMenu menuWithItems:gameOver, restartMi, returnMi, nil];
    [menu alignItemsVertically];
    [self addChild:menu];
}

-(void)restart:(id)sender{
    censorship = 1;
    nextGame = 0;
    i = 0;
    CCScene * sc = [GameScene scene];
	[[CCDirector sharedDirector] replaceScene:sc];
}

-(void)mainMenu:(id)sender{
    i = 0;
    censorship = 1;
    CCScene * sc = [StartScene scene];
    [[CCDirector sharedDirector] replaceScene:sc];
}

-(void)killAllTarget:(id)sender{
    if (killAllTargetcounts <= 0) {
        return;
    }
    killAllTargetcounts--;
    [self.killAllcount setString:[NSString stringWithFormat:@"%d", killAllTargetcounts]];
    for (int i = 10; i <= 480;i += 60) {
        
        
        CCSprite * bullet1 = [CCSprite spriteWithFile:@"Bullet.png"
                                                 rect:CGRectMake(0, 0, 20, 20)];
        CCSprite * bullet2 = [CCSprite spriteWithFile:@"Bullet.png"
                                                 rect:CGRectMake(0, 0, 20, 20)];
        bullet1.position = CGPointMake(20, 160);
        bullet2.position = CGPointMake(20, 160);
        CGPoint realDest1 = ccp(i, 320);
        CGPoint realDest2 = ccp(i, 0);
        
        float length = sqrtf(((i-20)*(i-20)) + (160*160));
        
        float velocity = 480/1;
        
        float realMoveDuration = length/velocity ;
        
        if (i < 320) {
            CCSprite * bullet3 = [CCSprite spriteWithFile:@"Bullet.png"
                                                     rect:CGRectMake(0, 0, 20, 20)];
            bullet3.position = CGPointMake(20, 160);
            CGPoint realDest3 = ccp(480, i);
            float length3;
            if (i != 160) {
                length3 = sqrtf((460* 460) + (i > 160 ? (i - 160)*(i - 160) : i*i));
            }
            else{
                length3 = 480;
            }
            bullet3.tag = 2;
            
            float realMoveDuration3 = length3/velocity ;
            
            [bullet3 runAction:[CCSequence actions:
                                [CCMoveTo actionWithDuration:realMoveDuration3
                                                    position:realDest3],
                                [CCCallFuncN actionWithTarget:self
                                                     selector:@selector(spriteMoveFinished:)],
                                nil]];
            [self   addChild:bullet3];
            [self.bulletArray addObject:bullet3];
           
        }

        
        bullet1.tag = 2;
        bullet2.tag = 2;
        [self  addChild:bullet1];
        [self.bulletArray addObject:bullet1];
        [self addChild:bullet2];
        [self.bulletArray addObject:bullet2];
        [bullet1 runAction:[CCSequence actions:
                            [CCMoveTo actionWithDuration:realMoveDuration
                                                position:realDest1],
                            [CCCallFuncN actionWithTarget:self
                                                 selector:@selector(spriteMoveFinished:)],
                            nil]];
        
        
        [bullet2 runAction:[CCSequence actions:
                            [CCMoveTo actionWithDuration:realMoveDuration
                                                position:realDest2],
                        [CCCallFuncN actionWithTarget:self
                                                selector:@selector(spriteMoveFinished:)],
                            nil]];

    }
    
}



-(void) setSpeed{


    if (nextGame == 0) {
        speedNow = 0;
    }
    else if(nextGame == 1){
        i += 0.2;
        speedNow = i;
    }else if(nextGame == 2){
        speedNow = 1.05;
    }
}

-(void) nextMenu{
    flag = 1;
    [self unscheduleAllSelectors];
    [CCMenuItemFont setFontName:@"Arial"];
    [CCMenuItemFont setFontSize:25];
    
    CCMenuItem* win = [CCMenuItemFont itemFromString:@"通关"];
    CCMenuItem* nextMi = [CCMenuItemFont itemFromString:@"下一关" target:self selector:@selector(nextMenu:)];
    CCMenuItem* returnMi = [CCMenuItemFont itemFromString:@"返回主菜单" target:self selector:@selector(mainMenu:)];
    CCMenu * menu = [CCMenu menuWithItems:win, nextMi, returnMi, nil];
    [menu alignItemsVertically];
    [self addChild:menu];
}


-(void)nextMenu:(id)sender{
    nextGame = 1;
    censorship++;
    CCScene * sc = [GameScene scene];
	[[CCDirector sharedDirector] replaceScene:sc];
}

-(void)theLastMenu{
    flag = 1;
    censorship++;
    [self unscheduleAllSelectors];
    [CCMenuItemFont setFontName:@"Arial"];
    [CCMenuItemFont setFontSize:25];
    
    CCMenuItem* win = [CCMenuItemFont itemFromString:@"成功通关!想要尝试终极难度?"];
    CCMenuItem* lastMi = [CCMenuItemFont itemFromString:@"YES" target:self selector:@selector(lastMenu:)];
    CCMenuItem* returnMi = [CCMenuItemFont itemFromString:@"NO" target:self selector:@selector(mainMenu:)];
    CCMenu * menu = [CCMenu menuWithItems:win, lastMi, returnMi, nil];
    [menu alignItemsVertically];
    [self addChild:menu];
}

-(void)lastMenu:(id)sender{
    nextGame = 2;
    CCScene * sc = [GameScene scene];
	[[CCDirector sharedDirector] replaceScene:sc];
}
@end