-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathViewController.swift
133 lines (99 loc) · 3.94 KB
/
ViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//
// ViewController.swift
// whack_that_jellyfish
//
// Created by Joe Hill on 11/25/17.
// Copyright © 2017 Dumb Unicorn. All rights reserved.
//
import UIKit
import ARKit
import Each
class ViewController: UIViewController {
var timer = Each(1).seconds
var countdown = 10
@IBOutlet weak var timerLabel: UILabel!
@IBOutlet weak var play: UIButton!
@IBOutlet weak var SceneView: ARSCNView!
let configuration = ARWorldTrackingConfiguration()
override func viewDidLoad() {
super.viewDidLoad()
self.SceneView.debugOptions = [ARSCNDebugOptions.showWorldOrigin, ARSCNDebugOptions.showFeaturePoints]
self.SceneView.session.run(configuration)
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleTap))
self.SceneView.addGestureRecognizer(tapGestureRecognizer)
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func play(_ sender: Any) {
self.setTimer()
self.addNode()
self.play.isEnabled = false
}
@IBAction func reset(_ sender: Any) {
self.timer.stop()
self.restoreTimer()
self.play.isEnabled = true
SceneView.scene.rootNode.enumerateChildNodes { (node, _) in
node.removeFromParentNode()
}
}
func addNode() {
let jellyFishScene = SCNScene(named: "art.scnassets/Jellyfish.dataset/Jellyfish.scn")
let jellyFishNode = jellyFishScene?.rootNode.childNode(withName: "Jellyfish", recursively: false)
jellyFishNode?.position = SCNVector3(randomNumbers(firstNum: -1, secondNum: 1),randomNumbers(firstNum: -0.5, secondNum: 0.5),randomNumbers(firstNum: -1, secondNum: 1))
self.SceneView.scene.rootNode.addChildNode(jellyFishNode!)
}
@objc func handleTap(sender: UITapGestureRecognizer) {
let sceneViewTappedOn = sender.view as! SCNView
let touchCoordinates = sender.location(in: sceneViewTappedOn)
let hitTest = sceneViewTappedOn.hitTest(touchCoordinates)
if hitTest.isEmpty {
print("Didn't touch anything")
} else {
if countdown > 0 {
let results = hitTest.first!
let node = results.node
if node.animationKeys.isEmpty {
SCNTransaction.begin()
self.animateNode(node: node)
SCNTransaction.completionBlock = {
node.removeFromParentNode()
self.addNode()
self.restoreTimer()
}
SCNTransaction.commit()
}
}
}
}
func animateNode(node: SCNNode) {
let spin = CABasicAnimation(keyPath: "position")
spin.fromValue = node.presentation.position
spin.toValue = SCNVector3(node.presentation.position.x - 0.2,node.presentation.position.y - 0.2, node.presentation.position.z - 0.2)
spin.duration = 0.08
spin.autoreverses = true
spin.repeatCount = 5
node.addAnimation(spin, forKey: "position")
}
func randomNumbers(firstNum: CGFloat, secondNum: CGFloat) -> CGFloat {
return CGFloat(arc4random()) / CGFloat(UINT32_MAX) * abs(firstNum - secondNum) + min(firstNum, secondNum)
}
func setTimer() {
self.timer.perform { () -> NextStep in
self.countdown -= 1
self.timerLabel.text = String(self.countdown)
if self.countdown == 0 {
self.timerLabel.text = "You Lose"
return .stop
}
return .continue
}
}
func restoreTimer() {
self.countdown = 10
self.timerLabel.text = String(self.countdown)
}
}