Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

executable file 185 lines (155 sloc) 5.675 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
//
// LevelSelection.m
// MooeeMath
//
// Created by Cyril Gaillard on 20/12/10.
// Copyright 2010 Voila Design. All rights reserved.
//

#import "Level5.h"
#import "DBWrapper.h"
#import "MooeeMathAppDelegate.h"
#import "CurrentLevel.h"
#import "LevelSelection.h"
#import "SharedFunctions.h"

@implementation Level5
@synthesize numberToFindArray;
@synthesize pickedNumbers;
@synthesize movableDominos;

+(id) scene
{
// 'scene' is an autorelease object.
CCScene *scene = [CCScene node];
// 'layer' is an autorelease object.
Level5 *layer = [Level5 node];
// add layer as a child to scene
[scene addChild: layer];
// return the scene
return scene;
}

-(void) showSelectionPage
{
    [[CCDirector sharedDirector] replaceScene:[CCTransitionFade transitionWithDuration:0.5f scene:[LevelSelection scene] withColor:ccc3(0,0,0)]];
}


-(void)startPlaying
{
if (selectedDomino!=nil) {
[self removeChild:selectedDomino cleanup:YES];
}
//CCLOG(@" startPlaying number of Iterations: %d",numberofIterations);
if (numberofIterations < kLoopNumber_10) {
        currentNumberIndex = [[idxArray objectAtIndex:numberofIterations] intValue];
[self playNumberSound];
}
else {
gameEnded(self);
}
}

-(void) playNumberSound
{
    NSString *soundStr = [NSString stringWithFormat:@"Number%d.mp3",currentNumberIndex];
    [[SimpleAudioEngine sharedEngine] playEffect:soundStr];
}

- (void) showPausePage: (CCMenuItem *) menuItem
{
}

-(void) loadObjectsToFind
{
    [CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_RGBA4444];
    
    NSString *frameName;
for(int i = 0; i <kLoopNumber_10 ; ++i) {
        frameName = [NSString stringWithFormat:@"Domino%02d.png",i];
//CCLOG(@"Domino image %@",sDominoImage);
CCSprite *dominoToDisplay = [CCSprite spriteWithSpriteFrameName:frameName];
        dominoToDisplay.scale=0.5;
float offsetFraction = ((float)(i+1))/(kLoopNumber_10+1);
dominoToDisplay.position = ccp(size.width*offsetFraction, size.height/2+100);
[self addChild:dominoToDisplay z:5];
[movableDominos addObject:dominoToDisplay];
}
    
}

- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
    CGPoint touchLocation = [self convertTouchToNodeSpace:touch];
    [self findTouchedDomino:touchLocation];
    return TRUE;
}

- (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event {
    CGPoint touchLocation = [self convertTouchToNodeSpace:touch];

if (CGRectContainsPoint(houseToFill.boundingBox, touchLocation)) {
numberofIterations++;
[selectedDomino runAction:[CCSequence actions:[CCScaleTo actionWithDuration:0.5f scale:0],[CCCallFunc actionWithTarget:self selector:@selector(startPlaying)] ,nil]];
}
}

- (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event {
    CGPoint touchLocation = [self convertTouchToNodeSpace:touch];
    
    CGPoint oldTouchLocation = [touch previousLocationInView:touch.view];
    oldTouchLocation = [[CCDirector sharedDirector] convertToGL:oldTouchLocation];
    oldTouchLocation = [self convertToNodeSpace:oldTouchLocation];
    
    CGPoint translation = ccpSub(touchLocation, oldTouchLocation);
    [self moveSelectedDomino:translation];
}

- (void)moveSelectedDomino:(CGPoint)translation {
    if (selectedDomino) {
        CGPoint newPos = ccpAdd(selectedDomino.position, translation);
        selectedDomino.position = newPos;
    }
}

- (void)findTouchedDomino:(CGPoint)touchLocation {
    CCSprite * newSprite = nil;
    for (CCSprite *sprite in movableDominos) {
        if (CGRectContainsPoint(sprite.boundingBox, touchLocation)) {
            newSprite = sprite;
            break;
        }
    }
    if (newSprite != selectedDomino) {
        [selectedDomino stopAllActions];
        [selectedDomino runAction:[CCRotateTo actionWithDuration:0.1 angle:0]];
        CCRotateTo * rotLeft = [CCRotateBy actionWithDuration:0.1 angle:-4.0];
        CCRotateTo * rotCenter = [CCRotateBy actionWithDuration:0.1 angle:0.0];
        CCRotateTo * rotRight = [CCRotateBy actionWithDuration:0.1 angle:4.0];
        CCSequence * rotSeq = [CCSequence actions:rotLeft, rotCenter, rotRight, rotCenter, nil];
        [newSprite runAction:[CCRepeatForever actionWithAction:rotSeq]];
        selectedDomino = newSprite;
    }
}

-(void)resetVariables
{
[pickedNumbers removeAllObjects];
numberofIterations = 0;
}

-(id) init
{
// always call "super" init
// Apple recommends to re-assign "self" with the "super" return value
if( (self=[super init] )) {

// ask director the the window size
size = [[CCDirector sharedDirector] winSize];
[[CurrentLevel sharedCurrentLevel] setLevelNumber:[NSNumber numberWithInt:5]];

numberofIterations = 0;
        
        [CCTexture2D PVRImagesHavePremultipliedAlpha:YES];
        
        CCSpriteBatchNode *levelBatch = loadBatchNode(self, @"level5Sprites");
displayBackground (levelBatch,@"Level5BG.png");

houseToFill = [CCSprite spriteWithSpriteFrameName:@"House.png"];
houseToFill.position= ccp(800,200);
[self addChild:houseToFill z:2];

pickedNumbers = [[NSMutableArray alloc] init];
movableDominos = [[NSMutableArray alloc] init];
idxArray = [[NSMutableArray alloc] init];
[self loadObjectsToFind];

        for(NSInteger i=0;i<kLoopNumber_10;i++){
            [idxArray addObject:[NSNumber numberWithInt:i]];
        }
        [idxArray shuffle];
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
[self startPlaying];
    }
return self;
}


- (void) dealloc
{
[pickedNumbers release];
[movableDominos release];
movableDominos=nil;
[super dealloc];
}
@end
Something went wrong with that request. Please try again.