import SpriteKit
import GameplayKit
enum CollisionTypes: UInt32 {
case ship = 1
case bullet = 2
case enemy = 4
case beam = 8
case rect = 16
case ball = 32
}
class GameScene: SKScene, SKPhysicsContactDelegate {
var label = SKLabelNode(fontNamed: "HelveticaNeue-Light")
var score = 5
var ship = SKSpriteNode()
var bullet : SKSpriteNode!
var enemy : SKSpriteNode!
var beam : SKSpriteNode!
var touched:Bool = false
var lastUpdateTime2 : TimeInterval = 0
var myrect : SKShapeNode!
var ball : SKShapeNode!
let red = CGFloat.random(in: 0...1)
let gr = CGFloat.random(in: 0...1)
let bl = CGFloat.random(in: 0...1)
override func didMove(to view: SKView) {
backgroundColor = .blue
self.physicsWorld.contactDelegate = self;
self.physicsBody = SKPhysicsBody(edgeLoopFrom: self.frame)
label.text = "0"
let labelSize: CGFloat = 60.0
label.fontSize = labelSize
label.position = CGPoint(x:self.frame.midX, y:self.frame.height-120)
label.fontColor = SKColor.black
self.addChild(label)
shipstart()
rectTo()
ballTo()
}
public func randomColor(opacity: CGFloat) -> UIColor {
let r: UInt32 = arc4random_uniform(255)
let g: UInt32 = arc4random_uniform(255)
let b: UInt32 = arc4random_uniform(255)
return UIColor(red: CGFloat(r) / 255.0, green: CGFloat(g) / 255.0, blue: CGFloat(b) / 255.0, alpha: opacity)
}
func rectTo(){
for _ in 0...7 {
myrect = SKShapeNode(rectOf: CGSize(width:40, height:40),cornerRadius: 5)
myrect.position = CGPoint(x: Int.random(in: 50...350), y: Int.random(in: 450...600))
myrect.fillColor = randomColor(opacity: 1.0)
myrect.lineWidth = 0.0
myrect.physicsBody = SKPhysicsBody(rectangleOf:myrect.frame.size)
myrect.physicsBody?.affectedByGravity = false
myrect.physicsBody?.isDynamic = false
myrect.name = "rect"
self.addChild(myrect)
myrect.physicsBody?.categoryBitMask = CollisionTypes.rect.rawValue
}
}
func ballTo(){
for j in 0...5 {
let color1 = UIColor(red: red*CGFloat(j), green: gr*CGFloat(j), blue: bl*CGFloat(j), alpha: 1)
let radius: CGFloat = 18
ball = SKShapeNode(circleOfRadius: radius)
let randIntX = CGFloat(Int.random(in : 0...300) + 50)
let randIntY = CGFloat(Int.random(in : 0...200) + 150)
ball.fillColor = color1
ball.position = CGPoint(x:randIntX, y:randIntY)
ball.physicsBody = SKPhysicsBody(circleOfRadius: ball.frame.width / 2)
ball.physicsBody!.isDynamic = true
ball.physicsBody!.affectedByGravity = false
addChild( ball)
ball.name = "ball"
ball.physicsBody?.categoryBitMask = CollisionTypes.ball.rawValue
}
}
func shipstart(){
ship = SKSpriteNode(imageNamed: "ship")
ship.position = CGPoint(x: self.frame.midX, y: frame.height / 11 )
ship.setScale(1.2)
addChild(self.ship)
ship.physicsBody = SKPhysicsBody(rectangleOf: ship.frame.size)
ship.physicsBody!.isDynamic = true
ship.physicsBody!.affectedByGravity = false
ship.physicsBody?.allowsRotation = false
ship.name = "ship"
ship.physicsBody?.categoryBitMask = CollisionTypes.ship.rawValue
ship.physicsBody?.collisionBitMask = 0
}
func shoot() {
bullet = SKSpriteNode(imageNamed: "beam")
bullet.physicsBody = SKPhysicsBody(circleOfRadius: bullet.size.width/2)
bullet.physicsBody!.isDynamic = true
bullet.physicsBody!.affectedByGravity = false
bullet.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 0))
bullet.physicsBody?.allowsRotation = false
bullet.physicsBody?.allowsRotation = false
// bullet.physicsBody?.usesPreciseCollisionDetection = true
bullet.physicsBody = SKPhysicsBody(rectangleOf: bullet.size)
self.addChild(bullet)
bullet.name = "bullet"
bullet.anchorPoint = CGPoint(x: 0.5, y: 0)
bullet.position = CGPoint(x: ship.position.x, y: ship.position.y + ship.size.height * 0.5)
bullet.physicsBody?.categoryBitMask = CollisionTypes.bullet.rawValue
bullet.physicsBody?.contactTestBitMask = CollisionTypes.enemy.rawValue | CollisionTypes.rect.rawValue | CollisionTypes.ball.rawValue
// bullet.position = CGPoint(x: self.ship.position.x - 2, y: self.ship.position.y)
let move = SKAction.moveTo(y: frame.height, duration: 0.6)
let remove = SKAction.removeFromParent()
bullet.run(SKAction.sequence([move, remove]))
}
// teki
private func spawnEnemy() {
enemy = SKSpriteNode(imageNamed: "enemy_ship")
enemy.anchorPoint = CGPoint(x: 0.5, y: 0.5)
enemy.position.x = size.width * (0.25 + CGFloat(arc4random_uniform(5)) / 10.0) enemy.position.y = size.height + enemy.size.height * 0.5
enemy.zPosition = ship.zPosition + 10
enemy.name = "enemy"
enemy.setScale(0.9)
enemy.physicsBody = SKPhysicsBody(rectangleOf: enemy.size)
enemy.physicsBody?.affectedByGravity = false
enemy.physicsBody?.categoryBitMask = CollisionTypes.enemy.rawValue
enemy.physicsBody?.contactTestBitMask = shipCategory
enemy.physicsBody?.collisionBitMask = 0
let verticalAction = SKAction.sequence([
SKAction.playSoundFileNamed("enemy_spawn.wav", waitForCompletion: false),
SKAction.moveBy(x: 0, y: -(size.height + enemy.size.height * 0.5), duration: TimeInterval(Int(3 + arc4random_uniform(3)))),
SKAction.removeFromParent()
])
let horizontalAction = SKAction.repeatForever(
SKAction.sequence([
SKAction.wait(forDuration: 0.9, withRange: 3),
SKAction.run {
self.enemy.run(SKAction.moveBy(x: 50.0 - CGFloat(arc4random_uniform(100)), y: 0, duration: 0.5))
}
])
)
let beamAction = SKAction.repeatForever(
SKAction.sequence([
SKAction.wait(forDuration: 0.9, withRange: 3),
SKAction.run {
self.spawnEnemyBeam(enemy: self.enemy);
}
])
)
enemy.run(SKAction.group([verticalAction, horizontalAction, beamAction]))
addChild(enemy)
}
private func spawnEnemyBeam(enemy: SKSpriteNode) {
beam = SKSpriteNode(imageNamed: "enemy_beam")
beam.anchorPoint = CGPoint(x: 0.5, y: 0)
beam.position = CGPoint(x: enemy.position.x, y: enemy.position.y - enemy.size.height * 0.5)
beam.zPosition = enemy.zPosition - 1
beam.name = "enemy_beam"
beam.physicsBody = SKPhysicsBody(rectangleOf: beam.size)
beam.physicsBody?.affectedByGravity = false
beam.physicsBody?.categoryBitMask = CollisionTypes.beam.rawValue
beam.physicsBody?.contactTestBitMask = CollisionTypes.ship.rawValue | CollisionTypes.ball.rawValue
beam.physicsBody?.collisionBitMask = 0
let action = SKAction.sequence([
SKAction.playSoundFileNamed("enemy_beam.wav", waitForCompletion: false),
SKAction.moveBy(x: 0, y: -size.height, duration: 0.8),
SKAction.removeFromParent()
])
beam.run(action)
addChild(beam)
}
override func touchesEnded(_ touches: Set, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: self)
let touchedNode = atPoint(location)
if touchedNode.name == "ship" {
shoot()
}
}
}
override func touchesMoved(_ touches: Set, with event: UIEvent?) {
// for touch in touches {
// let toucLocation = touch.location(in: self)
// ship.position.x = toucLocation.x
// }
guard let touch = touches.first else { return }
let toucLocation = touch.location(in: self)
ship.position.x = toucLocation.x
}
override func update(_ currentTime: TimeInterval) {
if lastUpdateTime2 == 0 {
lastUpdateTime2 = currentTime
}
if currentTime - lastUpdateTime2 > 3 {
// 更新コマンドは3秒ごとに起動
spawnEnemy()
lastUpdateTime2 = currentTime
}
}
func didBegin(_ contact: SKPhysicsContact) {
guard let nodeA = contact.bodyA.node else { return }
guard let nodeB = contact.bodyB.node else { return }
if nodeA == bullet {
playerCollided(with: nodeB)
} else if nodeB == bullet {
playerCollided(with: nodeA)
}
if nodeA == beam {
beamCollided(with: nodeB)
} else if nodeB == beam {
beamCollided(with: nodeA)
}
}
func beamCollided(with node: SKNode) {
if node.name == "ship" {
node.removeFromParent()
beam.removeFromParent()
let gameOverScene = GameOverScene(size: self.frame.size)
self.view?.presentScene(gameOverScene)
} else if node.name == "ball" {
node.removeFromParent()
beam.removeFromParent()
}
}
func playerCollided(with node: SKNode) {
if node.name == "enemy" {
node.removeFromParent()
bullet.removeFromParent()
score += 2
label.text = "\(score)"
if self.score >= 20 {
let gameOverScene = GameClearScene(size: self.frame.size)
self.view?.presentScene(gameOverScene)
}
} else if node.name == "rect" {
node.removeFromParent()
bullet.removeFromParent()
}else if node.name == "ball" {
node.removeFromParent()
bullet.removeFromParent()
}
}
override func touchesBegan(_ touches: Set, with event: UIEvent?) {
}