import SpriteKit
import GameplayKit
extension SKPhysicsBody {
func ideal() -> SKPhysicsBody {
self.friction = 0
self.linearDamping = 0
self.angularDamping = 0
self.restitution = 0.98
self.isDynamic = true
self.allowsRotation = false
self.affectedByGravity = true
return self
}
}
enum CollisionTypes: UInt32 {
case player = 1
case squareA = 2
case squareB = 4
case bottom = 8
}
class GameScene: SKScene, SKPhysicsContactDelegate {
var n = 0
let label = SKLabelNode(text: "kaisu: 0kai")
var audio = SKAudioNode()
var player = SKShapeNode()
var squareA = SKShapeNode()
override func didMove(to view: SKView) {
backgroundColor = .blue
self.physicsWorld.contactDelegate = self
physicsBody = SKPhysicsBody(edgeLoopFrom: frame)
physicsWorld.gravity = CGVector(dx: 0, dy: -1.5)
let labelSize: CGFloat = 30.0
label.fontSize = labelSize
label.position = CGPoint(x:self.frame.midX, y:self.frame.height/1.12)
label.fontColor = SKColor.white
self.addChild(label)
player = SKShapeNode(circleOfRadius: 30)
player.position = CGPoint(x:self.frame.midX, y:self.frame.midY + 140)
player.fillColor = UIColor.red
player.lineWidth = 4.0
player.strokeColor = UIColor.lightGray
self.addChild(player)
player.name = "player"
player.physicsBody = SKPhysicsBody(circleOfRadius: player.frame.width/2).ideal()
squareA = SKShapeNode(rectOf: CGSize(width:50, height:50),cornerRadius: 3)
squareA.position = CGPoint(x:self.frame.midX, y:self.frame.midY + 10)
squareA.fillColor = UIColor.yellow
squareA.lineWidth = 2.0
squareA.strokeColor = UIColor.lightGray
self.addChild(squareA)
squareA.name = "squareA"
squareA.physicsBody = SKPhysicsBody(rectangleOf: squareA.frame.size).ideal()
squareA.physicsBody?.affectedByGravity = false
let squareB = SKShapeNode(rectOf: CGSize(width:100, height:100),cornerRadius: 5)
squareB.position = CGPoint(x:self.frame.midX, y:self.frame.midY - 200)
squareB.fillColor = .green
squareB.lineWidth = 0.0
self.addChild(squareB)
squareB.name = "squareB"
squareB.physicsBody = SKPhysicsBody(rectangleOf: squareB.frame.size).ideal()
let bottomRect = CGRect(x: self.frame.origin.x, y: self.frame.origin.y, width: self.frame.size.width, height: 1)
let bottom = SKNode()
bottom.physicsBody = SKPhysicsBody(edgeLoopFrom: bottomRect)
self.addChild(bottom)
bottom.name = "bottom"
player.physicsBody?.categoryBitMask = CollisionTypes.player.rawValue
squareA.physicsBody?.categoryBitMask = CollisionTypes.squareA.rawValue
squareB.physicsBody?.categoryBitMask = CollisionTypes.squareB.rawValue
//bottomカテゴリ
bottom.physicsBody?.categoryBitMask = CollisionTypes.bottom.rawValue
player.physicsBody?.contactTestBitMask = CollisionTypes.squareA.rawValue | CollisionTypes.squareB.rawValue | CollisionTypes.bottom.rawValue
}
func testAudio() {
audio = SKAudioNode(fileNamed: "Sounds/sound.wav")
audio.autoplayLooped = false
addChild(audio)
let playAction = SKAction.play()
let wait = SKAction.wait(forDuration: 0.2)
let grp = SKAction.group([playAction, wait])
let rfp = SKAction.removeFromParent()
let sequence = SKAction.sequence([grp, rfp])
audio.run(sequence)
}
func testParticle() {
let explosion = SKEmitterNode(fileNamed: "MyParticle.sks")!
explosion.position = CGPoint(x: squareA.position.x, y: squareA.position.y + squareA.frame.size.height * 0.5)
// explosion.position = squareA.position
// beam.position = CGPoint(x: player.position.x, y: player.position.y + player.size.height * 0.5)
explosion.name = "explosion1"
explosion.xScale = 0.6
explosion.yScale = 0.6
explosion.zPosition = 10
let action1 = SKAction.fadeOut(withDuration: 0.2)
let action2 = SKAction.removeFromParent()
let actionAll = SKAction.sequence([action1, action2])
self.addChild(explosion)
explosion.run(actionAll)
}
func didBegin(_ contact: SKPhysicsContact) {
n += 1
label.text = "kaisu: \(n)kai"
guard let nodeA = contact.bodyA.node else { return }
guard let nodeB = contact.bodyB.node else { return }
if nodeA == player {
playerCollided(with: contact.bodyB.node!)
} else if nodeB == player {
playerCollided(with: contact.bodyA.node!)
}
}
func playerCollided(with node: SKNode) {
if node.name == "squareA" {
testAudio()
testParticle()
node.removeFromParent()
} else if node.name == "squareB" {
node.removeFromParent()
} else if node.name == "bottom" {
player.removeFromParent()
testAudio()
}
}
override func touchesBegan(_ touches: Set, with event: UIEvent?) {
}
}