Skip to content

Commit

Permalink
tweak demo scene for stabilty in macOS Sierra
Browse files Browse the repository at this point in the history
- add `SKTileObject.isDynamic` property
- add `SKTiledSceneCamera.overlay` property
  • Loading branch information
mfessenden committed Oct 28, 2016
1 parent 7b62182 commit d324d2e
Show file tree
Hide file tree
Showing 18 changed files with 146 additions and 150 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Change Log

#### Changes
- add `SKTileObject.isDynamic` property

- add `SKTiledSceneCamera.overlay` property

1.0.3
--------------------------
Expand Down
12 changes: 0 additions & 12 deletions SKTiled.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@
4C440BB51DAE913A00DEC9A4 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 4C440BAF1DAE913A00DEC9A4 /* Main.storyboard */; };
4C440BB61DAE913A00DEC9A4 /* GameViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C440BB11DAE913A00DEC9A4 /* GameViewController.swift */; };
4C5479FE1DC15AF6008B3473 /* GameWindowController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C5479FD1DC15AF6008B3473 /* GameWindowController.swift */; };
4C5589281DC1B57400EB89DC /* objects-dynamic.tmx in Resources */ = {isa = PBXBuildFile; fileRef = 4C5589271DC1B57400EB89DC /* objects-dynamic.tmx */; };
4C5589291DC1B57400EB89DC /* objects-dynamic.tmx in Resources */ = {isa = PBXBuildFile; fileRef = 4C5589271DC1B57400EB89DC /* objects-dynamic.tmx */; };
4C55892B1DC1B80000EB89DC /* sky-256x192.png in Resources */ = {isa = PBXBuildFile; fileRef = 4C55892A1DC1B80000EB89DC /* sky-256x192.png */; };
4C55892C1DC1B80000EB89DC /* sky-256x192.png in Resources */ = {isa = PBXBuildFile; fileRef = 4C55892A1DC1B80000EB89DC /* sky-256x192.png */; };
4C88E18C1D8A1BC400FCCFA3 /* roguelike-16x16.tmx in Resources */ = {isa = PBXBuildFile; fileRef = 4C88E18B1D8A1BC400FCCFA3 /* roguelike-16x16.tmx */; };
4C88E1AB1D8A241F00FCCFA3 /* ortho4-16x16.tmx in Resources */ = {isa = PBXBuildFile; fileRef = 4C88E1AA1D8A241F00FCCFA3 /* ortho4-16x16.tmx */; };
4C88E1AD1D8A242F00FCCFA3 /* ortho4-16x16.png in Resources */ = {isa = PBXBuildFile; fileRef = 4C88E1AC1D8A242F00FCCFA3 /* ortho4-16x16.png */; };
Expand Down Expand Up @@ -95,8 +91,6 @@
4C440BB11DAE913A00DEC9A4 /* GameViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GameViewController.swift; sourceTree = "<group>"; };
4C440BB21DAE913A00DEC9A4 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
4C5479FD1DC15AF6008B3473 /* GameWindowController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GameWindowController.swift; sourceTree = "<group>"; };
4C5589271DC1B57400EB89DC /* objects-dynamic.tmx */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; name = "objects-dynamic.tmx"; path = "Custom/objects-dynamic.tmx"; sourceTree = SOURCE_ROOT; };
4C55892A1DC1B80000EB89DC /* sky-256x192.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = "sky-256x192.png"; path = "Custom/sky-256x192.png"; sourceTree = SOURCE_ROOT; };
4C88E18B1D8A1BC400FCCFA3 /* roguelike-16x16.tmx */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = "roguelike-16x16.tmx"; sourceTree = "<group>"; };
4C88E1AA1D8A241F00FCCFA3 /* ortho4-16x16.tmx */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = "ortho4-16x16.tmx"; sourceTree = "<group>"; };
4C88E1AC1D8A242F00FCCFA3 /* ortho4-16x16.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "ortho4-16x16.png"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -200,8 +194,6 @@
4C7A77AA1DA6A21A0018A55D /* External */ = {
isa = PBXGroup;
children = (
4C55892A1DC1B80000EB89DC /* sky-256x192.png */,
4C5589271DC1B57400EB89DC /* objects-dynamic.tmx */,
);
name = External;
sourceTree = "<group>";
Expand Down Expand Up @@ -386,8 +378,6 @@
4C1774A81D90999C000C0AFD /* hex-65x65-65x230.png in Resources */,
4C440B9C1DAE911000DEC9A4 /* Assets.xcassets in Resources */,
4C1774A61D90999C000C0AFD /* roguelike-16x16.tmx in Resources */,
4C5589291DC1B57400EB89DC /* objects-dynamic.tmx in Resources */,
4C55892C1DC1B80000EB89DC /* sky-256x192.png in Resources */,
4C1774A51D90999C000C0AFD /* ortho4-16x16.tmx in Resources */,
4C440BA81DAE913000DEC9A4 /* Main.storyboard in Resources */,
4C1774AD1D90999C000C0AFD /* staggered-64x192.png in Resources */,
Expand All @@ -411,14 +401,12 @@
4C440B9B1DAE911000DEC9A4 /* Assets.xcassets in Resources */,
4C88E1AD1D8A242F00FCCFA3 /* ortho4-16x16.png in Resources */,
4C440BB51DAE913A00DEC9A4 /* Main.storyboard in Resources */,
4C5589281DC1B57400EB89DC /* objects-dynamic.tmx in Resources */,
4CFEA3591D8D02D20055C150 /* staggered-64x192.png in Resources */,
4CA3DA0E1D8AF07C001E165A /* DemoFiles.plist in Resources */,
4CD6D2DE1D8B9EA10083DA7B /* isometric-130x230.png in Resources */,
4CFEA35C1D8D04320055C150 /* hex-65x65-65x230.png in Resources */,
4CFEA3581D8D02D20055C150 /* staggered-64x33.tmx in Resources */,
4C440BB41DAE913A00DEC9A4 /* LaunchScreen.storyboard in Resources */,
4C55892B1DC1B80000EB89DC /* sky-256x192.png in Resources */,
4CD6D2D51D8B9DE20083DA7B /* roguelike-16x16.tsx in Resources */,
4CD6D2DD1D8B9EA10083DA7B /* isometric-130x66.tmx in Resources */,
);
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

83 changes: 13 additions & 70 deletions Shared/SKTiledDemoScene.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,6 @@ public class SKTiledDemoScene: SKTiledScene {
override public func didMove(to view: SKView) {
super.didMove(to: view)

#if os(OSX)
// add mouse tracking for OSX
let options = [NSTrackingAreaOptions.mouseMoved, NSTrackingAreaOptions.activeAlways] as NSTrackingAreaOptions
let trackingArea = NSTrackingArea(rect: view.frame, options: options, owner: self, userInfo: nil)
view.addTrackingArea(trackingArea)
#endif

// setup demo UI
setupDemoUI()
setupDebuggingLabels()
Expand All @@ -52,7 +45,6 @@ public class SKTiledDemoScene: SKTiledScene {
*/
public func setupDemoUI() {
guard let view = self.view else { return }

// set up camera overlay UI
let lastZPosition: CGFloat = (tilemap != nil) ? tilemap.lastZPosition * 10 : 5000

Expand Down Expand Up @@ -99,7 +91,9 @@ public class SKTiledDemoScene: SKTiledScene {
// position towards the bottom of the scene
showObjectsButton.position.y -= (view.bounds.size.height / 2.25)
showObjectsButton.zPosition = lastZPosition
showObjectsButton.isUserInteractionEnabled = tilemap.objectGroups.count > 0 ? true : false

let hasObjects = (tilemap != nil) ? tilemap.objectGroups.count > 0 : false
showObjectsButton.isUserInteractionEnabled = hasObjects
}


Expand All @@ -122,7 +116,6 @@ public class SKTiledDemoScene: SKTiledScene {
public func setupDebuggingLabels() {
guard let view = self.view else { return }
guard let cameraNode = cameraNode else { return }

var tilemapInfoY: CGFloat = 0.77
var tileInfoY: CGFloat = 0.81
var propertiesInfoY: CGFloat = 0.85
Expand Down Expand Up @@ -193,6 +186,13 @@ public class SKTiledDemoScene: SKTiledScene {
return tile
}

// MARK: - Deinitialization
deinit {
print("[SKTiledDemoScene]: Deinitializing...")
// Deregister for scene updates
NotificationCenter.default.removeObserver(self, name: Notification.Name(rawValue: "loadNextScene"), object: nil)
}

/**
Call back to the GameViewController to load the next scene.
*/
Expand All @@ -202,21 +202,12 @@ public class SKTiledDemoScene: SKTiledScene {

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

uiScale = size.width / 400
updateHud()

#if os(OSX)
if let view = self.view {
let options = [NSTrackingAreaOptions.mouseMoved, NSTrackingAreaOptions.activeAlways] as NSTrackingAreaOptions
// clear out old tracking areas
for oldTrackingArea in view.trackingAreas {
view.removeTrackingArea(oldTrackingArea)
}

let trackingArea = NSTrackingArea(rect: view.frame, options: options, owner: self, userInfo: nil)
view.addTrackingArea(trackingArea)
}
updateTrackingViews()
#endif
}

Expand All @@ -242,7 +233,6 @@ public class SKTiledDemoScene: SKTiledScene {
*/
private func updateHud(){
guard let view = self.view else { return }

let activeButtons = buttons.filter( {$0.isHidden == false})
guard activeButtons.count > 0 else { return }

Expand Down Expand Up @@ -302,18 +292,6 @@ public class SKTiledDemoScene: SKTiledScene {
}


public extension SKNode {

/**
Position the node by a percentage of the view size.
*/
public func posByCanvas(x: CGFloat, y: CGFloat) {
guard let scene = scene else { return }
guard let view = scene.view else { return }
self.position = scene.convertPoint(fromView: (CGPoint(x: CGFloat(view.bounds.size.width * x), y: CGFloat(view.bounds.size.height * (1.0 - y)))))
}
}


#if os(iOS) || os(tvOS)
// Touch-based event handling
Expand Down Expand Up @@ -417,42 +395,6 @@ extension SKTiledDemoScene {
propertiesInformationLabel.text = propertiesInfoString
}

override open func mouseMoved(with event: NSEvent) {
super.mouseMoved(with: event)

updateTrackingViews()

guard let tilemap = tilemap else { return }
let baseLayer = tilemap.baseLayer

// make sure there are no UI objects under the mouse
let scenePosition = event.location(in: self)
if !isValidPosition(point: scenePosition) { return }

// get the position in the baseLayer (inverted)
let positionInLayer = baseLayer.mouseLocation(event: event)
let coord = baseLayer.screenToTileCoords(positionInLayer)

tileInformationLabel?.isHidden = false
tileInformationLabel?.text = "Tile: \(coord.coordDescription), \(positionInLayer.roundTo())"

if (tilemap.isPaused == false){
// highlight the current coordinate
let _ = addTileAt(layer: baseLayer, Int(coord.x), Int(coord.y), duration: 0.05)
}

// tile properties output
var propertiesInfoString = "ID: ~"
if let tile = tilemap.firstTileAt(coord) {
propertiesInfoString = "ID: \(tile.tileData.id)"
if tile.tileData.propertiesString != "" {
propertiesInfoString += "; \(tile.tileData.propertiesString)"
}
}
propertiesInformationLabel.isHidden = false
propertiesInformationLabel.text = propertiesInfoString
}

override open func mouseDragged(with event: NSEvent) {
guard let cameraNode = cameraNode else { return }
cameraNode.scenePositionChanged(event)
Expand Down Expand Up @@ -531,6 +473,7 @@ extension SKTiledDemoScene {
*/
open func updateTrackingViews(){
if let view = self.view {
print("[SKTiledDemoScene]: updating tracking views...")
let options = [NSTrackingAreaOptions.mouseMoved, NSTrackingAreaOptions.activeAlways] as NSTrackingAreaOptions
// clear out old tracking areas
for oldTrackingArea in view.trackingAreas {
Expand Down
14 changes: 14 additions & 0 deletions Sources/SKTileLayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1441,6 +1441,20 @@ open class SKObjectGroup: TiledLayerObject {
open func getObjects(named: String) -> [SKTileObject] {
return objects.filter( {$0.name == named})
}

/**
Called when the layer is finished rendering.

- parameter duration: `TimeInterval` fade-in duration.
*/
override open func didFinishRendering(duration: TimeInterval=0) {
super.didFinishRendering(duration: duration)
for object in objects {
if object.hasKey("isDynamic") {

}
}
}
}


Expand Down
29 changes: 28 additions & 1 deletion Sources/SKTileObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,21 @@ public enum SKObjectType: String {
case polyline
}


/**
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 {
case none
case dynamic
case collision
}


/**
Label description orientation.

Expand Down Expand Up @@ -50,7 +65,7 @@ open class SKTileObject: SKShapeNode, SKTiledObject {
internal var objectType: SKObjectType = .rectangle // shape type
open 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 properties: [String: String] = [:] // custom properties

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

// dynamics
open var isDynamic: Bool = false

// MARK: - Init
override public init(){
super.init()
Expand Down Expand Up @@ -283,6 +301,15 @@ open class SKTileObject: SKShapeNode, SKTiledObject {
}
return vertices
}

open func setupPhysics() {
guard let objectPath = path else {
print("[SKTileObject]: WARNING: object path not set: \"\(self.name != nil ? self.name! : "null")\"")
return
}
physicsBody = SKPhysicsBody(edgeLoopFrom: objectPath)
physicsBody?.isDynamic = isDynamic
}
}


Expand Down
14 changes: 10 additions & 4 deletions Sources/SKTiled+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,6 @@ public func lerp(start: CGPoint, end: CGPoint, t: CGFloat) -> CGPoint {

public extension CGSize {

public init(width: Int, height: Int) {
self.init(width: CGFloat(width), height: CGFloat(height))
}

public var count: Int { return Int(width) * Int(height) }
public var halfSize: CGSize { return CGSize(width: width / 2, height: height / 2) }
public var halfWidth: CGFloat { return width / 2.0 }
Expand Down Expand Up @@ -248,6 +244,15 @@ public extension SKScene {

internal extension SKNode {

/**
Position the node by a percentage of the view size.
*/
internal func posByCanvas(x: CGFloat, y: CGFloat) {
guard let scene = scene else { return }
guard let view = scene.view else { return }
self.position = scene.convertPoint(fromView: (CGPoint(x: CGFloat(view.bounds.size.width * x), y: CGFloat(view.bounds.size.height * (1.0 - y)))))
}

/// visualize a node's anchor point.
internal var drawAnchor: Bool {
get {
Expand Down Expand Up @@ -305,6 +310,7 @@ internal extension SKNode {
}
}


public extension SKColor {

/// Returns the hue, saturation, brightess & alpha components of the color
Expand Down
10 changes: 8 additions & 2 deletions Sources/SKTiledScene.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public protocol SKTiledSceneDelegate {
- parameter cameraNode: `SKTiledSceneCamera!` scene camera node.
- parameter tilemap: `SKTilemap!` tile map node.
*/
open class SKTiledScene: SKScene, SKTiledSceneDelegate {
open class SKTiledScene: SKScene, SKPhysicsContactDelegate, SKTiledSceneDelegate {

/// World container node.
open var worldNode: SKNode!
Expand Down Expand Up @@ -68,21 +68,27 @@ open class SKTiledScene: SKScene, SKTiledSceneDelegate {
*/
public init(size: CGSize, tmxFile: String) {
super.init(size: size)
setupWorld()
tmxFilename = tmxFile
}

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

deinit {
print("[SKTiledScene]: Deinitializing...")
}

override open func sceneDidLoad() {
setupWorld()
}

override open func didMove(to view: SKView) {
guard let worldNode = worldNode else { return }

physicsWorld.gravity = CGVector.zero
physicsWorld.contactDelegate = self

// setup the camera
setupCamera()

Expand Down
Loading

0 comments on commit d324d2e

Please sign in to comment.