Skip to content

Commit

Permalink
Complete Challenge 2
Browse files Browse the repository at this point in the history
> Make the other two buttons fade out to 25% opacity.
  • Loading branch information
CypherPoet authored and CypherPoet committed Oct 27, 2019
1 parent 2c05af7 commit deeebfb
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@ final class FlagGame: ObservableObject {
@Published var latestChoiceWasCorrect = false


@Published var flagRotations: [Double] = [
0.0,
0.0,
0.0,
]

@Published var flagOpacities: [Double] = [
1.0,
1.0,
1.0,
]


init() {
startNewGame()
}
Expand All @@ -44,34 +57,55 @@ extension FlagGame {
if flag == flagToGuess {
currentScore += 1
latestChoiceWasCorrect = true
completionHandler(true)
} else {
currentScore = max(0, currentScore - 3)
latestChoiceWasCorrect = false
completionHandler(false)
}

completionHandler(latestChoiceWasCorrect)
}


public func startNewGame() {
currentRound = 1
currentScore = 0

resetFlagAppearances()
computedNewFlagChoices()
}


public func incrementRound() {
currentRound += 1

resetFlagAppearances()
computedNewFlagChoices()
}
}


// MARK: - Private Helpers
extension FlagGame {

private func computedNewFlagChoices() {
flagChoices = Array(flagsStore.flags.shuffled()[0...2])
flagToGuess = flagChoices.randomElement()
}


func resetFlagAppearances() {
flagOpacities = [1.0, 1.0, 1.0]
}


private func updateFlagAppearancesAfterGuess() {
guard
let latestChoice = latestChoice,
let choiceIndex = flagChoices.firstIndex(of: latestChoice)
else { fatalError() }


flagRotations[choiceIndex] = latestChoiceWasCorrect ? 0.0 : (2 * .pi)
flagOpacities[choiceIndex] = latestChoiceWasCorrect ? 1.0 : 0.2
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,6 @@ struct MainView: View {
@State private var answerWasCorrect = false


@State private var flagRotations: [Double] = [
0.0,
0.0,
0.0
]


var body: some View {
ZStack {
LinearGradient(
Expand Down Expand Up @@ -54,10 +47,10 @@ struct MainView: View {
}
)
.rotation3DEffect(
.radians(self.flagRotations[index]),
.radians(self.flagGame.flagRotations[index]),
axis: (x: 0, y: 1, z: 0)
)
.animation(.easeInOut(duration: 0.5))
.opacity(self.flagGame.flagOpacities[index])
}


Expand Down Expand Up @@ -136,10 +129,18 @@ extension MainView {
let spinAnimation = Animation.easeInOut(duration: spinDuration)

withAnimation(spinAnimation) {
self.flagRotations[indexToSpin] += 2 * .pi
self.flagGame.flagRotations[indexToSpin] += 2 * .pi

for (index, flag) in self.flagGame.flagChoices.enumerated() {
if flag != latestChoice {
self.flagGame.flagOpacities[index] = 0.2
} else {
self.flagGame.flagOpacities[index] = 1.0
}
}
}
DispatchQueue.main.asyncAfter(deadline: .now() + (spinDuration + 0.2)) {

DispatchQueue.main.asyncAfter(deadline: .now() + (spinDuration + 0.6)) {
self.flagGame.incrementRound()
}
}
Expand Down

0 comments on commit deeebfb

Please # to comment.