夏までにiPhone アプリつくってみっか!

趣味でiPhone/Androidアプリを開発し、日々勉強した事を書いています。オープンワールド系レースゲームをUnityで開発中です。

【BeeCluster】コンボヒットでスコアをインフレーション

BeeClusterは上手い人でも下手な人でもクリアしたときのスコアは1万3千何百点に落ち着いてしまうのでスコアアタックの楽しみはありませんでした。
せっかくなので上手い人は高得点を取れるようコンボヒット制度を取り入れます。
敵を連続して倒す事でコンボ数がどんどん増えて行きます。
敵を倒した時に得られる点数は、「敵の点数 x コンボ数」となるので、コンボ数を高い状態で敵を倒せば高得点となります。自分がダメージを受けるとコンボ数がリセットされますので高得点を狙うにはダメージを受けずに敵を連続して倒す事が重要になります。

こんな感じでスコアの下にコンボ数が表示されます。

f:id:takujidev:20130806201306j:plain:w300

BeeClusterには点数や残機数の表示を管理するInfoLayearというクラスがありますのでそれを改造してコンボ機能を実装しました。
コンボ数の記録、表示用のインスタンス変数として _comboCount, _comboCounteLabelを追加しました。
追加したメソッドはaddScore, resetComboCount, flashComboCountです。flashComboCountはダメージを受けたときにコンボカウンターを一瞬赤くする事でダメージ感を演出するために作ってみました。
ダメージを受けたときはこうなります。
f:id:takujidev:20130806201407j:plain:w300

@implementation InfoLayer
{
    NSInteger _score;
    NSInteger _highScore;
    NSInteger _beeCount;
    NSInteger _comboCount;
    CCLabelTTF* _scoreLabel;
    CCLabelTTF* _highScoreLabel;
    CCLabelTTF* _beeCountLabel;
    CCLabelTTF* _comboCountLabel;
}

+ (InfoLayer *)sharedInfo
{
    static InfoLayer* sharedInstance = nil;
    if (sharedInstance == nil) {
        sharedInstance = [[self alloc] init];
    }
    return sharedInstance;
}

- (id)init
{
    if ((self = [super init])) {
        _score = 0;
        _highScore = [[NSUserDefaults standardUserDefaults] integerForKey:@"highScore"];
        _beeCount = 0;
        _comboCount = 0;
        NSString* scoreString = [NSString stringWithFormat:@"%07d", _score];
        NSString* highScoreString = [NSString stringWithFormat:@"%07d", _highScore];
        NSString* beecountString = [NSString stringWithFormat:@"%d", _beeCount];
        NSString* comboCountString = [NSString stringWithFormat:@"Combo x %03d", _comboCount];
        _scoreLabel = [CCLabelTTF labelWithString:scoreString fontName:@"arial" fontSize:24];
        _highScoreLabel = [CCLabelTTF labelWithString:highScoreString fontName:@"arial" fontSize:24];
        _beeCountLabel = [CCLabelTTF labelWithString:beecountString fontName:@"arial" fontSize:24];
        _comboCountLabel = [CCLabelTTF labelWithString:comboCountString fontName:@"arial" fontSize:20];
        [self addChild:_scoreLabel];
        [self addChild:_highScoreLabel];
        [self addChild:_beeCountLabel];
        [self addChild:_comboCountLabel];
        _scoreLabel.anchorPoint = ccp(1.0, 1.0); //anchorPointは0.0〜1.0で指定!!
        _highScoreLabel.anchorPoint = ccp(0.0, 1.0);
        _beeCountLabel.anchorPoint = ccp(0.5, 1.0);
        _comboCountLabel.anchorPoint = ccp(1.0, 1.0);
        CGPoint scoreOffset = ccp(-10, 0);
        CGPoint highScoreOffset = ccp(10, 0);
        CGPoint beeCountOffset = ccp(0, 0);
        CGPoint comboCountOffset = ccp(-10, -30);
        CGSize winSize = [CCDirector sharedDirector].winSize;
        CGPoint topRight = ccp(winSize.width, winSize.height);
        CGPoint topLeft = ccp(0, winSize.height);
        CGPoint topCenter = ccp(winSize.width/2, winSize.height);
        _scoreLabel.position = ccpAdd(topRight, scoreOffset);
        _highScoreLabel.position = ccpAdd(topLeft, highScoreOffset);
        _beeCountLabel.position = ccpAdd(topCenter, beeCountOffset);
        _comboCountLabel.position = ccpAdd(topRight, comboCountOffset);
    }
    return self;
}

- (void)addScore:(NSInteger)score
{
    _comboCount++; //コンボ数を増やす
    _score = _score + score * _comboCount; //足されるスコアにコンボ数を掛けたものを現スコアに足す
    [_scoreLabel setString:[NSString stringWithFormat:@"%07d", _score]];
    if (_score > _highScore) {
        _highScore = _score;
        [[NSUserDefaults standardUserDefaults] setInteger:_highScore forKey:@"highScore"];
        [_highScoreLabel setString:[NSString stringWithFormat:@"%07d", _highScore]];
    }
    [_comboCountLabel setString:[NSString stringWithFormat:@"Combo x %03d", _comboCount]];
}

- (void)setScore:(NSInteger)score
{
    _score = score;
    [_scoreLabel setString:[NSString stringWithFormat:@"%07d", _score]];
    if (_score > _highScore) {
        _highScore = _score;
        [[NSUserDefaults standardUserDefaults] setInteger:_highScore forKey:@"highScore"];
        [_highScoreLabel setString:[NSString stringWithFormat:@"%07d", _highScore]];
    }
}


- (void)setBeeCount:(NSInteger)beeCount
{
    _beeCount = beeCount;
    [_beeCountLabel setString:[NSString stringWithFormat:@"%d", _beeCount]];
}

- (void)resetComboCount
{
    _comboCount = 0; //初期値に戻す
    [_comboCountLabel setString:[NSString stringWithFormat:@"Combo x %03d", _comboCount]]; //ラベルをアップデート
}

- (void)flashComboCount
{
    id red = [CCTintTo actionWithDuration:0.1 red:255 green:0 blue:0];
    id white = [CCTintTo actionWithDuration:0.2 red:255 green:255 blue:255];
    [_comboCountLabel runAction:[CCSequence actions:red, white, nil]];
}


@end