import UIKit
import SpriteKit
enum CollisionTypes: UInt32 {
case player = 1
case wall = 2
case star = 4
case vortex = 8
case finish = 16
}
class GameScene: SKScene, SKPhysicsContactDelegate {
var player: SKSpriteNode!
var block: SKSpriteNode!
var vortex: SKSpriteNode!
var star: SKSpriteNode!
var finish: SKSpriteNode!
var ball: SKShapeNode!
var isGameOver = false
override func didMove(to view: SKView) {
backgroundColor = .white
self.physicsWorld.contactDelegate = self
self.physicsBody = SKPhysicsBody(edgeLoopFrom: self.frame)
physicsWorld.gravity = CGVector(dx: 0.0, dy: 0.0)
ball = SKShapeNode(circleOfRadius: 10) //circleOfRadiusで円の半径
ball.position = CGPoint(x:self.frame.midX, y:self.frame.midY+200)
ball.fillColor = UIColor.red
ball.lineWidth = 0.0
ball.strokeColor = UIColor.red
ball.physicsBody = SKPhysicsBody(circleOfRadius: ball.frame.width/2)
self.addChild(ball)
ball.physicsBody?.applyImpulse(CGVector(dx: 40, dy: 40))
ball.physicsBody?.affectedByGravity = false
ball.physicsBody?.isDynamic = false
ball.physicsBody?.friction = 0
ball.physicsBody?.restitution = 1
ball.name = "ball"
ball.physicsBody?.collisionBitMask = 0
// ball.physicsBody?.categoryBitMask = CollisionTypes.wall.rawValue
// ball.physicsBody?.contactTestBitMask = CollisionTypes.player.rawValue
block = SKSpriteNode(imageNamed: "block")
block.position = CGPoint(x: 70, y: 200)
block.physicsBody = SKPhysicsBody(rectangleOf: block.size)
// block.physicsBody?.categoryBitMask = CollisionTypes.wall.rawValue
// block.physicsBody?.contactTestBitMask = CollisionTypes.player.rawValue
block.physicsBody?.collisionBitMask = 0
block.physicsBody?.isDynamic = false
block.name = "block"
addChild(block)
vortex = SKSpriteNode(imageNamed: "vortex")
vortex.name = "vortex"
vortex.position = CGPoint(x: 150, y: 200)
vortex.run(SKAction.repeatForever(SKAction.rotate(byAngle: CGFloat.pi, duration: 1)))
vortex.physicsBody = SKPhysicsBody(circleOfRadius: vortex.size.width / 2)
vortex.physicsBody?.isDynamic = false
// vortex.physicsBody?.categoryBitMask = CollisionTypes.vortex.rawValue
// vortex.physicsBody?.contactTestBitMask = CollisionTypes.player.rawValue
// vortex.physicsBody?.collisionBitMask = 0
addChild(vortex)
star = SKSpriteNode(imageNamed: "star")
star.physicsBody = SKPhysicsBody(circleOfRadius: star.size.width / 2)
star.name = "star"
star.position = CGPoint(x: 220, y: 200)
star.physicsBody?.isDynamic = false
// star.physicsBody?.categoryBitMask = CollisionTypes.star.rawValue
// star.physicsBody?.contactTestBitMask = CollisionTypes.player.rawValue
// star.physicsBody?.collisionBitMask = 0
addChild(star)
finish = SKSpriteNode(imageNamed: "finish")
finish.name = "finish"
finish.position = CGPoint(x: 300, y: 200)
finish.physicsBody = SKPhysicsBody(circleOfRadius: finish.size.width / 2)
finish.physicsBody?.isDynamic = false
// finish.physicsBody?.categoryBitMask = CollisionTypes.finish.rawValue
// finish.physicsBody?.contactTestBitMask = CollisionTypes.player.rawValue
// finish.physicsBody?.collisionBitMask = 0
addChild(finish)
player = SKSpriteNode(imageNamed: "player")
player.position = CGPoint(x: 96, y: 572)
player.physicsBody = SKPhysicsBody(circleOfRadius: player.size.width / 2)
player.physicsBody?.allowsRotation = false
player.physicsBody?.linearDamping = 0.5
player.physicsBody?.categoryBitMask = CollisionTypes.player.rawValue
player.physicsBody?.contactTestBitMask = CollisionTypes.finish.rawValue
// player.physicsBody?.contactTestBitMask = CollisionTypes.star.rawValue | CollisionTypes.vortex.rawValue | CollisionTypes.finish.rawValue | CollisionTypes.wall.rawValue
// player.physicsBody?.collisionBitMask = CollisionTypes.wall.rawValue
player.physicsBody?.collisionBitMask = 0
addChild(player)
}
override func touchesBegan(_ touches: Set, with event: UIEvent?) {
for touches : AnyObject in touches{
// タッチした点を得る.
let location = touches.location(in: self)
// circle.position = location
// 移動時間を1秒に指定しています。
let travelTime = SKAction.move(to: location, duration: 1)
player.run(travelTime)
}
}
override func touchesEnded(_ touches: Set, with event: UIEvent?) {
// Stop node from moving to touch
// touched = false
}
func didBegin(_ contact: SKPhysicsContact) {
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 == "vortex" {
player.physicsBody?.isDynamic = false
// if isGameOver == true {
// starfield.removeFromParent()
// sprite.removeFromParent()
// }
// isGameOver = true
// score -= 1
// let move = SKAction.move(to: node.position, duration: 0.25)
let fade = SKAction.fadeAlpha(to: 0.5, duration: 1)
// let scale = SKAction.scale(to: 0.01, duration: 1)
// let scale2 = SKAction.scale(to: 1, duration: 1)
// let remove = SKAction.removeFromParent()
// let sequence = SKAction.sequence([move, scale, scale2])
// let moveRepeat = SKAction.repeatForever(sequence)
player.run(fade, withKey:"moving")
player.removeFromParent()
// if let action = player.action(forKey: "moving") {
// action.speed = 1
// }
let scene = GameScene2(size: self.frame.size)
let transition = SKTransition.moveIn(with: SKTransitionDirection.right, duration: 1.5)
self.view?.presentScene(scene, transition: transition)
} else if node.name == "star" {
node.removeFromParent()
// score += 1
} else if node.name == "finish" {
let move = SKAction.move(to: node.position, duration: 0.25)
let scale = SKAction.scale(to: 0.01, duration: 1)
let remove = SKAction.removeFromParent()
let sequence = SKAction.sequence([move, scale, remove])
node.run(sequence, withKey:"moving")
// node.setScale(1.6)
// node.removeFromParent()
} else if node.name == "block" {
// node.removeFromParent()
}
else if node.name == "ball" {
// node.removeFromParent()
ball.fillColor = UIColor.blue
node.alpha = 0.6
node.setScale(5.0)
}
}
override func update(_ currentTime: TimeInterval) {
// if !isGameOver{
// score += 1
// }
}
}