Skip to content

Commit

Permalink
✨ (GameEngineKit): Add SuperSimon gameplay
Browse files Browse the repository at this point in the history
  • Loading branch information
HPezz committed Sep 25, 2023
1 parent 49d226a commit f2f4acc
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Leka - iOS Monorepo
// Copyright 2023 APF France handicap
// SPDX-License-Identifier: Apache-2.0

import Combine
import Foundation

public class SuperSimonGameplay: GameplayProtocol {
public var choices = CurrentValueSubject<[ChoiceViewModel], Never>([])
public var state = CurrentValueSubject<GameplayState, Never>(.idle)

private var rightAnswersGiven: [ChoiceViewModel] = []
private var answerIndexOrder: [Int]

public init(choices: [ChoiceViewModel], answerIndexOrder: [Int]) {
self.choices.send(choices)
self.state.send(.playing)
self.answerIndexOrder = answerIndexOrder

// TODO(@ladislas): Show the right color and song sequence on Leka's belt
for index in 0 ..< answerIndexOrder.count {
DispatchQueue.main.asyncAfter(deadline: .now() + 1.5 * Double(index)) {
let choiceIndex = self.answerIndexOrder[index]
let color = self.choices.value[choiceIndex].item
print("Leka is \(color)")
}
}
}

public func process(choice: ChoiceViewModel) {
if let index = choices.value.firstIndex(where: { $0.id == choice.id && $0.status != .playingRightAnimation }
) {
if index == answerIndexOrder[rightAnswersGiven.count] {
self.choices.value[index].status = .playingRightAnimation

rightAnswersGiven.append(self.choices.value[index])
} else {
self.choices.value[index].status = .playingWrongAnimation

DispatchQueue.main.asyncAfter(deadline: .now() + 0.8) {
self.choices.value[index].status = .notSelected
}
}
}

if rightAnswersGiven.count == answerIndexOrder.count {
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
for choice in self.choices.value.filter({ $0.status == .playingRightAnimation }) {
guard let index = self.choices.value.firstIndex(where: { $0.id == choice.id }) else { return }
self.choices.value[index].status = .notSelected
}
}
rightAnswersGiven.removeAll()
self.state.send(.finished)
// TODO(@ladislas): Run reinforcers and lottie animation
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public enum GameplayType {
case selectAllRightAnswers
case selectSomeRightAnswers(Int)
case colorQuest
case superSimon([Int])
}

public enum InterfaceType {
Expand Down
2 changes: 2 additions & 0 deletions Modules/GameEngineKit/Sources/StepManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ public class StepManager: ObservableObject {
choices: stepModel.choices, rightAnswersToFind: answersNumber)
case .colorQuest:
return ColorQuestGameplay(choices: stepModel.choices)
case .superSimon(let answerIndexOrder):
return SuperSimonGameplay(choices: stepModel.choices, answerIndexOrder: answerIndexOrder)
}
}

Expand Down

0 comments on commit f2f4acc

Please sign in to comment.