Chaparral Village: Building an immersive visionOS adventure game
At a glance
| Item | Summary |
|---|---|
| Purpose | Create an adventure game using SwiftUI, RealityKit, and Reality Composer Pro 3. |
| App architecture | A Shell, Swift sample with the source-visible chain ChaparralVillageApp → ContentView → ARManager → ContainerHandler → RealityKit / Spatial APIs. |
| Main patterns | View-controller organization, Protocol-oriented abstraction, Binding-based state propagation |
| Project style | 94 scanned source file(s) across Shell, Swift, organized around ranked entry, type, and file boundaries. |
| Execution model | Source-visible boundaries: @MainActor, Task, await suspension point, Sendable or @Sendable, Task closure isolated to MainActor; none alone proves a background thread. |
| State/event model | Source-visible mechanisms: SwiftUI state property wrapper, AnyCancellable, @Observable. |
| Key frameworks/packages | RealityKit, Foundation, os, Combine, Spatial, Remote package https://github.com/apple/reality-composer-pro-plugin, Remote package https://github.com/apple/realitykitscripting; these are source dependencies, not architecture labels. |
Project structure
Source bundle/
└── ChaparralVillageBuildingAnImmersiveVisionOSAdventureGame/
├── ChaparralVillage/
│ └── ChaparralVillageApp.swift
└── Packages/
└── RCPCustomComponents/
└── Sources/
└── RCPCustomComponents/
├── Managers/
│ ├── LevelManager.swift
│ └── InputManager.swift
├── ZoneManagement/
│ └── ZoneTracker.swift
├── Navigation/
│ └── PathfindPlayer.swift
├── Alchemy/
│ ├── Cauldron.swift
│ ├── Curtain.swift
│ ├── Effect.swift
│ ├── PlantGeneration.swift
│ └── Potion.swift
├── Cutscene/
│ └── Dialog.swift
└── EcsAbstractions/
└── EntityComponentQuery.swift
Structure observations
- Architecturally prominent files are ranked from entry points and role-named declarations; resource-only paths are omitted.
- Primary languages: Shell, Swift.
- The verified tree contains 5 project/configuration file(s) and 277 source declaration(s).
Overall architecture
flowchart LR
N1["ChaparralVillageApp"]
N2["ContentView"]
N3["ARManager"]
N4["ContainerHandler"]
N5["RealityKit / Spatial APIs"]
N1 --> N2
N2 --> N3
N3 --> N4
N4 --> N5
Reference code
ChaparralVillageBuildingAnImmersiveVisionOSAdventureGame/ChaparralVillage/ChaparralVillageApp.swift:15 — architecture anchor
@main
struct ChaparralVillageApp: App {
let saveManager = SaveManager()
@State private var appState = AppState()
@Environment(\.physicalMetrics) var physicalMetrics
init() {
RCPCustomComponentsPlugin
.create()
.setup(context: PluginHandler())
}
var body: some SwiftUI.Scene {
WindowGroup(id: AppState.mainMenu) {
ContentView(immersive: false, saveManager: saveManager, appState: appState)
.frame(width: physicalMetrics.convert(1.5, from: .meters), height: physicalMetrics.convert(1.0, from: .meters))
.frame(depth: physicalMetrics.convert(1.0, from: .meters))
}
.windowStyle(.volumetric)
.windowResizability(.contentSize)
ImmersiveSpace(id: AppState.immersiveSpace) {
ContentView(immersive: true, saveManager: saveManager, appState: appState)
}
// Coexist with the person's currently active system Environment so they
// remain in their chosen surroundings while playing instead of
// dropping back to passthrough.
.immersiveEnvironmentBehavior(.coexist)
}
}Interpretation
The arrows summarize the source-visible entry, role-named types or folders, and framework direction; when nodes come from structural folders, the sequence is a high-level interpretation rather than proof that every adjacent node calls the next. Ownership is claimed only where the next section cites a stored property or assignment. The diagram is intentionally limited to the dominant path into visionOS.
Ownership and state
classDiagram
ChaparralVillageApp *-- SaveManager : saveManager
ChaparralVillageApp *-- AppState : appState
Cauldron *-- SIMD3 : waterNormal
Cauldron *-- Float : waterLevel
Ownership evidence
ChaparralVillageBuildingAnImmersiveVisionOSAdventureGame/ChaparralVillage/ChaparralVillageApp.swift:17 — stored dependency or nearest verified ownership anchor
@main
struct ChaparralVillageApp: App {
let saveManager = SaveManager()
// ...
}| Owner | Object or state | Relationship | Mutation authority |
|---|---|---|---|
ChaparralVillageApp |
SaveManager (saveManager) |
creates and retains | Initialized by the owner; the binding is immutable |
ChaparralVillageApp |
AppState (appState) |
owns wrapper-managed state | Owning lexical scope |
Cauldron |
SIMD3 (waterNormal) |
owns value state | App/module collaborators |
Cauldron |
Float (waterLevel) |
owns value state | App/module collaborators |
Composition arrows indicate a source-visible construction expression or locally owned value state; aggregation means the owner stores or receives a dependency without proving exclusive lifetime ownership.
Concurrency, scheduling, and thread safety
Evidence limit: actor isolation, async/await, or Task creation does not by itself prove background-thread execution; Sendable conformance alone does not prove thread-safe mutation.
| Concern | Source mechanism | Verified placement or handoff | Evidence |
|---|---|---|---|
| Main isolation | @MainActor |
The cited annotation marks its attached declaration or closure as main-actor isolated. | ChaparralVillageBuildingAnImmersiveVisionOSAdventureGame/ChaparralVillage/ChaparralVillageApp.swift:48 |
| Task creation | Task |
The source creates an unstructured task; surrounding context determines inherited actor isolation. | ChaparralVillageBuildingAnImmersiveVisionOSAdventureGame/ChaparralVillage/ContentView.swift:41 |
| Suspension boundary | await suspension point |
The source declares or crosses an asynchronous boundary; it does not by itself establish background execution. | ChaparralVillageBuildingAnImmersiveVisionOSAdventureGame/ChaparralVillage/ContentView.swift:43 |
| Transfer contract | Sendable or @Sendable |
The source declares a sendability boundary; this alone does not synchronize mutable state. | ChaparralVillageBuildingAnImmersiveVisionOSAdventureGame/Packages/RCPCustomComponents/Sources/RCPCustomComponents/Alchemy/Cauldron.swift:44 |
| Main isolation | Task closure isolated to MainActor |
The cited operation explicitly enters a main-actor-isolated region. | ChaparralVillageBuildingAnImmersiveVisionOSAdventureGame/Packages/RCPCustomComponents/Sources/RCPCustomComponents/Cutscene/CutsceneManager.swift:40 |
@MainActor/MainActor.run, DispatchQueue.main, and RunLoop.main are reported as distinct isolation, queue, and event-loop mechanisms. A plain Task is kept separate from Task.detached; neither is labeled as a background thread.
Reference code
ChaparralVillageBuildingAnImmersiveVisionOSAdventureGame/ChaparralVillage/ChaparralVillageApp.swift:48 — representative execution boundary
private struct PluginHandler: @MainActor RealityComposerProContext {
// ...
_ component: ComponentType.Type
// ...
}State propagation, frameworks, and dependencies
Evidence limit: an import proves a source-level compilation dependency at the cited line; it does not prove runtime use, architectural adoption, or whether a Swift package is a direct application dependency.
| Category | Mechanism or module | Verified role | Evidence |
|---|---|---|---|
| State propagation | SwiftUI state property wrapper |
A SwiftUI property wrapper supplies or observes UI state. | ChaparralVillageBuildingAnImmersiveVisionOSAdventureGame/ChaparralVillage/ChaparralVillageApp.swift:18 |
| State propagation | AnyCancellable |
A cancellable value records subscription lifetime management. | ChaparralVillageBuildingAnImmersiveVisionOSAdventureGame/Packages/RCPCustomComponents/Sources/RCPCustomComponents/Alchemy/Curtain.swift:54 |
| State propagation | @Observable |
Observation macro publishes source-visible changes. | ChaparralVillageBuildingAnImmersiveVisionOSAdventureGame/Packages/RCPCustomComponents/Sources/RCPCustomComponents/Cutscene/Dialog.swift:59 |
| Package manifest | Remote package https://github.com/apple/reality-composer-pro-plugin |
The manifest declares a remote URL or registry dependency; direct target use and transitive position are not inferred. | ChaparralVillageBuildingAnImmersiveVisionOSAdventureGame/Packages/RCPCustomComponents/Package.swift:28 |
| Package manifest | Remote package https://github.com/apple/realitykitscripting |
The manifest declares a remote URL or registry dependency; direct target use and transitive position are not inferred. | ChaparralVillageBuildingAnImmersiveVisionOSAdventureGame/Packages/RCPCustomComponents/Package.swift:29 |
| Source import | RealityKit |
The cited file imports this module; runtime use and architectural role are not inferred. | ChaparralVillageBuildingAnImmersiveVisionOSAdventureGame/ChaparralVillage/ChaparralVillageApp.swift:10 |
| Source import | Foundation |
The cited file imports this module; runtime use and architectural role are not inferred. | ChaparralVillageBuildingAnImmersiveVisionOSAdventureGame/Packages/RCPCustomComponents/Sources/RCPCustomComponents/Alchemy/BuoyantObject.swift:8 |
| Source import | os |
The cited file imports this module; runtime use and architectural role are not inferred. | ChaparralVillageBuildingAnImmersiveVisionOSAdventureGame/ChaparralVillage/ContentView.swift:9 |
receive(on:) describes downstream delivery scheduling, while subscribe(on:) describes upstream subscription/request/cancel scheduling. An import Combine alone establishes neither behavior nor a Store, reducer, Redux, or other application architecture.
Class and protocol design
ChaparralVillageBuildingAnImmersiveVisionOSAdventureGame/Packages/RCPCustomComponents/Sources/RCPCustomComponents/ZoneManagement/ZoneTracker.swift:152 — representative type boundary
@MainActor
public protocol ZoneChangeHandler {
static func on(entered: Zone, entity: Entity)
static func on(overlapsChangedFor zone: Zone, entity: Entity)
static func on(exited: Zone, entity: Entity)
}| Type | Responsibility | Depends on or conforms to |
|---|---|---|
ChaparralVillageApp |
Application entry and top-level composition | App |
ZoneChangeHandler |
Defines a capability or collaboration contract | Concrete collaborators/imported frameworks |
TapHandler |
Defines a capability or collaboration contract | Identifiable |
DragHandler |
Defines a capability or collaboration contract | Identifiable |
PluginHandler |
Handles callbacks or feature events | @MainActor RealityComposerProContext |
LevelManager |
Long-lived feature or framework coordination | SceneBound |
ZoneTracker |
Owns tracking state and updates | Component, Codable |
ZoneTrackerSystem |
Runs entity-component-system update logic | System |
InputManager |
Long-lived feature or framework coordination | SceneBound, Inspectable |
PlayerPathfindMovementSystem |
Runs entity-component-system update logic | System |
The source explicitly defines local protocol relationships: BuoyancySim → PhysicsUpdateBehavior, CutsceneManager → SceneBound, ZoneCutsceneHandler → ZoneChangeHandler, DebugDraw → SceneBound, InputProxyManager → SceneBound, ARManager → SceneBound.
Access control
| Symbol | Access | Verified effect | Likely rationale |
|---|---|---|---|
appState (ChaparralVillageBuildingAnImmersiveVisionOSAdventureGame/ChaparralVillage/ChaparralVillageApp.swift:18) |
private |
Use is restricted to the lexical declaration and same-file extensions allowed by Swift. | Inference: keep state mutation or dependency lifetime inside the owning implementation. |
openImmersiveSpace (ChaparralVillageBuildingAnImmersiveVisionOSAdventureGame/ChaparralVillage/ContentView.swift:19) |
private |
Use is restricted to the lexical declaration and same-file extensions allowed by Swift. | Inference: keep state mutation or dependency lifetime inside the owning implementation. |
dismissWindow (ChaparralVillageBuildingAnImmersiveVisionOSAdventureGame/ChaparralVillage/ContentView.swift:20) |
private |
Use is restricted to the lexical declaration and same-file extensions allowed by Swift. | Inference: keep state mutation or dependency lifetime inside the owning implementation. |
waterDensity (ChaparralVillageBuildingAnImmersiveVisionOSAdventureGame/Packages/RCPCustomComponents/Sources/RCPCustomComponents/Alchemy/BuoyantObject.swift:12) |
private |
Use is restricted to the lexical declaration and same-file extensions allowed by Swift. | Inference: keep state mutation or dependency lifetime inside the owning implementation. |
Reference code
ChaparralVillageBuildingAnImmersiveVisionOSAdventureGame/ChaparralVillage/ChaparralVillageApp.swift:18 — representative boundary
@main
struct ChaparralVillageApp: App {
// ...
@State private var appState = AppState()
// ...
}Swift declarations without a modifier are internal; explicit private, fileprivate, private(set), public, or open entries above are interpreted by language semantics. Objective-C/C samples instead rely on header and implementation boundaries, which are not equivalent to Swift lexical privacy.
Logic ownership and placement
| Logic | Owning type or file | Placement rationale |
|---|---|---|
| Application entry and top-level composition | ChaparralVillageApp |
The source’s App suffix makes this role explicit. |
| Stores entity-component data or behavior | CapsuleTextComponent, CurtainClothBodyComponent, CurtainPinComponent, CurtainSimulationComponent |
The source’s Component suffix makes this role explicit. |
| View lifecycle, callbacks, and feature coordination | DioramaViewController |
The source’s Controller suffix makes this role explicit. |
| Generates feature data or resources | PlantGenerator |
The source’s Generator suffix makes this role explicit. |
| Handles callbacks or feature events | ContainerHandler, DragHandler, DraggableObjectHandler, LadderTapHandler |
The source’s Handler suffix makes this role explicit. |
| Long-lived feature or framework coordination | ARManager, AudioManager, CutsceneManager, InputManager |
The source’s Manager suffix makes this role explicit. |
| Feature data or observable state | DialogModel, LadderModel |
The source’s Model suffix makes this role explicit. |
Design patterns
| Pattern | Source evidence | Purpose or tradeoff |
|---|---|---|
| View-controller organization | ChaparralVillageBuildingAnImmersiveVisionOSAdventureGame/Packages/RCPCustomComponents/Sources/RCPCustomComponents/LevelMarkup/Diorama.swift:12 |
A controller is the verified coordination boundary; this is MVC-style only where a separate model is present. |
| Protocol-oriented abstraction | ChaparralVillageBuildingAnImmersiveVisionOSAdventureGame/Packages/RCPCustomComponents/Sources/RCPCustomComponents/Alchemy/BuoyantObject.swift:50 |
A local protocol and concrete conformance create an explicit capability boundary. |
| Binding-based state propagation | ChaparralVillageBuildingAnImmersiveVisionOSAdventureGame/ChaparralVillage/ContentView.swift:84 |
A binding exposes controlled read/write access while the upstream owner remains authoritative. |
Main application flow
sequenceDiagram
participant ARManager
participant Task
participant newSession
participant handProvider
ARManager->>Task: Start asynchronous work
ARManager->>ARManager: run()
ARManager->>Task: Start asynchronous work
ARManager->>newSession: requestAuthorization()
ARManager->>newSession: run()
ARManager->>Task: Start asynchronous work
handProvider-->>ARManager: hand() stream
Reference code
ChaparralVillageBuildingAnImmersiveVisionOSAdventureGame/Packages/RCPCustomComponents/Sources/RCPCustomComponents/Managers/ARManager.swift:81 — register()
public func register(scene: Scene, on entity: Entity) {
#if os(visionOS)
if hasTriedToRun {
return
}
hasTriedToRun = true
Task {
await self.spatialTrackingSession.run(.init(tracking: [.plane]))
}
Task {
let newSession = ARKitSession()
let auth = await newSession.requestAuthorization(for: [
.worldSensing,
.handTracking
])
hasRequestedAuthorization = true
// ...
do {
try await newSession.run(providers)
self.planesSupported = canSupportPlanes
Task {
for await hand in handProvider.anchorUpdates {
// ...
}
}
} catch {
Self.logger.error("Failed to start ar session: \(error)")
}
self.session = newSession
}
#endif
}Naming conventions
- Types: App: ChaparralVillageApp; Component: CapsuleTextComponent, CurtainClothBodyComponent, CurtainPinComponent, CurtainSimulationComponent, FadingComponent; Controller: DioramaViewController; Generator: PlantGenerator; Handler: ContainerHandler, DragHandler, DraggableObjectHandler, LadderTapHandler, PathfindPlayerTapHandler; Manager: ARManager, AudioManager, CutsceneManager, InputManager, InputProxyManager; Model: DialogModel, LadderModel; Player: Player.
- Protocols:
ZoneChangeHandler,TapHandler,DragHandler,CustomRestore,PhysicsUpdateBehavior,Affordance,SceneBound. - Methods:
registerComponent,registerAction,registerSystem,store,load,fadeInRestoredEntities,fadeOutExistingLevel,unload. - Files:
ChaparralVillageBuildingAnImmersiveVisionOSAdventureGame/ChaparralVillage/ChaparralVillageApp.swift,ChaparralVillageBuildingAnImmersiveVisionOSAdventureGame/Packages/RCPCustomComponents/Sources/RCPCustomComponents/Managers/LevelManager.swift,ChaparralVillageBuildingAnImmersiveVisionOSAdventureGame/Packages/RCPCustomComponents/Sources/RCPCustomComponents/ZoneManagement/ZoneTracker.swift,ChaparralVillageBuildingAnImmersiveVisionOSAdventureGame/Packages/RCPCustomComponents/Sources/RCPCustomComponents/Managers/InputManager.swift,ChaparralVillageBuildingAnImmersiveVisionOSAdventureGame/Packages/RCPCustomComponents/Sources/RCPCustomComponents/Alchemy/Cauldron.swift,ChaparralVillageBuildingAnImmersiveVisionOSAdventureGame/Packages/RCPCustomComponents/Sources/RCPCustomComponents/Alchemy/Effect.swift.
Architecture takeaways
ChaparralVillageAppis the main source-visible entry or composition anchor for this sample.- Framework work reaches RealityKit, Spatial, SwiftUI, RealityKitScripting through a deliberately small high-level chain; the detailed API graph remains inside the cited implementation files.
- Stored-property evidence identifies lifecycle collaboration; it does not by itself prove exclusive object ownership.
- Access-control conclusions separate verified language visibility from the likely design rationale.
- Local protocol relationships provide an explicit substitution boundary.
Source map
| Source file | Relevant symbols |
|---|---|
ChaparralVillageBuildingAnImmersiveVisionOSAdventureGame/ChaparralVillage/ChaparralVillageApp.swift |
Cited implementation, @MainActor, SwiftUI state property wrapper, RealityKit, ChaparralVillageApp, PluginHandler |
ChaparralVillageBuildingAnImmersiveVisionOSAdventureGame/Packages/RCPCustomComponents/Sources/RCPCustomComponents/ZoneManagement/ZoneTracker.swift |
ZoneChangeHandler, ZoneTracker, EventType, CodingKeys, ZoneTrackerSystem |
ChaparralVillageBuildingAnImmersiveVisionOSAdventureGame/ChaparralVillage/ContentView.swift |
Cited implementation, Task, await suspension point, os, ContentView, ChaparralVillageRealityView |
ChaparralVillageBuildingAnImmersiveVisionOSAdventureGame/Packages/RCPCustomComponents/Sources/RCPCustomComponents/Alchemy/BuoyantObject.swift |
Cited implementation, Foundation, BuoyantObject, CodingKeys, BuoyantObjectSystem, BuoyancySim |
ChaparralVillageBuildingAnImmersiveVisionOSAdventureGame/Packages/RCPCustomComponents/Sources/RCPCustomComponents/LevelMarkup/Diorama.swift |
DioramaViewController, ZoneDisplay, CodingKeys, DioramaWorldRoot, DioramaSpaceTransition, DioramaFloorCollision, DioramaControllerSystem, TargetTransformInfo |
ChaparralVillageBuildingAnImmersiveVisionOSAdventureGame/Packages/RCPCustomComponents/Sources/RCPCustomComponents/Alchemy/Cauldron.swift |
Sendable or @Sendable, Cauldron, CodingKeys, Surface, StirringSystem, CauldronWaterDisplay, CauldronWaterDisplaySystem |
ChaparralVillageBuildingAnImmersiveVisionOSAdventureGame/Packages/RCPCustomComponents/Sources/RCPCustomComponents/Cutscene/CutsceneManager.swift |
Task closure isolated to MainActor, CutsceneManager |
ChaparralVillageBuildingAnImmersiveVisionOSAdventureGame/Packages/RCPCustomComponents/Sources/RCPCustomComponents/Alchemy/Curtain.swift |
AnyCancellable, CurtainPinComponent, CurtainClothBodyComponent, CurtainClothBodySystem, CurtainSimulationComponent, CurtainMaterial, CurtainSimulationSystem |
ChaparralVillageBuildingAnImmersiveVisionOSAdventureGame/Packages/RCPCustomComponents/Sources/RCPCustomComponents/Cutscene/Dialog.swift |
@Observable, Dialog, CodingKeys, DialogSequence, DialogModel, Error, DialogView |
ChaparralVillageBuildingAnImmersiveVisionOSAdventureGame/Packages/RCPCustomComponents/Package.swift |
Cited implementation |
ChaparralVillageBuildingAnImmersiveVisionOSAdventureGame/Packages/RCPCustomComponents/Sources/RCPCustomComponents/Managers/LevelManager.swift |
LevelManager, LoadSlotData, LevelLoadedEvent, Level, CodingKeys, LevelLoadSlot |
ChaparralVillageBuildingAnImmersiveVisionOSAdventureGame/Packages/RCPCustomComponents/Sources/RCPCustomComponents/Managers/InputManager.swift |
DragInput, InputManager, EventStateStatus, EventState |
ChaparralVillageBuildingAnImmersiveVisionOSAdventureGame/Packages/RCPCustomComponents/Sources/RCPCustomComponents/Navigation/PathfindPlayer.swift |
Constants, PathfindMovement, Connection, PlayerPathfindMovementSystem |
ChaparralVillageBuildingAnImmersiveVisionOSAdventureGame/Packages/RCPCustomComponents/Sources/RCPCustomComponents/Alchemy/Effect.swift |
Effect, Kind, Resizable, Effects, EffectCloud, CodingKeys, EffectCloudSystem |
ChaparralVillageBuildingAnImmersiveVisionOSAdventureGame/Packages/RCPCustomComponents/Sources/RCPCustomComponents/Alchemy/PlantGeneration.swift |
PlantGenerator, PlantPart, Kind, Error, PlantTemplates, PlantSequenceConfig, GrowthData |
ChaparralVillageBuildingAnImmersiveVisionOSAdventureGame/Packages/RCPCustomComponents/Sources/RCPCustomComponents/Alchemy/Potion.swift |
Potion, CodingKeys, SubmergedTracker, PotionSystem, PotionBreaker, PotionDisplay, PotionDisplaySystem, PotionVariant |
ChaparralVillageBuildingAnImmersiveVisionOSAdventureGame/Packages/RCPCustomComponents/Sources/RCPCustomComponents/Managers/ARManager.swift |
register |