import SpriteKit
import GameplayKit
class GameScene: SKScene, SKPhysicsContactDelegate {
var bird = SKSpriteNode()
var skyColor:SKColor!
let birdCategory: UInt32 = 0x1 << 0
let rightCategory: UInt32 = 0x1 << 1
let leftCategory: UInt32 = 0x1 << 2
override func didMove(to view: SKView) {
self.physicsWorld.gravity = CGVector( dx: 0.0, dy: -2 )
self.physicsWorld.contactDelegate = self
createGround()
skyColor = SKColor(red: 81.0/255.0, green: 192.0/255.0, blue: 201.0/255.0, alpha: 1.0)
self.backgroundColor = skyColor
let framesOpening: [SKTexture] = {
let totalFrames = 10
let rows = 1
let columns = 4
let sheet = SpriteSheet(texture: SKTexture(imageNamed: "de3.png"), rows: rows, columns: columns, spacing: 0, margin: 0)
var frames = [SKTexture]()
var count = 0
for row in (0.., with event: UIEvent?) {
if bird.position.x >= self.frame.size.width{
bird.physicsBody?.velocity.dx = -60
}
if bird.position.x <= 0{
bird.physicsBody?.velocity.dx = 60
}
bird.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 50))
}
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 == birdCategory && secondBody.categoryBitMask == rightCategory{
firstBody.node?.xScale = -1.0
createGround2()
bird.physicsBody?.velocity.dx = -60
}
if firstBody.categoryBitMask == birdCategory && secondBody.categoryBitMask == leftCategory{
firstBody.node?.xScale = 1.0
createGround()
bird.physicsBody?.velocity.dx = 60
}
}
func createGround() {
let groundTexture = SKTexture(imageNamed: "ground")
for i in 0 ... 1 {
let ground = SKSpriteNode(texture: groundTexture)
ground.zPosition = -10
ground.position = CGPoint(x: (groundTexture.size().width / 2.0 + (groundTexture.size().width * CGFloat(i))), y: groundTexture.size().height / 4)
addChild(ground)
let moveLeft = SKAction.moveBy(x: -groundTexture.size().width, y: 0, duration: 10)
let moveReset = SKAction.moveBy(x: groundTexture.size().width, y: 0, duration: 0)
let moveLoop = SKAction.sequence([moveLeft, moveReset])
let moveForever = SKAction.repeatForever(moveLoop)
ground.run(moveForever)
//当たり判定の大きさ
ground.physicsBody = SKPhysicsBody(rectangleOf: ground.frame.size)
ground.physicsBody?.restitution = 0.4
ground.physicsBody?.friction = 0
ground.physicsBody?.linearDamping = 0
ground.physicsBody?.isDynamic = false
}
}
func createGround2() {
let groundTexture = SKTexture(imageNamed: "ground")
for i in 0 ... 1 {
let ground = SKSpriteNode(texture: groundTexture)
ground.zPosition = -10
ground.position = CGPoint(x: (groundTexture.size().width / 2.0 + (groundTexture.size().width * CGFloat(i))), y: groundTexture.size().height / 4)
addChild(ground)
let moveright = SKAction.moveBy(x: -groundTexture.size().width, y: 0, duration: 0)
let moveReset = SKAction.moveBy(x: groundTexture.size().width, y: 0, duration: 10)
let moveLoop = SKAction.sequence([moveright, moveReset])
let moveForever = SKAction.repeatForever(moveLoop)
ground.run(moveForever)
ground.physicsBody = SKPhysicsBody(rectangleOf: ground.frame.size)
ground.physicsBody?.restitution = 0.4
ground.physicsBody?.friction = 0
ground.physicsBody?.linearDamping = 0
ground.physicsBody?.isDynamic = false
}
}
}
class SpriteSheet {
let texture: SKTexture
let rows: Int
let columns: Int
var margin: CGFloat=0
var spacing: CGFloat=0
var frameSize: CGSize {
return CGSize(width: (self.texture.size().width-(self.margin*2+self.spacing*CGFloat(self.columns-1)))/CGFloat(self.columns),
height: (self.texture.size().height-(self.margin*2+self.spacing*CGFloat(self.rows-1)))/CGFloat(self.rows))
}
init(texture: SKTexture, rows: Int, columns: Int, spacing: CGFloat, margin: CGFloat) {
self.texture=texture
self.rows=rows
self.columns=columns
self.spacing=spacing
self.margin=margin
}
convenience init(texture: SKTexture, rows: Int, columns: Int) {
self.init(texture: texture, rows: rows, columns: columns, spacing: 0, margin: 0)
}
func textureForColumn(column: Int, row: Int)->SKTexture? {
if !(0...self.rows ~= row && 0...self.columns ~= column) {
return nil
}
var textureRect = CGRect(x: self.margin + CGFloat(column) * (self.frameSize.width+self.spacing)-self.spacing, y: self.margin + CGFloat(self.rows - row - 1) * (self.frameSize.height+self.spacing)-self.spacing,
height: self.frameSize.height)
textureRect = CGRect(x: textureRect.origin.x/self.texture.size().width, y: textureRect.origin.y/self.texture.size().height,
width: textureRect.size.width/self.texture.size().width, height: textureRect.size.height/self.texture.size().height)
return SKTexture(rect: textureRect, in: self.texture)
}
}