diff --git a/Pacman3D-iOS.xcodeproj/project.pbxproj b/Pacman3D-iOS.xcodeproj/project.pbxproj index d723f1c6821afdae33b6f390791f68238272e131..b873d1d59c9e1140ae721138a887800e8abf83ad 100644 --- a/Pacman3D-iOS.xcodeproj/project.pbxproj +++ b/Pacman3D-iOS.xcodeproj/project.pbxproj @@ -18,6 +18,9 @@ F6FEE7161EF80AD500C1DE35 /* Level.swift in Sources */ = {isa = PBXBuildFile; fileRef = F6FEE7151EF80AD500C1DE35 /* Level.swift */; }; F6FEE7181EF80C1500C1DE35 /* StreamReader.swift in Sources */ = {isa = PBXBuildFile; fileRef = F6FEE7171EF80C1500C1DE35 /* StreamReader.swift */; }; F6FEE71B1EF8135200C1DE35 /* level.plv in Resources */ = {isa = PBXBuildFile; fileRef = F6FEE71A1EF8135200C1DE35 /* level.plv */; }; + F6FEE71F1EF8379600C1DE35 /* run.png in Resources */ = {isa = PBXBuildFile; fileRef = F6FEE71E1EF8379600C1DE35 /* run.png */; }; + F6FEE7211EF83C1300C1DE35 /* Opterators.swift in Sources */ = {isa = PBXBuildFile; fileRef = F6FEE7201EF83C1300C1DE35 /* Opterators.swift */; }; + F6FEE7231EF83DAF00C1DE35 /* Player.swift in Sources */ = {isa = PBXBuildFile; fileRef = F6FEE7221EF83DAF00C1DE35 /* Player.swift */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -55,6 +58,9 @@ F6FEE7151EF80AD500C1DE35 /* Level.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Level.swift; sourceTree = "<group>"; }; F6FEE7171EF80C1500C1DE35 /* StreamReader.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StreamReader.swift; sourceTree = "<group>"; }; F6FEE71A1EF8135200C1DE35 /* level.plv */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = level.plv; sourceTree = "<group>"; }; + F6FEE71E1EF8379600C1DE35 /* run.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = run.png; sourceTree = "<group>"; }; + F6FEE7201EF83C1300C1DE35 /* Opterators.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Opterators.swift; sourceTree = "<group>"; }; + F6FEE7221EF83DAF00C1DE35 /* Player.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Player.swift; sourceTree = "<group>"; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -115,6 +121,7 @@ F6BEB8111EF7F99B00EDAA66 /* LaunchScreen.storyboard */, F6BEB8141EF7F99B00EDAA66 /* Info.plist */, F6FEE71A1EF8135200C1DE35 /* level.plv */, + F6FEE71E1EF8379600C1DE35 /* run.png */, ); path = "Pacman3D-iOS"; sourceTree = "<group>"; @@ -141,6 +148,7 @@ isa = PBXGroup; children = ( F6FEE7151EF80AD500C1DE35 /* Level.swift */, + F6FEE7221EF83DAF00C1DE35 /* Player.swift */, ); name = Level; sourceTree = "<group>"; @@ -149,6 +157,7 @@ isa = PBXGroup; children = ( F6FEE7171EF80C1500C1DE35 /* StreamReader.swift */, + F6FEE7201EF83C1300C1DE35 /* Opterators.swift */, ); name = Uilts; sourceTree = "<group>"; @@ -264,6 +273,7 @@ buildActionMask = 2147483647; files = ( F6BEB8091EF7F99B00EDAA66 /* art.scnassets in Resources */, + F6FEE71F1EF8379600C1DE35 /* run.png in Resources */, F6BEB8131EF7F99B00EDAA66 /* LaunchScreen.storyboard in Resources */, F6BEB8101EF7F99B00EDAA66 /* Assets.xcassets in Resources */, F6BEB80E1EF7F99B00EDAA66 /* Main.storyboard in Resources */, @@ -296,6 +306,8 @@ F6FEE7161EF80AD500C1DE35 /* Level.swift in Sources */, F6BEB80B1EF7F99B00EDAA66 /* GameViewController.swift in Sources */, F6BEB8071EF7F99B00EDAA66 /* AppDelegate.swift in Sources */, + F6FEE7231EF83DAF00C1DE35 /* Player.swift in Sources */, + F6FEE7211EF83C1300C1DE35 /* Opterators.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/Pacman3D-iOS/GameViewController.swift b/Pacman3D-iOS/GameViewController.swift index 0268b796a5475cc85ef1de36735da6b5fcf5018d..6b8c8c539ad36473aed97380efc9e6f4ed974e27 100644 --- a/Pacman3D-iOS/GameViewController.swift +++ b/Pacman3D-iOS/GameViewController.swift @@ -9,21 +9,25 @@ import UIKit import QuartzCore import SceneKit +import SpriteKit import CoreMotion -class GameViewController: UIViewController { +class GameViewController: UIViewController, SKSceneDelegate { var motionManager: CMMotionManager? + var scene: SCNScene! var isRotating: Bool = false + var direction: Player.Direction = .north + override func viewDidLoad() { super.viewDidLoad() let level = Level(named: "level") // create a new scene - let scene = SCNScene(named: "art.scnassets/ship.scn")! + scene = SCNScene(named: "art.scnassets/ship.scn")! // create and add a camera to the scene let cameraNode = SCNNode() @@ -65,7 +69,7 @@ class GameViewController: UIViewController { scnView.scene = scene // allows the user to manipulate the camera - scnView.allowsCameraControl = true + scnView.allowsCameraControl = false // show statistics such as fps and timing information scnView.showsStatistics = true @@ -83,53 +87,52 @@ class GameViewController: UIViewController { motionManager?.startAccelerometerUpdates(to: OperationQueue.main, withHandler: { (data, error) in let rotate = data!.acceleration.y //print(rotate) - let pacman = scene.rootNode.childNode(withName: "Pacman", recursively: true)! + let pacman = self.scene.rootNode.childNode(withName: "Pacman", recursively: true)! let direction: Float = rotate < 0 ? 1.0 : -1.0 if abs(rotate) > 0.3 { if !self.isRotating { let action = SCNAction.rotateBy(x: 0, y: CGFloat(direction * Float.pi * 0.5), z: 0, duration: 0.25) pacman.runAction(action) self.isRotating = true + + var directionVal = self.direction.rawValue + Int(direction) + if directionVal == 5 { + directionVal = 1 + } + if directionVal == 0 { + directionVal = 4 + } + self.direction = Player.Direction(rawValue: directionVal)! } } else { self.isRotating = false } }) } + if let view = self.view as? SCNView { + view.overlaySKScene = createOverlay() + } + } + + func createOverlay() -> SKScene { + let scene = SKScene(size: self.view.frame.size) + let node = SKSpriteNode(imageNamed: "run.png") + node.position = CGPoint(x: 100, y: 100) + node.size = CGSize(width: 50, height: 100) + scene.addChild(node) + return scene } func handleTap(_ gestureRecognize: UIGestureRecognizer) { - // retrieve the SCNView - let scnView = self.view as! SCNView - - // check what nodes are tapped - let p = gestureRecognize.location(in: scnView) - let hitResults = scnView.hitTest(p, options: [:]) - // check that we clicked on at least one object - if hitResults.count > 0 { - // retrieved the first clicked object - let result: AnyObject = hitResults[0] - - // get its material - let material = result.node!.geometry!.firstMaterial! - - // highlight it - SCNTransaction.begin() - SCNTransaction.animationDuration = 0.5 - - // on completion - unhighlight - SCNTransaction.completionBlock = { - SCNTransaction.begin() - SCNTransaction.animationDuration = 0.5 - - material.emission.contents = UIColor.black - - SCNTransaction.commit() - } - - material.emission.contents = UIColor.red - - SCNTransaction.commit() + let pacman = scene.rootNode.childNode(withName: "Pacman", recursively: true)! + if direction == .north { + pacman.position.x += 5 + } else if direction == .east { + pacman.position.z -= 5 + } else if direction == .south { + pacman.position.x -= 5 + } else if direction == .west { + pacman.position.z += 5 } } diff --git a/Pacman3D-iOS/Player.swift b/Pacman3D-iOS/Player.swift new file mode 100644 index 0000000000000000000000000000000000000000..24a897edb21a27812f6886e216df78f035d859a3 --- /dev/null +++ b/Pacman3D-iOS/Player.swift @@ -0,0 +1,18 @@ +// +// Player.swift +// Pacman3D-iOS +// +// Created by Tobias on 19.06.17. +// Copyright © 2017 Tobias. All rights reserved. +// + +import Foundation + +class Player { + enum Direction: Int { + case north = 1 + case east + case south + case west + } +} diff --git a/Pacman3D-iOS/run.png b/Pacman3D-iOS/run.png new file mode 100644 index 0000000000000000000000000000000000000000..f09b7ec7f076aff1f5d19bd7620dab2af8867aff Binary files /dev/null and b/Pacman3D-iOS/run.png differ