summary refs log tree commit diff stats
path: root/Classes/GameLayer.m
blob: 2ec8ef5e12de6091b464ab0181a3c22a1c1729fc (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.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
class CreateEntries < ActiveRecord::Migration[5.1]
  def change
    create_table :entries do |t|
      t.string :title
      t.text :body
      t.string :slug, null: false
      t.string :type, null: false

      t.timestamps
    end
  end
end
45' href='#n245'>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 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
//
//  GameLayer.m
//  Cart Collect
//
//  Created by iD Student Account on 7/18/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "GameLayer.h"


@implementation GameLayer

@synthesize currentTutorial;

+ (CCScene*)scene
{
	CCScene* scene = [CCScene node];
	
	CCLayerColor* backgroundLayer = [CCLayerColor layerWithColor:ccc4(255, 255, 255, 255)];
	[scene addChild:backgroundLayer];
	
	GameLayer* layer = [GameLayer node];
    layer.tag = GAME_LAYER;
	[scene addChild:layer];
	
	scene.tag = GAME_SCENE;
	
	return scene;
}

- (void)tick:(ccTime)dt
{
	// Move the cart based on acceleration gathered from accelerometer
	cartSprite.position = ccp(MIN(MAX(cartSprite.position.x+accelX, 16),464), cartSprite.position.y);
	
	int lastScore = score;
	
	for (FallingObject* object in objects)
	{
		// Move objects down
		object.sprite.position = ccp(object.sprite.position.x, object.sprite.position.y-object.weight);
	
        // Cart collision detection
        CGSize first = [cartSprite boundingBox].size;
        CGSize second = [object.sprite boundingBox].size;
        
        if (cartSprite.position.x > (object.sprite.position.x - second.width/2 - first.width/2))
        {
            if (cartSprite.position.x < (object.sprite.position.x + second.width/2 + first.width/2))
            {
                if (cartSprite.position.y > (object.sprite.position.y - second.height/2 - first.height/2))
                {
                    if (cartSprite.position.y < (object.sprite.position.y + second.height/2 + first.height/2))
                    {
                        [object retain];
                        [objects removeObject:object];
                        
                        // If a cart collides with an object, it's going to vanish no matter what
                        [object.sprite.parent removeChild:object.sprite cleanup:YES];
                        
                        NSString* audioFile = nil;
                        if ([object isKindOfClass:[OneUp class]])
                        {
                            audioFile = [[NSBundle mainBundle] pathForResource:@"1up" ofType:@"wav"];
                            lives++;
                        } else if ([object isKindOfClass:[Rock class]])
                        {
                            audioFile = [[NSBundle mainBundle] pathForResource:@"Damage1" ofType:@"wav"];
                            lives--;
                        } else if ([object conformsToProtocol:@protocol(ValuableObject)]) {
                            audioFile = [[NSBundle mainBundle] pathForResource:@"Item1" ofType:@"wav"];
                            score += [((FallingObject<ValuableObject>*)object) pointValue];
                        }
                        
                        if (audioFile != nil)
                        {
                            [[SimpleAudioEngine sharedEngine] playEffect:audioFile];
                        }
                        
                        [self updateLabels];
                        
                        continue; // Don't check for collision with floor
                    }
                }
            }
        }
        
		// Collision detection with floor
        if (object.sprite.position.y - (object.sprite.contentSize.height/2) < 0)
		{
            [object retain];
            [objects removeObject:object];
            
			[object.sprite.parent removeChild:object.sprite cleanup:YES];
			
			if ([object conformsToProtocol:@protocol(ValuableObject)])
			{
				NSString* audioFile = [[NSBundle mainBundle] pathForResource:@"Damage1" ofType:@"wav"];
                [[SimpleAudioEngine sharedEngine] playEffect:audioFile];
				
				lives--;
				
				[self updateLabels];
			}
		}	
	}
	
	if (lives == 0)
	{
        [self unschedule:@selector(randomlyAddObject:)];
        [self unschedule:@selector(tick:)];
        [[CCDirector sharedDirector] replaceScene:[CCTransitionSlideInT transitionWithDuration:1.5f scene:[GameOverLayer sceneWithScore:score]]];
	} else if (score > lastScore)
	{
		if ((lastScore < 6500) && (score >= 6500))
		{
			[self unschedule:@selector(randomlyAddObject:)];
			[self schedule:@selector(randomlyAddObject:) interval:0.6f];
			addSpeed = 0.6f;
		} else if ((lastScore < 4500) && (score >= 4500))
		{
			[self unschedule:@selector(randomlyAddObject:)];
			[self schedule:@selector(randomlyAddObject:) interval:0.7f];
			addSpeed = 0.7f;
		} else if ((lastScore < 2500) && (score >= 2500))
		{
			[self unschedule:@selector(randomlyAddObject:)];
			[self schedule:@selector(randomlyAddObject:) interval:0.8f];
			addSpeed = 0.8f;
		} else if ((lastScore < 1500) && (score >= 1500))
		{
			[self unschedule:@selector(randomlyAddObject:)];
			[self schedule:@selector(randomlyAddObject:) interval:0.9f];
			addSpeed = 0.9f;
		} else if ((lastScore < 500) && (score >= 500))
		{
			[self unschedule:@selector(randomlyAddObject:)];
			[self schedule:@selector(randomlyAddObject:) interval:1.0f];
			addSpeed = 1.0f;
		} else if ((lastScore < 150) && (score >= 150))
		{
			[self unschedule:@selector(randomlyAddObject:)];
			[self schedule:@selector(randomlyAddObject:) interval:2.0f];
			addSpeed = 2.0f;
		}
	}
}

- (void)randomlyAddObject:(ccTime)dt
{
	FallingObject* object;
    int oneuppercent = 98 - (lives == 1 ? 1 : 0);

	if (score < 1000)
	{
		int randomval = arc4random()%100;
		
		if (randomval < 65)
		{
			object = [[Cherry alloc] init];
		} else if (randomval < oneuppercent)
		{
			object = [[Bottle alloc] init];
		} else {
			object = [[OneUp alloc] init];
		}
	} else {
		int randomval = arc4random()%100;
		
		if (randomval < 40)
		{
			object = [[Cherry alloc] init];
		} else if (randomval < 70)
		{
			object = [[Rock alloc] init];
		} else if (randomval < oneuppercent)
		{
			object = [[Bottle alloc] init];
		} else {
			object = [[OneUp alloc] init];
		}
	}
	
	int objectX = arc4random()%448+16;
	object.sprite.position = ccp(objectX, 360);
	object.sprite.scale = 1;
	[self addChild:object.sprite];
	
	[objects addObject:object];
    [object release];
	
	if (score >= 2000)
	{
		if (arc4random() % 100 > 80)
		{
			object = [[Rock alloc] init];
			
			objectX = arc4random()%448+16;
			object.sprite.position = ccp(objectX, 360);
			object.sprite.scale = 1;
			[self addChild:object.sprite];
			
			[objects addObject:object];
            [object release];
		}
	}
	
	if (score >= 4000)
	{
		if (arc4random() % 100 > 80)
		{
			object = [[Rock alloc] init];
			
			objectX = arc4random()%448+16;
			object.sprite.position = ccp(objectX, 360);
			object.sprite.scale = 1;
			[self addChild:object.sprite];
			
			[objects addObject:object];
            [object release];
		}
	}
}

- (id)init
{
	self = [super init];
	
	int winWidth = [CCDirector sharedDirector].winSize.width;
	//int winHeight = [CCDirector sharedDirector].winSize.height;
	int cartScale = 2;
	
	if (self != nil)
	{
		CCSprite* backgroundImage = [CCSprite spriteWithFile:@"SeaBeach.png"];
		backgroundImage.position = ccp(240, 160);
		[self addChild:backgroundImage z:0];
		
		isAccelerometerEnabled_ = YES;
		
		//cart = [[Cart alloc] init];
        cartSprite = [CCSprite spriteWithFile:@"cart.png"];
		cartSprite.position = ccp(winWidth/2, 22);
		cartSprite.scale = cartScale;
		[self addChild:cartSprite];
		
		objects = [[NSMutableSet alloc] init];
		
		score = 0;
		lives = 3;
		
		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];
		
		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];
		
		addSpeed = 2.5f;
	}
	
	return self;
}

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

- (void)accelerometer:(UIAccelerometer*)accelerometer didAccelerate:(UIAcceleration*)acceleration
{	
	static float prevY=0;
	
#define kFilterFactor 0.05f
	
	float accelY = -((float) acceleration.y * kFilterFactor + (1- kFilterFactor)*prevY);
	
	prevY = accelY;
	accelX = accelY * 750;
}

- (void)updateLabels
{
	[scoreLabel setString:[NSString stringWithFormat:@"Score: %d", score]];
	[livesLabel setString:[NSString stringWithFormat:@"Lives: %d", lives]];
}

- (void)pause
{
    if (self.currentTutorial != nil)
    {
        [self.currentTutorial removeFromSuperview];
    }
    
    [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
{
    [[[CCDirector sharedDirector] runningScene] removeChild:shadedLayer cleanup:YES];
    [[[CCDirector sharedDirector] runningScene] removeChild:pauseLayer cleanup:YES];
    
    shadedLayer = nil;
    pauseLayer = nil;
    
    if (self.currentTutorial != nil)
    {
        [[[CCDirector sharedDirector] openGLView] addSubview:self.currentTutorial];
    } else {
        [self resumeSchedulerAndActions];
    }
}

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

- (void)setCurrentTutorial:(TutorialBubble *)m_currentTutorial
{
    @synchronized(self)
    {
        if (currentTutorial != m_currentTutorial)
        {
            [currentTutorial release];
            currentTutorial = [m_currentTutorial retain];
        }
    }
    
    if (currentTutorial != nil)
    {
        [currentTutorial setTarget:self action:@selector(endTutorial)];
        [[[CCDirector sharedDirector] openGLView] addSubview:currentTutorial];
        [self pauseSchedulerAndActions];
    }
}

- (void)endTutorial
{
    self.currentTutorial = nil;
    [self resumeSchedulerAndActions];
}

@end