Skip to content

backslash-f/cskscene

Repository files navigation

swift-version swift-package-manager platforms ci-status license

CSKScene πŸŒƒ

A custom SKScene subclass with debug options enabled by default and the ability to observe game controllers via GCOverseer.

Usage Examples

Subclass CSKScene to have access to its properties:

class MyScene: CSKScene { ... }

Debug Settings

All debug settings are enable by default (in DEBUG mode):

  • showsFPS
  • showsFields
  • showsPhysics
  • showsDrawCount
  • showsNodeCount
  • showsQuadCount

To disable all options at once:

let debugSettings = DebugSettings(disableAll: true)
let myScene = CSKScene(size: someSize, debugSettings: debugSettings)

To disable some options:

let debugSettings = DebugSettings(showsFPS: false, showsPhysics: false, showsNodeCount: false)
let myScene = CSKScene(size: someSize, debugSettings: debugSettings)

It's also possible to change debug settings after initialization:

let myScene = CSKScene(size: someSize) // All debug settings are enabled by default...
myScene.debugSettings = DebugSettings(showsFields: false, showsQuadCount: false) // ... but these will be disabled

Observe Game Controllers

sink into the gcOverseer to keep track of connect / disconnect events of game controllers. E.g.:

class MyScene: CSKScene {
    func observeGameControllers() {
        gcOverseer.$isGameControllerConnected // `gcOverseer` from parent `CSKScene`
            .sink { isConnected in
                // Do something
            }
            .store(in: &cancellables) // `cancellables` from parent `CSKScene`
    }
}

Available Properties

Property Description Notes
var viewTop: CGFloat The "highest SKScene point" converted from the "highest SKView point". -
var viewBottom: CGFloat The "lowest SKScene point" converted from the "lowestSKView point". -
var viewLeft: CGFloat The "leftmost SKScene point" converted from the "leftmostSKView point". -
var viewRight: CGFloat The "rightmost SKScene point" converted from the "rightmostSKView point". -

Default Values

These properties have the following default values:

Property name Default value Notes
ignoresSiblingOrder true Prevents arbitrary z positions that may change every time a new frame is rendered.
isMultipleTouchEnabled true Surprinsingly this had to be set to true to support multiple touches when working with SceneView / SwiftUI.

To set then to false, override CSKScene.didMove(to:) in your subclass. For example:

class MyScene: CSKScene {
    override func didMove(to view: SKView) {
        super.didMove(to: view)
        view.ignoresSiblingOrder = false
        view.isMultipleTouchEnabled = false
    }
}

CSKNode

A custom SKNode subclass that provides observable properties based on KVO + Combine.

Usage Examples

Subscribe to didAttachToParent to get informed whenever the node is attached to or detached from a parent node.

class MyNode: CSKNode {

    override init() {
        super.init()
        sinkOnParentChanges()
    }

    func sinkOnParentChanges() {
        $didAttachToParent // `didAttachToParent` from parent `CSKNode`
            .sink { [weak self] isAttachedToParent in
                if isAttachedToParent {
                    // Setup your node πŸ‘ˆπŸ»
                }
            }
            .store(in: &cancellables) // `cancellables` from parent `CSKNode`
    }
}

Integration

Xcode

Use Xcode's built-in support for SPM.

or...

Package.swift

In your Package.swift, add CSKScene as a dependency:

dependencies: [
  .package(url: "https://github.com/backslash-f/cskscene", from: "0.1.0")
],

Associate the dependency with your target:

targets: [
  .target(name: "App", dependencies: ["CSKScene"])
]

Run: swift build

About

Custom SKScene with debugging features and game controller observing capabilities πŸŒƒ

Topics

Resources

License

Stars

Watchers

Forks

Languages