Skip to content

Instantly share code, notes, and snippets.

@mzsima
Last active September 10, 2015 13:19
Show Gist options
  • Save mzsima/12c1545f5c55184de6ea to your computer and use it in GitHub Desktop.
Save mzsima/12c1545f5c55184de6ea to your computer and use it in GitHub Desktop.
I got a MarioMaker!
//
// ViewController.swift
// GetFlag
//
// Created by MizushimaYusuke on 9/10/15.
// Copyright (c) 2015 MizushimaYusuke. All rights reserved.
//
import UIKit
import SceneKit
class ViewController: UIViewController {
weak var sceneView : SCNView?
override func viewDidLoad() {
super.viewDidLoad()
setupScene()
createBlocks()
createFlag()
createMushroom()
createCamera()
}
func setupScene() {
let sv = SCNView(frame: view.bounds)
sv.scene = SCNScene()
sv.backgroundColor = UIColor(hue: 0.5, saturation: 0.2, brightness: 1, alpha: 1)
sv.autoenablesDefaultLighting = true
sv.allowsCameraControl = true
view.addSubview(sv)
sceneView = sv
}
func createBlocks() {
let b = SCNBox(width: 0.98, height: 0.98, length: 0.98, chamferRadius: 0.1)
b.firstMaterial?.diffuse.contents = UIColor(hue:0.12, saturation: 1, brightness: 0.6, alpha: 1)
for i in 0...10 {
let bk = SCNNode(geometry: b)
bk.position = SCNVector3(x: Float(i) - 5, y: -3, z: 0)
self.sceneView?.scene?.rootNode.addChildNode(bk)
}
}
func createFlag() {
let pole = SCNCylinder(radius: 0.08, height: 5)
pole.firstMaterial?.diffuse.contents = UIColor(white: 0.99, alpha: 1)
let poleNode = SCNNode(geometry: pole)
poleNode.position = SCNVector3(x: 5, y: 0, z: 0);
self.sceneView?.scene?.rootNode.addChildNode(poleNode)
let flagBlack = SCNBox(width: 2, height: 0.8, length: 0.01, chamferRadius: 0)
flagBlack.firstMaterial?.diffuse.contents = UIColor.blackColor()
let blackNode = SCNNode(geometry: flagBlack)
blackNode.name = "black flag"
blackNode.position = SCNVector3(x: -1, y: 2, z: 0.01)
poleNode.addChildNode(blackNode)
let textForFlagBlack = SCNText(string: "on Sale", extrusionDepth: 0.01)
textForFlagBlack.firstMaterial?.diffuse.contents = UIColor.whiteColor()
textForFlagBlack.font = UIFont.systemFontOfSize(0.4)
let tnb = SCNNode(geometry: textForFlagBlack)
tnb.position = SCNVector3(x: -0.8, y: -0.11, z: 0)
blackNode.addChildNode(tnb)
}
func createMushroom() {
let player = SCNNode()
player.name = "player"
let head = SCNCylinder(radius: 0.5, height: 0.6)
head.firstMaterial?.diffuse.contents = UIColor(hue:0.12, saturation: 1, brightness: 0.6, alpha: 1)
let hNode = SCNNode(geometry: head)
let body = SCNCylinder(radius: 0.3, height: 0.2)
body.firstMaterial?.diffuse.contents = UIColor(hue: 0.12, saturation: 0.1, brightness: 0.6, alpha: 1)
let bNode = SCNNode(geometry: body)
bNode.position = SCNVector3(x: 0, y: -0.4, z: 0)
for i in 0...1 {
let foot = SCNBox(width: 0.4, height: 0.25, length: 0.1, chamferRadius: 0.1)
foot.firstMaterial?.diffuse.contents = UIColor.blackColor()
let fNode = SCNNode(geometry: foot)
if i == 0 {
fNode.position = SCNVector3(x: 0.3, y: -0.5, z: -0)
}
else {
fNode.position = SCNVector3(x: -0.3, y: -0.5, z: 0.5)
}
fNode.runAction(
SCNAction.repeatActionForever(
SCNAction.sequence([
SCNAction.moveByX(0, y: 0, z: i==0 ? 0.5 : -0.5, duration: 0.1),
SCNAction.waitForDuration(0.4),
SCNAction.moveByX(0, y: 0, z: i==0 ? -0.5 : 0.5, duration: 0.1),
SCNAction.waitForDuration(0.4)
])))
player.addChildNode(fNode)
}
player.addChildNode(hNode)
player.addChildNode(bNode)
player.position = SCNVector3(x: -5, y: -1.9, z: 0)
sceneView?.scene?.rootNode.addChildNode(player)
}
func createCamera() {
let camera = SCNNode()
camera.camera = SCNCamera()
camera.camera?.usesOrthographicProjection = true
camera.camera?.orthographicScale = 4
camera.position = SCNVector3(x: 0, y: 0, z: 10)
sceneView?.scene?.rootNode.addChildNode(camera)
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
let p = self.sceneView?.scene?.rootNode.childNodeWithName("player", recursively: false)
p?.runAction(SCNAction.sequence([
SCNAction.moveByX(9.5, y: 0, z: 0, duration: 5.0),
SCNAction.runBlock({ s in
let bFlag = self.sceneView?.scene?.rootNode.childNodeWithName("black flag", recursively: true)
bFlag?.runAction(SCNAction.moveByX(0, y: -5, z: 0, duration: 1))
let red = SCNBox(width: 2, height: 0.8, length: 0.01, chamferRadius: 0)
red.firstMaterial?.diffuse.contents = UIColor.redColor()
let node = SCNNode(geometry: red)
node.position = SCNVector3(x: 4, y: -3, z: -0.01)
let text = SCNText(string: "I got it!", extrusionDepth: 0.01)
text.firstMaterial?.diffuse.contents = UIColor.whiteColor()
text.font = UIFont.systemFontOfSize(0.4)
let ln = SCNNode(geometry: text)
ln.position = SCNVector3(x: -0.8, y: -0.11, z: 0)
node.addChildNode(ln)
self.sceneView?.scene?.rootNode.addChildNode(node)
node.runAction(SCNAction.moveByX(0, y: 5, z: 0, duration: 1.0))
})]))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment