summary refs log tree commit diff stats
path: root/Classes/GameMode.m
blob: 7b07f15a02841365098be544c675f97a15a293f3 (plain) (blame)
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
186
187
188
189
//
//  GameMode.m
//  Cart Collect
//
//  Created by Starla Insigna on 8/9/11.
//  Copyright 2011 Four Island. All rights reserved.
//

#import "GameMode.h"
#import "FallingObject.h"
#import "MainMenuLayer.h"

@implementation GameMode

@synthesize cart, score, lives, isPaused;

+ (CCScene*)scene
{
	CCScene* scene = [CCScene node];
    
	GameMode* layer = [self node];
    layer.tag = GAME_LAYER;
	[scene addChild:layer];
	
	scene.tag = GAME_SCENE;
	
	return scene;
}

- (id)init
{
    self = [super init];
    
    if (nil != self)
    {
        isAccelerometerEnabled_ = YES;
        
        objects = [[NSMutableSet alloc] init];
        
        cart = [[Cart alloc] initWithSprite:[CCSprite spriteWithFile:@"cart.png"]];
		cart.sprite.position = ccp(240, 22);
		cart.sprite.scale = 2;
		[self addChild:cart.sprite];
        
        scoreLabel = [CCLabelBMFont labelWithString:@"Score: 0" fntFile:@"helvetica2.fnt"];
		scoreLabel.position = ccp(50, 300);
		[self addChild:scoreLabel];
		
		livesLabel = [CCLabelBMFont labelWithString:@"Lives: 3" fntFile:@"helvetica2.fnt"];
		livesLabel.position = ccp(50, 280);
		[self addChild:livesLabel];
		
		score = 0;
		lives = 3;
		
        if ([self canPause])
        {
            CCMenuItemImage* pauseButton = [CCMenuItemImage itemFromNormalImage:@"pause2.png" selectedImage:@"pause.png" target:self selector:@selector(pause)];
            CCMenu* pauseMenu = [CCMenu menuWithItems:pauseButton, nil];
            [pauseMenu setPosition:ccp(480-8-16, 320-8-16)];
            [self addChild:pauseMenu];
        }
        
        isPaused = NO;
    }
    
    return self;
}

- (void)accelerometer:(UIAccelerometer*)accelerometer didAccelerate:(UIAcceleration*)acceleration
{	
	[cart accelerometer:accelerometer didAccelerate:acceleration];
}

- (void)onEnterTransitionDidFinish
{
    [super onEnterTransitionDidFinish];
    
    [[UIAccelerometer sharedAccelerometer] setUpdateInterval:(1.0 / 60)];
    [self schedule:@selector(tick:) interval:1.0f/60.0f];
}

- (void)tick:(ccTime)dt
{
    [cart tick];
    
    for (FallingObject* object in objects)
	{
        if ([object tick])
        {
            [objects removeObject:object];
        }
    }
}

- (BOOL)canPause
{
    return YES;
}

- (void)pause
{
    if (([self canPause]) && (!isPaused))
    {
        isPaused = YES;
        
        [self pauseSchedulerAndActions];
        
        shadedLayer = [CCLayerColor layerWithColor:ccc4(0, 0, 0, 127)];
        [[[CCDirector sharedDirector] runningScene] addChild:shadedLayer];
        
        pauseLayer = [CCLayer node];
        CCLabelBMFont* scoreLabel2 = [CCLabelBMFont labelWithString:@"PAUSE" fntFile:@"helvetica.fnt"];
        scoreLabel2.position = ccp(240,90);
        [pauseLayer addChild:scoreLabel2];
        
        CCMenuItemImage* pauseButton = [CCMenuItemImage itemFromNormalImage:@"pause2.png" selectedImage:@"pause.png" target:self selector:@selector(unpause)];
        CCMenu* pauseMenu = [CCMenu menuWithItems:pauseButton, nil];
        [pauseMenu setPosition:ccp(480-8-16, 320-8-16)];
        [pauseLayer addChild:pauseMenu];
        
        CCMenuItemImage* newgameMenuItem = [CCMenuItemImage itemFromNormalImage:@"back.png" selectedImage:@"back2.png" target:self selector:@selector(mainmenu)];
        CCMenu* myMenu = [CCMenu menuWithItems:newgameMenuItem, nil];
        myMenu.position = ccp(240, 60);
        [pauseLayer addChild:myMenu];
        
        [[[CCDirector sharedDirector] runningScene] addChild:pauseLayer];
    }
}

- (void)unpause
{
    if (isPaused)
    {
        [[[CCDirector sharedDirector] runningScene] removeChild:shadedLayer cleanup:YES];
        [[[CCDirector sharedDirector] runningScene] removeChild:pauseLayer cleanup:YES];
        
        shadedLayer = nil;
        pauseLayer = nil;
        
        [self resumeSchedulerAndActions];
        
        isPaused = NO;
    }
}

- (void)mainmenu
{
    [[CCDirector sharedDirector] replaceScene:[MainMenuLayer scene]];
}

- (void)setScore:(int)m_score
{
    score = m_score;
    
    [scoreLabel setString:[NSString stringWithFormat:@"Score: %d", score]];
}

- (void)setLives:(int)m_lives
{
    lives = m_lives;
    
    [livesLabel setString:[NSString stringWithFormat:@"Lives: %d", lives]];
}

- (void)scheduleDelayedAction:(void(^)(void))m_delayedAction delay:(float)delay
{
    delayedAction = Block_copy([m_delayedAction retain]);
    
    [self schedule:@selector(runDelayedAction) interval:delay];
}

- (void)runDelayedAction
{
    [self unschedule:@selector(runDelayedAction)];
    
    delayedAction();
    [delayedAction release];
    delayedAction = nil;
}

- (void)dealloc
{
    [objects release];
    [cart release];
    [super dealloc];
}

@end