Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @implementation Asteroid {
- SKLabelNode* _score;
- SKLabelNode* _highscore;
- }
- - (instancetype)initWithAsteroidType:(AsteroidType)asteroidType {
- int maxHp;
- float scale;
- switch (asteroidType) {
- case AsteroidTypeSmall:
- maxHp = 1;
- scale = 0.25;
- break;
- case AsteroidTypeMedium:
- maxHp = 3;
- scale = 0.4;
- break;
- default:
- return nil;
- }
- if ((self = [super initWithImageNamed:@"spaceship" maxHp:maxHp healthBarType:HealthBarTypeRed])) {
- self.asteroidType = asteroidType;
- [self setupCollisionBody];
- [self setScale:scale];
- }
- return self;
- }
- - (void)setupCollisionBody {
- CGPoint offset = CGPointMake(self.size.width * self.anchorPoint.x, self.size.height * self.anchorPoint.y);
- CGMutablePathRef path = CGPathCreateMutable();
- [self moveToPoint:CGPointMake(30, 105) path:path offset:offset];
- [self addLineToPoint:CGPointMake(47, 119) path:path offset:offset];
- [self addLineToPoint:CGPointMake(78, 123) path:path offset:offset];
- [self addLineToPoint:CGPointMake(105, 112) path:path offset:offset];
- [self addLineToPoint:CGPointMake(123, 83) path:path offset:offset];
- [self addLineToPoint:CGPointMake(119, 47) path:path offset:offset];
- [self addLineToPoint:CGPointMake(99, 24) path:path offset:offset];
- [self addLineToPoint:CGPointMake(46, 22) path:path offset:offset];
- [self addLineToPoint:CGPointMake(25, 45) path:path offset:offset];
- [self addLineToPoint:CGPointMake(16, 79) path:path offset:offset];
- CGPathCloseSubpath(path);
- self.physicsBody = [SKPhysicsBody bodyWithPolygonFromPath:path];
- [self attachDebugFrameFromPath:path color:[SKColor redColor]];
- self.physicsBody.categoryBitMask = EntityCategoryAsteroid;
- self.physicsBody.collisionBitMask = 0;
- self.physicsBody.contactTestBitMask = EntityCategoryPlayerLaser | EntityCategoryPlayer;
- }
- - (void)collidedWith:(SKPhysicsBody *)body contact:(SKPhysicsContact *)contact {
- if (body.categoryBitMask & EntityCategoryPlayerLaser) {
- Entity * other = (Entity *)body.node;
- [other destroy];
- [self takeHit];
- MyScene *scene = (MyScene *)self.scene;
- if ([self isDead]) {
- [RWGameData sharedGameData].score += 2;
- _score.text = [NSString stringWithFormat:@"%li", [RWGameData sharedGameData].score];
- [scene spawnExplosionAtPosition:contact.contactPoint scale:self.xScale large:YES];
- } else {
- [scene spawnExplosionAtPosition:contact.contactPoint scale:self.xScale large:NO];
- }
- }
- }
- @end
- @implementation MyScene {
- SKLabelNode* _score;
- SKLabelNode* _highscore;
- }
- -(id)initWithSize:(CGSize)size {
- if (self = [super initWithSize:size]) {
- SKSpriteNode *bgImage = [SKSpriteNode spriteNodeWithImageNamed:@"blue.jpg"];
- bgImage.size = self.frame.size;
- bgImage.position = CGPointMake(self.size.width/2, self.size.height/2);
- [self addChild:bgImage];
- NSArray *parallaxBackgroundNames = @[@"planet.png", @"planet1.png", @"planet2.png", @"planet3.png"];
- CGSize planetSizes = CGSizeMake(80.0, 80.0);
- _parallaxNodeBackgrounds = [[FMMParallaxNode alloc] initWithBackgrounds:parallaxBackgroundNames
- size:planetSizes
- pointsPerSecondSpeed:10.0];
- _parallaxNodeBackgrounds.position = CGPointMake(600, size.height/2.0);
- _parallaxNodeBackgrounds.alpha = 0.5;
- [_parallaxNodeBackgrounds randomizeNodesPositions];
- [self addChild:_parallaxNodeBackgrounds];
- NSArray *parallaxBackground2Names = @[@"bg_front_spacedust.png",@"bg_front_spacedust.png"];
- _parallaxSpaceDust = [[FMMParallaxNode alloc] initWithBackgrounds:parallaxBackground2Names
- size:size
- pointsPerSecondSpeed:25.0];
- _parallaxSpaceDust.position = CGPointMake(0, 0);
- _parallaxSpaceDust.alpha = 0.6;
- [self addChild:_parallaxSpaceDust];
- [self setupSound];
- [self setupLayers];
- [self setupTitle];
- [self setupLevelManager];
- [self setupPlayer];
- [self setupMotionManager];
- [self setupPhysics];
- [self initDistanceLabel];
- [self initScoreLabel];
- [self initHighscoreLabel];
- [self addChild:[self loadEmitterNode:@"Stars1"]];
- [self addChild:[self loadEmitterNode:@"Stars2"]];
- [self addChild:[self loadEmitterNode:@"Stars3"]];
- }
- return self;
- }
- - (SKEmitterNode *)loadEmitterNode:(NSString *)emitterFileName
- {
- NSString *emitterPath = [[NSBundle mainBundle] pathForResource:emitterFileName ofType:@"sks"];
- SKEmitterNode *emitterNode = [NSKeyedUnarchiver unarchiveObjectWithFile:emitterPath];
- emitterNode.particlePosition = CGPointMake(self.size.width/2.0, self.size.height/2.0);
- emitterNode.particlePositionRange = CGVectorMake(self.size.width+100, self.size.height);
- return emitterNode;
- }
- -(void)initScoreLabel {
- _score = [[SKLabelNode alloc] initWithFontNamed:@"ArialRoundedMTBold"];
- _score.text = [NSString stringWithFormat:@"%li", [RWGameData sharedGameData].score];
- _score.fontSize = 12.0;
- _score.position = CGPointMake(150, 15);
- _score.horizontalAlignmentMode = SKLabelHorizontalAlignmentModeLeft;
- _score.fontColor = [SKColor yellowColor];
- [self addChild:_score];
- }
- -(void)initHighscoreLabel {
- _highscore = [[SKLabelNode alloc] initWithFontNamed:@"ArialRoundedMTBold"];
- _highscore.text = [NSString stringWithFormat:@"%li", [RWGameData sharedGameData].highScore];
- _highscore.fontSize = 12.0;
- _highscore.horizontalAlignmentMode = SKLabelHorizontalAlignmentModeLeft;
- _highscore.position = CGPointMake(250, 15);
- _highscore.fontColor = [SKColor yellowColor];
- [self addChild:_highscore];
- }
- - (void)setupLayers {
- _gameLayer = [SKNode node];
- [self addChild:_gameLayer];
- _hudLayer = [SKNode node];
- [self addChild:_hudLayer];
- }
- - (void)setupTitle {
- NSString *fontName = @"Avenir-Light";
- _titleLabel1 = [SKLabelNode labelNodeWithFontNamed:fontName];
- _titleLabel1.text = @"Ice World";
- _titleLabel1.fontSize = [self fontSizeForDevice:48.0];
- _titleLabel1.fontColor = [SKColor colorWithRed:0.7 green:0.7 blue:0.7 alpha:1.0];
- _titleLabel1.position = CGPointMake(self.size.width/2, self.size.height * 0.8);
- _titleLabel1.verticalAlignmentMode = SKLabelVerticalAlignmentModeCenter;
- [_hudLayer addChild:_titleLabel1];
- _titleLabel2 = [SKLabelNode labelNodeWithFontNamed:fontName];
- _titleLabel2.text = @"Battle For Survival";
- _titleLabel2.fontSize = [self fontSizeForDevice:56.0];
- _titleLabel2.fontColor = [SKColor colorWithRed:0.7 green:0.7 blue:0.7 alpha:1.0];
- _titleLabel2.position = CGPointMake(self.size.width/2, self.size.height * 0.6);
- _titleLabel2.verticalAlignmentMode = SKLabelVerticalAlignmentModeCenter;
- [_hudLayer addChild:_titleLabel2];
- [_titleLabel1 setScale:0];
- SKAction *waitAction1 = [SKAction waitForDuration:1.0];
- SKAction *scaleAction1 = [SKAction scaleTo:1 duration:0.5];
- scaleAction1.timingMode = SKActionTimingEaseOut;
- [_titleLabel1 runAction:[SKAction sequence:@[waitAction1, _soundTitle, scaleAction1]]];
- [_titleLabel2 setScale:0];
- SKAction *waitAction2 = [SKAction waitForDuration:2.0];
- SKAction *scaleAction2 = [SKAction scaleTo:1 duration:1.0];
- scaleAction2.timingMode = SKActionTimingEaseOut;
- [_titleLabel2 runAction:[SKAction sequence:@[waitAction2, scaleAction2]]];
- _playLabel = [SKLabelNode labelNodeWithFontNamed:fontName];
- [_playLabel setScale:0];
- _playLabel.text = @"Tap to Play";
- _playLabel.fontSize = [self fontSizeForDevice:32.0];
- _playLabel.fontColor = [SKColor colorWithRed:0.7 green:0.7 blue:0.7 alpha:1.0];
- _playLabel.position = CGPointMake(self.size.width/2, self.size.height * 0.25);
- _playLabel.verticalAlignmentMode = SKLabelVerticalAlignmentModeCenter;
- [_hudLayer addChild:_playLabel];
- SKAction *waitAction3 = [SKAction waitForDuration:3.0];
- SKAction *scaleAction3 = [SKAction scaleTo:1 duration:0.5];
- scaleAction3.timingMode = SKActionTimingEaseOut;
- SKAction *scaleUpAction = [SKAction scaleTo:1.1 duration:0.5];
- scaleUpAction.timingMode = SKActionTimingEaseInEaseOut;
- SKAction *scaleDownAction = [SKAction scaleTo:0.9 duration:0.5];
- scaleDownAction.timingMode = SKActionTimingEaseInEaseOut;
- SKAction *throbAction = [SKAction repeatActionForever:[SKAction sequence:@[scaleUpAction, scaleDownAction]]];
- SKAction *displayAndThrob = [SKAction sequence:@[waitAction3, scaleAction3, throbAction]];
- [_playLabel runAction:displayAndThrob];
- }
- - (void)setupSound {
- [[SKTAudio sharedInstance] playBackgroundMusic:@"SpaceGame.caf"];
- _soundExplosionLarge = [SKAction playSoundFileNamed:@"explosion_large.caf" waitForCompletion:NO];
- _soundExplosionSmall = [SKAction playSoundFileNamed:@"explosion_small.caf" waitForCompletion:NO];
- _soundLaserEnemy = [SKAction playSoundFileNamed:@"laser_enemy.caf" waitForCompletion:NO];
- _soundLaserShip = [SKAction playSoundFileNamed:@"laser_ship.caf" waitForCompletion:NO];
- _soundShake = [SKAction playSoundFileNamed:@"shake.caf" waitForCompletion:NO];
- _soundPowerup = [SKAction playSoundFileNamed:@"powerup.caf" waitForCompletion:NO];
- _soundBoss = [SKAction playSoundFileNamed:@"boss.caf" waitForCompletion:NO];
- _soundCannon = [SKAction playSoundFileNamed:@"cannon.caf" waitForCompletion:NO];
- _soundTitle = [SKAction playSoundFileNamed:@"title.caf" waitForCompletion:NO];
- }
- - (void)setupLevelManager {
- _levelManager = [[LevelManager alloc] init];
- }
- - (void)setupPlayer {
- _player = [[Player alloc] init];
- _player.position = CGPointMake(-_player.size.width/2, self.size.height * 0.5);
- [_player setScale:0.7];
- _player.zPosition = 1;
- _player.name = @"player";
- //add fire to the ball using emitters
- NSString *firePath = [[NSBundle mainBundle] pathForResource:@"fireParticle" ofType:@"sks"];
- SKEmitterNode *fireEmitter = [NSKeyedUnarchiver unarchiveObjectWithFile:firePath];
- fireEmitter.position = CGPointMake(- 32, 20);
- [_player addChild:fireEmitter];
- //add fire to the ball using emitters
- NSString *firePath1 = [[NSBundle mainBundle] pathForResource:@"fireParticle" ofType:@"sks"];
- SKEmitterNode *fireEmitter1 = [NSKeyedUnarchiver unarchiveObjectWithFile:firePath1];
- fireEmitter1.position = CGPointMake(- 32, - 20);
- [_player addChild:fireEmitter1];
- [_gameLayer addChild:_player];
- }
- - (void)setupMotionManager {
- _motionManager = [[CMMotionManager alloc] init];
- _motionManager.accelerometerUpdateInterval = 0.05;
- [_motionManager startAccelerometerUpdates];
- }
- - (void)setupPhysics {
- self.physicsWorld.contactDelegate = self;
- self.physicsWorld.gravity = CGVectorMake(0, 0);
- }
- /*
- - (void)setupBackground {
- _spacedust1 = [SKSpriteNode spriteNodeWithImageNamed:@"bg_front_spacedust"];
- _spacedust1.position = CGPointMake(0, self.size.height/2);
- _spacedust2 = [SKSpriteNode spriteNodeWithImageNamed:@"bg_front_spacedust"];
- _spacedust2.position = CGPointMake(_spacedust2.size.width, self.size.height/2);
- _planetsunrise = [SKSpriteNode spriteNodeWithImageNamed:@"bg_planetsunrise"];
- _planetsunrise.position = CGPointMake(600, 0);
- _galaxy = [SKSpriteNode spriteNodeWithImageNamed:@"bg_galaxy"];
- _galaxy.position = CGPointMake(0, self.size.height * 0.7);
- _spatialanomaly = [SKSpriteNode spriteNodeWithImageNamed:@"bg_spacialanomaly"];
- _spatialanomaly.position = CGPointMake(900, self.size.height * 0.3);
- _spatialanomaly2 = [SKSpriteNode spriteNodeWithImageNamed:@"bg_spacialanomaly2"];
- _spatialanomaly2.position = CGPointMake(1500, self.size.height * 0.9);
- _parallaxNode = [[ParallaxNode alloc] initWithVelocity:CGPointMake(-100, 0)];
- _parallaxNode.position = CGPointMake(0, 0);
- [_parallaxNode addChild:_spacedust1 parallaxRatio:1];
- [_parallaxNode addChild:_spacedust2 parallaxRatio:1];
- [_parallaxNode addChild:_planetsunrise parallaxRatio:0.5];
- [_parallaxNode addChild:_galaxy parallaxRatio:0.5];
- [_parallaxNode addChild:_spatialanomaly parallaxRatio:0.5];
- [_parallaxNode addChild:_spatialanomaly2 parallaxRatio:0.5];
- _parallaxNode.zPosition = -1;
- [_gameLayer addChild:_parallaxNode];
- }
- - (void)playExplosionLargeSound {
- [self runAction:_soundExplosionLarge];
- }
- - (void)shakeScreen:(int)oscillations {
- SKAction *action = [SKAction skt_screenShakeWithNode:_gameLayer amount:CGPointMake(0, 10.0) oscillations:oscillations duration:0.1*oscillations];
- [_gameLayer runAction:action];
- }
- - (void)spawnExplosionAtPosition:(CGPoint)position scale:(float)scale large:(BOOL)large {
- SKEmitterNode *emitter;
- if (large) {
- emitter = [NSKeyedUnarchiver unarchiveObjectWithFile:[[NSBundle mainBundle] pathForResource:@"Explosion" ofType:@"sks"]];
- } else {
- emitter = [NSKeyedUnarchiver unarchiveObjectWithFile:[[NSBundle mainBundle] pathForResource:@"SmallExplosion" ofType:@"sks"]];
- }
- emitter.position = position;
- emitter.particleScale = scale;
- emitter.numParticlesToEmit *= scale;
- emitter.particleLifetime /=scale;
- emitter.particlePositionRange = CGVectorMake(emitter.particlePositionRange.dx * scale, emitter.particlePositionRange.dy * scale);
- [emitter runAction:[SKAction skt_removeFromParentAfterDelay:1.0]];
- [_gameLayer addChild:emitter];
- if (large) {
- [self runAction:_soundExplosionLarge];
- [self shakeScreen:10*scale];
- } else {
- [self runAction:_soundExplosionSmall];
- }
- }
- - (void)nextStage {
- [_levelManager nextStage];
- [self newStageStarted];
- }
- - (void)newStageStarted {
- if (_levelManager.gameState == GameStateDone) {
- [self endScene:YES];
- } else if ([_levelManager boolForProp:@"SpawnLevelIntro"]) {
- [self doLevelIntro];
- } else if ([_levelManager hasProp:@"SpawnBoss"]) {
- [self spawnBoss];
- }else if ([_levelManager hasProp:@"SpawnEnemyShip1"]) {
- [self spawnEnemyShip1];
- }
- }
- - (void)startSpawn {
- _levelManager.gameState = GameStatePlay;
- [self runAction:_soundPowerup];
- NSArray *nodes = @[_titleLabel1, _titleLabel2, _playLabel];
- for (SKNode *node in nodes) {
- SKAction *scaleAction = [SKAction scaleTo:0 duration:0.5];
- scaleAction.timingMode = SKActionTimingEaseOut;
- SKAction *removeAction = [SKAction removeFromParent];
- [node runAction:[SKAction sequence:@[scaleAction, removeAction]]];
- }
- [self spawnPlayer];
- [self nextStage];
- }
- - (void)spawnPlayer {
- SKAction *moveAction1 = [SKAction moveBy:CGVectorMake(_player.size.width/2 + self.size.width * 0.3, 0) duration:0.5];
- moveAction1.timingMode = SKActionTimingEaseOut;
- SKAction *moveAction2 = [SKAction moveBy:CGVectorMake(-self.size.width * 0.2, 0) duration:0.5];
- moveAction2.timingMode = SKActionTimingEaseInEaseOut;
- [_player runAction:[SKAction sequence:@[moveAction1, moveAction2]]];
- }
- - (void)endScene:(BOOL)win {
- if (_levelManager.gameState == GameStateGameOver) return;
- _levelManager.gameState = GameStateGameOver;
- // NSString *fontName = @"Avenir-Light";
- NSString *message;
- if (win) {
- message = @"You win!";
- SKScene * gameOverScene =
- [[EndGameScene alloc] initWithSize:self.size won:YES];
- SKTransition *reveal =
- [SKTransition flipHorizontalWithDuration:0.5];
- [self.view presentScene:gameOverScene transition:reveal];
- } else {
- message = @"You lose!";
- SKScene * gameOverScene =
- [[EndGameScene alloc] initWithSize:self.size won:NO];
- SKTransition *reveal =
- [SKTransition flipHorizontalWithDuration:0.5];
- [self.view presentScene:gameOverScene transition:reveal];
- }
- - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
- if (_levelManager.gameState == GameStateMainMenu) {
- [self startSpawn];
- return;
- }
- if (_levelManager.gameState == GameStatePlay) {
- [self spawnPlayerLaser];
- return;
- }
- }
- - (void)updatePlayer {
- CGFloat kFilteringFactor = 0.75;
- static UIAccelerationValue rollingX = 0, rollingY = 0, rollingZ = 0;
- rollingX = (_motionManager.accelerometerData.acceleration.x * kFilteringFactor) +
- (rollingX * (1.0 - kFilteringFactor));
- rollingY = (_motionManager.accelerometerData.acceleration.y * kFilteringFactor) +
- (rollingY * (1.0 - kFilteringFactor));
- rollingZ = (_motionManager.accelerometerData.acceleration.z * kFilteringFactor) +
- (rollingZ * (1.0 - kFilteringFactor));
- CGFloat accelX = rollingX;
- CGFloat kRestAccelX = 0.6;
- CGFloat kPlayerMaxPointsPerSec = self.size.height*0.5;
- CGFloat kMaxDiffX = 0.2;
- CGFloat accelDiffX = kRestAccelX - ABS(accelX);
- CGFloat accelFractionX = accelDiffX / kMaxDiffX;
- CGFloat pointsPerSecX = kPlayerMaxPointsPerSec * accelFractionX;
- CGFloat playerPointsPerSecY = pointsPerSecX;
- CGFloat maxY = self.size.height - _player.size.height/2;
- CGFloat minY = _player.size.height/2;
- CGFloat newY = _player.position.y + (playerPointsPerSecY * _deltaTime);
- newY = MIN(MAX(newY, minY), maxY);
- _player.position = CGPointMake(_player.position.x, newY);
- }
- - (void)updateLevel {
- BOOL newStage = [_levelManager update];
- if (newStage) {
- [self newStageStarted];
- }
- }
- - (void)updateChildren {
- [_gameLayer.children enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
- if ([obj isKindOfClass:[Entity class]]) {
- Entity *entity = (Entity *)obj;
- [entity update:_deltaTime];
- }
- }];
- }
- - (void)update:(CFTimeInterval)currentTime {
- if (_lastUpdateTime) {
- _deltaTime = currentTime - _lastUpdateTime;
- } else {
- _deltaTime = 0;
- }
- _lastUpdateTime = currentTime;
- [_parallaxSpaceDust update:currentTime];
- [_parallaxNodeBackgrounds update:currentTime];
- if (_levelManager.gameState != GameStatePlay) return;
- [self updatePlayer];
- [self updateAsteroids];
- static NSTimeInterval _lastCurrentTime = 0;
- if (currentTime-_lastCurrentTime>1) {
- [RWGameData sharedGameData].distance++;
- [RWGameData sharedGameData].totalDistance++;
- _distance.text = [NSString stringWithFormat:@"%li miles", [RWGameData sharedGameData].totalDistance];
- [RWGameData sharedGameData].highScore = MAX([RWGameData sharedGameData].score, [RWGameData sharedGameData].highScore);
- _lastCurrentTime = currentTime;
- }
- [self updateLevel];
- [self updateAliens];
- [self updateChildren];
- [self updatePowerups];
- }
- - (void)spawnAsteroid {
- CGFloat const moveDurationLow = 2.0;
- CGFloat const moveDurationHigh = 10.0;
- Asteroid *asteroid = [[Asteroid alloc] initWithAsteroidType:arc4random_uniform(NumAsteroidTypes)];
- asteroid.name = @"asteroid";
- asteroid.position = CGPointMake(
- self.size.width + asteroid.size.width/2,
- RandomFloatRange(0, self.size.height));
- [_gameLayer addChild:asteroid];
- SKAction *oneRevolution = [SKAction rotateByAngle: M_PI*0.5 duration: 2.0];
- SKAction *repeat = [SKAction repeatActionForever:oneRevolution];
- [asteroid runAction:repeat];
- [asteroid runAction:
- [SKAction sequence:@[
- [SKAction moveBy:CGVectorMake(-self.size.width*1.5, 0) duration:RandomFloatRange(moveDurationLow, moveDurationHigh)],
- [SKAction removeFromParent]
- ]
- ]];
- }
- - (void)updateAsteroids {
- if (![_levelManager boolForProp:@"SpawnAsteroids"]) return;
- float spawnSecsLow = [_levelManager floatForProp:@"ASpawnSecsLow"];
- float spawnSecsHigh = [_levelManager floatForProp:@"ASpawnSecsHigh"];
- _timeSinceLastAsteroidSpawn += _deltaTime;
- if (_timeSinceLastAsteroidSpawn > _timeForNextAsteroidSpawn) {
- _timeSinceLastAsteroidSpawn = 0;
- _timeForNextAsteroidSpawn = RandomFloatRange(spawnSecsLow, spawnSecsHigh);
- [self spawnAsteroid];
- }
- }
- - (void)spawnPlayerLaser {
- PlayerLaser *laser = [[PlayerLaser alloc] init];
- laser.position = CGPointMake(_player.position.x - 30, _player.position.y - 0);
- laser.name = @"laser";
- [_gameLayer addChild:laser];
- laser.alpha = 0;
- [laser runAction:[SKAction fadeAlphaTo:1.0 duration:0.1]];
- SKAction *actionMove =
- [SKAction moveToX:self.size.width + laser.size.width/2 duration:0.75];
- SKAction *actionRemove = [SKAction removeFromParent];
- [laser runAction:
- [SKAction sequence:@[actionMove, actionRemove]]];
- [self runAction:_soundLaserShip];
- }
- - (void)spawnAlienLaserAtPosition:(CGPoint)position {
- AlienLaser * laser = [[AlienLaser alloc] init];
- laser.position = position;
- [_gameLayer addChild:laser];
- SKAction *moveAction = [SKAction moveBy:CGVectorMake(-self.size.width, 0) duration:2.0];
- SKAction *removeAction = [SKAction removeFromParent];
- [laser runAction:[SKAction sequence:@[moveAction, removeAction]]];
- [self runAction:_soundLaserEnemy];
- }
- - (void)spawnAlienLaser1AtPosition:(CGPoint)position {
- AlienLaser1 * laser1 = [[AlienLaser1 alloc] init];
- laser1.position = position;
- [_gameLayer addChild:laser1];
- SKAction *moveAction = [SKAction moveBy:CGVectorMake(-self.size.width, 0) duration:0.8];
- SKAction *removeAction = [SKAction removeFromParent];
- [laser1 runAction:[SKAction sequence:@[moveAction, removeAction]]];
- [self runAction:_soundLaserEnemy];
- }
- - (void)shootCannonBallAtPlayerFromPosition:(CGPoint)position {
- CannonBall * cannonBall = [[CannonBall alloc] init];
- cannonBall.position = position;
- [_gameLayer addChild:cannonBall];
- CGPoint offset = CGPointSubtract(_player.position, cannonBall.position);
- CGPoint shootVector = CGPointNormalize(offset);
- CGPoint shootTarget = CGPointMultiplyScalar(shootVector, self.size.width * 2);
- [cannonBall runAction:[SKAction sequence:@[
- [SKAction moveByX:shootTarget.x y:shootTarget.y duration:5.0],
- [SKAction removeFromParent]
- ]]];
- }
- - (void)spawnAlien {
- if (_numAlienSpawns == 0) {
- CGPoint alienPosStart = CGPointMake(
- self.size.width * 1.3,
- RandomFloatRange(self.size.height*0.9, self.size.height * 1.0));
- CGPoint cp1 = CGPointMake(
- RandomFloatRange(-self.size.width * 0.1, self.size.width * 0.6),
- RandomFloatRange(self.size.height * 0.7, self.size.height * 1.0));
- CGPoint alienPosEnd = CGPointMake(
- self.size.width * 1.3,
- RandomFloatRange(0, self.size.height * 0.1));
- CGPoint cp2 = CGPointMake(
- RandomFloatRange(-self.size.width * 0.1, self.size.width * 0.6),
- RandomFloatRange(0, self.size.height * 0.3));
- _alienPath = [[UIBezierPath alloc] init];
- [_alienPath moveToPoint:alienPosStart];
- [_alienPath addCurveToPoint:alienPosEnd controlPoint1:cp1 controlPoint2:cp2];
- _numAlienSpawns = RandomFloatRange(1, 20);
- _timeForNextAlienSpawn = 1.0;
- [_dd1 removeFromParent];
- [_dd2 removeFromParent];
- [_dd3 removeFromParent];
- _dd1 = [self attachDebugFrameFromPath:_alienPath.CGPath color:[SKColor greenColor]];
- _dd2 = [self attachDebugFrameFromPoint:alienPosStart toPoint:cp1 color:[SKColor blueColor]];
- _dd3 = [self attachDebugFrameFromPoint:alienPosEnd toPoint:cp2 color:[SKColor blueColor]];
- } else {
- _numAlienSpawns -= 1;
- Alien *alien = [[Alien alloc] init];
- alien.name = @"alien";
- SKAction *pathAction = [SKAction followPath:_alienPath.CGPath asOffset:NO orientToPath:NO duration:3.0];
- SKAction *removeAction = [SKAction removeFromParent];
- [alien runAction:[SKAction sequence:@[pathAction, removeAction]]];
- [alien setScale:0.7];
- [_gameLayer addChild:alien];
- }
- }
- - (void)updateAliens {
- if (![_levelManager boolForProp:@"SpawnAlienSwarm"]) return;
- _timeSinceLastAlienSpawn += _deltaTime;
- if (_timeSinceLastAlienSpawn > _timeForNextAlienSpawn) {
- _timeSinceLastAlienSpawn = 0;
- _timeForNextAlienSpawn = 0.3;
- [self spawnAlien];
- }
- }
- - (void)didBeginContact:(SKPhysicsContact *)contact {
- SKNode *node = contact.bodyA.node;
- if ([node isKindOfClass:[Entity class]]) {
- [(Entity*)node collidedWith:contact.bodyB contact:contact];
- }
- node = contact.bodyB.node;
- if ([node isKindOfClass:[Entity class]]) {
- [(Entity*)node collidedWith:contact.bodyA contact:contact];
- }
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement