Swiftpack.co - 0xOpenBytes/Planar as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by 0xOpenBytes.
0xOpenBytes/Planar 0.1.0
👾 Build 2D games and simulations in Swift using SpriteKit
⭐️ 3
🕓 1 year ago
iOS macOS watchOS tvOS
.package(url: "https://github.com/0xOpenBytes/Planar.git", from: "0.1.0")

Planar

Planar is a library for building 2D games and simulations in Swift using SpriteKit. It provides a set of abstractions and utilities for organizing game objects, managing tasks, and handling input events.

Usage

To use Planar, simply create a new scene and inherit from PlanarScene:

class GameScene: PlanarScene<GameSceneNodeKey> {
    // ...
}

Next, define a set of unique node keys using an enum:

enum GameSceneNodeKey: Hashable {
    case player(UInt)
    case enemy(UInt)
    case powerUp
}

You can then add nodes to the scene using the add(node:forKey:) method, passing in a key that matches one of your defined keys:

add(node: playerNode, forKey: .player(0))
add(node: enemyNode, forKey: .enemy(0))
add(node: powerUpNode, forKey: .powerUp)

To retrieve a node from the scene, use the get(_:) or resolve(_:) methods, passing in the appropriate key:

if let player = get(.player(0)) {
    // Do something with the player node
}

You can also create plugins to modify properties of nodes in a type-safe way. Here's an example:

struct PositionPlugin: Plugin {
    var keyPath: WritableKeyPath<PlanarNode, CGPoint>

    func handle(value: CGPoint, output: inout CGPoint) async throws {
        output = value
    }
}

let playerNode = PlanarNode(node: SKSpriteNode(imageNamed: "player"))
playerNode.register(plugin: PositionPlugin(keyPath: \.position))

try await playerNode.handle(value: CGPoint(x: 100, y: 100))

In this example, we create a PositionPlugin that modifies the position property of a PlanarNode. We then create a PlanarNode representing a player, and use the handle method to apply the PositionPlugin to set the player's position to (100, 100).

GitHub

link
Stars: 3
Last commit: 49 weeks ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Dependencies

Release Notes

0.1.0
1 year ago

Planar

Planar is a library for building 2D games and simulations in Swift using SpriteKit. It provides a set of abstractions and utilities for organizing game objects, managing tasks, and handling input events.

Usage

To use Planar, simply create a new scene and inherit from PlanarScene:

class GameScene: PlanarScene<GameSceneNodeKey> {
    // ...
}

Next, define a set of unique node keys using an enum:

enum GameSceneNodeKey: Hashable {
    case player(UInt)
    case enemy(UInt)
    case powerUp
}

You can then add nodes to the scene using the add(node:forKey:) method, passing in a key that matches one of your defined keys:

add(node: playerNode, forKey: .player(0))
add(node: enemyNode, forKey: .enemy(0))
add(node: powerUpNode, forKey: .powerUp)

To retrieve a node from the scene, use the get(_:) or resolve(_:) methods, passing in the appropriate key:

if let player = get(.player(0)) {
    // Do something with the player node
}

You can also create plugins to modify properties of nodes in a type-safe way. Here's an example:

struct PositionPlugin: Plugin {
    var keyPath: WritableKeyPath<PlanarNode, CGPoint>

    func handle(value: CGPoint, output: inout CGPoint) async throws {
        output = value
    }
}

let playerNode = PlanarNode(node: SKSpriteNode(imageNamed: "player"))
let positionPlugin = PositionPlugin(keyPath: \.position)

try await playerNode.handle(value: CGPoint(x: 100, y: 100))

In this example, we create a PositionPlugin that modifies the position property of a PlanarNode. We then create a PlanarNode representing a player, and use the handle method to apply the PositionPlugin to set the player's position to (100, 100).


Full Changelog: https://github.com/0xOpenBytes/Planar/commits/0.1.0

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics