Tabletopkit

作者 dpearson26998d90fd121a26无许可证1.1K 个星标收录于 2026年10月8日更新于 2026年10月8日仓库2个月前更新

Builds multiplayer spatial board games using TabletopKit on visionOS. Use when creating tabletop game experiences with boards, pieces, cards, or dice; managing seats, turns, equipment state, TabletopAction flows, or TabletopInteraction delegates; synchronizing gameplay through FaceTime Group Activities; rendering with RealityKit; or implementing snapping, tosses, and physics on a virtual table surface.

AI 生成的概览

指导使用 TabletopKit 在 visionOS 上构建多人空间桌游,涵盖棋具、座位、动作与 RealityKit 渲染。

功能
该技能提供在 visionOS 上使用 TabletopKit 构建桌面棋盘游戏的参考说明,涵盖桌面与棋盘设置、棋子/卡牌/骰子等棋具、玩家座位、动作与回合、手势交互以及 RealityKit 渲染。它还涉及通过 Group Activities 实现 FaceTime 多人同步,并包含常见错误与审查清单。产出的是指导与代码模式,而非可直接运行的成品。
适用场景
在创建包含棋盘、棋子、卡牌或骰子的 visionOS 桌游体验时使用,也适用于处理座位、回合、棋具状态、TabletopAction 流程、TabletopInteraction 委托,以及虚拟桌面上的吸附、抛掷与物理效果。通过 FaceTime Group Activities 同步游戏或使用 RealityKit 渲染游戏状态时同样适用。
运行要求
需要 visionOS 开发环境以及 TabletopKit 和 RealityKit 框架,多人模式还需 Group Activities 能力。桌面、棋子、卡牌和骰子的视觉表现需要 RealityKit 内容包中的 USDZ 资源。多人测试需要多台 Apple Vision Pro 设备进行 FaceTime 通话,因为模拟器仅支持单人布局测试。仅为说明文档,不附带脚本。

TabletopKit

Build visionOS board games whose synchronized state changes flow through TabletopAction and render with RealityKit. The availability matrix below owns version details.

Contents

Setup

TierAPIs
visionOS 2.0+Core gameplay, equipment, seats, actions, rendering, Group Activities
visionOS 2.2+TabletopInteraction.Configuration
visionOS 26.0+Custom actions/state, registration, advanced toss outcomes, discarded-action observation

Simulator supports single-player layout testing, not multiplayer.

Project Configuration

  1. import TabletopKit in source files that define game logic.
  2. import RealityKit for entity-based rendering.
  3. For multiplayer, add the Group Activities capability in Signing & Capabilities.
  4. Provide table, piece, card, and dice USDZ assets in a RealityKit content bundle.

Key Types Overview

TypeRole
TabletopGameCentral game manager; owns setup, actions, observers, rendering
TableSetupConfiguration object passed to TabletopGame init
Tabletop / EntityTabletopProtocol for the table surface
Equipment / EntityEquipmentProtocol for interactive game pieces
TableSeat / EntityTableSeatProtocol for player seat positions
TabletopActionCommands that modify game state
TabletopInteractionGesture-driven player interactions with equipment
TabletopGame.ObserverCallback protocol for reacting to confirmed actions
TabletopGame.RenderDelegateCallback protocol for visual updates
EntityRenderDelegateRealityKit-specific render delegate

Game Configuration

Build and validate a game in this order:

  1. Define the tabletop, equipment, and seats.
  2. Configure TableSetup and register every custom action type.
  3. Create the game, attach its observer and renderer, claim a seat, and establish automatic or manual update handling.
  4. Inspect the current snapshot for required equipment IDs, parents, seats, and counters before starting multiplayer. Fix the setup and rebuild if an invariant fails.
swift
import TabletopKitimport RealityKit
let table = GameTable()var setup = TableSetup(tabletop: table)setup.add(seat: PlayerSeat(index: 0, pose: seatPose0))setup.add(seat: PlayerSeat(index: 1, pose: seatPose1))setup.add(equipment: GamePawn(id: .init(1)))setup.add(equipment: GameDie(id: .init(2)))
let game = TabletopGame(tableSetup: setup)game.claimAnySeat()

Call update(deltaTime:) each frame if automatic updates are not enabled via the .tabletopGame(_:parent:automaticUpdate:) modifier. Read state safely with withCurrentSnapshot(_:).

Table and Board

Tabletop Protocol

Conform to EntityTabletop to define the playing surface. Provide a shape (round or rectangular) and a RealityKit Entity for visual representation.

swift
struct GameTable: EntityTabletop {    var shape: TabletopShape    var entity: Entity    var id: EquipmentIdentifier
    init() {        entity = try! Entity.load(named: "table/game_table", in: contentBundle)        shape = .round(entity: entity)        id = .init(0)    }}

Table Shapes

Use factory methods on TabletopShape:

swift
// Round table from dimensionslet round = TabletopShape.round(    center: .init(x: 0, y: 0, z: 0),    radius: 0.5,    thickness: 0.05,    in: .meters)
// Rectangular table from entitylet rect = TabletopShape.rectangular(entity: tableEntity)

Equipment (Pieces, Cards, Dice)

Equipment Protocol

All interactive game objects conform to Equipment (or EntityEquipment for RealityKit-rendered pieces). Each piece has an id (EquipmentIdentifier) and an initialState property.

Choose the state type based on the equipment:

State TypeUse Case
BaseEquipmentStateGeneric pieces, pawns, tokens
CardStatePlaying cards (tracks faceUp / face-down)
DieStateDice with an integer value
RawValueStateCustom data encoded as UInt64
CustomEquipmentStateCustom state with a BaseEquipmentState plus game data; see the availability matrix

Defining Equipment

swift
// Pawn -- uses BaseEquipmentStatestruct GamePawn: EntityEquipment {    var id: EquipmentIdentifier    var initialState: BaseEquipmentState    var entity: Entity
    init(id: EquipmentIdentifier) {        self.id = id        self.entity = try! Entity.load(named: "pieces/pawn", in: contentBundle)        self.initialState = BaseEquipmentState(            parentID: .init(0), seatControl: .any,            pose: .identity, entity: entity        )    }}
// Card -- uses CardState (tracks faceUp)struct PlayingCard: EntityEquipment {    var id: EquipmentIdentifier    var initialState: CardState    var entity: Entity
    init(id: EquipmentIdentifier) {        self.id = id        self.entity = try! Entity.load(named: "cards/card", in: contentBundle)        self.initialState = .faceDown(            parentID: .init(0), seatControl: .any,            pose: .identity, entity: entity        )    }}
// Die -- uses DieState (tracks integer value)struct GameDie: EntityEquipment {    var id: EquipmentIdentifier    var initialState: DieState    var entity: Entity
    init(id: EquipmentIdentifier) {        self.id = id        self.entity = try! Entity.load(named: "dice/d6", in: contentBundle)        self.initialState = DieState(            value: 1, parentID: .init(0), seatControl: .any,            pose: .identity, entity: entity        )    }}

ControllingSeats

Restrict which players can interact with a piece via seatControl:

  • .any -- any player
  • .restricted([seatID1, seatID2]) -- specific seats only
  • .restrictedCurrent([seatID1, seatID2]) -- specific seats only while they are in turn
  • .current -- only the seat whose turn it is
  • .inherited -- inherits from parent equipment

Equipment Hierarchy and Layout

Equipment can be parented to other equipment. Override layoutChildren(for:visualState:) to position children. Return one of:

  • .planarStacked(layout:animationDuration:) -- cards/tiles stacked vertically
  • .planarOverlapping(layout:animationDuration:) -- cards fanned or overlapping
  • .volumetric(layout:animationDuration:) -- full 3D layout

See references/tabletopkit-patterns.md [blocked] for card fan, grid, and overlap layout examples.

Player Seats

Conform to EntityTableSeat and provide a pose around the table:

swift
struct PlayerSeat: EntityTableSeat {    var id: TableSeatIdentifier    var initialState: TableSeatState    var entity: Entity
    init(index: Int, pose: TableVisualState.Pose2D) {        self.id = TableSeatIdentifier(index)        self.entity = Entity()        self.initialState = TableSeatState(pose: pose, context: 0)    }}

Claim a seat before interacting: game.claimAnySeat(), game.claimSeat(matching:), or game.releaseSeat(). Observe changes via TabletopGame.Observer.playerChangedSeats.

Game Actions and Turns

Built-in Actions

Use TabletopAction factory methods to modify game state:

swift
// Move equipment to a new parentgame.addAction(.moveEquipment(matching: pieceID, childOf: targetID, pose: newPose))
// Flip a card face-upgame.addAction(.updateEquipment(card, faceUp: true))
// Update die valuegame.addAction(.updateEquipment(die, value: 6))
// Set whose turn it isgame.addAction(.setTurn(matching: TableSeatIdentifier(1)))
// Update a score countergame.addAction(.updateCounter(matching: counterID, value: 100))
// Create a state bookmark (for undo/reset)game.addAction(.createBookmark(id: StateBookmarkIdentifier(1)))

Custom Actions

For game-specific logic, conform to CustomAction when the availability matrix permits it. Custom action application and validation must depend only on the action data and the supplied TableState / TableSnapshot so every peer resolves the same result. Register custom action types during setup before dispatching them:

swift
setup.register(action: CollectCoin.self)game.addAction(CollectCoin(coinID: coinID, playerID: playerID))

Register each custom action type before dispatching it. See references/tabletopkit-patterns.md [blocked] for full custom action and custom state examples.

Score Counters

swift
setup.add(counter: ScoreCounter(id: .init(0), value: 0))// Update: game.addAction(.updateCounter(matching: .init(0), value: 42))// Read:   snapshot.counter(matching: .init(0))?.value

State Bookmarks

Save and restore game state for undo/reset:

swift
game.addAction(.createBookmark(id: StateBookmarkIdentifier(1)))game.jumpToBookmark(matching: StateBookmarkIdentifier(1))

Bookmark restoration is asynchronous and network ordered. Wait for stateDidResetToBookmark, then read withCurrentSnapshot, rebuild local UI from that authoritative callback state, and validate the restored invariant. Do not inspect immediately after jumpToBookmark or enqueue another jump on mismatch. Load State Bookmarks and Undo [blocked] for the observer reconciliation pattern.

Interactions

TabletopInteraction.Delegate

Return an interaction delegate from the .tabletopGame modifier to handle player gestures on equipment:

swift
.tabletopGame(game.tabletopGame, parent: game.renderer.root) { value in    if game.tabletopGame.equipment(of: GameDie.self, matching: value.startingEquipmentID) != nil {        return DieInteraction(game: game)    }    return DefaultInteraction(game: game)}

Use interaction.value.gesture for gesture-specific state. Avoid deprecated gesturePhase. For destination control, prefer interaction.setConfiguration(.init(allowedDestinations: ...)) when available rather than deprecated setAllowedDestinations(_:) or value.allowedDestinations.

Handling Gestures and Tossing Dice

Basic toss(equipmentID:as:) is core TabletopKit; the availability matrix owns advanced toss outcomes.

swift
class DieInteraction: TabletopInteraction.Delegate {    let game: Game
    func update(interaction: TabletopInteraction) {        switch interaction.value.phase {        case .started:            interaction.setConfiguration(.init(allowedDestinations: .any))        case .update:            if interaction.value.gesture?.phase == .ended {                interaction.toss(                    equipmentID: interaction.value.controlledEquipmentID,                    as: .cube(height: 0.02, in: .meters)                )            }        case .ended, .cancelled:            break        }    }
    func onTossStart(interaction: TabletopInteraction,                     outcomes: [TabletopInteraction.TossOutcome]) {        for outcome in outcomes {            let face = outcome.tossableRepresentation.face(for: outcome.restingOrientation)            interaction.addAction(.updateEquipment(                die, rawValue: face.rawValue, pose: outcome.pose            ))        }    }}

Tossable Representations

Dice physics shapes: .cube (d6), .tetrahedron (d4), .octahedron (d8), .decahedron (d10), .dodecahedron (d12), .icosahedron (d20), .sphere. All take height:in: (or radius:in: for sphere) and optional restitution:.

Programmatic Interactions

Start interactions from code: game.startInteraction(onEquipmentID: pieceID).

See references/tabletopkit-patterns.md [blocked] for group toss, predetermined outcomes, interaction acceptance/rejection, and destination restriction patterns.

RealityKit Rendering

Conform to EntityRenderDelegate to bridge state to RealityKit. Provide a root entity. TabletopKit automatically positions EntityEquipment entities.

swift
class GameRenderer: EntityRenderDelegate {    let root = Entity()
    func onUpdate(timeInterval: Double, snapshot: TableSnapshot,                  visualState: TableVisualState) {        // Custom visual updates beyond automatic positioning    }}

Connect to SwiftUI with .tabletopGame(_:parent:automaticUpdate:) on a RealityView:

swift
struct GameView: View {    let game: Game
    var body: some View {        RealityView { content in            content.entities.append(game.renderer.root)        }        .tabletopGame(game.tabletopGame, parent: game.renderer.root) { value in            GameInteraction(game: game)        }    }}

Debug outlines: game.tabletopGame.debugDraw(options: [.drawTable, .drawSeats, .drawEquipment])

Group Activities Integration

TabletopKit integrates directly with GroupActivities for FaceTime-based multiplayer. Define a GroupActivity, then call coordinateWithSession(_:). TabletopKit automatically synchronizes all equipment state, seat assignments, actions, and interactions. No manual message passing required.

swift
import GroupActivities
struct BoardGameActivity: GroupActivity {    var metadata: GroupActivityMetadata {        var meta = GroupActivityMetadata()        meta.type = .generic        meta.title = "Board Game"        return meta    }}
@Observableclass GroupActivityManager {    let tabletopGame: TabletopGame    private var sessionTask: Task<Void, Never>?
    init(tabletopGame: TabletopGame) {        self.tabletopGame = tabletopGame        sessionTask = Task { @MainActor in            for await session in BoardGameActivity.sessions() {                tabletopGame.coordinateWithSession(session)            }        }    }
    deinit { tabletopGame.detachNetworkCoordinator() }}

Implement TabletopGame.MultiplayerDelegate for joinAccepted(), playerJoined(_:), didRejectPlayer(_:reason:), and multiplayerSessionFailed(reason:). See references/tabletopkit-patterns.md [blocked] for custom network coordinators and arbiter role management.

Common Mistakes

  • Skipping seat claim. Players must call claimAnySeat() or claimSeat(_:) before interacting with equipment. Without a seat, actions are rejected.
  • Mutating state outside actions. All state changes must go through TabletopAction or CustomAction. Directly modifying equipment properties bypasses synchronization.
  • Missing custom action registration. Register every custom action with setup.register(action:) before use.
  • Not handling action rollback. Actions are optimistically applied and can be rolled back if validation fails on the arbiter. Implement actionWasRolledBack(_:snapshot:) to revert UI state.
  • Ignoring discarded actions when available. Implement actionWasDiscarded(_:) when local action queue pressure matters; it is called for local actions that cannot be enqueued.
  • Using wrong parent ID. Equipment parentID in state must reference a valid equipment ID (typically the table or a container). An invalid parent causes the piece to disappear.
  • Ignoring TossOutcome faces. After a toss, read the face from outcome.tossableRepresentation.face(for: outcome.restingOrientation) rather than generating a random value. The physics simulation determines the result.
  • Testing multiplayer in Simulator. Group Activities do not work in Simulator. Multiplayer requires physical Apple Vision Pro devices on a FaceTime call.

Review Checklist

  • The centralized platform/availability matrix is applied
  • TableSetup created with a Tabletop/EntityTabletop conforming type
  • All equipment conforms to Equipment or EntityEquipment with correct state type
  • Seats added and claimAnySeat() / claimSeat(_:) called at game start
  • All custom actions registered with setup.register(action:)
  • TabletopGame.Observer reconciles confirmed, rolled-back, discarded, and bookmark-reset outcomes with the current snapshot
  • EntityRenderDelegate or RenderDelegate connected
  • .tabletopGame(_:parent:automaticUpdate:) modifier on RealityView
  • GroupActivity defined and coordinateWithSession(_:) called; multiplayer described as Group Activities/SharePlay synchronization
  • Group Activities capability added in Xcode for multiplayer builds
  • Debug visualization (debugDraw) disabled before release
  • Device notes state Simulator is single-player only; multiplayer requires 2+ Apple Vision Pro units on FaceTime

References

来源与署名

来源:dpearson2699/swift-ios-skills位于skills/tabletopkit提交8d90fd1

许可证: 无许可证

内容归原作者所有。SourceWeft 从公开仓库中收录这些内容。

举报或申请下架

更多来自 dpearson2699/swift-ios-skills 的技能

Widgetkit

dpearson2699

指导实现、审查和改进 iOS、iPadOS、watchOS 与 CarPlay 上的 WidgetKit 小组件和控制项。

Software Development1.1K2个月前更新

Weatherkit

dpearson2699

指导 iOS 开发者使用 WeatherService 获取 WeatherKit 预报、警报与署名信息。

Software Development1.1K2个月前更新

Vision Framework

dpearson2699

Implement computer vision features including text recognition (OCR), face detection, barcode scanning, image segmentation, object tracking, and document scanning in iOS apps. Covers both the modern Swift-native Vision API (iOS 18+) and legacy VNRequest patterns, VisionKit DataScannerViewController for live camera scanning, and CoreMLRequest/VNCoreMLRequest for custom model inference. Use when adding OCR, barcode scanning, face detection, or custom Core ML model inference with Vision.

待分类1.1K2个月前更新

Tipkit

dpearson2699

Implement and review Apple TipKit feature-discovery UI for iOS 17+ apps. Use when adding or auditing in-app tips, contextual help, coach marks, Tip, TipView, popoverTip, rules, events, actions, display frequency, testing overrides, reusable tip identifiers, or iOS 18+ TipGroup and CloudKit tip sync; avoid for generic SwiftUI navigation or layout outside tip presentation.

待分类1.1K2个月前更新

Swiftui Webkit

dpearson2699

指导在 iOS 26 及更高版本的 SwiftUI 应用中使用 WebKit for SwiftUI 嵌入和控制网页内容。

Software Development1.1K2个月前更新

Swiftui Uikit Interop

dpearson2699

指导使用可表示视图、托管控制器、协调器和共享可观察状态来桥接 UIKit 与 SwiftUI。

Software Development1.1K2个月前更新