Skip to content

Commit

Permalink
update project to Xcode 9
Browse files Browse the repository at this point in the history
- add `SKTileObject.tileData` property
  • Loading branch information
mfessenden committed Sep 21, 2017
1 parent 4af8f98 commit 4444fc5
Show file tree
Hide file tree
Showing 23 changed files with 423 additions and 156 deletions.
2 changes: 1 addition & 1 deletion .swift-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.1
3.2
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
language: swift
osx_image: xcode8.0
osx_image: xcode9.0
xcode_project: SKTiled.xcodeproj
xcode_scheme: SKTiled-iOS
branches:
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ Change Log
- add `SKTilesetData.frameAt(index:)`
- add `SKTilesetData.setTexture(_:forFrame:)`
- add `SKTilesetData.setDuration(interval:forFrame:)`
- add `SKTileObject.tileData` property
- add `SKTiledSceneCamera.clampZoomValue`
- add `SKTiledSceneCamera.zoomClamping` property
- remove `SKTile.pauseAnimation`


Expand Down
183 changes: 121 additions & 62 deletions Demo/SKTiledDemoScene.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ public class SKTiledDemoScene: SKTiledScene {
internal var selected: [SKTiledLayerObject] = []

internal var clickshapes: Set<TileShape> = []
internal var pathshapes: Set<TileShape> = []
internal var cleanup: Set<TileShape> = []

internal var graphCoordinates: [CGPoint] = []
internal var plotPathfindingPath: Bool = true
internal var graphStartCoordinate: CGPoint?
internal var graphEndCoordinate: CGPoint?

internal var currentPath: [GKGridGraphNode] = []

/// Cleanup tile shapes queue
Expand All @@ -49,7 +51,9 @@ public class SKTiledDemoScene: SKTiledScene {
didSet {
guard oldValue != liveMode else { return }
self.cleanupTileShapes()
self.graphCoordinates = []
self.cleanupPathfindingShapes()
self.graphStartCoordinate = nil
self.graphEndCoordinate = nil
}
}

Expand Down Expand Up @@ -88,6 +92,12 @@ public class SKTiledDemoScene: SKTiledScene {
updateHud(tilemap)
}

/**
Return tile nodes at the given point.

- parameter coord: `CGPoint` event point.
- returns: `[SKTile]` tile nodes.
*/
func getTilesAt(coord: CGPoint) -> [SKTile] {
var result: [SKTile] = []
guard let tilemap = tilemap else { return result }
Expand All @@ -100,6 +110,12 @@ public class SKTiledDemoScene: SKTiledScene {
return result
}

/**
Return renderable nodes (tile & tile objects) at the given point.

- parameter point: `CGPoint` event point.
- returns: `[SKNode]` renderable nodes.
*/
func renderableNodesAt(point: CGPoint) -> [SKNode] {
var result: [SKNode] = []
let nodes = self.nodes(at: point)
Expand Down Expand Up @@ -191,13 +207,6 @@ public class SKTiledDemoScene: SKTiledScene {
_ = lowerGraphLayer.initializeGraph(walkable: walkableTiles)
}



case "graphtest-8x8.tmx":
if let graphLayer = tilemap.tileLayers(named: "Graph").first {
_ = graphLayer.initializeGraph(walkable: walkableTiles)
}

case "pacman.tmx":
if let graphLayer = tilemap.tileLayers(named: "Graph").first {
if let graph = graphLayer.initializeGraph(walkable: walkableTiles) {
Expand All @@ -211,20 +220,11 @@ public class SKTiledDemoScene: SKTiledScene {
}
}

case "level-ramps.tmx", "level-rivets.tmx":
//log("setting up Donkey Kong level: \(baseFilename)", level: .info)
if let actorsGroup = tilemap.objectGroups(named: "Actors").first {
log("found actors group", level: .info)
let actorObjects = actorsGroup.getObjects()
for object in actorObjects {
print("object: \(object)")
}
case "roguelike-16x16.tmx":
if let graphLayer = tilemap.tileLayers(named: "Graph").first {
_ = graphLayer.initializeGraph(walkable: walkableTiles)
}

case "line-widths.tmx":

let objectsLayer = tilemap.objectGroups(named: "objects1").first!
objectsLayer.lineWidth = 4

default:
return
Expand Down Expand Up @@ -277,6 +277,10 @@ public class SKTiledDemoScene: SKTiledScene {
NotificationCenter.default.post(name: Notification.Name(rawValue: "updateDebugLabels"), object: nil, userInfo: ["isolatedInfo": msg])
}

public func updateCoordinateInfo(msg: String) {
NotificationCenter.default.post(name: Notification.Name(rawValue: "updateDebugLabels"), object: nil, userInfo: ["coordinateInfo": msg])
}

/**
Send a command to the UI to update status.

Expand Down Expand Up @@ -321,12 +325,13 @@ public class SKTiledDemoScene: SKTiledScene {
*/
func plotNavigationPath() {
currentPath = []
guard (graphCoordinates.count == 2) else { return }
//guard (graphCoordinates.count == 2) else { return }
guard let startCoord = graphStartCoordinate,
let endCoord = graphEndCoordinate else { return }

// cleanup

let startPoint = graphCoordinates.first!.toVec2
let endPoint = graphCoordinates[1].toVec2
let startPoint = startCoord.toVec2
let endPoint = endCoord.toVec2

for (_, graph) in graphs {
if let startNode = graph.node(atGridPosition: startPoint) {
Expand All @@ -337,6 +342,49 @@ public class SKTiledDemoScene: SKTiledScene {
}
}

/**
Visualize the current grid graph path with a line.
*/
func drawCurrentPath(withColor: SKColor = TiledObjectColors.lime) {
guard let worldNode = worldNode,
let tilemap = tilemap else { return }
guard (currentPath.count > 2) else { return }

worldNode.childNode(withName: "CURRENT_PATH")?.removeFromParent()

// line dimensions
let headWidth: CGFloat = tilemap.tileSize.height
let lineWidth: CGFloat = tilemap.tileSize.halfWidth / 4

let lastZPosition = tilemap.lastZPosition + (tilemap.zDeltaForLayers * 4)
var points: [CGPoint] = []

for node in currentPath {
let nodePosition = worldNode.convert(tilemap.pointForCoordinate(vec2: node.gridPosition), from: tilemap.defaultLayer)
points.append(nodePosition)
}

// path shape
let path = polygonPath(points, threshold: 16)
let shape = SKShapeNode(path: path)
shape.isAntialiased = false
shape.lineWidth = lineWidth * 2
shape.strokeColor = withColor
shape.fillColor = .clear

worldNode.addChild(shape)
shape.zPosition = lastZPosition
shape.name = "CURRENT_PATH"

// arrowhead shape
let arrow = arrowFromPoints(startPoint: points[points.count - 2], endPoint: points.last!, tailWidth: lineWidth, headWidth: headWidth, headLength: headWidth)
let arrowShape = SKShapeNode(path: arrow)
arrowShape.strokeColor = .clear
arrowShape.fillColor = withColor
shape.addChild(arrowShape)
arrowShape.zPosition = lastZPosition
}

/**
Cleanup all tile shapes outside of the given coordinate.

Expand Down Expand Up @@ -384,14 +432,9 @@ public class SKTiledDemoScene: SKTiledScene {
Cleanup all tile shapes representing the current path.
*/
open func cleanupPathfindingShapes() {
// clean the current path shapes...
for pathshape in self.pathshapes {
let fadeAction = SKAction.fadeOut(withDuration: 0.2)
pathshape.run(fadeAction, completion: {
self.pathshapes.remove(pathshape)
self.cleanup.insert(pathshape)
})
}
// cleanup pathfinding shapes
guard let worldNode = worldNode else { return }
worldNode.childNode(withName: "CURRENT_PATH")?.removeFromParent()
}

/**
Expand Down Expand Up @@ -421,6 +464,17 @@ public class SKTiledDemoScene: SKTiledScene {
tile.removeFromParent()
}
}


var coordinateMessage = ""
if let graphStartCoordinate = graphStartCoordinate {
coordinateMessage += "Start: \(graphStartCoordinate.shortDescription)"
if (currentPath.isEmpty == false) {
coordinateMessage += ", \(currentPath.count) nodes"
}
}

updateCoordinateInfo(msg: coordinateMessage)
}

// MARK: - Delegate Callbacks
Expand Down Expand Up @@ -515,40 +569,20 @@ extension SKTiledDemoScene {

// double click
if (event.clickCount > 1) {
plotPathfindingPath = true
let hasStartCoordinate: Bool = (graphStartCoordinate != nil)

if graphCoordinates.contains(coord) {
cleanupPathfindingShapes()
return
}

graphCoordinates.append(coord)
if graphCoordinates.count > 2 {
let eventMessage = (hasStartCoordinate == true) ? "clearing coordinate" : "setting coordinate: \(coord.shortDescription)"
graphStartCoordinate = (hasStartCoordinate == true) ? nil : coord
updateCommandString(eventMessage)

if hasStartCoordinate == true {
cleanupPathfindingShapes()
graphCoordinates = graphCoordinates.reversed()[0...1].reversed()
}

if (graphCoordinates.count == 2) {
plotNavigationPath()
if (currentPath.isEmpty == false) {
for node in currentPath {
let xcoord = Int(node.gridPosition.x)
let ycoord = Int(node.gridPosition.y)

var nodeWeight: Float = 1
if let weightedNode = node as? SKTiledGraphNode {
nodeWeight = weightedNode.weight
}

if let tile = addTileToWorld(xcoord, ycoord, role: .pathfinding, weight: nodeWeight) {
self.pathshapes.insert(tile)
}
}
}
}

// single click
} else {
plotPathfindingPath = false
// highlight the current coordinate
if let tile = addTileToWorld(Int(coord.x), Int(coord.y), role: .coordinate) {
self.clickshapes.insert(tile)
Expand Down Expand Up @@ -590,6 +624,15 @@ extension SKTiledDemoScene {
let coord = defaultLayer.coordinateAtMouseEvent(event: event)
let validCoord = defaultLayer.isValid(Int(coord.x), Int(coord.y))

graphEndCoordinate = (validCoord == true) ? coord : nil

if let endCoord = graphEndCoordinate {
if (plotPathfindingPath == true) {
plotNavigationPath()
drawCurrentPath(withColor: tilemap.navigationColor)
}
}

// query nodes under the cursor to update the properties label
var propertiesInfoString = "--"

Expand Down Expand Up @@ -639,7 +682,6 @@ extension SKTiledDemoScene {
mouseTracker.coord = coord
mouseTracker.isValid = validCoord


if (tileShapesUnderCursor.isEmpty) {
if (liveMode == true) && (isPaused == false) {
_ = self.addTileToWorld(Int(coord.x), Int(coord.y), role: .highlight)
Expand Down Expand Up @@ -999,5 +1041,22 @@ extension SKTiledDemoScene {
self.speed -= 0.5
updateCommandString("scene speed: \(speed.roundTo())", duration: 1.0)
}

// 'c' adjusts the camera zoom clamp value
if eventKey == 0x8 {
var newClampValue: CameraZoomClamping = .none
switch cameraNode.zoomClamping {
case .none:
newClampValue = .tenth
case .tenth:
newClampValue = .quarter
case .quarter:
newClampValue = .half
case .half:
newClampValue = .none
}
self.cameraNode.zoomClamping = newClampValue
updateCommandString("camera zoom clamping: \(newClampValue)", duration: 1.0)
}
}
}
Binary file modified Resources/roguelike-16x16-anim.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 7 additions & 1 deletion Resources/roguelike-16x16.tmx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.0" tiledversion="1.0.2" orientation="orthogonal" renderorder="right-down" width="42" height="42" tilewidth="16" tileheight="16" backgroundcolor="#6eb4d5" nextobjectid="22">
<map version="1.0" tiledversion="1.0.3" orientation="orthogonal" renderorder="right-down" width="42" height="42" tilewidth="16" tileheight="16" backgroundcolor="#6eb4d5" nextobjectid="22">
<properties>
<property name="gridColor" type="color" value="#99252525"/>
<property name="highlightColor" type="color" value="#ff3271d3"/>
<property name="level" type="int" value="1"/>
<property name="navigationColor" type="color" value="#ff166dff"/>
<property name="worldScale" type="float" value="1.4"/>
</properties>
<tileset firstgid="1" source="roguelike-16x16.tsx"/>
Expand Down Expand Up @@ -77,4 +78,9 @@
<ellipse/>
</object>
</objectgroup>
<layer name="Graph" width="42" height="42" visible="0">
<data encoding="base64" compression="zlib">
eJztl1EKAjEMRP3y/sdcbyF+LIRhJplkW0XYQtDdtsnrJJH6ej4er9tu+yP7jF8zdDh38x6LGM9RaZ2d5wCLa6ec2ZhyIq/i7OTO4ax8nnMHcDBNd3HiJ9ur2JiG3bzH+NXo9IbiZO9Xcjq+kCPyIBc+OzGcfLvnVbyshpVvpU+X03lX5SbjvNJHqlYmOar6qIrtcMZn/O4M5FQxrnCyfVc5q5rp5mkHZ+bPiYH5cPxNOF3fDudKRsa5s993clZx45xay9a4rBhP9bvDqZ6rmF3G2EfZGrcOos/s/jDlzHTq1Oqk5h3tmZ6urnENrp/cwVhcdZ+qrNI9nr3LxvxVv+2T3DNONTINWV5wz1VO5t/lZLqyHH6bE/PKtO32Y3euw5n5wzsSy4fLyeYVJ/vP0znHRMtMg5WmarfLqbSc+HDvbmy+yzq5f50xqr2oq1ufinOSB+RkDOi3qofVerqcnTr4Fuduuzlvzl9xvgE4egCv
</data>
</layer>
</map>
18 changes: 18 additions & 0 deletions Resources/roguelike-16x16.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -498,4 +498,22 @@
<property name="type" value="water"/>
</properties>
</tile>
<tile id="1767">
<properties>
<property name="walkable" type="bool" value="true"/>
<property name="weight" type="float" value="-5"/>
</properties>
</tile>
<tile id="1768">
<properties>
<property name="walkable" type="bool" value="true"/>
<property name="weight" type="float" value="1"/>
</properties>
</tile>
<tile id="1769">
<properties>
<property name="walkable" type="bool" value="true"/>
<property name="weight" type="float" value="200"/>
</properties>
</tile>
</tileset>
6 changes: 2 additions & 4 deletions SKTiled.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "SKTiled"
s.version = "1.15"
s.version = "1.16"
s.summary = "SKTiled is a framework for using Tiled content with Apple's SpriteKit."
s.description = <<-DESC
SKTiled is a framework for using Tiled content with Apple's SpriteKit, allowing the creation of game assets from .tmx files.
Expand All @@ -10,12 +10,10 @@ Pod::Spec.new do |s|
s.license = { :type => 'MIT', :file => 'LICENSE.md' }

s.ios.deployment_target = '9.0'
s.osx.deployment_target = '10.11'
s.osx.deployment_target = '10.12'
s.source = { :git => "https://github.com/mfessenden/SKTiled.git", :tag => s.version }

s.source_files = 'Sources/*.swift'
s.requires_arc = true
s.library = 'z'
s.preserve_path = 'zlib/*'
s.pod_target_xcconfig = { 'SWIFT_INCLUDE_PATHS' => '$(PODS_ROOT)/SKTiled/zlib' }
end
Loading

0 comments on commit 4444fc5

Please sign in to comment.