Skip to content

Commit

Permalink
completed chap19.
Browse files Browse the repository at this point in the history
  • Loading branch information
PP100893 committed Jul 13, 2015
1 parent 7bc72ac commit b140655
Showing 5 changed files with 75 additions and 16 deletions.
8 changes: 4 additions & 4 deletions Xmaze/Xmaze.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
@@ -18,8 +18,8 @@
F811212C1B507056002D9380 /* hero.swift in Sources */ = {isa = PBXBuildFile; fileRef = F811212B1B507056002D9380 /* hero.swift */; };
F811212E1B507075002D9380 /* Boundary.swift in Sources */ = {isa = PBXBuildFile; fileRef = F811212D1B507075002D9380 /* Boundary.swift */; };
F89FC9AD1B51C9660033BEFA /* moving.atlas in Resources */ = {isa = PBXBuildFile; fileRef = F89FC9AC1B51C9660033BEFA /* moving.atlas */; };
F89FC9BD1B51EFF50033BEFA /* Maze.tmx in Resources */ = {isa = PBXBuildFile; fileRef = F89FC9BC1B51EFF50033BEFA /* Maze.tmx */; };
F89FC9BF1B51F5860033BEFA /* Star.swift in Sources */ = {isa = PBXBuildFile; fileRef = F89FC9BE1B51F5860033BEFA /* Star.swift */; };
F8CD0CE51B532DE1001E697C /* Maze.tmx in Resources */ = {isa = PBXBuildFile; fileRef = F8CD0CE41B532DE1001E697C /* Maze.tmx */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
@@ -48,8 +48,8 @@
F811212B1B507056002D9380 /* hero.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = hero.swift; sourceTree = "<group>"; };
F811212D1B507075002D9380 /* Boundary.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Boundary.swift; sourceTree = "<group>"; };
F89FC9AC1B51C9660033BEFA /* moving.atlas */ = {isa = PBXFileReference; lastKnownFileType = folder.skatlas; path = moving.atlas; sourceTree = "<group>"; };
F89FC9BC1B51EFF50033BEFA /* Maze.tmx */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = Maze.tmx; sourceTree = "<group>"; };
F89FC9BE1B51F5860033BEFA /* Star.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Star.swift; sourceTree = "<group>"; };
F8CD0CE41B532DE1001E697C /* Maze.tmx */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = Maze.tmx; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
@@ -99,7 +99,7 @@
F811212D1B507075002D9380 /* Boundary.swift */,
F811210D1B506FD0002D9380 /* GameViewController.swift */,
F811210F1B506FD0002D9380 /* Main.storyboard */,
F89FC9BC1B51EFF50033BEFA /* Maze.tmx */,
F8CD0CE41B532DE1001E697C /* Maze.tmx */,
F81121121B506FD0002D9380 /* Images.xcassets */,
F81121141B506FD0002D9380 /* LaunchScreen.xib */,
F89FC9AB1B51C4320033BEFA /* TextureAtlases */,
@@ -223,7 +223,7 @@
files = (
F81121161B506FD0002D9380 /* LaunchScreen.xib in Resources */,
F81121131B506FD0002D9380 /* Images.xcassets in Resources */,
F89FC9BD1B51EFF50033BEFA /* Maze.tmx in Resources */,
F8CD0CE51B532DE1001E697C /* Maze.tmx in Resources */,
F811210A1B506FD0002D9380 /* GameScene.sks in Resources */,
F89FC9AD1B51C9660033BEFA /* moving.atlas in Resources */,
F81121111B506FD0002D9380 /* Main.storyboard in Resources */,
31 changes: 26 additions & 5 deletions Xmaze/Xmaze/Boundary.swift
Original file line number Diff line number Diff line change
@@ -20,20 +20,33 @@ class Boundary:SKNode {
fatalError("init(coder:) has not been implemented")
}

init (fromSKSwithRect rect:CGRect){
init (fromSKSwithRect rect:CGRect, isEdge:Bool){
super.init()

let newLocation = CGPoint(x: -(rect.size.width / 2), y: -(rect.size.height / 2) )
let newRect:CGRect = CGRect (origin: newLocation, size: rect.size)


createBoundary(newRect)
createBoundary(newRect, createAsEdge: isEdge)
}

init (theDict:Dictionary<NSObject, AnyObject> ) {

super.init()

let isEdgeAsString:String = theDict ["isEdge"] as AnyObject? as! String
var isEdge:Bool

if(isEdgeAsString == "true") {

isEdge = true
} else {

isEdge = false
}



let theX:String = theDict["x"] as AnyObject? as! String
let x:Int = theX.toInt()!

@@ -53,21 +66,29 @@ class Boundary:SKNode {

let rect:CGRect = CGRectMake( -(size.width / 2), -(size.height / 2), size.width, size.height )

createBoundary(rect)
createBoundary(rect, createAsEdge: isEdge)


}


func createBoundary(rect:CGRect){
func createBoundary(rect:CGRect, createAsEdge:Bool){
let shape = SKShapeNode(rect: rect, cornerRadius: 19)
shape.fillColor = SKColor.clearColor()
shape.strokeColor = SKColor.whiteColor()
shape.lineWidth = 1

addChild(shape)

self.physicsBody = SKPhysicsBody(rectangleOfSize: rect.size)
if (createAsEdge == false) {

self.physicsBody = SKPhysicsBody(rectangleOfSize: rect.size)
} else {

self.physicsBody = SKPhysicsBody(edgeLoopFromRect: rect)
}


self.physicsBody!.dynamic = false
self.physicsBody!.categoryBitMask = BodyType.boundary.rawValue
self.physicsBody!.friction = 0
Binary file modified Xmaze/Xmaze/GameScene.sks
Binary file not shown.
46 changes: 42 additions & 4 deletions Xmaze/Xmaze/GameScene.swift
Original file line number Diff line number Diff line change
@@ -40,7 +40,7 @@ class GameScene: SKScene, SKPhysicsContactDelegate, NSXMLParserDelegate{
/* initial properties */

self.backgroundColor = SKColor.blackColor()
view.showsPhysics = true
view.showsPhysics = false

self.physicsWorld.gravity = CGVector(dx: 0, dy: 0)
physicsWorld.contactDelegate = self
@@ -109,6 +109,7 @@ class GameScene: SKScene, SKPhysicsContactDelegate, NSXMLParserDelegate{
if(useTMXFiles==false){

setUpBoundaryFromSKS()
setUpEdgeFromSKS()
setUpStarsFromSKS()

} else {
@@ -130,16 +131,41 @@ class GameScene: SKScene, SKPhysicsContactDelegate, NSXMLParserDelegate{

println("found boundary")
let rect:CGRect = CGRect(origin: boundary.position, size: boundary.size)
let newBoundary:Boundary = Boundary(fromSKSwithRect: rect)
let newBoundary:Boundary = Boundary(fromSKSwithRect: rect, isEdge:false)
self.mazeWorld!.addChild(newBoundary)
newBoundary.position = boundary.position
boundary.removeFromParent()
}
}

}



func setUpEdgeFromSKS(){

mazeWorld!.enumerateChildNodesWithName("edge"){
node, stop in



if let edge = node as? SKSpriteNode{

println("found boundary")
let rect:CGRect = CGRect(origin: edge.position, size: edge.size)
let newEdge:Boundary = Boundary(fromSKSwithRect: rect, isEdge:true)
self.mazeWorld!.addChild(newEdge)
newEdge.position = edge.position

edge.removeFromParent()
}
}

}




func setUpStarsFromSKS(){

mazeWorld!.enumerateChildNodesWithName("star"){
@@ -310,12 +336,24 @@ class GameScene: SKScene, SKPhysicsContactDelegate, NSXMLParserDelegate{

let type:AnyObject? = attributeDict["type"]
if (type as? String == "Boundary") {
var tmxDict = attributeDict
tmxDict.updateValue("false", forKey: "isEdge")
let newBoundary:Boundary = Boundary(theDict: tmxDict)
mazeWorld!.addChild(newBoundary)

let newBoundary:Boundary = Boundary(theDict: attributeDict)

} else if (type as? String == "Edge") {

var tmxDict = attributeDict
tmxDict.updateValue("true", forKey: "isEdge")
let newBoundary:Boundary = Boundary(theDict: tmxDict)
mazeWorld!.addChild(newBoundary)


}else if (type as? String == "Star") {
}


else if (type as? String == "Star") {

let newStar:Star = Star(fromTMXFileWithDict: attributeDict)
mazeWorld!.addChild(newStar)
6 changes: 3 additions & 3 deletions Xmaze/Xmaze/maze.tmx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.0" orientation="orthogonal" renderorder="right-down" width="200" height="200" tilewidth="30" tileheight="30" nextobjectid="68">
<map version="1.0" orientation="orthogonal" renderorder="right-down" width="200" height="200" tilewidth="30" tileheight="30" nextobjectid="69">
<objectgroup name="Object Layer 1">
<object id="1" name="StartingPoint" type="Portal" x="930" y="2070" width="30" height="30"/>
<object id="1" name="StartingPoint" type="Portal" x="990" y="2070" width="30" height="30"/>
<object id="4" type="Boundary" x="1080" y="1290" width="90" height="180"/>
<object id="5" type="Boundary" x="1080" y="1590" width="90" height="180"/>
<object id="6" type="Boundary" x="1290" y="1290" width="90" height="180"/>
@@ -48,7 +48,6 @@
<object id="55" type="Star" x="1200" y="1830" width="30" height="30"/>
<object id="56" type="Star" x="1110" y="2070" width="30" height="30"/>
<object id="57" type="Star" x="1200" y="2070" width="30" height="30"/>
<object id="58" type="Star" x="1020" y="2070" width="30" height="30"/>
<object id="59" type="Star" x="1080" y="2370" width="30" height="30"/>
<object id="60" type="Star" x="1170" y="2370" width="30" height="30"/>
<object id="61" type="Star" x="990" y="2370" width="30" height="30"/>
@@ -58,5 +57,6 @@
<object id="65" type="Star" x="1800" y="2070" width="30" height="30"/>
<object id="66" type="Star" x="1890" y="2070" width="30" height="30"/>
<object id="67" type="Star" x="1710" y="2070" width="30" height="30"/>
<object id="68" type="Edge" x="900" y="1170" width="2070" height="1800"/>
</objectgroup>
</map>

0 comments on commit b140655

Please sign in to comment.