Advertisement
Guest User

.m file

a guest
Apr 27th, 2014
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  ORBMyScene.m
  3. //  (removed)
  4. //
  5. //  Created by (removed) on 2014-04-27.
  6. //  Copyright (c) 2014 (removed). All rights reserved.
  7. //
  8.  
  9. #import "ORBGameScene.h"
  10. #import "ORBMenuScene.h"
  11. #import "CGVector+TC.h"
  12.  
  13. enum {
  14.     CollisionPlayer = 1<<1,
  15.     CollisionEnemy = 1<<2,
  16. };
  17.  
  18.  
  19.  
  20. @interface ORBGameScene () <SKPhysicsContactDelegate>
  21. @end
  22.  
  23. @implementation ORBGameScene
  24. {
  25.     SKNode *_player;
  26.     NSMutableArray *_enemies;
  27.     BOOL _dead;
  28.     SKLabelNode *_scorelabel;
  29. }
  30.  
  31. -(id)initWithSize:(CGSize)size {    
  32.     if (self = [super initWithSize:size]) {
  33.        
  34.         SKSpriteNode *background = [SKSpriteNode spriteNodeWithImageNamed:@"backbground"];
  35.         background.position = CGPointMake(CGRectGetMidX(self.frame),CGRectGetMidY(self.frame));
  36.        
  37.         self.physicsWorld.gravity = CGVectorMake(0, 0);
  38.         self.physicsWorld.contactDelegate = self;
  39.        
  40.         _enemies = [NSMutableArray new];
  41.        
  42.         _player = [SKNode node];
  43.             SKShapeNode *circle = [SKShapeNode node];
  44.             circle.path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 20, 20)].CGPath;
  45.             circle.fillColor = [UIColor blueColor];
  46.             circle.strokeColor = [UIColor blueColor];
  47.             circle.glowWidth = 5;
  48.        
  49.             SKEmitterNode *trail = [SKEmitterNode orb_emitterNamed:@"Trail"];
  50.             trail.targetNode = self;
  51.             trail.position = CGPointMake(CGRectGetMidX(circle.frame), CGRectGetMidY(circle.frame));
  52.             [_player addChild:trail];
  53.        
  54.             _player.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:10];
  55.             _player.physicsBody.mass = 100000;
  56.             _player.physicsBody.categoryBitMask = CollisionPlayer;
  57.             _player.physicsBody.contactTestBitMask = CollisionEnemy;
  58.        
  59.             _player.position = CGPointMake(size.width/2, size.height/2);
  60.        
  61.               [self addChild:background];
  62.         [self addChild:_player];
  63.     }
  64.     return self;
  65. }
  66.  
  67. - (void)didMoveToView:(SKView *)view
  68. {
  69.         [self runAction:[SKAction group:@[
  70. /*            [SKAction spawnPlayer] => spawn animation, then add player to world,*/
  71.         ]]];
  72.     [self performSelector:@selector(spawnEnemy) withObject:nil afterDelay:1.0];
  73. }
  74.  
  75. - (void)spawnEnemy
  76. {
  77.     SKNode *enemy = [SKNode node];
  78.    
  79.         SKEmitterNode *trail = [SKEmitterNode orb_emitterNamed:@"Trail"];
  80.         trail.targetNode = self;
  81.         trail.particleColorSequence = [[SKKeyframeSequence alloc] initWithKeyframeValues:@[
  82.             [SKColor redColor],
  83.             [SKColor colorWithHue:0.1 saturation:.5 brightness:1 alpha:1],
  84.             [SKColor redColor],
  85.         ] times:@[@0, @0.02, @0.2]];
  86.         trail.particleScale /= 2;
  87.         trail.position = CGPointMake(10, 10);
  88.         [enemy addChild:trail];
  89.    
  90.         CGFloat radius = MAX(self.size.height, self.size.width)/2;
  91.         CGFloat angle = (arc4random_uniform(1000)/1000.) * M_PI*2;
  92.         CGPoint p = CGPointMake(cos(angle)*radius, sin(angle)*radius);
  93.         enemy.position = CGPointMake(self.size.width/2 + p.x, self.size.width/2 + p.y);
  94.    
  95.         enemy.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:6];
  96.         enemy.physicsBody.categoryBitMask = CollisionEnemy;
  97.         enemy.physicsBody.allowsRotation = NO;
  98.    
  99.     [_enemies addObject:enemy];
  100.     [self addChild:enemy];
  101.    
  102.     if(!_scorelabel) {
  103.         _scorelabel = [SKLabelNode labelNodeWithFontNamed:@"Courier-Bold"];
  104.        
  105.         _scorelabel.fontSize = 200;
  106.         _scorelabel.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
  107.         _scorelabel.fontColor = [SKColor colorWithHue: 0 saturation: 0 brightness: 1 alpha: .5];
  108.         [self addChild:_scorelabel];
  109.        
  110.     }
  111.     _scorelabel.text = [NSString stringWithFormat:@"%02d", _enemies.count];
  112.  
  113.     // Next spawn
  114.     [self runAction:[SKAction sequence:@[
  115.         [SKAction waitForDuration:5],
  116.         [SKAction performSelector:@selector(spawnEnemy) onTarget:self],
  117.     ]]];
  118. }
  119.  
  120. - (void)dieFrom:(SKNode*)killingEnemy
  121. {
  122.     _dead = YES;
  123.    
  124.     SKEmitterNode *explosion = [SKEmitterNode orb_emitterNamed:@"Explosion"];
  125.     explosion.position = _player.position;
  126.     [self addChild:explosion];
  127.     [explosion runAction:[SKAction sequence:@[
  128.         [SKAction playSoundFileNamed:@"Explosion.wav" waitForCompletion:NO],
  129.         [SKAction waitForDuration:0.4],
  130.         [SKAction runBlock:^{
  131.             // TODO: Remove these more nicely
  132.             [killingEnemy removeFromParent];
  133.             [_player removeFromParent];
  134.         }],
  135.         [SKAction waitForDuration:0.4],
  136.         [SKAction runBlock:^{
  137.             explosion.particleBirthRate = 0;
  138.         }],
  139.         [SKAction waitForDuration:1.2],
  140.        
  141.         [SKAction runBlock:^{
  142.             ORBMenuScene *menu = [[ORBMenuScene alloc] initWithSize:self.size];
  143.             [self.view presentScene:menu transition:[SKTransition doorsCloseHorizontalWithDuration:0.5]];
  144.         }],
  145.     ]]];
  146. }
  147.  
  148. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
  149. {
  150.     [self touchesMoved:touches withEvent:event];
  151. }
  152.  
  153. - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
  154. {
  155.     if(_dead)
  156.         return;
  157.    
  158.     [_player runAction:[SKAction moveTo:[[touches anyObject] locationInNode:self] duration:0.01]];
  159. }
  160.  
  161. -(void)update:(CFTimeInterval)currentTime
  162. {
  163.    
  164.     CGPoint playerPos = _player.position;
  165.    
  166.     for(SKNode *enemyNode in _enemies) {
  167.         CGPoint enemyPos = enemyNode.position;
  168.        
  169.         /* Uniform speed: */
  170.         CGVector diff = TCVectorMinus(playerPos, enemyPos);
  171.         CGVector normalized = TCVectorUnit(diff);
  172.         CGVector force = TCVectorMultiply(normalized, 4);
  173.        
  174.         /* Inversely proportional:
  175.         CGVector diff = TCVectorMinus(playerPos, enemyPos);
  176.         CGVector normalized = TCVectorUnit(diff);
  177.         CGVector force = TCVectorMultiply(normalized, 1/sqrt(TCVectorLength(diff))*40);
  178.         */
  179.        
  180.         /* Inverse square root
  181.         CGVector diff = TCVectorMinus(playerPos, enemyPos);
  182.         CGVector normalized = TCVectorUnit(diff);
  183.         CGVector force = TCVectorMultiply(normalized, 1/sqrt(TCVectorLength(diff))*40);
  184.         */
  185.        
  186.         [enemyNode.physicsBody applyForce:force];
  187.     }
  188.    
  189.     _player.physicsBody.velocity = CGVectorMake(0, 0);
  190. }
  191.  
  192. - (void)didBeginContact:(SKPhysicsContact *)contact
  193. {
  194.     if(_dead)
  195.         return;
  196.    
  197.     [self dieFrom:contact.bodyB.node];
  198.     contact.bodyB.node.physicsBody = nil;
  199. }
  200.  
  201. @end
  202.  
  203. @implementation SKEmitterNode (fromFile)
  204. + (instancetype)orb_emitterNamed:(NSString*)name
  205. {
  206.     return [NSKeyedUnarchiver unarchiveObjectWithFile:[[NSBundle mainBundle] pathForResource:name ofType:@"sks"]];
  207. }
  208. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement