Sample CodevisionOSReviewed 2026-07-21View on Apple Developer

Happy Beam

At a glance

Item Summary
Purpose Leverage a Full Space to create a fun game using ARKit.
App architecture HappyBeamApp composes WindowGroup + ImmersiveSpace; GameModel owns GroupActivities session state and HappyBeam reflects synchronized transitions.
Main patterns SwiftUI scene composition, SwiftUI–RealityKit bridge, Explicit immersive-space lifecycle, Observable state owner, Provider session boundary, Reliable/unreliable SharePlay channels
Project style Code-rich sample with 19 scanned Swift file(s) and 2610 Swift line(s); resources and generated assets are excluded from those counts.

Project structure

Source bundle/
├── HappyBeam/HappyBeamApp.swift  # HappyBeamApp, HeartGestureModelContainer
├── HappyBeam/GameModel.swift  # GameModel, InputKind
├── HappyBeam/Gameplay/HeartGestureModel.swift  # HeartGestureModel, HandsUpdates
├── HappyBeam/Gameplay/Multiplayer.swift  # HeartProjection, BeamMessage, ScoreMessage
├── HappyBeam/Views/HappyBeam.swift  # HappyBeam, GameScreen
├── HappyBeam/HappyBeamSpace.swift  # HappyBeamSpace, BeamType
├── HappyBeam/Gameplay/Clouds.swift  # Cloud, CloudAnimations, CloudSpawnParameters
├── Packages/HappyBeamAssets/Sources/HappyBeamAssets/HappyBeamAssets.swift
└── Packages/HappyBeamAssets/Package.realitycomposerpro/ProjectData/main.json  # authored RealityKit content

Structure observations

  • The runtime boundary is WindowGroup + ImmersiveSpace; the pruned tree lists only files that explain lifecycle, state, or framework integration.
  • App code is split into role-named views, models, managers, providers, components, or systems.
  • Authored .reality or Reality Composer Pro content is a real implementation boundary; Swift loads or drives it rather than reproducing its entity graph.

Overall architecture

Reference code

HappyBeam/HappyBeamApp.swift:13 — the app or executable entry declares the outer scene lifecycle.

struct HappyBeamApp: App {
    // ...
}

The diagram is a responsibility flow, not a claim that every adjacent node directly calls the next. It keeps scene ownership, shared state, RealityKit content, and framework-provider work at separate levels.

Ownership and state

Ownership evidence

HappyBeam/HappyBeamApp.swift:14 — representative stored state or the nearest verified lifecycle anchor.

    @State private var gameModel = GameModel()
Owner Object or state Relationship Mutation authority
HappyBeamApp GameModel as gameModel creates and retains Only the declaring scope writes
HappyBeamApp ImmersionStyle as immersionState owns SwiftUI value state Only the declaring scope writes
HeartGestureModel ARKitSession as session creates and retains The owning type coordinates writes
HappyBeam GroupSession as session owns a SwiftUI-managed reference slot Only the declaring scope writes

Ownership here is deliberately narrow: @Environment and weak references are shared links, initialized @State or stored services are lifecycle ownership, and a RealityView content closure owns additions to its entity graph without making the SwiftUI view a reference-type owner.

Class and protocol design

Type Responsibility Depends on or conforms to
HappyBeamApp (HappyBeam/HappyBeamApp.swift:13) Declares app scenes and top-level dependency lifetime. App
HappyBeam (HappyBeam/Views/HappyBeam.swift:13) Presents UI and forwards gestures or lifecycle events. View
GameModel (HappyBeam/GameModel.swift:14) Owns observable feature state and domain transitions. Concrete framework collaborators
HeartGestureModel (HappyBeam/Gameplay/HeartGestureModel.swift:13) Owns observable feature state and domain transitions. ObservableObject, @unchecked Sendable

The source defines no local substitution protocol in the reviewed boundary. Its protocol use is framework-facing (App, View, RealityKit/ARKit protocols, or platform adapters), so this document does not label the whole app protocol-oriented.

Access control

Symbol Access Verified effect Likely rationale
private(set) static var heartGestureModel = HeartGestureModel() (HappyBeam/HappyBeamApp.swift:41) private(set) The getter keeps its wider visibility, while mutation stays inside the declaring type and its permitted same-file extensions. Inference: Protect a state invariant while allowing observation.
@State private var gameModel = GameModel() (HappyBeam/HappyBeamApp.swift:14) private Use is restricted to the declaration and same-file extensions permitted by Swift. Inference: Hide implementation details and lifecycle-sensitive state.
public let happyBeamAssetsBundle = Bundle.module (Packages/HappyBeamAssets/Sources/HappyBeamAssets/HappyBeamAssets.swift:11) public The declaration is available to importing modules, subject to its containing type’s visibility. Inference: Export the type across the package-module boundary.

No reviewed declaration uses fileprivate, open; unmodified Swift declarations are internal.

Reference code

HappyBeam/HappyBeamApp.swift:41 — representative visibility boundary.

    private(set) static var heartGestureModel = HeartGestureModel()

Logic ownership and placement

Logic Owning type or file Placement rationale
Scene declaration and dependency lifetime HappyBeamApp The App/entry boundary determines window, volume, and immersive-space lifetime.
Presentation, attachments, and gestures HappyBeam SwiftUI view code forwards user intent and RealityView lifecycle events.
Shared feature state and commands GameModel A role-named owner prevents sibling views from duplicating transitions.
Tracking authorization and update streams HappyBeam/Gameplay/HeartGestureModel.swift:14 Provider lifetime and async updates remain outside render-only view code.

Design patterns

Pattern Source evidence Purpose or tradeoff
SwiftUI scene composition HappyBeam/HappyBeamApp.swift:13 Keeps windows, volumes, and immersive-space lifecycle visible at the app boundary.
SwiftUI–RealityKit bridge HappyBeam/HappyBeamSpace.swift:33 Builds and updates a RealityKit entity graph from SwiftUI lifecycle closures.
Explicit immersive-space lifecycle HappyBeam/HappyBeamApp.swift:31 Makes immersive presentation a scene transition rather than hidden global state.
Observable state owner HappyBeam/GameModel.swift:14 Shares feature state across multiple views without moving framework resources into view values.
Provider session boundary HappyBeam/Gameplay/HeartGestureModel.swift:14 Owns provider lifetime separately from the SwiftUI view tree.
Reliable/unreliable SharePlay channels HappyBeam/Gameplay/Multiplayer.swift:52 Separates transient beam projections from reliable score and readiness messages within one group session.

Naming conventions

  • Role suffixes are evidence, not decoration: App: HappyBeamApp; Model: GameModel, HeartGestureModel.
  • Protocols: no app-defined protocol in the reviewed source.
  • Commands use verb-led methods: clear, reset, generateCloudMovementAnimations, start, publishHandTrackingUpdates, monitorSessionEvents, computeTransformOfUserPerformedHeartGesture, startSession.
  • Files generally match their primary type; Views, Models, Managers, Providers, Components, Systems, and Packages folders describe architectural roles where present.

Architecture takeaways

  • Treat HappyBeamApp as the owner of scene declarations, not as the owner of every RealityKit entity created later.
  • Keep view-local interaction in SwiftUI, but move provider sessions, playback resources, shared game state, or transport state into a stable owner when their lifetime exceeds one render pass.
  • Run ARKit providers for the scene lifetime and consume their asynchronous updates in cancellable tasks; map anchors to entities at the boundary.
  • Model immersive-space open, transition, and close states explicitly so windows and immersive content cannot drift apart.

Source map

Source file Relevant symbols
HappyBeam/HappyBeamApp.swift:13 HappyBeamApp, HeartGestureModelContainer
HappyBeam/GameModel.swift:14 GameModel, InputKind
HappyBeam/Gameplay/HeartGestureModel.swift:13 HeartGestureModel, HandsUpdates
HappyBeam/Gameplay/Multiplayer.swift:20 HeartProjection, BeamMessage, ScoreMessage, ReadyStateMessage, ProjectionSessionInfo
HappyBeam/Views/HappyBeam.swift:13 HappyBeam, GameScreen
HappyBeam/HappyBeamSpace.swift:17 HappyBeamSpace, BeamType
HappyBeam/Gameplay/Clouds.swift:13 Cloud, CloudAnimations, CloudSpawnParameters
Packages/HappyBeamAssets/Sources/HappyBeamAssets/HappyBeamAssets.swift:1 Feature implementation