https://github.com/0xopenbytes/planar
👾 Build 2D games and simulations in Swift using SpriteKit
https://github.com/0xopenbytes/planar
2d game game-engine game-engine-2d ios macos spritekit swift tvos watchos
Last synced: about 1 year ago
JSON representation
👾 Build 2D games and simulations in Swift using SpriteKit
- Host: GitHub
- URL: https://github.com/0xopenbytes/planar
- Owner: 0xOpenBytes
- License: mit
- Created: 2023-03-25T18:35:22.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2023-05-18T23:02:38.000Z (about 3 years ago)
- Last Synced: 2025-03-29T04:25:48.913Z (about 1 year ago)
- Topics: 2d, game, game-engine, game-engine-2d, ios, macos, spritekit, swift, tvos, watchos
- Language: Swift
- Homepage: https://0xopenbytes.github.io/Planar/
- Size: 17.6 KB
- Stars: 4
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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`:
```swift
class GameScene: PlanarScene {
// ...
}
```
Next, define a set of unique node keys using an enum:
```swift
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:
```swift
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:
```swift
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:
```swift
struct PositionPlugin: Plugin {
var keyPath: WritableKeyPath
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).