Sample CodeiOS, iPadOS, Mac Catalyst, macOSReviewed 2026-07-21View on Apple Developer

Generate dynamic game content with guided generation and tools

At a glance

Item Summary
Purpose Make gameplay more lively with AI generated dialog and encounters personalized to the player.
App architecture A Swift sample with the source-visible chain FoundationModelsCoffeeGameAppMainMenuViewDialogEngineFoundationModels APIs.
Main patterns Protocol-oriented abstraction, Actor-isolated state
Project style 21 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries.

Project structure

Source bundle/
└── FoundationModelsCoffeeGame/
    └── FoundationModelsCoffeeGame/
        ├── FoundationModelsCoffeeGameApp.swift
        ├── GenerateDialog/
        │   ├── Characters.swift
        │   ├── DialogBoxView.swift
        │   └── DialogEngine.swift
        ├── GameView.swift
        ├── GenerateEncounters/
        │   ├── EncounterEngine.swift
        │   ├── CustomerProfileView.swift
        │   └── EncounterView.swift
        ├── MainMenu/
        │   ├── CloudsBackgroundView.swift
        │   └── MainMenuView.swift
        └── OrderCoffee/
            ├── CoffeeDrink.swift
            └── CoffeeOrderView.swift

Structure observations

  • Architecturally prominent files are ranked from entry points and role-named declarations; resource-only paths are omitted.
  • Primary languages: Swift.
  • The verified tree contains 4 project/configuration file(s) and 40 source declaration(s).

Overall architecture

Reference code

FoundationModelsCoffeeGame/FoundationModelsCoffeeGame/FoundationModelsCoffeeGameApp.swift:10 — architecture anchor

@main
struct FoundationModelsCoffeeGameApp: App {
    // ...
}

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 Foundation Models.

Ownership and state

Ownership evidence

FoundationModelsCoffeeGame/FoundationModelsCoffeeGame/GenerateDialog/Characters.swift:29 — stored dependency or nearest verified ownership anchor

struct Barista: Character {
    let id = UUID()
    // ...
}
Owner Object or state Relationship Mutation authority
Barista UUID (id) owns value state Initialized by the owner; the binding is immutable
CustomerLisa UUID (id) owns value state Initialized by the owner; the binding is immutable
GeneratedCustomer UUID (id) owns value state Initialized by the owner; the binding is immutable
GeneratedCustomer String (displayName) owns value state Initialized by the owner; the binding is immutable

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.

Class and protocol design

FoundationModelsCoffeeGame/FoundationModelsCoffeeGame/GenerateDialog/Characters.swift:11 — representative type boundary

protocol Character: Identifiable {
    // ...
}
Type Responsibility Depends on or conforms to
FoundationModelsCoffeeGameApp Application entry and top-level composition App
GameView User-interface presentation and input forwarding View
EncounterEngine Owns processing or simulation work Concrete collaborators/imported frameworks
CloudsBackgroundView User-interface presentation and input forwarding View
MainMenuView User-interface presentation and input forwarding View
DialogBoxView User-interface presentation and input forwarding View
DialogEngine Owns processing or simulation work Concrete collaborators/imported frameworks
CustomerProfileView User-interface presentation and input forwarding View
EncounterView User-interface presentation and input forwarding View
CoffeeOrderView User-interface presentation and input forwarding View

The source explicitly defines local protocol relationships: BaristaCharacter, CustomerLisaCharacter, GeneratedCustomerCharacter.

Access control

Symbol Access Verified effect Likely rationale
isFocused (FoundationModelsCoffeeGame/FoundationModelsCoffeeGame/GenerateDialog/DialogBoxView.swift:25) 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.
session (FoundationModelsCoffeeGame/FoundationModelsCoffeeGame/GenerateDialog/DialogEngine.swift:17) 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.
currentTask (FoundationModelsCoffeeGame/FoundationModelsCoffeeGame/GenerateDialog/DialogEngine.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.
conversations (FoundationModelsCoffeeGame/FoundationModelsCoffeeGame/GenerateDialog/DialogEngine.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.

Reference code

FoundationModelsCoffeeGame/FoundationModelsCoffeeGame/GenerateDialog/DialogBoxView.swift:25 — representative boundary

struct DialogBoxView: View {
    // ...
    @FocusState private var isFocused: Bool
    // ...
}

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 FoundationModelsCoffeeGameApp The source’s App suffix makes this role explicit.
Owns processing or simulation work DialogEngine, EncounterEngine The source’s Engine suffix makes this role explicit.
Generates feature data or resources RandomCustomerGenerator The source’s Generator suffix makes this role explicit.
Scene lifecycle or scene-level composition CoffeeShopScene The source’s Scene suffix makes this role explicit.
User-interface presentation and input forwarding CloudsBackgroundView, CoffeeOrderView, CustomerProfileView, DialogBoxView The source’s View suffix makes this role explicit.

Design patterns

Pattern Source evidence Purpose or tradeoff
Protocol-oriented abstraction FoundationModelsCoffeeGame/FoundationModelsCoffeeGame/GenerateDialog/Characters.swift:28 A local protocol and concrete conformance create an explicit capability boundary.
Actor-isolated state FoundationModelsCoffeeGame/FoundationModelsCoffeeGame/GenerateDialog/DialogEngine.swift:11 Actor annotations make the concurrency ownership boundary explicit.

Naming conventions

  • Types: App: FoundationModelsCoffeeGameApp; Engine: DialogEngine, EncounterEngine; Generator: RandomCustomerGenerator; Scene: CoffeeShopScene; View: CloudsBackgroundView, CoffeeOrderView, CustomerProfileView, DialogBoxView, EncounterView.
  • Protocols: Character.
  • Methods: showDialog, generateDreamCustomer, triggerEncounter, body, generateNPC, judgeDrink, createClouds, startCloudAnimation.
  • Files: FoundationModelsCoffeeGame/FoundationModelsCoffeeGame/FoundationModelsCoffeeGameApp.swift, FoundationModelsCoffeeGame/FoundationModelsCoffeeGame/GameView.swift, FoundationModelsCoffeeGame/FoundationModelsCoffeeGame/GenerateEncounters/EncounterEngine.swift, FoundationModelsCoffeeGame/FoundationModelsCoffeeGame/MainMenu/CloudsBackgroundView.swift, FoundationModelsCoffeeGame/FoundationModelsCoffeeGame/MainMenu/MainMenuView.swift, FoundationModelsCoffeeGame/FoundationModelsCoffeeGame/OrderCoffee/CoffeeDrink.swift.

Architecture takeaways

  • FoundationModelsCoffeeGameApp is the main source-visible entry or composition anchor for this sample.
  • Framework work reaches SwiftUI, FoundationModels, SpriteKit, Contacts 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
FoundationModelsCoffeeGame/FoundationModelsCoffeeGame/FoundationModelsCoffeeGameApp.swift FoundationModelsCoffeeGameApp
FoundationModelsCoffeeGame/FoundationModelsCoffeeGame/GenerateDialog/Characters.swift Character, Barista, CustomerLisa, GeneratedCustomer, Attribute, Encounter
FoundationModelsCoffeeGame/FoundationModelsCoffeeGame/GameView.swift GameView, GameBoxStyle
FoundationModelsCoffeeGame/FoundationModelsCoffeeGame/GenerateEncounters/EncounterEngine.swift NPC, EncounterEngine
FoundationModelsCoffeeGame/FoundationModelsCoffeeGame/MainMenu/CloudsBackgroundView.swift Cloud, CloudsBackgroundView
FoundationModelsCoffeeGame/FoundationModelsCoffeeGame/MainMenu/MainMenuView.swift MainMenuView, ViewState
FoundationModelsCoffeeGame/FoundationModelsCoffeeGame/OrderCoffee/CoffeeDrink.swift CoffeeDrink, Temp, DrinkType, MilkType, Flavor
FoundationModelsCoffeeGame/FoundationModelsCoffeeGame/GenerateDialog/DialogBoxView.swift DialogBoxView
FoundationModelsCoffeeGame/FoundationModelsCoffeeGame/GenerateDialog/DialogEngine.swift DialogEngine
FoundationModelsCoffeeGame/FoundationModelsCoffeeGame/GenerateEncounters/CustomerProfileView.swift CustomerProfileView