Skip to content
Snippets Groups Projects
Commit b3e4eb06 authored by Tobias Ullerich's avatar Tobias Ullerich
Browse files

Add basic player movement

parent e786ddd5
No related branches found
No related tags found
No related merge requests found
...@@ -18,6 +18,9 @@ ...@@ -18,6 +18,9 @@
F6FEE7161EF80AD500C1DE35 /* Level.swift in Sources */ = {isa = PBXBuildFile; fileRef = F6FEE7151EF80AD500C1DE35 /* Level.swift */; }; F6FEE7161EF80AD500C1DE35 /* Level.swift in Sources */ = {isa = PBXBuildFile; fileRef = F6FEE7151EF80AD500C1DE35 /* Level.swift */; };
F6FEE7181EF80C1500C1DE35 /* StreamReader.swift in Sources */ = {isa = PBXBuildFile; fileRef = F6FEE7171EF80C1500C1DE35 /* StreamReader.swift */; }; F6FEE7181EF80C1500C1DE35 /* StreamReader.swift in Sources */ = {isa = PBXBuildFile; fileRef = F6FEE7171EF80C1500C1DE35 /* StreamReader.swift */; };
F6FEE71B1EF8135200C1DE35 /* level.plv in Resources */ = {isa = PBXBuildFile; fileRef = F6FEE71A1EF8135200C1DE35 /* level.plv */; }; 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 */ /* End PBXBuildFile section */
/* Begin PBXContainerItemProxy section */ /* Begin PBXContainerItemProxy section */
...@@ -55,6 +58,9 @@ ...@@ -55,6 +58,9 @@
F6FEE7151EF80AD500C1DE35 /* Level.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Level.swift; sourceTree = "<group>"; }; 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>"; }; 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>"; }; 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 */ /* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */ /* Begin PBXFrameworksBuildPhase section */
...@@ -115,6 +121,7 @@ ...@@ -115,6 +121,7 @@
F6BEB8111EF7F99B00EDAA66 /* LaunchScreen.storyboard */, F6BEB8111EF7F99B00EDAA66 /* LaunchScreen.storyboard */,
F6BEB8141EF7F99B00EDAA66 /* Info.plist */, F6BEB8141EF7F99B00EDAA66 /* Info.plist */,
F6FEE71A1EF8135200C1DE35 /* level.plv */, F6FEE71A1EF8135200C1DE35 /* level.plv */,
F6FEE71E1EF8379600C1DE35 /* run.png */,
); );
path = "Pacman3D-iOS"; path = "Pacman3D-iOS";
sourceTree = "<group>"; sourceTree = "<group>";
...@@ -141,6 +148,7 @@ ...@@ -141,6 +148,7 @@
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
F6FEE7151EF80AD500C1DE35 /* Level.swift */, F6FEE7151EF80AD500C1DE35 /* Level.swift */,
F6FEE7221EF83DAF00C1DE35 /* Player.swift */,
); );
name = Level; name = Level;
sourceTree = "<group>"; sourceTree = "<group>";
...@@ -149,6 +157,7 @@ ...@@ -149,6 +157,7 @@
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
F6FEE7171EF80C1500C1DE35 /* StreamReader.swift */, F6FEE7171EF80C1500C1DE35 /* StreamReader.swift */,
F6FEE7201EF83C1300C1DE35 /* Opterators.swift */,
); );
name = Uilts; name = Uilts;
sourceTree = "<group>"; sourceTree = "<group>";
...@@ -264,6 +273,7 @@ ...@@ -264,6 +273,7 @@
buildActionMask = 2147483647; buildActionMask = 2147483647;
files = ( files = (
F6BEB8091EF7F99B00EDAA66 /* art.scnassets in Resources */, F6BEB8091EF7F99B00EDAA66 /* art.scnassets in Resources */,
F6FEE71F1EF8379600C1DE35 /* run.png in Resources */,
F6BEB8131EF7F99B00EDAA66 /* LaunchScreen.storyboard in Resources */, F6BEB8131EF7F99B00EDAA66 /* LaunchScreen.storyboard in Resources */,
F6BEB8101EF7F99B00EDAA66 /* Assets.xcassets in Resources */, F6BEB8101EF7F99B00EDAA66 /* Assets.xcassets in Resources */,
F6BEB80E1EF7F99B00EDAA66 /* Main.storyboard in Resources */, F6BEB80E1EF7F99B00EDAA66 /* Main.storyboard in Resources */,
...@@ -296,6 +306,8 @@ ...@@ -296,6 +306,8 @@
F6FEE7161EF80AD500C1DE35 /* Level.swift in Sources */, F6FEE7161EF80AD500C1DE35 /* Level.swift in Sources */,
F6BEB80B1EF7F99B00EDAA66 /* GameViewController.swift in Sources */, F6BEB80B1EF7F99B00EDAA66 /* GameViewController.swift in Sources */,
F6BEB8071EF7F99B00EDAA66 /* AppDelegate.swift in Sources */, F6BEB8071EF7F99B00EDAA66 /* AppDelegate.swift in Sources */,
F6FEE7231EF83DAF00C1DE35 /* Player.swift in Sources */,
F6FEE7211EF83C1300C1DE35 /* Opterators.swift in Sources */,
); );
runOnlyForDeploymentPostprocessing = 0; runOnlyForDeploymentPostprocessing = 0;
}; };
......
...@@ -9,21 +9,25 @@ ...@@ -9,21 +9,25 @@
import UIKit import UIKit
import QuartzCore import QuartzCore
import SceneKit import SceneKit
import SpriteKit
import CoreMotion import CoreMotion
class GameViewController: UIViewController { class GameViewController: UIViewController, SKSceneDelegate {
var motionManager: CMMotionManager? var motionManager: CMMotionManager?
var scene: SCNScene!
var isRotating: Bool = false var isRotating: Bool = false
var direction: Player.Direction = .north
override func viewDidLoad() { override func viewDidLoad() {
super.viewDidLoad() super.viewDidLoad()
let level = Level(named: "level") let level = Level(named: "level")
// create a new scene // 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 // create and add a camera to the scene
let cameraNode = SCNNode() let cameraNode = SCNNode()
...@@ -65,7 +69,7 @@ class GameViewController: UIViewController { ...@@ -65,7 +69,7 @@ class GameViewController: UIViewController {
scnView.scene = scene scnView.scene = scene
// allows the user to manipulate the camera // allows the user to manipulate the camera
scnView.allowsCameraControl = true scnView.allowsCameraControl = false
// show statistics such as fps and timing information // show statistics such as fps and timing information
scnView.showsStatistics = true scnView.showsStatistics = true
...@@ -83,53 +87,52 @@ class GameViewController: UIViewController { ...@@ -83,53 +87,52 @@ class GameViewController: UIViewController {
motionManager?.startAccelerometerUpdates(to: OperationQueue.main, withHandler: { (data, error) in motionManager?.startAccelerometerUpdates(to: OperationQueue.main, withHandler: { (data, error) in
let rotate = data!.acceleration.y let rotate = data!.acceleration.y
//print(rotate) //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 let direction: Float = rotate < 0 ? 1.0 : -1.0
if abs(rotate) > 0.3 { if abs(rotate) > 0.3 {
if !self.isRotating { if !self.isRotating {
let action = SCNAction.rotateBy(x: 0, y: CGFloat(direction * Float.pi * 0.5), z: 0, duration: 0.25) let action = SCNAction.rotateBy(x: 0, y: CGFloat(direction * Float.pi * 0.5), z: 0, duration: 0.25)
pacman.runAction(action) pacman.runAction(action)
self.isRotating = true 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 { } else {
self.isRotating = false self.isRotating = false
} }
}) })
} }
if let view = self.view as? SCNView {
view.overlaySKScene = createOverlay()
} }
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 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
}
SCNTransaction.commit() func handleTap(_ gestureRecognize: UIGestureRecognizer) {
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
} }
} }
......
//
// 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
}
}
Pacman3D-iOS/run.png

6.53 KiB

0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment