Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored View Controller into Scene Class Singleton Pattern Instance #3

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions FPSControls.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
01D1D1A71A29EEDF0067D6D3 /* Tile.swift in Sources */ = {isa = PBXBuildFile; fileRef = 01D1D1A51A29EEDF0067D6D3 /* Tile.swift */; };
01D1D1AC1A29EF320067D6D3 /* Map.swift in Sources */ = {isa = PBXBuildFile; fileRef = 01D1D1AB1A29EF320067D6D3 /* Map.swift */; };
01D1D1AE1A29F7B00067D6D3 /* FireGestureRecognizer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 01D1D1AD1A29F7B00067D6D3 /* FireGestureRecognizer.swift */; };
49E5D23E1C1238B600F99427 /* GestureRecognizerExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 49E5D23D1C1238B600F99427 /* GestureRecognizerExtension.swift */; };
49E5D2421C12417700F99427 /* Scene.swift in Sources */ = {isa = PBXBuildFile; fileRef = 49E5D2411C12417700F99427 /* Scene.swift */; };
49E5D2491C12A00100F99427 /* SceneRendererExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 49E5D2481C12A00100F99427 /* SceneRendererExtension.swift */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand All @@ -32,6 +35,9 @@
01D1D1A51A29EEDF0067D6D3 /* Tile.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Tile.swift; sourceTree = "<group>"; };
01D1D1AB1A29EF320067D6D3 /* Map.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Map.swift; sourceTree = "<group>"; };
01D1D1AD1A29F7B00067D6D3 /* FireGestureRecognizer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FireGestureRecognizer.swift; sourceTree = "<group>"; };
49E5D23D1C1238B600F99427 /* GestureRecognizerExtension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GestureRecognizerExtension.swift; sourceTree = "<group>"; };
49E5D2411C12417700F99427 /* Scene.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Scene.swift; sourceTree = "<group>"; };
49E5D2481C12A00100F99427 /* SceneRendererExtension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SceneRendererExtension.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -66,6 +72,7 @@
01558A321A0291A700083EB1 /* FPSControls */ = {
isa = PBXGroup;
children = (
49E5D2431C12418400F99427 /* Game */,
01D1D1A81A29EEF50067D6D3 /* Model */,
01D1D1A91A29EF040067D6D3 /* Controllers */,
01D1D1AA1A29EF0E0067D6D3 /* Resources */,
Expand Down Expand Up @@ -95,6 +102,7 @@
01D1D1A91A29EF040067D6D3 /* Controllers */ = {
isa = PBXGroup;
children = (
49E5D2471C12742E00F99427 /* Extensions */,
01558A351A0291A700083EB1 /* AppDelegate.swift */,
01558A371A0291A700083EB1 /* ViewController.swift */,
01D1D1AD1A29F7B00067D6D3 /* FireGestureRecognizer.swift */,
Expand All @@ -113,6 +121,23 @@
name = Resources;
sourceTree = "<group>";
};
49E5D2431C12418400F99427 /* Game */ = {
isa = PBXGroup;
children = (
49E5D2411C12417700F99427 /* Scene.swift */,
);
name = Game;
sourceTree = "<group>";
};
49E5D2471C12742E00F99427 /* Extensions */ = {
isa = PBXGroup;
children = (
49E5D23D1C1238B600F99427 /* GestureRecognizerExtension.swift */,
49E5D2481C12A00100F99427 /* SceneRendererExtension.swift */,
);
name = Extensions;
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXNativeTarget section */
Expand Down Expand Up @@ -187,10 +212,13 @@
files = (
01D1D1AC1A29EF320067D6D3 /* Map.swift in Sources */,
01558A381A0291A700083EB1 /* ViewController.swift in Sources */,
49E5D2491C12A00100F99427 /* SceneRendererExtension.swift in Sources */,
01D1D1A61A29EEDF0067D6D3 /* Entity.swift in Sources */,
01D1D1AE1A29F7B00067D6D3 /* FireGestureRecognizer.swift in Sources */,
01D1D1A71A29EEDF0067D6D3 /* Tile.swift in Sources */,
49E5D23E1C1238B600F99427 /* GestureRecognizerExtension.swift in Sources */,
01558A361A0291A700083EB1 /* AppDelegate.swift in Sources */,
49E5D2421C12417700F99427 /* Scene.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
85 changes: 85 additions & 0 deletions FPSControls/GestureRecognizerExtension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
//
// GestureRecognizerExtension.swift
// FPSControls
//
// Created by Luke Schoen on 4/12/2015.
// Copyright © 2015 Nick Lockwood. All rights reserved.
//

import UIKit
import SceneKit

extension ViewController: UIGestureRecognizerDelegate {

internal func setupGestureRecognizers() {

//look gesture
lookGesture = UIPanGestureRecognizer(target: self, action: "lookGestureRecognized:")
lookGesture.delegate = self
self.sceneView.addGestureRecognizer(lookGesture)

//walk gesture
walkGesture = UIPanGestureRecognizer(target: self, action: "walkGestureRecognized:")
walkGesture.delegate = self
self.sceneView.addGestureRecognizer(walkGesture)

//fire gesture
fireGesture = FireGestureRecognizer(target: self, action: "fireGestureRecognized:")
fireGesture.delegate = self
self.sceneView.addGestureRecognizer(fireGesture)
}

//implement protocol methods for conformance
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool {

if gestureRecognizer == lookGesture {
return touch.locationInView(self.view).x > self.view.frame.size.width / 2
} else if gestureRecognizer == walkGesture {
return touch.locationInView(self.view).x < self.view.frame.size.width / 2
}
return true
}

func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {

return true
}

//custom methods
func lookGestureRecognized(gesture: UIPanGestureRecognizer) {

//get translation and convert to rotation
let translation = gesture.translationInView(self.sceneView)
let hAngle = acos(Float(translation.x) / 200) - Float(M_PI_2)
let vAngle = acos(Float(translation.y) / 200) - Float(M_PI_2)

//rotate hero
Scene.sharedInstance.heroNode.physicsBody?.applyTorque(SCNVector4(x: 0, y: 1, z: 0, w: hAngle), impulse: true)

//tilt camera
Scene.sharedInstance.elevation = max(Float(-M_PI_4), min(Float(M_PI_4), Scene.sharedInstance.elevation + vAngle))
Scene.sharedInstance.camNode.rotation = SCNVector4(x: 1, y: 0, z: 0, w: Scene.sharedInstance.elevation)

//reset translation
gesture.setTranslation(CGPointZero, inView: self.sceneView)
}

func walkGestureRecognized(gesture: UIPanGestureRecognizer) {

if gesture.state == UIGestureRecognizerState.Ended || gesture.state == UIGestureRecognizerState.Cancelled {
gesture.setTranslation(CGPointZero, inView: self.sceneView)
}
}

func fireGestureRecognized(gesture: FireGestureRecognizer) {

//update timestamp
let now = CFAbsoluteTimeGetCurrent()
if now - lastTappedFire < autofireTapTimeThreshold {
tapCount += 1
} else {
tapCount = 1
}
lastTappedFire = now
}
}
2 changes: 1 addition & 1 deletion FPSControls/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>1</string>
<string>2</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>UILaunchStoryboardName</key>
Expand Down
207 changes: 207 additions & 0 deletions FPSControls/Scene.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
//
// Scene.swift
// FPSControls
//
// Created by Luke Schoen on 4/12/2015.
// Copyright © 2015 Nick Lockwood. All rights reserved.
//

import Foundation
import SceneKit

struct CollisionCategory {

static let None: Int = 0b00000000
static let All: Int = 0b11111111
static let Map: Int = 0b00000001
static let Hero: Int = 0b00000010
static let Monster: Int = 0b00000100
static let Bullet: Int = 0b00001000
}

class Scene: SCNScene, SCNSceneRendererDelegate {

// MARK: Properties

internal var sceneView: SCNView?
internal var cameraNode: SCNNode?
internal var heroNode: SCNNode!
internal var camNode: SCNNode!
internal var elevation: Float = 0
var mapNode: SCNNode!
var map: Map!

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}

override init() {
super.init()
}

func setupSceneWithView(scnView: SCNView) {

/**
* Setup Map, Scene View, Entities, Camera,
* Level, Floor, Ceiling, and Physics
*/
self.setupMap()
self.setupView(scnView)
self.setupEntities()
self.setupCamera()
self.setupLevel()
self.setupFloor()
self.setupCeiling()
self.setupPhysics()
}

func setupMap() {

map = Map(image: UIImage(named:"Map")!)
}

func setupView(view: SCNView) {

self.sceneView = view
}

func setupEntities() {

for entity in map.entities {
switch entity.type {
case .Hero:

heroNode = SCNNode()
heroNode.physicsBody = SCNPhysicsBody(type: .Dynamic, shape: SCNPhysicsShape(geometry: SCNCylinder(radius: 0.2, height: 1), options: nil))
heroNode.physicsBody?.angularDamping = 0.9999999
heroNode.physicsBody?.damping = 0.9999999
heroNode.physicsBody?.rollingFriction = 0
heroNode.physicsBody?.friction = 0
heroNode.physicsBody?.restitution = 0
heroNode.physicsBody?.velocityFactor = SCNVector3(x: 1, y: 0, z: 1)
heroNode.physicsBody?.categoryBitMask = CollisionCategory.Hero
heroNode.physicsBody?.collisionBitMask = CollisionCategory.All ^ CollisionCategory.Bullet
if #available(iOS 9.0, *) {
heroNode.physicsBody?.contactTestBitMask = ~0
}
heroNode.position = SCNVector3(x: entity.x, y: 0.5, z: entity.y)
self.rootNode.addChildNode(heroNode)

case .Monster:

let monsterNode = SCNNode()
monsterNode.position = SCNVector3(x: entity.x, y: 0.3, z: entity.y)
monsterNode.geometry = SCNCylinder(radius: 0.15, height: 0.6)
monsterNode.physicsBody = SCNPhysicsBody(type: .Dynamic, shape: SCNPhysicsShape(geometry: monsterNode.geometry!, options: nil))
monsterNode.physicsBody?.categoryBitMask = CollisionCategory.Monster
monsterNode.physicsBody?.collisionBitMask = CollisionCategory.All
if #available(iOS 9.0, *) {
monsterNode.physicsBody?.contactTestBitMask = ~0
}
self.rootNode.addChildNode(monsterNode)
}
}
}

func setupCamera() {

//add a camera node
camNode = SCNNode()
camNode.position = SCNVector3(x: 0, y: 0, z: 0)
heroNode.addChildNode(camNode)

//add camera
let camera = SCNCamera()
camera.zNear = 0.01
camera.zFar = Double(max(map.width, map.height))
camNode.camera = camera
}

func setupLevel() {

//create map node
mapNode = SCNNode()

//add walls
for tile in map.tiles {

if tile.type == .Wall {

//create walls
if tile.visibility.contains(.Top) {
let wallNode = SCNNode()
wallNode.geometry = SCNPlane(width: 1, height: 1)
wallNode.rotation = SCNVector4(x: 0, y: 1, z: 0, w: Float(M_PI))
wallNode.position = SCNVector3(x: Float(tile.x) + 0.5, y: 0.5, z: Float(tile.y))
mapNode.addChildNode(wallNode)
}
if tile.visibility.contains(.Right) {
let wallNode = SCNNode()
wallNode.geometry = SCNPlane(width: 1, height: 1)
wallNode.rotation = SCNVector4(x: 0, y: 1, z: 0, w: Float(M_PI_2))
wallNode.position = SCNVector3(x: Float(tile.x) + 1, y: 0.5, z: Float(tile.y) + 0.5)
mapNode.addChildNode(wallNode)
}
if tile.visibility.contains(.Bottom) {
let wallNode = SCNNode()
wallNode.geometry = SCNPlane(width: 1, height: 1)
wallNode.rotation = SCNVector4(x: 0, y: 1, z: 0, w: 0)
wallNode.position = SCNVector3(x: Float(tile.x) + 0.5, y: 0.5, z: Float(tile.y) + 1)
mapNode.addChildNode(wallNode)
}
if tile.visibility.contains(.Left) {
let wallNode = SCNNode()
wallNode.geometry = SCNPlane(width: 1, height: 1)
wallNode.rotation = SCNVector4(x: 0, y: 1, z: 0, w: Float(-M_PI_2))
wallNode.position = SCNVector3(x: Float(tile.x), y: 0.5, z: Float(tile.y) + 0.5)
mapNode.addChildNode(wallNode)
}
}
}
}

func setupFloor() {

//add floor
let floorNode = SCNNode()
floorNode.geometry = SCNPlane(width: CGFloat(map.width), height: CGFloat(map.height))
floorNode.rotation = SCNVector4(x: 1, y: 0, z: 0, w: Float(-M_PI_2))
floorNode.position = SCNVector3(x: Float(map.width)/2, y: 0, z: Float(map.height)/2)
mapNode.addChildNode(floorNode)
}

func setupCeiling() {

//add ceiling
let ceilingNode = SCNNode()
ceilingNode.geometry = SCNPlane(width: CGFloat(map.width), height: CGFloat(map.height))
ceilingNode.rotation = SCNVector4(x: 1, y: 0, z: 0, w: Float(M_PI_2))
ceilingNode.position = SCNVector3(x: Float(map.width)/2, y: 1, z: Float(map.height)/2)
mapNode.addChildNode(ceilingNode)
}

func setupPhysics() {

self.physicsWorld.gravity = SCNVector3(x: 0, y: -9, z: 0)
self.physicsWorld.timeStep = 1.0/360

//set up map physics
mapNode.physicsBody = SCNPhysicsBody(type: .Static, shape: SCNPhysicsShape(node: mapNode, options: [SCNPhysicsShapeKeepAsCompoundKey: true]))
mapNode.physicsBody?.categoryBitMask = CollisionCategory.Map
mapNode.physicsBody?.collisionBitMask = CollisionCategory.All
if #available(iOS 9.0, *) {
mapNode.physicsBody?.contactTestBitMask = ~0
}
self.rootNode.addChildNode(mapNode)
}

// MARK: Thread Safe Singleton Pattern

/**
* Threadsafe Singleton declaration of static constant to
* hold the single instance of class. Supports lazy initialisation
* since Swift lazily initialises class constants and variables
* and is thread safe by the definition of let
*/
static let sharedInstance = Scene()
}
Loading