見出し画像

Swiftでいこう--ゲームをつくろ!8

もうそろそろ仕上げをしましょう。

とりあえず、コード全体です。スプライトを表示させて、あたり判定をつけて、ミサイルに当たった場合には惑星を消滅させます。

import SpriteKit
import GameplayKit
class GameScene: SKScene, SKPhysicsContactDelegate {
   var timer: Timer?
   var timerForAsteroud: Timer?
   var asteroudDuration: TimeInterval = 6.0 {
       didSet {
           if asteroudDuration < 2.0 {
               timerForAsteroud?.invalidate()
           }
       }
   }
   let spaceshipCategory: UInt32 = 0b0001
   let missileCategory: UInt32 = 0b0010
   let asteroidCategory: UInt32 = 0b0100
   let earthCategory: UInt32 = 0b1000
   var earth: SKSpriteNode!
   var spaceship: SKSpriteNode!
 
   override func didMove(to view: SKView) {
       physicsWorld.gravity = CGVector(dx: 0, dy: 0)
       physicsWorld.contactDelegate = self
       self.earth = SKSpriteNode(imageNamed: "earth")
       self.earth.xScale = 1.5
       self.earth.yScale = 0.3
       self.earth.position = CGPoint(x: 0, y: -frame.height / 2)
       self.earth.zPosition = -1.0
       self.earth.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: frame.width, height: 100))
       self.earth.physicsBody?.categoryBitMask = earthCategory
       self.earth.physicsBody?.contactTestBitMask = asteroidCategory
       self.earth.physicsBody?.collisionBitMask = 0
       addChild(self.earth)
       self.spaceship = SKSpriteNode(imageNamed: "spaceship")
       self.spaceship.scale(to: CGSize(width: frame.width / 5, height: frame.width / 5))
       self.spaceship.position = CGPoint(x: 0, y: self.earth.frame.maxY + 50)
       self.spaceship.physicsBody = SKPhysicsBody(circleOfRadius: self.spaceship.frame.width * 0.1)
       self.spaceship.physicsBody?.categoryBitMask = spaceshipCategory
       self.spaceship.physicsBody?.contactTestBitMask = asteroidCategory
       self.spaceship.physicsBody?.collisionBitMask = 0
       addChild(self.spaceship)
       timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true, block: { _ in
           self.addAsteroid()
       })
   }
   override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
       if isPaused { return }
       let missile = SKSpriteNode(imageNamed: "missile")
       missile.position = CGPoint(x: self.spaceship.position.x, y: self.spaceship.position.y + 50)
       missile.physicsBody = SKPhysicsBody(circleOfRadius: missile.frame.height / 2)
       missile.physicsBody?.categoryBitMask = missileCategory
       missile.physicsBody?.contactTestBitMask = asteroidCategory
       missile.physicsBody?.collisionBitMask = 0
       addChild(missile)
       let moveToTop = SKAction.moveTo(y: frame.height + 10, duration: 0.3)
       let remove = SKAction.removeFromParent()
       missile.run(SKAction.sequence([moveToTop, remove]))
   }
   func addAsteroid() {
       let names = ["asteroid1", "asteroid2", "asteroid3"]
       let index = Int(arc4random_uniform(UInt32(names.count)))
       let name = names[index]
       let asteroid = SKSpriteNode(imageNamed: name)
       let random = CGFloat(arc4random_uniform(UINT32_MAX)) / CGFloat(UINT32_MAX)
       let positionX = frame.width * (random - 0.5)
       asteroid.position = CGPoint(x: positionX, y: frame.height / 2 + asteroid.frame.height)
       asteroid.scale(to: CGSize(width: 70, height: 70))
       asteroid.physicsBody = SKPhysicsBody(circleOfRadius: asteroid.frame.width)
       asteroid.physicsBody?.categoryBitMask = asteroidCategory
       asteroid.physicsBody?.contactTestBitMask = missileCategory + spaceshipCategory + earthCategory
       asteroid.physicsBody?.collisionBitMask = 0
       addChild(asteroid)
       let move = SKAction.moveTo(y: -frame.height / 2 - asteroid.frame.height, duration: asteroudDuration)
       let remove = SKAction.removeFromParent()
       asteroid.run(SKAction.sequence([move, remove]))
   }
   func didBegin(_ contact: SKPhysicsContact) {
       var asteroid: SKPhysicsBody
       var target: SKPhysicsBody
       if contact.bodyA.categoryBitMask == asteroidCategory {
           asteroid = contact.bodyA
           target = contact.bodyB
       } else {
           asteroid = contact.bodyB
           target = contact.bodyA
       }
       guard let asteroidNode = asteroid.node else { return }
       guard target.node != nil else { return }
       guard let explosion = SKEmitterNode(fileNamed: "Explosion") else { return }
       explosion.position = asteroidNode.position
       addChild(explosion)
       asteroidNode.removeFromParent()
       self.run(SKAction.wait(forDuration: 1.0)) {
           explosion.removeFromParent()
       }
  }
     
   
   override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
       for touch in touches {
           let location = touch.location(in: self)
           print(location)
           
           spaceship.position.x = location.x
       }
   }
}

この記事が気に入ったらサポートをしてみませんか?