Advertisement
Guest User

Untitled

a guest
Aug 8th, 2015
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 24.35 KB | None | 0 0
  1. @implementation Asteroid {
  2. SKLabelNode* _score;
  3. SKLabelNode* _highscore;
  4. }
  5. - (instancetype)initWithAsteroidType:(AsteroidType)asteroidType {
  6. int maxHp;
  7. float scale;
  8. switch (asteroidType) {
  9. case AsteroidTypeSmall:
  10. maxHp = 1;
  11. scale = 0.25;
  12. break;
  13. case AsteroidTypeMedium:
  14. maxHp = 3;
  15. scale = 0.4;
  16. break;
  17. default:
  18. return nil;
  19. }
  20. if ((self = [super initWithImageNamed:@"spaceship" maxHp:maxHp healthBarType:HealthBarTypeRed])) {
  21. self.asteroidType = asteroidType;
  22. [self setupCollisionBody];
  23. [self setScale:scale];
  24. }
  25. return self;
  26. }
  27. - (void)setupCollisionBody {
  28. CGPoint offset = CGPointMake(self.size.width * self.anchorPoint.x, self.size.height * self.anchorPoint.y);
  29. CGMutablePathRef path = CGPathCreateMutable();
  30. [self moveToPoint:CGPointMake(30, 105) path:path offset:offset];
  31. [self addLineToPoint:CGPointMake(47, 119) path:path offset:offset];
  32. [self addLineToPoint:CGPointMake(78, 123) path:path offset:offset];
  33. [self addLineToPoint:CGPointMake(105, 112) path:path offset:offset];
  34. [self addLineToPoint:CGPointMake(123, 83) path:path offset:offset];
  35. [self addLineToPoint:CGPointMake(119, 47) path:path offset:offset];
  36. [self addLineToPoint:CGPointMake(99, 24) path:path offset:offset];
  37. [self addLineToPoint:CGPointMake(46, 22) path:path offset:offset];
  38. [self addLineToPoint:CGPointMake(25, 45) path:path offset:offset];
  39. [self addLineToPoint:CGPointMake(16, 79) path:path offset:offset];
  40. CGPathCloseSubpath(path);
  41. self.physicsBody = [SKPhysicsBody bodyWithPolygonFromPath:path];
  42. [self attachDebugFrameFromPath:path color:[SKColor redColor]];
  43. self.physicsBody.categoryBitMask = EntityCategoryAsteroid;
  44. self.physicsBody.collisionBitMask = 0;
  45. self.physicsBody.contactTestBitMask = EntityCategoryPlayerLaser | EntityCategoryPlayer;
  46. }
  47. - (void)collidedWith:(SKPhysicsBody *)body contact:(SKPhysicsContact *)contact {
  48. if (body.categoryBitMask & EntityCategoryPlayerLaser) {
  49. Entity * other = (Entity *)body.node;
  50. [other destroy];
  51. [self takeHit];
  52. MyScene *scene = (MyScene *)self.scene;
  53. if ([self isDead]) {
  54. [RWGameData sharedGameData].score += 2;
  55. _score.text = [NSString stringWithFormat:@"%li", [RWGameData sharedGameData].score];
  56. [scene spawnExplosionAtPosition:contact.contactPoint scale:self.xScale large:YES];
  57. } else {
  58. [scene spawnExplosionAtPosition:contact.contactPoint scale:self.xScale large:NO];
  59. }
  60. }
  61. }
  62. @end
  63.  
  64. @implementation MyScene {
  65. SKLabelNode* _score;
  66. SKLabelNode* _highscore;
  67. }
  68. -(id)initWithSize:(CGSize)size {
  69. if (self = [super initWithSize:size]) {
  70.  
  71. SKSpriteNode *bgImage = [SKSpriteNode spriteNodeWithImageNamed:@"blue.jpg"];
  72. bgImage.size = self.frame.size;
  73. bgImage.position = CGPointMake(self.size.width/2, self.size.height/2);
  74. [self addChild:bgImage];
  75. NSArray *parallaxBackgroundNames = @[@"planet.png", @"planet1.png", @"planet2.png", @"planet3.png"];
  76. CGSize planetSizes = CGSizeMake(80.0, 80.0);
  77. _parallaxNodeBackgrounds = [[FMMParallaxNode alloc] initWithBackgrounds:parallaxBackgroundNames
  78. size:planetSizes
  79. pointsPerSecondSpeed:10.0];
  80. _parallaxNodeBackgrounds.position = CGPointMake(600, size.height/2.0);
  81. _parallaxNodeBackgrounds.alpha = 0.5;
  82. [_parallaxNodeBackgrounds randomizeNodesPositions];
  83. [self addChild:_parallaxNodeBackgrounds];
  84. NSArray *parallaxBackground2Names = @[@"bg_front_spacedust.png",@"bg_front_spacedust.png"];
  85. _parallaxSpaceDust = [[FMMParallaxNode alloc] initWithBackgrounds:parallaxBackground2Names
  86. size:size
  87. pointsPerSecondSpeed:25.0];
  88. _parallaxSpaceDust.position = CGPointMake(0, 0);
  89. _parallaxSpaceDust.alpha = 0.6;
  90. [self addChild:_parallaxSpaceDust];
  91.  
  92. [self setupSound];
  93. [self setupLayers];
  94. [self setupTitle];
  95. [self setupLevelManager];
  96. [self setupPlayer];
  97. [self setupMotionManager];
  98. [self setupPhysics];
  99. [self initDistanceLabel];
  100. [self initScoreLabel];
  101. [self initHighscoreLabel];
  102. [self addChild:[self loadEmitterNode:@"Stars1"]];
  103. [self addChild:[self loadEmitterNode:@"Stars2"]];
  104. [self addChild:[self loadEmitterNode:@"Stars3"]];
  105. }
  106. return self;
  107. }
  108. - (SKEmitterNode *)loadEmitterNode:(NSString *)emitterFileName
  109. {
  110. NSString *emitterPath = [[NSBundle mainBundle] pathForResource:emitterFileName ofType:@"sks"];
  111. SKEmitterNode *emitterNode = [NSKeyedUnarchiver unarchiveObjectWithFile:emitterPath];
  112. emitterNode.particlePosition = CGPointMake(self.size.width/2.0, self.size.height/2.0);
  113. emitterNode.particlePositionRange = CGVectorMake(self.size.width+100, self.size.height);
  114.  
  115. return emitterNode;
  116. }
  117. -(void)initScoreLabel {
  118. _score = [[SKLabelNode alloc] initWithFontNamed:@"ArialRoundedMTBold"];
  119. _score.text = [NSString stringWithFormat:@"%li", [RWGameData sharedGameData].score];
  120. _score.fontSize = 12.0;
  121. _score.position = CGPointMake(150, 15);
  122. _score.horizontalAlignmentMode = SKLabelHorizontalAlignmentModeLeft;
  123. _score.fontColor = [SKColor yellowColor];
  124. [self addChild:_score];
  125. }
  126. -(void)initHighscoreLabel {
  127. _highscore = [[SKLabelNode alloc] initWithFontNamed:@"ArialRoundedMTBold"];
  128. _highscore.text = [NSString stringWithFormat:@"%li", [RWGameData sharedGameData].highScore];
  129. _highscore.fontSize = 12.0;
  130. _highscore.horizontalAlignmentMode = SKLabelHorizontalAlignmentModeLeft;
  131. _highscore.position = CGPointMake(250, 15);
  132. _highscore.fontColor = [SKColor yellowColor];
  133. [self addChild:_highscore];
  134. }
  135. - (void)setupLayers {
  136. _gameLayer = [SKNode node];
  137. [self addChild:_gameLayer];
  138. _hudLayer = [SKNode node];
  139. [self addChild:_hudLayer];
  140. }
  141. - (void)setupTitle {
  142. NSString *fontName = @"Avenir-Light";
  143. _titleLabel1 = [SKLabelNode labelNodeWithFontNamed:fontName];
  144. _titleLabel1.text = @"Ice World";
  145. _titleLabel1.fontSize = [self fontSizeForDevice:48.0];
  146. _titleLabel1.fontColor = [SKColor colorWithRed:0.7 green:0.7 blue:0.7 alpha:1.0];
  147. _titleLabel1.position = CGPointMake(self.size.width/2, self.size.height * 0.8);
  148. _titleLabel1.verticalAlignmentMode = SKLabelVerticalAlignmentModeCenter;
  149. [_hudLayer addChild:_titleLabel1];
  150. _titleLabel2 = [SKLabelNode labelNodeWithFontNamed:fontName];
  151. _titleLabel2.text = @"Battle For Survival";
  152. _titleLabel2.fontSize = [self fontSizeForDevice:56.0];
  153. _titleLabel2.fontColor = [SKColor colorWithRed:0.7 green:0.7 blue:0.7 alpha:1.0];
  154. _titleLabel2.position = CGPointMake(self.size.width/2, self.size.height * 0.6);
  155. _titleLabel2.verticalAlignmentMode = SKLabelVerticalAlignmentModeCenter;
  156. [_hudLayer addChild:_titleLabel2];
  157. [_titleLabel1 setScale:0];
  158. SKAction *waitAction1 = [SKAction waitForDuration:1.0];
  159. SKAction *scaleAction1 = [SKAction scaleTo:1 duration:0.5];
  160. scaleAction1.timingMode = SKActionTimingEaseOut;
  161. [_titleLabel1 runAction:[SKAction sequence:@[waitAction1, _soundTitle, scaleAction1]]];
  162. [_titleLabel2 setScale:0];
  163. SKAction *waitAction2 = [SKAction waitForDuration:2.0];
  164. SKAction *scaleAction2 = [SKAction scaleTo:1 duration:1.0];
  165. scaleAction2.timingMode = SKActionTimingEaseOut;
  166. [_titleLabel2 runAction:[SKAction sequence:@[waitAction2, scaleAction2]]];
  167. _playLabel = [SKLabelNode labelNodeWithFontNamed:fontName];
  168. [_playLabel setScale:0];
  169. _playLabel.text = @"Tap to Play";
  170. _playLabel.fontSize = [self fontSizeForDevice:32.0];
  171. _playLabel.fontColor = [SKColor colorWithRed:0.7 green:0.7 blue:0.7 alpha:1.0];
  172. _playLabel.position = CGPointMake(self.size.width/2, self.size.height * 0.25);
  173. _playLabel.verticalAlignmentMode = SKLabelVerticalAlignmentModeCenter;
  174. [_hudLayer addChild:_playLabel];
  175. SKAction *waitAction3 = [SKAction waitForDuration:3.0];
  176. SKAction *scaleAction3 = [SKAction scaleTo:1 duration:0.5];
  177. scaleAction3.timingMode = SKActionTimingEaseOut;
  178. SKAction *scaleUpAction = [SKAction scaleTo:1.1 duration:0.5];
  179. scaleUpAction.timingMode = SKActionTimingEaseInEaseOut;
  180. SKAction *scaleDownAction = [SKAction scaleTo:0.9 duration:0.5];
  181. scaleDownAction.timingMode = SKActionTimingEaseInEaseOut;
  182. SKAction *throbAction = [SKAction repeatActionForever:[SKAction sequence:@[scaleUpAction, scaleDownAction]]];
  183. SKAction *displayAndThrob = [SKAction sequence:@[waitAction3, scaleAction3, throbAction]];
  184. [_playLabel runAction:displayAndThrob];
  185. }
  186. - (void)setupSound {
  187. [[SKTAudio sharedInstance] playBackgroundMusic:@"SpaceGame.caf"];
  188. _soundExplosionLarge = [SKAction playSoundFileNamed:@"explosion_large.caf" waitForCompletion:NO];
  189. _soundExplosionSmall = [SKAction playSoundFileNamed:@"explosion_small.caf" waitForCompletion:NO];
  190. _soundLaserEnemy = [SKAction playSoundFileNamed:@"laser_enemy.caf" waitForCompletion:NO];
  191. _soundLaserShip = [SKAction playSoundFileNamed:@"laser_ship.caf" waitForCompletion:NO];
  192. _soundShake = [SKAction playSoundFileNamed:@"shake.caf" waitForCompletion:NO];
  193. _soundPowerup = [SKAction playSoundFileNamed:@"powerup.caf" waitForCompletion:NO];
  194. _soundBoss = [SKAction playSoundFileNamed:@"boss.caf" waitForCompletion:NO];
  195. _soundCannon = [SKAction playSoundFileNamed:@"cannon.caf" waitForCompletion:NO];
  196. _soundTitle = [SKAction playSoundFileNamed:@"title.caf" waitForCompletion:NO];
  197. }
  198. - (void)setupLevelManager {
  199. _levelManager = [[LevelManager alloc] init];
  200. }
  201.  
  202. - (void)setupPlayer {
  203. _player = [[Player alloc] init];
  204. _player.position = CGPointMake(-_player.size.width/2, self.size.height * 0.5);
  205. [_player setScale:0.7];
  206. _player.zPosition = 1;
  207. _player.name = @"player";
  208. //add fire to the ball using emitters
  209. NSString *firePath = [[NSBundle mainBundle] pathForResource:@"fireParticle" ofType:@"sks"];
  210. SKEmitterNode *fireEmitter = [NSKeyedUnarchiver unarchiveObjectWithFile:firePath];
  211. fireEmitter.position = CGPointMake(- 32, 20);
  212. [_player addChild:fireEmitter];
  213. //add fire to the ball using emitters
  214. NSString *firePath1 = [[NSBundle mainBundle] pathForResource:@"fireParticle" ofType:@"sks"];
  215. SKEmitterNode *fireEmitter1 = [NSKeyedUnarchiver unarchiveObjectWithFile:firePath1];
  216. fireEmitter1.position = CGPointMake(- 32, - 20);
  217. [_player addChild:fireEmitter1];
  218. [_gameLayer addChild:_player];
  219. }
  220.  
  221. - (void)setupMotionManager {
  222. _motionManager = [[CMMotionManager alloc] init];
  223. _motionManager.accelerometerUpdateInterval = 0.05;
  224. [_motionManager startAccelerometerUpdates];
  225. }
  226.  
  227. - (void)setupPhysics {
  228. self.physicsWorld.contactDelegate = self;
  229. self.physicsWorld.gravity = CGVectorMake(0, 0);
  230. }
  231. /*
  232. - (void)setupBackground {
  233.  
  234. _spacedust1 = [SKSpriteNode spriteNodeWithImageNamed:@"bg_front_spacedust"];
  235. _spacedust1.position = CGPointMake(0, self.size.height/2);
  236. _spacedust2 = [SKSpriteNode spriteNodeWithImageNamed:@"bg_front_spacedust"];
  237. _spacedust2.position = CGPointMake(_spacedust2.size.width, self.size.height/2);
  238. _planetsunrise = [SKSpriteNode spriteNodeWithImageNamed:@"bg_planetsunrise"];
  239. _planetsunrise.position = CGPointMake(600, 0);
  240. _galaxy = [SKSpriteNode spriteNodeWithImageNamed:@"bg_galaxy"];
  241. _galaxy.position = CGPointMake(0, self.size.height * 0.7);
  242. _spatialanomaly = [SKSpriteNode spriteNodeWithImageNamed:@"bg_spacialanomaly"];
  243. _spatialanomaly.position = CGPointMake(900, self.size.height * 0.3);
  244. _spatialanomaly2 = [SKSpriteNode spriteNodeWithImageNamed:@"bg_spacialanomaly2"];
  245. _spatialanomaly2.position = CGPointMake(1500, self.size.height * 0.9);
  246.  
  247. _parallaxNode = [[ParallaxNode alloc] initWithVelocity:CGPointMake(-100, 0)];
  248. _parallaxNode.position = CGPointMake(0, 0);
  249. [_parallaxNode addChild:_spacedust1 parallaxRatio:1];
  250. [_parallaxNode addChild:_spacedust2 parallaxRatio:1];
  251. [_parallaxNode addChild:_planetsunrise parallaxRatio:0.5];
  252. [_parallaxNode addChild:_galaxy parallaxRatio:0.5];
  253. [_parallaxNode addChild:_spatialanomaly parallaxRatio:0.5];
  254. [_parallaxNode addChild:_spatialanomaly2 parallaxRatio:0.5];
  255. _parallaxNode.zPosition = -1;
  256. [_gameLayer addChild:_parallaxNode];
  257.  
  258. }
  259. - (void)playExplosionLargeSound {
  260. [self runAction:_soundExplosionLarge];
  261. }
  262. - (void)shakeScreen:(int)oscillations {
  263. SKAction *action = [SKAction skt_screenShakeWithNode:_gameLayer amount:CGPointMake(0, 10.0) oscillations:oscillations duration:0.1*oscillations];
  264. [_gameLayer runAction:action];
  265. }
  266. - (void)spawnExplosionAtPosition:(CGPoint)position scale:(float)scale large:(BOOL)large {
  267. SKEmitterNode *emitter;
  268. if (large) {
  269. emitter = [NSKeyedUnarchiver unarchiveObjectWithFile:[[NSBundle mainBundle] pathForResource:@"Explosion" ofType:@"sks"]];
  270. } else {
  271. emitter = [NSKeyedUnarchiver unarchiveObjectWithFile:[[NSBundle mainBundle] pathForResource:@"SmallExplosion" ofType:@"sks"]];
  272. }
  273. emitter.position = position;
  274. emitter.particleScale = scale;
  275. emitter.numParticlesToEmit *= scale;
  276. emitter.particleLifetime /=scale;
  277. emitter.particlePositionRange = CGVectorMake(emitter.particlePositionRange.dx * scale, emitter.particlePositionRange.dy * scale);
  278. [emitter runAction:[SKAction skt_removeFromParentAfterDelay:1.0]];
  279. [_gameLayer addChild:emitter];
  280. if (large) {
  281. [self runAction:_soundExplosionLarge];
  282. [self shakeScreen:10*scale];
  283. } else {
  284. [self runAction:_soundExplosionSmall];
  285. }
  286.  
  287. }
  288. - (void)nextStage {
  289.  
  290. [_levelManager nextStage];
  291. [self newStageStarted];
  292. }
  293. - (void)newStageStarted {
  294. if (_levelManager.gameState == GameStateDone) {
  295. [self endScene:YES];
  296. } else if ([_levelManager boolForProp:@"SpawnLevelIntro"]) {
  297. [self doLevelIntro];
  298. } else if ([_levelManager hasProp:@"SpawnBoss"]) {
  299. [self spawnBoss];
  300. }else if ([_levelManager hasProp:@"SpawnEnemyShip1"]) {
  301. [self spawnEnemyShip1];
  302. }
  303. }
  304. - (void)startSpawn {
  305. _levelManager.gameState = GameStatePlay;
  306. [self runAction:_soundPowerup];
  307. NSArray *nodes = @[_titleLabel1, _titleLabel2, _playLabel];
  308. for (SKNode *node in nodes) {
  309. SKAction *scaleAction = [SKAction scaleTo:0 duration:0.5];
  310. scaleAction.timingMode = SKActionTimingEaseOut;
  311. SKAction *removeAction = [SKAction removeFromParent];
  312. [node runAction:[SKAction sequence:@[scaleAction, removeAction]]];
  313. }
  314. [self spawnPlayer];
  315. [self nextStage];
  316. }
  317. - (void)spawnPlayer {
  318. SKAction *moveAction1 = [SKAction moveBy:CGVectorMake(_player.size.width/2 + self.size.width * 0.3, 0) duration:0.5];
  319. moveAction1.timingMode = SKActionTimingEaseOut;
  320. SKAction *moveAction2 = [SKAction moveBy:CGVectorMake(-self.size.width * 0.2, 0) duration:0.5];
  321. moveAction2.timingMode = SKActionTimingEaseInEaseOut;
  322. [_player runAction:[SKAction sequence:@[moveAction1, moveAction2]]];
  323.  
  324. }
  325. - (void)endScene:(BOOL)win {
  326. if (_levelManager.gameState == GameStateGameOver) return;
  327. _levelManager.gameState = GameStateGameOver;
  328.  
  329. // NSString *fontName = @"Avenir-Light";
  330.  
  331. NSString *message;
  332. if (win) {
  333. message = @"You win!";
  334. SKScene * gameOverScene =
  335. [[EndGameScene alloc] initWithSize:self.size won:YES];
  336. SKTransition *reveal =
  337. [SKTransition flipHorizontalWithDuration:0.5];
  338. [self.view presentScene:gameOverScene transition:reveal];
  339. } else {
  340. message = @"You lose!";
  341. SKScene * gameOverScene =
  342. [[EndGameScene alloc] initWithSize:self.size won:NO];
  343. SKTransition *reveal =
  344. [SKTransition flipHorizontalWithDuration:0.5];
  345. [self.view presentScene:gameOverScene transition:reveal];
  346. }
  347. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
  348. if (_levelManager.gameState == GameStateMainMenu) {
  349. [self startSpawn];
  350. return;
  351. }
  352. if (_levelManager.gameState == GameStatePlay) {
  353. [self spawnPlayerLaser];
  354. return;
  355. }
  356. }
  357. - (void)updatePlayer {
  358. CGFloat kFilteringFactor = 0.75;
  359. static UIAccelerationValue rollingX = 0, rollingY = 0, rollingZ = 0;
  360.  
  361. rollingX = (_motionManager.accelerometerData.acceleration.x * kFilteringFactor) +
  362. (rollingX * (1.0 - kFilteringFactor));
  363. rollingY = (_motionManager.accelerometerData.acceleration.y * kFilteringFactor) +
  364. (rollingY * (1.0 - kFilteringFactor));
  365. rollingZ = (_motionManager.accelerometerData.acceleration.z * kFilteringFactor) +
  366. (rollingZ * (1.0 - kFilteringFactor));
  367. CGFloat accelX = rollingX;
  368. CGFloat kRestAccelX = 0.6;
  369. CGFloat kPlayerMaxPointsPerSec = self.size.height*0.5;
  370. CGFloat kMaxDiffX = 0.2;
  371. CGFloat accelDiffX = kRestAccelX - ABS(accelX);
  372. CGFloat accelFractionX = accelDiffX / kMaxDiffX;
  373. CGFloat pointsPerSecX = kPlayerMaxPointsPerSec * accelFractionX;
  374.  
  375. CGFloat playerPointsPerSecY = pointsPerSecX;
  376. CGFloat maxY = self.size.height - _player.size.height/2;
  377. CGFloat minY = _player.size.height/2;
  378.  
  379. CGFloat newY = _player.position.y + (playerPointsPerSecY * _deltaTime);
  380. newY = MIN(MAX(newY, minY), maxY);
  381. _player.position = CGPointMake(_player.position.x, newY);
  382. }
  383. - (void)updateLevel {
  384. BOOL newStage = [_levelManager update];
  385. if (newStage) {
  386. [self newStageStarted];
  387. }
  388. }
  389. - (void)updateChildren {
  390. [_gameLayer.children enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
  391. if ([obj isKindOfClass:[Entity class]]) {
  392. Entity *entity = (Entity *)obj;
  393. [entity update:_deltaTime];
  394. }
  395. }];
  396.  
  397. }
  398. - (void)update:(CFTimeInterval)currentTime {
  399. if (_lastUpdateTime) {
  400. _deltaTime = currentTime - _lastUpdateTime;
  401. } else {
  402. _deltaTime = 0;
  403. }
  404. _lastUpdateTime = currentTime;
  405. [_parallaxSpaceDust update:currentTime];
  406. [_parallaxNodeBackgrounds update:currentTime];
  407. if (_levelManager.gameState != GameStatePlay) return;
  408. [self updatePlayer];
  409. [self updateAsteroids];
  410. static NSTimeInterval _lastCurrentTime = 0;
  411. if (currentTime-_lastCurrentTime>1) {
  412. [RWGameData sharedGameData].distance++;
  413. [RWGameData sharedGameData].totalDistance++;
  414. _distance.text = [NSString stringWithFormat:@"%li miles", [RWGameData sharedGameData].totalDistance];
  415. [RWGameData sharedGameData].highScore = MAX([RWGameData sharedGameData].score, [RWGameData sharedGameData].highScore);
  416. _lastCurrentTime = currentTime;
  417. }
  418. [self updateLevel];
  419. [self updateAliens];
  420. [self updateChildren];
  421. [self updatePowerups];
  422. }
  423. - (void)spawnAsteroid {
  424. CGFloat const moveDurationLow = 2.0;
  425. CGFloat const moveDurationHigh = 10.0;
  426.  
  427. Asteroid *asteroid = [[Asteroid alloc] initWithAsteroidType:arc4random_uniform(NumAsteroidTypes)];
  428. asteroid.name = @"asteroid";
  429. asteroid.position = CGPointMake(
  430. self.size.width + asteroid.size.width/2,
  431. RandomFloatRange(0, self.size.height));
  432. [_gameLayer addChild:asteroid];
  433. SKAction *oneRevolution = [SKAction rotateByAngle: M_PI*0.5 duration: 2.0];
  434. SKAction *repeat = [SKAction repeatActionForever:oneRevolution];
  435. [asteroid runAction:repeat];
  436. [asteroid runAction:
  437. [SKAction sequence:@[
  438. [SKAction moveBy:CGVectorMake(-self.size.width*1.5, 0) duration:RandomFloatRange(moveDurationLow, moveDurationHigh)],
  439. [SKAction removeFromParent]
  440. ]
  441. ]];
  442. }
  443. - (void)updateAsteroids {
  444. if (![_levelManager boolForProp:@"SpawnAsteroids"]) return;
  445. float spawnSecsLow = [_levelManager floatForProp:@"ASpawnSecsLow"];
  446. float spawnSecsHigh = [_levelManager floatForProp:@"ASpawnSecsHigh"];
  447. _timeSinceLastAsteroidSpawn += _deltaTime;
  448. if (_timeSinceLastAsteroidSpawn > _timeForNextAsteroidSpawn) {
  449. _timeSinceLastAsteroidSpawn = 0;
  450. _timeForNextAsteroidSpawn = RandomFloatRange(spawnSecsLow, spawnSecsHigh);
  451. [self spawnAsteroid];
  452. }
  453. }
  454. - (void)spawnPlayerLaser {
  455. PlayerLaser *laser = [[PlayerLaser alloc] init];
  456. laser.position = CGPointMake(_player.position.x - 30, _player.position.y - 0);
  457. laser.name = @"laser";
  458. [_gameLayer addChild:laser];
  459. laser.alpha = 0;
  460. [laser runAction:[SKAction fadeAlphaTo:1.0 duration:0.1]];
  461. SKAction *actionMove =
  462. [SKAction moveToX:self.size.width + laser.size.width/2 duration:0.75];
  463. SKAction *actionRemove = [SKAction removeFromParent];
  464. [laser runAction:
  465. [SKAction sequence:@[actionMove, actionRemove]]];
  466. [self runAction:_soundLaserShip];
  467. }
  468. - (void)spawnAlienLaserAtPosition:(CGPoint)position {
  469. AlienLaser * laser = [[AlienLaser alloc] init];
  470. laser.position = position;
  471. [_gameLayer addChild:laser];
  472. SKAction *moveAction = [SKAction moveBy:CGVectorMake(-self.size.width, 0) duration:2.0];
  473. SKAction *removeAction = [SKAction removeFromParent];
  474. [laser runAction:[SKAction sequence:@[moveAction, removeAction]]];
  475. [self runAction:_soundLaserEnemy];
  476. }
  477. - (void)spawnAlienLaser1AtPosition:(CGPoint)position {
  478. AlienLaser1 * laser1 = [[AlienLaser1 alloc] init];
  479. laser1.position = position;
  480. [_gameLayer addChild:laser1];
  481. SKAction *moveAction = [SKAction moveBy:CGVectorMake(-self.size.width, 0) duration:0.8];
  482. SKAction *removeAction = [SKAction removeFromParent];
  483. [laser1 runAction:[SKAction sequence:@[moveAction, removeAction]]];
  484. [self runAction:_soundLaserEnemy];
  485. }
  486. - (void)shootCannonBallAtPlayerFromPosition:(CGPoint)position {
  487. CannonBall * cannonBall = [[CannonBall alloc] init];
  488. cannonBall.position = position;
  489. [_gameLayer addChild:cannonBall];
  490. CGPoint offset = CGPointSubtract(_player.position, cannonBall.position);
  491. CGPoint shootVector = CGPointNormalize(offset);
  492. CGPoint shootTarget = CGPointMultiplyScalar(shootVector, self.size.width * 2);
  493. [cannonBall runAction:[SKAction sequence:@[
  494. [SKAction moveByX:shootTarget.x y:shootTarget.y duration:5.0],
  495. [SKAction removeFromParent]
  496. ]]];
  497. }
  498. - (void)spawnAlien {
  499. if (_numAlienSpawns == 0) {
  500. CGPoint alienPosStart = CGPointMake(
  501. self.size.width * 1.3,
  502. RandomFloatRange(self.size.height*0.9, self.size.height * 1.0));
  503. CGPoint cp1 = CGPointMake(
  504. RandomFloatRange(-self.size.width * 0.1, self.size.width * 0.6),
  505. RandomFloatRange(self.size.height * 0.7, self.size.height * 1.0));
  506. CGPoint alienPosEnd = CGPointMake(
  507. self.size.width * 1.3,
  508. RandomFloatRange(0, self.size.height * 0.1));
  509. CGPoint cp2 = CGPointMake(
  510. RandomFloatRange(-self.size.width * 0.1, self.size.width * 0.6),
  511. RandomFloatRange(0, self.size.height * 0.3));
  512. _alienPath = [[UIBezierPath alloc] init];
  513. [_alienPath moveToPoint:alienPosStart];
  514. [_alienPath addCurveToPoint:alienPosEnd controlPoint1:cp1 controlPoint2:cp2];
  515. _numAlienSpawns = RandomFloatRange(1, 20);
  516. _timeForNextAlienSpawn = 1.0;
  517. [_dd1 removeFromParent];
  518. [_dd2 removeFromParent];
  519. [_dd3 removeFromParent];
  520. _dd1 = [self attachDebugFrameFromPath:_alienPath.CGPath color:[SKColor greenColor]];
  521. _dd2 = [self attachDebugFrameFromPoint:alienPosStart toPoint:cp1 color:[SKColor blueColor]];
  522. _dd3 = [self attachDebugFrameFromPoint:alienPosEnd toPoint:cp2 color:[SKColor blueColor]];
  523.  
  524. } else {
  525. _numAlienSpawns -= 1;
  526. Alien *alien = [[Alien alloc] init];
  527. alien.name = @"alien";
  528. SKAction *pathAction = [SKAction followPath:_alienPath.CGPath asOffset:NO orientToPath:NO duration:3.0];
  529. SKAction *removeAction = [SKAction removeFromParent];
  530. [alien runAction:[SKAction sequence:@[pathAction, removeAction]]];
  531. [alien setScale:0.7];
  532. [_gameLayer addChild:alien];
  533. }
  534. }
  535. - (void)updateAliens {
  536. if (![_levelManager boolForProp:@"SpawnAlienSwarm"]) return;
  537. _timeSinceLastAlienSpawn += _deltaTime;
  538. if (_timeSinceLastAlienSpawn > _timeForNextAlienSpawn) {
  539. _timeSinceLastAlienSpawn = 0;
  540. _timeForNextAlienSpawn = 0.3;
  541. [self spawnAlien];
  542. }
  543. }
  544. - (void)didBeginContact:(SKPhysicsContact *)contact {
  545. SKNode *node = contact.bodyA.node;
  546. if ([node isKindOfClass:[Entity class]]) {
  547. [(Entity*)node collidedWith:contact.bodyB contact:contact];
  548. }
  549. node = contact.bodyB.node;
  550. if ([node isKindOfClass:[Entity class]]) {
  551. [(Entity*)node collidedWith:contact.bodyA contact:contact];
  552. }
  553. }
  554. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement