-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGameLayer.m
330 lines (260 loc) · 9.91 KB
/
GameLayer.m
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
//
// GameLayer.m
//
#import "SimpleAudioEngine.h"
#import "CocosDenshion.h"
#import "CDAudioManager.h"
// Import the interfaces
#import "GameLayer.h"
#import "MenuLayer.h"
// Needed to obtain the Navigation Controller
#import "AppDelegate.h"
#define TARGET_WIDTH
#pragma mark - GameLayer
// GameLayer implementation
@implementation GameLayer
// Helper class method that creates a Scene with the GameLayer as the only child.
+(CCScene *) scene
{
// 'scene' is an autorelease object.
CCScene *scene = [CCScene node];
// 'layer' is an autorelease object.
GameLayer *layer = [GameLayer node];
// add layer as a child to scene
[scene addChild: layer];
// return the scene
return scene;
}
@synthesize activeEmitters=activeEmitters_;
@synthesize activeSounds=activeSounds_;
@synthesize grassBlocks=grassBlocks_;
@synthesize inactiveEmitters=inactiveEmitters_;
// on "init" you need to initialize your instance
-(id) init
{
// always call "super" init
// Apple recommends to re-assign "self" with the "super's" return value
if( (self=[super init]) ) {
self.isTouchEnabled = YES;
//only CFDictionary supports UITouch as key
NSMutableDictionary *original = [[NSMutableDictionary alloc] init];
self.activeEmitters = (CFMutableDictionaryRef)original;
self.activeSounds = [[ NSMutableArray alloc] init];
self.grassBlocks = [[ NSMutableArray alloc] init];
self.inactiveEmitters = [[ NSMutableArray alloc] init];
SimpleAudioEngine *sae = [SimpleAudioEngine sharedEngine];
if (sae != nil) {
[sae preloadBackgroundMusic:@"rain.mp3"];
if (sae.willPlayBackgroundMusic) {
sae.backgroundMusicVolume = 0.5f;
}
}
[self schedule:@selector(gameLogic:) interval:3.0];
}
return self;
}
- (void)addStars {
CCSprite * bg = [CCSprite spriteWithFile:@"stars.png" rect:CGRectMake(0, 0, 2048, 2048)];
[bg setPosition:ccp(0, 0)];
ccTexParams params = {GL_LINEAR,GL_LINEAR,GL_REPEAT,GL_REPEAT};
[bg.texture setTexParameters:¶ms];
[self addChild:bg z:0];
}
-(void)onEnterTransitionDidFinish
{
[self schedule:@selector(gameLogic:) interval:3.0];
[self addStars];
[self addCloud];
[self addGrass];
[self addLeaf];
}
-(void)spriteMoveFinished:(id)sender {
CCSprite *sprite = (CCSprite *)sender;
[self removeChild:sprite cleanup:YES];
}
-(void)gameLogic:(ccTime)dt {
[self addCloud];
}
-(void)addLeaf {
CCSprite *target = [CCSprite spriteWithFile:@"leaf.png"];
CGSize winSize = [[CCDirector sharedDirector] winSize];
CGFloat targetWidth = target.contentSize.width;
CGFloat targetHeight = target.contentSize.height;
if (winSize.height < 786) {
target.scale = 0.75;
targetWidth *= 0.75;
targetHeight *= 0.75;
}
CGFloat X = winSize.width - (targetWidth/2);
CGFloat Y = targetHeight/2;
target.position = ccp(X, Y);
[self addChild:target];
}
-(void)addGrass {
CGSize winSize = [[CCDirector sharedDirector] winSize];
// IMPORTANT:
// The sprite frames will be cached AND RETAINED, and they won't be released unless you call
// [[CCSpriteFrameCache sharedSpriteFrameCache] removeUnusedSpriteFrames];
//NOTE:
//The name of your .png and .plist must be the same name
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"grass_sprite.plist"];
CCSpriteBatchNode *batchNode = [CCSpriteBatchNode batchNodeWithFile:@"grass_sprite.png"];
CCSprite *target = NULL;
int i = 0;
while (target == NULL || ((i - 1) * target.contentSize.width) < winSize.width) {
//TODO: save all targets in an array
target = [CCSprite spriteWithSpriteFrameName:@"grass_sprite00.png"];
CGFloat X = target.contentSize.width * i;
CGFloat Y = target.contentSize.height * 0.3;
target.position = ccp(X, Y);
[batchNode addChild:target];
[self.grassBlocks addObject:target];
i++;
}
[self addChild:batchNode];
}
-(void)animateGrass:(int)index {
CCSprite *target = [self.grassBlocks objectAtIndex:index];
NSMutableArray *animFrames = [NSMutableArray array];
for(int i = 1; i < 12; i++) {
CCSpriteFrame *frame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"grass_sprite%02d.png",i]];
[animFrames addObject:frame];
}
CCAnimation *animation = [CCAnimation animationWithSpriteFrames:animFrames delay:0.05f];
[target runAction:[CCSequence actions:
[CCDelayTime actionWithDuration:1],[CCAnimate actionWithAnimation:animation], nil]];
}
-(void)addCloud {
CCSprite *target = [CCSprite spriteWithFile:@"cartoon-clouds.png"];
// Determine where to spawn the target along the Y axis
CGSize winSize = [[CCDirector sharedDirector] winSize];
//Scale clouds
float cloudSize = (arc4random() % 6) * 0.1 + 0.5;
if (winSize.height < 786) {
cloudSize *= 0.75;
}
target.scale = cloudSize;
CGFloat targetHeight = target.contentSize.height * cloudSize;
CGFloat targetWidth = target.contentSize.width * cloudSize;
int minY = targetHeight/2 + winSize.height * 0.75;
int maxY = winSize.height - targetHeight/2;
int rangeY = maxY - minY;
int actualY = (arc4random() % rangeY) + minY;
// Create the cloud slightly off-screen along the right edge,
// and along a random position along the Y axis as calculated above
target.position = ccp(winSize.width + (targetWidth/2), actualY);
[self addChild:target];
// Determine speed of the cloud
int minDuration = 15.0;
int maxDuration = 25.0;
int rangeDuration = maxDuration - minDuration;
int actualDuration = (arc4random() % rangeDuration) + minDuration;
// Create the actions
id actionMove = [CCMoveTo actionWithDuration:actualDuration
position:ccp(-targetWidth/2, actualY)];
id actionMoveDone = [CCCallFuncN actionWithTarget:self
selector:@selector(spriteMoveFinished:)];
[target runAction:[CCSequence actions:actionMove, actionMoveDone, nil]];
}
- (void) dealloc
{
[self.inactiveEmitters dealloc];
[self.activeSounds dealloc];
[self.grassBlocks dealloc];
[tapGesture dealloc];
[super dealloc];
}
-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
for ( UITouch *touch in touches) {
CCParticleSystem *emitter = [self getEmitterForTouch:touch];
[self moveEmitterToTouch:touch emitter:emitter];
[self animateGrassAtTouch:touch];
[emitter resetSystem];
[self playSound];
}
}
-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
for ( UITouch *touch in touches) {
CCParticleSystem *emitter = [self getEmitterForTouch:touch];
[self moveEmitterToTouch:touch emitter:emitter];
[self animateGrassAtTouch:touch];
}
}
-(void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
for ( UITouch *touch in touches) {
CCParticleSystem *emitter = [self getEmitterForTouch:touch];
[emitter stopSystem];
[self fadeOutSound];
[self recycleEmitter:emitter forTouch:touch];
}
}
- (void)ccTouchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
for ( UITouch *touch in touches) {
CCParticleSystem *emitter = [self getEmitterForTouch:touch];
[emitter stopSystem];
[self fadeOutSound];
[self recycleEmitter:emitter forTouch:touch];
}
}
- (void)animateGrassAtTouch:(UITouch *)touch
{
CGPoint location = [touch locationInView: [touch view]];
int index = location.x/211;
[self animateGrass:index];
}
- (void)moveEmitterToTouch:(UITouch*)touch emitter:(CCParticleSystem*) emitter
{
CGPoint location = [touch locationInView: [touch view]];
CGPoint convertedLocation = [[CCDirector sharedDirector] convertToGL:location];
CGPoint pos = CGPointZero;
emitter.position = ccpSub(convertedLocation, pos);
}
- (CCParticleSystem*) getEmitterForTouch:(UITouch*)touch
{
CCParticleSystem* emitter = CFDictionaryGetValue(self.activeEmitters, touch);
if(emitter == nil) {
emitter = [self getNewEmitter];
CFDictionarySetValue(self.activeEmitters, touch, emitter);
}
return emitter;
}
- (void) playSound
{
ALuint sound = [[SimpleAudioEngine sharedEngine] playEffect:@"rain.mp3"];
[self.activeSounds addObject:[NSNumber numberWithInt:sound]];
}
- (void) fadeOutSound
{
if([self.activeSounds count] > 0) {
float currentVolume = [SimpleAudioEngine sharedEngine].effectsVolume;
id fadeOut = [CCActionTween actionWithDuration:1 key:@"effectsVolume" from:currentVolume to:0.0f];
id stop =
[CCCallBlock actionWithBlock:^
{
ALuint sound = [[self.activeSounds objectAtIndex:0] intValue];
[[SimpleAudioEngine sharedEngine] stopEffect:sound];
[self.activeSounds removeObjectAtIndex:0];
[SimpleAudioEngine sharedEngine].effectsVolume = 1;
}];
[[[CCDirector sharedDirector] actionManager] addAction:[CCSequence actions:fadeOut, stop, nil] target:[SimpleAudioEngine sharedEngine] paused:NO];
}
}
- (CCParticleSystem*) getNewEmitter
{
CCParticleSystem* emitter;
if([self.inactiveEmitters count] > 0) {
emitter = [self.inactiveEmitters objectAtIndex:0];
[self.inactiveEmitters removeObjectAtIndex:0];
}
else {
emitter = [CCParticleSystemQuad particleWithFile:@"BurstPipe.plist"];
[emitter stopSystem];
[self addChild:emitter z:10];
}
return emitter;
}
- (void) recycleEmitter:(CCParticleSystem*) emitter forTouch:(UITouch*) touch {
CFDictionaryRemoveValue(self.activeEmitters, touch);
[self.inactiveEmitters addObject:emitter];
}
@end