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 FoundationModelsCoffeeGameApp → MainMenuView → DialogEngine → FoundationModels APIs. |
| Main patterns | Protocol-oriented abstraction |
| Project style | 21 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries. |
| Execution model | Source-visible boundaries: Task, await suspension point, @MainActor, Task closure isolated to MainActor; none alone proves a background thread. |
| State/event model | Source-visible mechanisms: SwiftUI state property wrapper, @Observable. |
| Key frameworks/packages | SwiftUI, FoundationModels, SpriteKit, Foundation, Contacts; these are source dependencies, not architecture labels. |
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
flowchart LR
N1["FoundationModelsCoffeeGameApp"]
N2["MainMenuView"]
N3["DialogEngine"]
N4["FoundationModels APIs"]
N1 --> N2
N2 --> N3
N3 --> N4
Reference code
FoundationModelsCoffeeGame/FoundationModelsCoffeeGame/FoundationModelsCoffeeGameApp.swift:10 — architecture anchor
@main
struct FoundationModelsCoffeeGameApp: App {
var body: some Scene {
WindowGroup {
MainMenuView()
#if os(iOS)
.statusBar(hidden: true)
#endif
}
}
}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
classDiagram
Barista *-- UUID : id
CustomerLisa *-- UUID : id
GeneratedCustomer *-- UUID : id
GeneratedCustomer *-- String : displayName
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.
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 |
|---|---|---|---|
| Task creation | Task |
The source creates an unstructured task; surrounding context determines inherited actor isolation. | FoundationModelsCoffeeGame/FoundationModelsCoffeeGame/GameView.swift:60 |
| Suspension boundary | await suspension point |
The source declares or crosses an asynchronous boundary; it does not by itself establish background execution. | FoundationModelsCoffeeGame/FoundationModelsCoffeeGame/GameView.swift:63 |
| Main isolation | @MainActor |
The cited annotation marks its attached declaration or closure as main-actor isolated. | FoundationModelsCoffeeGame/FoundationModelsCoffeeGame/GenerateDialog/DialogEngine.swift:11 |
| Main isolation | Task closure isolated to MainActor |
The cited operation explicitly enters a main-actor-isolated region. | FoundationModelsCoffeeGame/FoundationModelsCoffeeGame/MainMenu/CloudsBackgroundView.swift:84 |
@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
FoundationModelsCoffeeGame/FoundationModelsCoffeeGame/GameView.swift:60 — representative execution boundary
Task {
// ...
let npc = try await encounterEngine.generateNPC()
// ...
}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. | FoundationModelsCoffeeGame/FoundationModelsCoffeeGame/GameView.swift:12 |
| State propagation | @Observable |
Observation macro publishes source-visible changes. | FoundationModelsCoffeeGame/FoundationModelsCoffeeGame/GenerateDialog/DialogEngine.swift:12 |
| Source import | SwiftUI |
The cited file imports this module; runtime use and architectural role are not inferred. | FoundationModelsCoffeeGame/FoundationModelsCoffeeGame/FoundationModelsCoffeeGameApp.swift:8 |
| Source import | FoundationModels |
The cited file imports this module; runtime use and architectural role are not inferred. | FoundationModelsCoffeeGame/FoundationModelsCoffeeGame/GenerateDialog/Characters.swift:9 |
| Source import | SpriteKit |
The cited file imports this module; runtime use and architectural role are not inferred. | FoundationModelsCoffeeGame/FoundationModelsCoffeeGame/GameView.swift:8 |
| Source import | Foundation |
The cited file imports this module; runtime use and architectural role are not inferred. | FoundationModelsCoffeeGame/FoundationModelsCoffeeGame/GenerateDialog/Characters.swift:8 |
| Source import | Contacts |
The cited file imports this module; runtime use and architectural role are not inferred. | FoundationModelsCoffeeGame/FoundationModelsCoffeeGame/ToolCalling/ContactsTool.swift:8 |
| Source import | EventKit |
The cited file imports this module; runtime use and architectural role are not inferred. | FoundationModelsCoffeeGame/FoundationModelsCoffeeGame/ToolCalling/CalendarTool.swift:8 |
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
FoundationModelsCoffeeGame/FoundationModelsCoffeeGame/GenerateDialog/Characters.swift:11 — representative type boundary
protocol Character: Identifiable {
var id: UUID { get }
// ...
}| 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: Barista → Character, CustomerLisa → Character, GeneratedCustomer → Character.
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. |
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
FoundationModelsCoffeeGameAppis 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 |
Cited implementation, SwiftUI, FoundationModelsCoffeeGameApp |
FoundationModelsCoffeeGame/FoundationModelsCoffeeGame/GenerateDialog/Characters.swift |
Cited implementation, Character, FoundationModels, Foundation, Barista, CustomerLisa, GeneratedCustomer, Attribute, Encounter |
FoundationModelsCoffeeGame/FoundationModelsCoffeeGame/GenerateDialog/DialogBoxView.swift |
Cited implementation, DialogBoxView |
FoundationModelsCoffeeGame/FoundationModelsCoffeeGame/GenerateDialog/DialogEngine.swift |
Cited implementation, @MainActor, @Observable, DialogEngine |
FoundationModelsCoffeeGame/FoundationModelsCoffeeGame/GameView.swift |
Task, await suspension point, SwiftUI state property wrapper, SpriteKit, GameView, GameBoxStyle |
FoundationModelsCoffeeGame/FoundationModelsCoffeeGame/MainMenu/CloudsBackgroundView.swift |
Task closure isolated to MainActor, Cloud, CloudsBackgroundView |
FoundationModelsCoffeeGame/FoundationModelsCoffeeGame/ToolCalling/ContactsTool.swift |
Contacts, ContactsTool, Arguments, Generation |
FoundationModelsCoffeeGame/FoundationModelsCoffeeGame/ToolCalling/CalendarTool.swift |
EventKit, CalendarTool, Arguments |
FoundationModelsCoffeeGame/FoundationModelsCoffeeGame/GenerateEncounters/EncounterEngine.swift |
NPC, EncounterEngine |
FoundationModelsCoffeeGame/FoundationModelsCoffeeGame/MainMenu/MainMenuView.swift |
MainMenuView, ViewState |
FoundationModelsCoffeeGame/FoundationModelsCoffeeGame/OrderCoffee/CoffeeDrink.swift |
CoffeeDrink, Temp, DrinkType, MilkType, Flavor |
FoundationModelsCoffeeGame/FoundationModelsCoffeeGame/GenerateEncounters/CustomerProfileView.swift |
CustomerProfileView |
FoundationModelsCoffeeGame/FoundationModelsCoffeeGame/GenerateEncounters/EncounterView.swift |
EncounterView |
FoundationModelsCoffeeGame/FoundationModelsCoffeeGame/OrderCoffee/CoffeeOrderView.swift |
CoffeeOrderView |
FoundationModelsCoffeeGame/FoundationModelsCoffeeGame/SpriteKitScene/CharacterSprite.swift |
CharacterSprite, BaristaSprite, CustomerLisaSprite, GeneratedCustomerSprite |
FoundationModelsCoffeeGame/FoundationModelsCoffeeGame/SpriteKitScene/CoffeeShopScene.swift |
CoffeeShopScene |