import SpriteKit
import GameplayKit
class GameScene: SKScene, SKPhysicsContactDelegate {
let playerCategory: UInt32 = 0x1 << 0
let backgroundCategory: UInt32 = 0x1 << 1
let leftCategory: UInt32 = 0x1 << 2
var player = SKSpriteNode()
let cam = SKCameraNode()
private var background = SKSpriteNode()
override func didMove(to view: SKView) {
self.physicsWorld.gravity = CGVector( dx: 0.0, dy: 0.0 )
self.physicsWorld.contactDelegate = self
self.backgroundColor = .white
self.camera = cam
self.addChild(cam)
cam.physicsBody?.isDynamic = false
cam.physicsBody?.velocity = CGVector(dx: 60, dy: 0)
background = SKSpriteNode(imageNamed: "bg3")
background.position = CGPoint(x: frame.width / 2, y: frame.height / 2)
background.size = CGSize(width: 4500, height: frame.size.height)
background.name = "back"
background.zPosition = -10
self.addChild(background)
background.physicsBody?.restitution = 0.9
background.physicsBody?.friction = 0
background.physicsBody?.linearDamping = 0
background.physicsBody?.isDynamic = false
createPlayer()
let leftRect = CGRect(x: 0, y: 0, width: 1, height: self.frame.size.height)
let left = SKNode()
left.physicsBody = SKPhysicsBody(edgeLoopFrom: leftRect)
self.addChild(left)
left.name = "left"
let rightRect = CGRect(x: 2040, y: 0, width: 1, height: background.frame.size.height)
background.physicsBody = SKPhysicsBody(edgeLoopFrom: rightRect)
player.physicsBody?.categoryBitMask = playerCategory
background.physicsBody?.categoryBitMask = backgroundCategory
left.physicsBody?.categoryBitMask = leftCategory
player.physicsBody?.contactTestBitMask = backgroundCategory | leftCategory
let rect = SKSpriteNode(imageNamed: "blue.png")
rect.position = CGPoint(x: 400, y: 210)
rect.size = CGSize(width: frame.size.width/2, height: frame.size.width/2)
self.addChild(rect)
let rectCopy1 = rect.copy() as! SKSpriteNode;
rectCopy1.position = CGPoint(x: 800, y: 207)
rectCopy1.color = .yellow
rectCopy1.colorBlendFactor = 1.0
self.addChild(rectCopy1)
let rectCopy2 = rect.copy() as! SKSpriteNode;
rectCopy2.position = CGPoint(x: 1200, y: 207)
rectCopy2.alpha = 0.5
self.addChild(rectCopy2)
let rectCopy3 = rect.copy() as! SKSpriteNode;
rectCopy3.position = CGPoint(x: 1600, y: 207)
rectCopy3.color = .purple
rectCopy3.colorBlendFactor = 0.6
self.addChild(rectCopy3)
let rectCopy4 = rect.copy() as! SKSpriteNode;
rectCopy4.position = CGPoint(x: 2000, y: 207)
rectCopy4.color = .yellow
rectCopy4.colorBlendFactor = 0.3
self.addChild(rectCopy4)
}
func createPlayer() {
let playerTexture = SKTexture(imageNamed: "kaeru1")
player = SKSpriteNode(texture: playerTexture)
player.zPosition = 10
player.position = CGPoint(x: frame.width / 2, y: frame.height / 2)
addChild(player)
let f0 = SKTexture(imageNamed: "kaeru1")
let f1 = SKTexture(imageNamed: "kaeru2")
let f2 = SKTexture(imageNamed: "kaeru3")
let f3 = SKTexture(imageNamed: "kaeru4")
let f4 = SKTexture(imageNamed: "kaeru5")
let f5 = SKTexture(imageNamed: "kaeru6")
let f6 = SKTexture(imageNamed: "kaeru7")
let f7 = SKTexture(imageNamed: "kaeru8")
let animation = SKAction.animate(with: [playerTexture, f0, f1, f2, f3, f4, f5, f6, f7], timePerFrame: 0.1)
let runForever = SKAction.repeatForever(animation)
player.run(runForever)
player.xScale = -1.0
player.physicsBody = SKPhysicsBody(circleOfRadius: player.frame.width/1.55)
player.physicsBody?.friction = 0
player.physicsBody?.linearDamping = 0
player.physicsBody?.angularDamping = 0
player.physicsBody?.isDynamic = true
player.physicsBody?.allowsRotation = false
player.physicsBody?.restitution = 0.6
player.physicsBody?.velocity = CGVector(dx: 60, dy: 0)
}
override func update(_ currentTime: TimeInterval) {
super.update(currentTime)
}
//衝突判定処理
func didBegin(_ contact: SKPhysicsContact) {
var firstBody: SKPhysicsBody
var secondBody: SKPhysicsBody
if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
firstBody = contact.bodyA
secondBody = contact.bodyB
} else {
firstBody = contact.bodyB
secondBody = contact.bodyA
}
if firstBody.categoryBitMask == playerCategory && secondBody.categoryBitMask == backgroundCategory{
firstBody.node?.xScale = 1.0
player.physicsBody?.velocity.dx = -60
}
if firstBody.categoryBitMask == playerCategory && secondBody.categoryBitMask == leftCategory{
firstBody.node?.xScale = -1.0
player.physicsBody?.velocity.dx = 60
}
}
override func didSimulatePhysics() {
self.camera?.position = CGPoint(x: self.player.position.x + 50, y:self.player.position.y)
// self.camera?.position = player.position
}
}