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

Add level file loading, add walls to scene

parent 56018183
Branches
Tags
No related merge requests found
......@@ -15,6 +15,9 @@
F6BEB8131EF7F99B00EDAA66 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = F6BEB8111EF7F99B00EDAA66 /* LaunchScreen.storyboard */; };
F6BEB81E1EF7F99B00EDAA66 /* Pacman3D_iOSTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = F6BEB81D1EF7F99B00EDAA66 /* Pacman3D_iOSTests.swift */; };
F6BEB8291EF7F99C00EDAA66 /* Pacman3D_iOSUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = F6BEB8281EF7F99C00EDAA66 /* Pacman3D_iOSUITests.swift */; };
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 */; };
/* End PBXBuildFile section */
/* Begin PBXContainerItemProxy section */
......@@ -49,6 +52,9 @@
F6BEB8241EF7F99C00EDAA66 /* Pacman3D-iOSUITests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "Pacman3D-iOSUITests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
F6BEB8281EF7F99C00EDAA66 /* Pacman3D_iOSUITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Pacman3D_iOSUITests.swift; sourceTree = "<group>"; };
F6BEB82A1EF7F99C00EDAA66 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; 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>"; };
F6FEE71A1EF8135200C1DE35 /* level.plv */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = level.plv; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
......@@ -99,6 +105,8 @@
F6BEB8051EF7F99B00EDAA66 /* Pacman3D-iOS */ = {
isa = PBXGroup;
children = (
F6FEE7141EF80AB900C1DE35 /* Level */,
F6FEE7191EF80C1900C1DE35 /* Uilts */,
F6BEB8061EF7F99B00EDAA66 /* AppDelegate.swift */,
F6BEB8081EF7F99B00EDAA66 /* art.scnassets */,
F6BEB80A1EF7F99B00EDAA66 /* GameViewController.swift */,
......@@ -106,6 +114,7 @@
F6BEB80F1EF7F99B00EDAA66 /* Assets.xcassets */,
F6BEB8111EF7F99B00EDAA66 /* LaunchScreen.storyboard */,
F6BEB8141EF7F99B00EDAA66 /* Info.plist */,
F6FEE71A1EF8135200C1DE35 /* level.plv */,
);
path = "Pacman3D-iOS";
sourceTree = "<group>";
......@@ -128,6 +137,22 @@
path = "Pacman3D-iOSUITests";
sourceTree = "<group>";
};
F6FEE7141EF80AB900C1DE35 /* Level */ = {
isa = PBXGroup;
children = (
F6FEE7151EF80AD500C1DE35 /* Level.swift */,
);
name = Level;
sourceTree = "<group>";
};
F6FEE7191EF80C1900C1DE35 /* Uilts */ = {
isa = PBXGroup;
children = (
F6FEE7171EF80C1500C1DE35 /* StreamReader.swift */,
);
name = Uilts;
sourceTree = "<group>";
};
/* End PBXGroup section */
/* Begin PBXNativeTarget section */
......@@ -242,6 +267,7 @@
F6BEB8131EF7F99B00EDAA66 /* LaunchScreen.storyboard in Resources */,
F6BEB8101EF7F99B00EDAA66 /* Assets.xcassets in Resources */,
F6BEB80E1EF7F99B00EDAA66 /* Main.storyboard in Resources */,
F6FEE71B1EF8135200C1DE35 /* level.plv in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
......@@ -266,6 +292,8 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
F6FEE7181EF80C1500C1DE35 /* StreamReader.swift in Sources */,
F6FEE7161EF80AD500C1DE35 /* Level.swift in Sources */,
F6BEB80B1EF7F99B00EDAA66 /* GameViewController.swift in Sources */,
F6BEB8071EF7F99B00EDAA66 /* AppDelegate.swift in Sources */,
);
......@@ -520,6 +548,7 @@
F6BEB82F1EF7F99C00EDAA66 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
F6BEB8301EF7F99C00EDAA66 /* Build configuration list for PBXNativeTarget "Pacman3D-iOSTests" */ = {
isa = XCConfigurationList;
......@@ -528,6 +557,7 @@
F6BEB8321EF7F99C00EDAA66 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
F6BEB8331EF7F99C00EDAA66 /* Build configuration list for PBXNativeTarget "Pacman3D-iOSUITests" */ = {
isa = XCConfigurationList;
......@@ -536,6 +566,7 @@
F6BEB8351EF7F99C00EDAA66 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};
......
......@@ -15,6 +15,8 @@ class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let level = Level(named: "level")
// create a new scene
let scene = SCNScene(named: "art.scnassets/ship.scn")!
......@@ -26,6 +28,17 @@ class GameViewController: UIViewController {
// place the camera
cameraNode.position = SCNVector3(x: 0, y: 0, z: 15)
for (y, line) in level.data.enumerated() {
for (x, block) in line.enumerated() {
if block == .wall {
let box = SCNBox(width: 5, height: 5, length: 5, chamferRadius: 0)
let node = SCNNode(geometry: box)
node.position = SCNVector3(x: Float(x * 5), y: 2.5, z:Float(y * 5))
scene.rootNode.addChildNode(node)
}
}
}
// // create and add a light to the scene
// let lightNode = SCNNode()
// lightNode.light = SCNLight()
......@@ -47,7 +60,7 @@ class GameViewController: UIViewController {
scnView.scene = scene
// allows the user to manipulate the camera
scnView.allowsCameraControl = false
scnView.allowsCameraControl = true
// show statistics such as fps and timing information
scnView.showsStatistics = true
......
//
// Level.swift
// Pacman3D-iOS
//
// Created by Tobias on 19.06.17.
// Copyright © 2017 Tobias. All rights reserved.
//
import Foundation
class Level {
enum TileType {
case wall
case blank
}
let data: [[TileType]]
init(named: String) {
if let path = Bundle.main.path(forResource: named, ofType: "plv") {
if let aStreamReader = StreamReader(path: path, delimiter: "\n") {
defer {
aStreamReader.close()
}
var tempLines: [[TileType]] = []
while let line = aStreamReader.nextLine() {
var tempLine: [TileType] = []
for index in line.characters.indices {
let c = line[index]
if c == "w" {
tempLine.append(TileType.blank)
} else if c == "s" {
tempLine.append(TileType.wall)
}
}
tempLines.append(tempLine)
}
self.data = tempLines
return
}
}
data = [[]]
}
}
//
// StreamReader.swift
// Pacman3D-iOS
//
// Created by Tobias on 19.06.17.
// Copyright © 2017 Tobias. All rights reserved.
//
import Foundation
class StreamReader {
let encoding: String.Encoding
let chunkSize: Int
var fileHandle: FileHandle!
let buffer: NSMutableData!
let delimData: NSData!
var atEof: Bool = false
init?(path: String, delimiter: String = "\n", encoding : String.Encoding = String.Encoding.utf8, chunkSize : Int = 4096) {
self.chunkSize = chunkSize
self.encoding = encoding
if let fileHandle = FileHandle(forReadingAtPath: path), let delimData = delimiter.data(using: encoding), let buffer = NSMutableData(capacity: chunkSize) {
self.fileHandle = fileHandle
self.delimData = delimData as NSData
self.buffer = buffer
} else {
self.fileHandle = nil
self.delimData = nil
self.buffer = nil
return nil
}
}
deinit {
self.close()
}
/// Return next line, or nil on EOF.
func nextLine() -> String? {
precondition(fileHandle != nil, "Attempt to read from closed file")
if atEof {
return nil
}
// Read data chunks from file until a line delimiter is found:
var range = buffer.range(of: delimData as Data, options: [], in: NSMakeRange(0, buffer.length))
while range.location == NSNotFound {
let tmpData = fileHandle.readData(ofLength: chunkSize)
if tmpData.count == 0 {
// EOF or read error.
atEof = true
if buffer.length > 0 {
// Buffer contains last line in file (not terminated by delimiter).
let line = String(data: buffer as Data, encoding: encoding)
buffer.length = 0
return line
}
// No more lines.
return nil
}
buffer.append(tmpData)
range = buffer.range(of: delimData as Data, options: [], in: NSMakeRange(0, buffer.length))
}
// Convert complete line (excluding the delimiter) to a string:
let line = String(data: buffer.subdata(with: NSMakeRange(0, range.location)), encoding: encoding)
// Remove line (and the delimiter) from the buffer:
buffer.replaceBytes(in: NSMakeRange(0, range.location + range.length), withBytes: nil, length: 0)
return line as String?
}
/// Start reading from the beginning of file.
func rewind() -> Void {
fileHandle.seek(toFileOffset: 0)
buffer.length = 0
atEof = false
}
/// Close the underlying file. No reading must be done after calling this method.
func close() -> Void {
fileHandle?.closeFile()
fileHandle = nil
}
}
No preview for this file type
ssssssssssssssssssss
swwwwwwwwwwwwwwwwwws
swssssswsssswsswwsws
swswwwwwwwsswsssssws
swswssssswsswwwwwwws
swswssswwwsswsssssws
swswssswsssswwswwwws
swswswwwssssswswssws
swwwsswwwwwwwwwwwwws
swswssssswsssswswsws
swswssssswwwsswswsws
swswwwwwwwswsswwwsws
swsssswswsswwswsssws
swwwwswswwsswswsssws
swsswswswwwwwswsssws
swsswswssssssswsssws
swwwwwwwwwwwwwwwwwws
ssssssssssssssssssss
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment