From f2f4acccd4fdccf0aae39e32186c8c8ba4d49921 Mon Sep 17 00:00:00 2001 From: Hugo Pezziardi Date: Thu, 21 Sep 2023 10:41:32 +0200 Subject: [PATCH] :sparkles: (GameEngineKit): Add SuperSimon gameplay --- .../Specific/SuperSimonGameplay.swift | 58 +++++++++++++++++++ .../Model/Protocol/StepModelProtocol.swift | 1 + .../GameEngineKit/Sources/StepManager.swift | 2 + 3 files changed, 61 insertions(+) create mode 100644 Modules/GameEngineKit/Sources/Gameplay/Specific/SuperSimonGameplay.swift diff --git a/Modules/GameEngineKit/Sources/Gameplay/Specific/SuperSimonGameplay.swift b/Modules/GameEngineKit/Sources/Gameplay/Specific/SuperSimonGameplay.swift new file mode 100644 index 0000000000..c9515bc257 --- /dev/null +++ b/Modules/GameEngineKit/Sources/Gameplay/Specific/SuperSimonGameplay.swift @@ -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(.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 + } + } +} diff --git a/Modules/GameEngineKit/Sources/Model/Protocol/StepModelProtocol.swift b/Modules/GameEngineKit/Sources/Model/Protocol/StepModelProtocol.swift index c363302310..d31259660d 100644 --- a/Modules/GameEngineKit/Sources/Model/Protocol/StepModelProtocol.swift +++ b/Modules/GameEngineKit/Sources/Model/Protocol/StepModelProtocol.swift @@ -10,6 +10,7 @@ public enum GameplayType { case selectAllRightAnswers case selectSomeRightAnswers(Int) case colorQuest + case superSimon([Int]) } public enum InterfaceType { diff --git a/Modules/GameEngineKit/Sources/StepManager.swift b/Modules/GameEngineKit/Sources/StepManager.swift index a29b41eccc..f49268a7ab 100644 --- a/Modules/GameEngineKit/Sources/StepManager.swift +++ b/Modules/GameEngineKit/Sources/StepManager.swift @@ -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) } }