iPhoneポテトスティク

ポテトっぽいスティクの当たりくじゲームiPhoneアプリのサンプルコードを描いてみます。

import UIKit

import SceneKit

class ViewController: UIViewController {

    weak var sceneView : SCNView?

    

    override func viewDidLoad() {

        super.viewDidLoad()

        setupScene()

        createBox()

        createSticks()

        createCamera()

    }

    

    func setupScene() {

        sceneView = {

            let sv = SCNView(frame: self.view.bounds)

            sv.backgroundColor = UIColor(hue: 0, saturation: 0.1, brightness: 1, alpha: 1)

            sv.scene = SCNScene()

            sv.autoenablesDefaultLighting = true

            sv.allowsCameraControl = true

            self.view.addSubview(sv)

            return sv

        }()

    }

    

    func createBox() {

        

        let part = SCNTube(innerRadius: 9.5, outerRadius: 10, height: 20)

        part.firstMaterial?.diffuse.contents = UIColor(hue: 0, saturation: 0.6, brightness: 1, alpha: 1)

        let partNode = SCNNode(geometry: part)

        partNode.transform = SCNMatrix4Scale(partNode.transform, 1.0, 1.0, 0.6)

        self.sceneView?.scene?.rootNode.addChildNode(partNode)

        

        partNode.physicsBody = SCNPhysicsBody.staticBody()

        partNode.physicsBody?.physicsShape = SCNPhysicsShape(node: partNode, options: [SCNPhysicsShapeTypeKey : SCNPhysicsShapeTypeConcavePolyhedron, SCNPhysicsShapeScaleKey : NSValue(SCNVector3: SCNVector3(x: 1, y: 1, z: 0.6))])

        

        let bottom = SCNCylinder(radius: 9.5, height: 0.5)

        bottom.firstMaterial?.diffuse.contents = part.firstMaterial?.diffuse.contents

        let bottomNode = SCNNode(geometry: bottom)

        bottomNode.position = SCNVector3(x: 0, y: –9.5, z: 0)

        bottomNode.transform = SCNMatrix4Scale(bottomNode.transform, 1.0, 1.0, 0.6)

        self.sceneView?.scene?.rootNode.addChildNode(bottomNode)

        

        bottomNode.physicsBody = SCNPhysicsBody.staticBody()

        bottomNode.physicsBody?.physicsShape = SCNPhysicsShape(node: bottomNode, options: [SCNPhysicsShapeTypeKey : SCNPhysicsShapeTypeConcavePolyhedron, SCNPhysicsShapeScaleKey : NSValue(SCNVector3: SCNVector3(x: 1, y: 1, z: 0.6))])

    }

    

    func createSticks () {

        

        let target = [Int](0...10)[Int(arc4random() % 10)]

        

        [Int](010).each {i in

            let stick = SCNBox(width: 1.6, height: 25, length: 1.6, chamferRadius: 0.1)

            stick.firstMaterial?.diffuse.contents = UIColor(hue: 0.14, saturation: 0.7, brightness: 0.9, alpha: 1)

            let stickNode = SCNNode(geometry: stick)

            stickNode.name = “stick \(i)

            stickNode.position = SCNVector3(x: 8Float(i) * 1.6 , y: 5, z: 0)

            self.sceneView?.scene?.rootNode.addChildNode(stickNode)

            stickNode.physicsBody = SCNPhysicsBody.dynamicBody()

            stickNode.physicsBody?.physicsShape = SCNPhysicsShape(geometry: stick, options: nil)

            return stickNode

            }

            .filter {$0.name == “stick \(target)}

            .map { j -> Void in

                let mark = SCNBox(width: 1.8, height: 3, length: 1.8, chamferRadius: 0.12)

                mark.firstMaterial?.diffuse.contents = UIColor(hue: 0, saturation: 0.8, brightness: 0.6, alpha: 1)

                let markNode = SCNNode(geometry: mark)

                markNode.position = SCNVector3(x: 0, y: –10, z: 0)

                j.addChildNode(markNode)

            }

    }

    func createCamera() {

        let camera = SCNNode()

        camera.camera = SCNCamera()

        camera.position = SCNVector3(x: 0, y: 30, z: 70)

        camera.rotation = SCNVector4(x: 1, y: 0, z: 0, w: –0.4)

        self.sceneView?.scene?.rootNode.addChildNode(camera)

    }

    

    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {

        let p = touches.anyObject()!.locationInView(self.sceneView)

        if let hit = self.sceneView?.hitTest(p, options: [SCNHitTestSortResultsKey : true])?.first as? SCNHitTestResult {

            hit.node.physicsBody?.applyForce(SCNVector3(x: 0, y: 30, z: 0), impulse: true)

        }

    }

}

extension Array {

    func each( doit:(T) -> AnyObject) -> [AnyObject] {

        var after = [AnyObject]()

        for i in self {

            let res : AnyObject = doit(i)

            after.append(res)

        }

        return after

    }

}