Skip to content

Commit

Permalink
tweaks from jazzy branch
Browse files Browse the repository at this point in the history
  • Loading branch information
mfessenden committed Nov 1, 2016
1 parent 2c08471 commit 5ea1064
Show file tree
Hide file tree
Showing 14 changed files with 193 additions and 78 deletions.
24 changes: 16 additions & 8 deletions Shared/SKTiledDemoScene.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,6 @@ public class SKTiledDemoScene: SKTiledScene {
#if os(OSX)
updateTrackingViews()
#endif


if let tilemap = tilemap {
tilemap.baseLayer.showGrid = true
}


}

// MARK: - Setup
Expand Down Expand Up @@ -204,6 +197,7 @@ public class SKTiledDemoScene: SKTiledScene {
deinit {
// Deregister for scene updates
NotificationCenter.default.removeObserver(self, name: Notification.Name(rawValue: "loadNextScene"), object: nil)
NotificationCenter.default.removeObserver(self, name: Notification.Name(rawValue: "loadPreviousScene"), object: nil)
removeAllActions()
removeAllChildren()
}
Expand All @@ -215,6 +209,10 @@ public class SKTiledDemoScene: SKTiledScene {
NotificationCenter.default.post(name: Notification.Name(rawValue: "loadNextScene"), object: nil)
}

public func loadPreviousScene() {
NotificationCenter.default.post(name: Notification.Name(rawValue: "loadPreviousScene"), object: nil)
}

override public func didChangeSize(_ oldSize: CGSize) {
super.didChangeSize(oldSize)

Expand Down Expand Up @@ -493,7 +491,7 @@ extension SKTiledDemoScene {

// 'H' hides the HUD
if event.keyCode == 0x04 {
cameraNode.overlay.isHidden = !cameraNode.overlay.isHidden
cameraNode.showOverlay = !cameraNode.showOverlay
}

// 'A', '0' reset the camera to 100%
Expand All @@ -504,6 +502,16 @@ extension SKTiledDemoScene {
cameraNode.resetCamera()
}
}

// '→' advances to the next scene
if event.keyCode == 0x7C {
self.loadNextScene()
}

// '←' advances to the next scene
if event.keyCode == 0x7B {
self.loadPreviousScene()
}
}

/**
Expand Down
35 changes: 27 additions & 8 deletions Sources/SKTile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,18 @@
import SpriteKit

/**
Represents the tile's physics type
Describes a tile's physics body shape.

- none: tile has no physics body.
- rectangle: tile physics shape is a rectangle.
- texture: tile physics shape is based on texture.
*/
public enum TilePhysics {
public enum PhysicsShape {
case none
case rectangle
case ellipse
case texture
case path
}

/**
Expand All @@ -35,7 +37,7 @@ public class SKTile: SKSpriteNode {
open var highlightColor: SKColor = SKColor.white // tile highlight color

// dynamics
open var physicsType: TilePhysics = .rectangle // physics type
open var physicsShape: PhysicsShape = .rectangle // physics type

/// Opacity value of the tile
open var opacity: CGFloat {
Expand Down Expand Up @@ -114,46 +116,63 @@ public class SKTile: SKSpriteNode {
- parameter isDynamic: `Bool` physics body is active.
*/
public func setupPhysics(isDynamic: Bool = false){
switch physicsType {
switch physicsShape {
case .none:
physicsBody = nil

case .rectangle:
physicsBody = SKPhysicsBody(rectangleOf: tileSize)

case .texture:
guard let texture = texture else {
physicsBody = nil
return
}
physicsBody = SKPhysicsBody(texture: texture, size: tileSize)

default:
physicsBody = nil
}

physicsBody?.isDynamic = isDynamic
}

/**
Set up the tile's dynamics body.
Set up the tile's dynamics body with a rectanglular shape.

- parameter rectSize: `CGSize` rectangle size.
- parameter isDynamic: `Bool` physics body is active.
*/
public func setupPhysics(rectSize: CGSize, isDynamic: Bool = false){
physicsType = .rectangle
physicsShape = .rectangle
physicsBody = SKPhysicsBody(rectangleOf: rectSize)
physicsBody?.isDynamic = isDynamic
}

/**
Set up the tile's dynamics body.
Set up the tile's dynamics body with a rectanglular shape.

- parameter withSize: `CGFloat` rectangle size.
- parameter isDynamic: `Bool` physics body is active.
*/
public func setupPhysics(withSize: CGFloat, isDynamic: Bool = false){
physicsType = .rectangle
physicsShape = .rectangle
physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: withSize, height: withSize))
physicsBody?.isDynamic = isDynamic
}

/**
Set up the tile's dynamics body with a circular shape.

- parameter radius: `CGFloat` circle radius.
- parameter isDynamic: `Bool` physics body is active.
*/
public func setupPhysics(radius: CGFloat, isDynamic: Bool = false){
physicsShape = .ellipse
physicsBody = SKPhysicsBody(circleOfRadius: radius)
physicsBody?.isDynamic = isDynamic
}

/**
Remove tile dynamics body.

Expand Down
28 changes: 8 additions & 20 deletions Sources/SKTileLayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ open class TiledLayerObject: SKNode, SKTiledObject {
}

/// Returns the frame rectangle of the layer (used to draw bounds).
override open var frame: CGRect {
open var boundingRect: CGRect {
return CGRect(x: 0, y: 0, width: sizeInPoints.width, height: -sizeInPoints.height)
}

Expand Down Expand Up @@ -676,7 +676,7 @@ open class TiledLayerObject: SKNode, SKTiledObject {

switch orientation {
case .orthogonal:
objectPath = polygonPath(self.frame.points)
objectPath = polygonPath(self.boundingRect.points)

case .isometric:
let topPoint = CGPoint(x: 0, y: 0)
Expand All @@ -696,7 +696,7 @@ open class TiledLayerObject: SKNode, SKTiledObject {
objectPath = polygonPath(invertedPoints)

case .hexagonal, .staggered:
objectPath = polygonPath(self.frame.points)
objectPath = polygonPath(self.boundingRect.points)
}

if let objectPath = objectPath {
Expand Down Expand Up @@ -757,10 +757,12 @@ open class TiledLayerObject: SKNode, SKTiledObject {

/**
Set up physics for the entire layer.

- parameter isDynamic: `Bool` layer is dynamic.
*/
open func setupPhysics(){
physicsBody = SKPhysicsBody(edgeLoopFrom: self.frame)
physicsBody?.isDynamic = false
open func setupPhysics(isDynamic: Bool=false){
physicsBody = SKPhysicsBody(edgeLoopFrom: self.boundingRect)
physicsBody?.isDynamic = isDynamic
}

override open var hashValue: Int {
Expand Down Expand Up @@ -1728,20 +1730,6 @@ extension TiledLayerObject {
addChild(node, coord: coord, offset: offset, zpos: zpos)
}

/**
Add a node at the given coordinates. By default, the zPositon
will be higher than all of the other nodes in the layer.

- parameter node: `SKNode` object.
- parameter x: `Int` x-coordinate.
- parameter y: `Int` y-coordinate.
- parameter zpos: `CGFloat?` optional z-position.
*/
public func addChild(_ node: SKNode, _ x: Int, _ y: Int, zpos: CGFloat? = nil) {
let coord = CGPoint(x, y)
addChild(node, coord: coord, offset: CGPoint.zero, zpos: zpos)
}

/**
Returns a point for a given coordinate in the layer.

Expand Down
24 changes: 12 additions & 12 deletions Sources/SKTileObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ public enum SKObjectType: String {


/**
Represents the object's physics body type
Represents the object's physics body type.

- none: object has no physics properties.
- dynamic: object is an active physics body.
- collision: object is a passive physics body.
*/
public enum ObjectPhysics {
public enum CollisionType {
case none
case dynamic
case collision
Expand All @@ -44,7 +44,7 @@ public enum ObjectPhysics {
- above: labels are rendered above the object.
- below: labels are rendered below the object.
*/
public enum LabelPosition {
internal enum LabelPosition {
case above
case below
}
Expand All @@ -62,12 +62,13 @@ open class SKTileObject: SKShapeNode, SKTiledObject {
open var id: Int = 0 // object id
open var gid: Int! // tile gid
open var type: String! // object type

internal var objectType: SKObjectType = .rectangle // shape type
open var points: [CGPoint] = [] // points that describe the object's shape.
internal var points: [CGPoint] = [] // points that describe the object's shape.

open var size: CGSize = CGSize.zero
open var properties: [String: String] = [:] // custom properties

open var physicsType: ObjectPhysics = .none // physics body type
internal var physicsType: CollisionType = .none // physics collision type

/// Object opacity
open var opacity: CGFloat {
Expand All @@ -81,9 +82,8 @@ open class SKTileObject: SKShapeNode, SKTiledObject {
set { self.isHidden = !newValue }
}


/// Returns the bounding box of the shape.
override open var frame: CGRect {
open var boundingRect: CGRect {
return CGRect(x: 0, y: 0, width: size.width, height: -size.height)
}

Expand Down Expand Up @@ -276,14 +276,14 @@ open class SKTileObject: SKShapeNode, SKTiledObject {
internal func renderWith(gid: Int) {
if let objectGroup = layer {
if let tileData = objectGroup.tilemap.getTileData(gid) {
let boundingRect = calculateAccumulatedFrame()
let boundingBox = calculateAccumulatedFrame()

if (tileData.texture != nil) {
childNode(withName: "GID_Sprite")?.removeFromParent()
let sprite = SKSpriteNode(texture: tileData.texture)
sprite.name = "GID_Sprite"
sprite.size.width = boundingRect.size.width
sprite.size.height = boundingRect.size.height
sprite.size.width = boundingBox.size.width
sprite.size.height = boundingBox.size.height
addChild(sprite)
strokeColor = SKColor.clear
fillColor = SKColor.clear
Expand Down Expand Up @@ -322,7 +322,7 @@ open class SKTileObject: SKShapeNode, SKTiledObject {
}

/**
Return the current points.
Returns the internal `SKTileObject.points` translated into the current map projection.

- returns: `[CGPoint]?` array of points.
*/
Expand Down
3 changes: 2 additions & 1 deletion Sources/SKTiledScene.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ open class SKTiledScene: SKScene, SKPhysicsContactDelegate, SKTiledSceneDelegate
}

required public init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
super.init()
}

deinit {
Expand Down Expand Up @@ -117,6 +117,7 @@ open class SKTiledScene: SKScene, SKPhysicsContactDelegate, SKTiledSceneDelegate
}
}


// MARK: - Setup

/**
Expand Down
6 changes: 6 additions & 0 deletions Sources/SKTiledSceneCamera.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ open class SKTiledSceneCamera: SKCameraNode {

// quick & dirty overlay node
internal let overlay: SKNode = SKNode()
open var showOverlay: Bool = true {
didSet {
guard oldValue != showOverlay else { return }
overlay.isHidden = !showOverlay
}
}

// MARK: - Init
public init(view: SKView, world node: SKNode) {
Expand Down
11 changes: 7 additions & 4 deletions Sources/SKTilemap.swift
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ open class SKTilemap: SKNode, SKTiledObject{
didSet {
guard oldValue != isPaused else { return }
let newColor: SKColor = isPaused ? SKColor(white: 0, alpha: 0.25) : SKColor.clear
let newColorBlendFactor: CGFloat = isPaused ? 0.3 : 0.0
let newColorBlendFactor: CGFloat = isPaused ? 0.2 : 0.0

speed = isPaused ? 0 : 1.0
color = newColor
Expand Down Expand Up @@ -981,7 +981,7 @@ open class SKTilemap: SKNode, SKTiledObject{
// time results
let timeInterval = Date().timeIntervalSince(timeStarted)
let timeStamp = String(format: "%.\(String(3))f", timeInterval)
print("\n# Success! tile map \"\(name!)\" rendered in: \(timeStamp)s\n")
print("\n# Success! tile map \"\(name != nil ? name! : "null")\" rendered in: \(timeStamp)s\n")

// dump the output of the current map to stdout
if (verbose == true) {
Expand Down Expand Up @@ -1225,11 +1225,14 @@ extension SKTilemap {
Output a summary of the current scenes layer data.
*/
public func debugLayers(reverse: Bool = false) {
guard (layerCount > 0) else { return }
guard (layerCount > 0) else {
print("# Tilemap \"\(name != nil ? name! : "null")\": 0 Layers")
return
}
let largestName = layerNames().max() { (a, b) -> Bool in a.characters.count < b.characters.count }

// format the header
let tilemapHeaderString = "# Tilemap \"\(name!)\": \(tileCount) Tiles: \(layerCount) Layers"
let tilemapHeaderString = "# Tilemap \"\(name != nil ? name! : "null")\": \(tileCount) Tiles: \(layerCount) Layers"
let filled = String(repeating: "-", count: tilemapHeaderString.characters.count)
print("\n\(tilemapHeaderString)\n\(filled)")

Expand Down
2 changes: 1 addition & 1 deletion Sources/SKTilesetData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ The `SKTilesetData` class stores data for a single tileset tile, with texture, i
*/
open class SKTilesetData: SKTiledObject {

weak open var tileset: SKTileset! // is assigned on add
weak open var tileset: SKTileset! // reference to parent tileset
open var uuid: String = UUID().uuidString // unique id
open var id: Int = 0 // tile id
open var texture: SKTexture! // initial tile texture
Expand Down
Binary file modified docs/Images/dynamic-objects.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 5ea1064

Please sign in to comment.