-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameView.swift
119 lines (104 loc) · 3.21 KB
/
GameView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import ComposableArchitecture
import SwiftUI
struct GameView: View {
// Dependencies
let store: Store<GameState, GameAction>
public var body: some View {
WithViewStore(store) { viewStore in
BoardView()
// Trigger rerendering when the GameState is changed
.id(viewStore.state)
// Prepare the game
.onAppear { viewStore.send(.reset) }
.gesture(ControlGesture())
// Pass the ViewStore so our subviews can interact with it.
.alert(
store.scope(state: \.gameoverAlertState),
dismiss: GameAction.actionSheetDismiss
)
.environmentObject(viewStore)
.padding(.horizontal, 20)
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color.black.ignoresSafeArea())
}
}
// MARK: SubViews
fileprivate struct BoardView: View {
// Dependencies
@EnvironmentObject var viewStore: ViewStore<GameState, GameAction>
var body: some View {
Canvas { context, size in
// Draw the snake & the player!
var player = Path()
viewStore
.player
.map { coordinate in
Path(size.gridItemFrame(
row: coordinate.row,
column: coordinate.column,
rows: viewStore.rows,
columns: viewStore.columns
)
)
}
.forEach { path in
player.addPath(path)
player.closeSubpath()
}
// Draw Player
context.fill(player, with: .color(viewStore.gameoverAlertState == nil ? .red : .white))
// Draw Food
context.fill(Path(
size.gridItemFrame(
row: viewStore.food.row,
column: viewStore.food.column,
rows: viewStore.rows,
columns: viewStore.columns
)
),
with: .color(.yellow)
)
}
.aspectRatio(Double(viewStore.rows) / Double(viewStore.columns), contentMode: .fit)
.background(Color.blue)
}
}
// MARK: Gestures
struct ControlGesture: Gesture {
// Dependencies
@EnvironmentObject var viewStore: ViewStore<GameState, GameAction>
func handle(value: DragGesture.Value) {
let vertical = abs(value.translation.width) < abs(value.translation.height)
if vertical {
if value.translation.height > 0 {
viewStore.send(.change(.up))
} else {
viewStore.send(.change(.down))
}
} else {
if value.translation.width > 0 {
viewStore.send(.change(.right))
} else {
viewStore.send(.change(.left))
}
}
}
var body: some Gesture {
DragGesture(minimumDistance: 10, coordinateSpace: .local)
.onEnded(handle(value:))
}
}
// MARK: Util
fileprivate extension CGSize {
func gridItemFrame(row: Int, column: Int, rows: Int, columns: Int) -> CGRect {
let tileWidth: CGFloat = width / CGFloat(rows)
let tileHeight: CGFloat = height / CGFloat(columns)
return CGRect(
x: CGFloat(row) * tileWidth,
y: CGFloat(column) * tileHeight,
width: tileWidth,
height: tileHeight
)
}
}