Skip to content

Instantly share code, notes, and snippets.

@mzsima
Created August 29, 2015 15:15
Show Gist options
  • Save mzsima/30c28e72d4dde5b6d2b1 to your computer and use it in GitHub Desktop.
Save mzsima/30c28e72d4dde5b6d2b1 to your computer and use it in GitHub Desktop.
Hopscotch tile
//
// ViewController.swift
// HopscotchTile
//
// Created by Mizushima Yusuke on 8/29/15.
// Copyright (c) 2015 Yusuke Mizusima. All rights reserved.
//
import UIKit
import SceneKit
class ViewController: UIViewController {
weak var sceneView : SCNView?
var count = 0
override func viewDidLoad() {
super.viewDidLoad()
setupScene()
createTile()
}
func setupScene() {
let sv = SCNView(frame: view.bounds)
sv.backgroundColor = UIColor.brownColor()
sv.scene = SCNScene()
sv.autoenablesDefaultLighting = true
view.addSubview(sv)
self.sceneView = sv
}
func createTile() {
let s = 1 as CGFloat
for i in 0...99 {
let x = Float(i % 10) * 1.4 + Float(i/10) * 0.4
let y = Float(i / 10) + Float(i%10) * 0.6
let box = SCNBox(width: s, height: s, length: 0.1, chamferRadius: 0.05);
box.firstMaterial?.diffuse.contents = UIColor.lightGrayColor()
let boxnode = SCNNode(geometry: box)
boxnode.name = "\(i)"
boxnode.position = SCNVector3(x: x, y: y, z: 0)
self.sceneView?.scene?.rootNode.addChildNode(boxnode)
}
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
func typeA(n: SCNNode) -> Void {
let numA = n.name!.toInt()! / 10
let numB = n.name!.toInt()! % 10
n.runAction(SCNAction.sequence([
SCNAction.moveByX(-CGFloat(numA) * 0.4, y: 0, z: 0, duration: 1.0),
SCNAction.moveByX(-CGFloat(numB) * 0.4, y: 0, z: 0, duration: 1.0),
SCNAction.moveByX(0, y: -CGFloat(numB) * 0.6, z: 0, duration: 1.0),
]))
}
func typeB(n: SCNNode) -> Void {
let i = n.name!.toInt()!
let x = Float(i % 10) * 1.4 + Float(i/10) * 0.4
let y = Float(i / 10) + Float(i%10) * 0.6
n.runAction(SCNAction.moveTo(SCNVector3(x: x, y: y, z: 0), duration: 3.0))
}
self.sceneView?.scene?.rootNode.childNodes
.filter { ($0 as! SCNNode).name != nil }
.foreach { n -> Void in
if self.count == 0 { typeA(n as! SCNNode) }
else { typeB(n as! SCNNode) }
}
self.count = (self.count + 1) % 2;
}
}
extension Array {
func foreach(doit:(T)->Void) {for i in self {doit(i)} }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment