Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- import QuartzCore
- import SceneKit
- class GameViewController: UIViewController, SCNSceneRendererDelegate {
- var gameView:SCNView!
- var SceneGame:SCNScene!
- var NodeCamera:SCNNode!
- var targetCreationTime:TimeInterval = 0
- //HERE THE TIME
- var time: CGFloat = 30
- override func viewDidLoad() {
- super.viewDidLoad()
- View_in()
- initScene()
- initCamera()
- //Let's create this method
- // call initTimer() after all your initialization
- initTimer()
- }
- //Here's the function
- func initTimer(){
- //One second before decrease the time
- let wait = SCNAction.wait(forDuration: 1)
- //This is the heart of this answer
- // An action that reduce the time and when it is less than 1 (it reached zero) do whatever you want
- let reduceTime = SCNAction.run{ _ in
- self.time -= 1
- if self.time < 1 {
- // Do whatever you want
- // for example show a game over scene or something else
- print("Remaining time: \(self.time)")
- }
- }
- }
- self.SceneGame.rootNode.run(SCNAction.repeatForever(SCNAction.sequence([wait,reduceTime])))
- }
- func View_in(){
- gameView = self.view as! SCNView
- gameView.allowsCameraControl = true
- gameView.autoenablesDefaultLighting = true
- gameView.delegate = self
- }
- func initScene (){
- SceneGame = SCNScene()
- gameView.scene = SceneGame
- gameView.isPlaying = true
- }
- func initCamera(){
- NodeCamera = SCNNode()
- NodeCamera.camera = SCNCamera()
- NodeCamera.position = SCNVector3(x:0, y:5, z:10)
- SceneGame.rootNode.addChildNode(NodeCamera)
- }
- func createTarget(){
- let geometry:SCNGeometry = SCNPyramid( width: 1, height: 1, length: 1)
- let randomColor = arc4random_uniform(2
- ) == 0 ? UIColor.green : UIColor.red
- geometry.materials.first?.diffuse.contents = randomColor
- let geometryNode = SCNNode(geometry: geometry)
- geometryNode.physicsBody = SCNPhysicsBody(type: .dynamic, shape: nil)
- if randomColor == UIColor.red {
- geometryNode.name = "enemy"
- }else{
- geometryNode.name = "friend"
- }
- SceneGame.rootNode.addChildNode(geometryNode)
- let randomDirection:Float = arc4random_uniform(2) == 0 ? -1.0 : 1.0
- let force = SCNVector3(x: randomDirection, y: 15, z: 0)
- geometryNode.physicsBody?.applyForce(force, at: SCNVector3(x: 0.05, y: 0.05, z: 0.05), asImpulse: true)
- }
- func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval) {
- if time > targetCreationTime{
- createTarget()
- targetCreationTime = time + 0.6
- }
- cleanUp()
- }
- override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
- let touch = touches.first!
- let location = touch.location(in: gameView)
- let hitList = gameView.hitTest(location, options: nil)
- if let hitObject = hitList.first{
- let node = hitObject.node
- if node.name == "friend" {
- node.removeFromParentNode()
- self.gameView.backgroundColor = UIColor.black
- }else {
- node.removeFromParentNode()
- self.gameView.backgroundColor = UIColor.red
- }
- }
- }
- func cleanUp() {
- for node in SceneGame.rootNode.childNodes {
- if node.presentation.position.y < -2 {
- node.removeFromParentNode()
- }
- }
- }
- override var shouldAutorotate: Bool {
- return true
- }
- override var prefersStatusBarHidden: Bool {
- return true
- }
- override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
- if UIDevice.current.userInterfaceIdiom == .phone {
- return .allButUpsideDown
- } else {
- return .all
- }
- }
- override func didReceiveMemoryWarning() {
- super.didReceiveMemoryWarning()
- // Release any cached data, images, etc that aren't in use.
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement