Skip to content

Commit 0197d6e

Browse files
Add tutorial for using a MinecraftMap
1 parent 1a91a73 commit 0197d6e

11 files changed

Lines changed: 250 additions & 0 deletions
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import CubiomesKit
2+
import SwiftUI
3+
4+
struct HermitcraftMap: View {
5+
@State private var world: MinecraftWorld?
6+
7+
var body: some View {
8+
Group {
9+
if let world {
10+
MinecraftMap(world: world)
11+
.ignoresSafeArea()
12+
} else {
13+
ContentUnavailableView(
14+
"Welcome to Hermitcraft",
15+
systemImage: "globe")
16+
.navigationTitle("Hermitcraft")
17+
}
18+
}
19+
.onAppear {
20+
loadHermitcraftWorld()
21+
}
22+
}
23+
24+
private func loadHermitcraftWorld() {
25+
do {
26+
world = try MinecraftWorld(version: "1.20", seed: Int64(HermitcraftWorld.seed))
27+
} catch {
28+
print("Failed to load the world: \(error.localizedDescription)")
29+
}
30+
}
31+
}
598 KB
Loading
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import CubiomesKit
2+
import SwiftUI
3+
4+
struct HermitcraftMap: View {
5+
@State private var world: MinecraftWorld?
6+
7+
var body: some View {
8+
Group {
9+
if let world {
10+
MinecraftMap(world: world)
11+
.mapColorScheme(.natural)
12+
.ornaments([.zoom, .compass])
13+
.ignoresSafeArea()
14+
} else {
15+
ContentUnavailableView(
16+
"Welcome to Hermitcraft",
17+
systemImage: "globe")
18+
.navigationTitle("Hermitcraft")
19+
}
20+
}
21+
.onAppear {
22+
loadHermitcraftWorld()
23+
}
24+
}
25+
26+
private func loadHermitcraftWorld() {
27+
do {
28+
world = try MinecraftWorld(version: "1.20", seed: Int64(HermitcraftWorld.seed))
29+
} catch {
30+
print("Failed to load the world: \(error.localizedDescription)")
31+
}
32+
}
33+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import CubiomesKit
2+
import SwiftUI
3+
4+
struct HermitcraftMap: View {
5+
@State private var world: MinecraftWorld?
6+
7+
var body: some View {
8+
ContentUnavailableView(
9+
"Welcome to Hermitcraft",
10+
systemImage: "globe")
11+
.navigationTitle("Hermitcraft")
12+
.onAppear {
13+
loadHermitcraftWorld()
14+
}
15+
}
16+
17+
private func loadHermitcraftWorld() {
18+
do {
19+
world = try MinecraftWorld(version: "1.20", seed: Int64(HermitcraftWorld.seed))
20+
} catch {
21+
print("Failed to load the world: \(error.localizedDescription)")
22+
}
23+
}
24+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import CubiomesKit
2+
import SwiftUI
3+
4+
struct HermitcraftMap: View {
5+
@State private var world: MinecraftWorld?
6+
7+
var body: some View {
8+
Group {
9+
if let world {
10+
MinecraftMap(world: world)
11+
.mapColorScheme(.natural)
12+
.ignoresSafeArea()
13+
} else {
14+
ContentUnavailableView(
15+
"Welcome to Hermitcraft",
16+
systemImage: "globe")
17+
.navigationTitle("Hermitcraft")
18+
}
19+
}
20+
.onAppear {
21+
loadHermitcraftWorld()
22+
}
23+
}
24+
25+
private func loadHermitcraftWorld() {
26+
do {
27+
world = try MinecraftWorld(version: "1.20", seed: Int64(HermitcraftWorld.seed))
28+
} catch {
29+
print("Failed to load the world: \(error.localizedDescription)")
30+
}
31+
}
32+
}
295 KB
Loading
900 KB
Loading
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import SwiftUI
2+
3+
struct HermitcraftMap: View {
4+
var body: some View {
5+
ContentUnavailableView(
6+
"Welcome to Hermitcraft",
7+
systemImage: "globe")
8+
.navigationTitle("Hermitcraft")
9+
}
10+
}
Binary file not shown.
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
@Tutorial(time: 30, projectFiles: "HermitcraftMaps.zip") {
2+
@Intro(title: "Display interactive maps on iOS, iPadOS, and macOS") {
3+
Display an interactive map of a Minecraft world using CubiomesKit, MapKit, and SwiftUI.
4+
}
5+
6+
@Section(title: "Add a map to Hermitcraft Maps") {
7+
A ``MinecraftMap`` view allows you to insert a map of a Minecraft world into an existing SwiftUI view, and it
8+
functions like a MapKit map.
9+
10+
@Steps {
11+
@Step {
12+
Open the sample project in Xcode.
13+
}
14+
@Step {
15+
In `HermitcraftMap.swift`, create a `MinecraftWorld` using the Hermitcraft seed.
16+
17+
@Code(name: "HermitcraftMap.swift",
18+
file: "HermitcraftMap-AddedWorld.swift",
19+
previousFile: "HermitcraftMap-Original.swift")
20+
}
21+
@Step {
22+
Create a `MinecraftMap` with the world we created as an argument.
23+
24+
> Tip: To hide the title bar, use `.windowStyle(.hiddenTitleBar)` on the WindowGroup in
25+
> `HermitcraftMapsApp.swift`
26+
27+
@Code(name: "HermitcraftMap.swift", file: "HermitcraftMap-AddedMap.swift") {
28+
@Image(source: "HermitcraftMap-AddedMapVisual",
29+
alt: "The Hermitcraft Maps app with the map loaded.")
30+
}
31+
}
32+
33+
}
34+
}
35+
@Section(title: "Customize the map") {
36+
The map provides a lot of functionality out of the box, such as tiling, panning, and zooming. We can customize
37+
the experience even further to better suit our use case.
38+
39+
@Steps {
40+
@Step {
41+
Use the `mapColorScheme(_:)` modifier to use a more natural color scheme that resembles the real
42+
Minecraft terrain.
43+
44+
@Code(name: "HermitcraftMap.swift",
45+
file: "HermitcraftMap-ColorSchemes.swift",
46+
previousFile: "HermitcraftMap-AddedMap.swift") {
47+
@Image(source: "HermitcraftMap-NaturalColors",
48+
alt: "The Hermitcraft Maps app with a more natural color scheme.")
49+
}
50+
}
51+
@Step {
52+
Add an `ornaments(_:)` modifier and include the zoom and compass controls. These will display on the map
53+
to allow players to interact with the map in other ways.
54+
55+
@Code(name: "HermitcraftMap.swift", file: "HermitcraftMap-AddedOrnaments.swift") {
56+
@Image(source: "HermitcraftMap-MapControls", alt: "The Hermitcraft Maps app with map controls.")
57+
}
58+
}
59+
}
60+
}
61+
@Assessments {
62+
@MultipleChoice {
63+
Which SwiftUI view displays a Minecraft world map?
64+
65+
@Choice(isCorrect: false) {
66+
`MinecraftWorldMap`
67+
68+
@Justification(reaction: "Not quite!") {
69+
Refer to the "Add a map to Hermitcraft Maps" section.
70+
}
71+
}
72+
73+
@Choice(isCorrect: true) {
74+
`MinecraftMap`
75+
76+
@Justification(reaction: "That's right!") {
77+
The ``MinecraftMap`` view allows displaying Minecraft world maps.
78+
}
79+
}
80+
81+
@Choice(isCorrect: false) {
82+
`Map`
83+
84+
@Justification(reaction: "Try again!") {
85+
Refer to the "Add a map to Hermitcraft Maps" section.
86+
}
87+
}
88+
}
89+
90+
@MultipleChoice {
91+
**True or False**: To change the map's color scheme, the `colorScheme(_:)` modifier can be used.
92+
93+
@Choice(isCorrect: false) {
94+
True
95+
96+
@Justification(reaction: "Try again!") {
97+
Consider what the `colorScheme(_:)` modifier does in SwiftUI and revisit the "Customize the map"
98+
section.
99+
}
100+
}
101+
102+
@Choice(isCorrect: true) {
103+
False
104+
105+
@Justification(reaction: "That's right!") {
106+
`colorScheme(_:)` is used to control the view's preferred color scheme and is a default modifier of
107+
SwiftUI. To change the map's color scheme, use `mapColorScheme(_:)`.
108+
}
109+
}
110+
}
111+
}
112+
}

0 commit comments

Comments
 (0)