Skip to content

Commit

Permalink
feat: maze prompt game over
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron Christopher Tanhar committed Dec 3, 2023
1 parent ccaa7f8 commit cc11e2c
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 91 deletions.
2 changes: 2 additions & 0 deletions Moco/Model/MazePromptModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ struct MazePromptModel {
var isCorrectAnswer: Bool = false
var isWrongAnswer: Bool = false
var isTutorialDone = GlobalStorage.mazeTutorialFinished
var isGameOver = false
var mazeCount = -1
var currentMazeIndex = 0

Expand All @@ -28,6 +29,7 @@ struct MazePromptModel {
progress = 0.0
isCorrectAnswer = false
isWrongAnswer = false
isGameOver = false
isTutorialDone = GlobalStorage.mazeTutorialFinished
mazeCount = -1
currentMazeIndex = 0
Expand Down
27 changes: 25 additions & 2 deletions Moco/View/Components/Prompts/Maze/MazePrompt.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
import SwiftUI

struct MazePrompt: View {
@Environment(\.navigate) private var navigate
@Environment(\.settingsViewModel) private var settingsViewModel
@Environment(\.audioViewModel) private var audioViewModel
@Environment(\.episodeViewModel) private var episodeViewModel
@Environment(\.mazePromptViewModel) private var mazePromptViewModel

@State private var isCorrectAnswerPopup = false
@State private var isWrongAnswerPopup = false
@State private var gameOverPopup = false

@State private var updateTimer = true
@State private var elapsedSecond = 0
Expand All @@ -30,6 +32,8 @@ struct MazePrompt: View {

var action: () -> Void = {}

var onRestart: (() -> Void)?

func playInitialNarration() {
if mazePromptViewModel.isTutorialDone {
audioViewModel.playSound(
Expand All @@ -49,7 +53,14 @@ struct MazePrompt: View {
Spacer()
TimerView(
durationParamInSeconds: mazePromptViewModel.durationInSeconds
)
) {
// MARK: - Game Over

gameOverPopup = true
mazePromptViewModel.isGameOver = true

// MARK: -
}
.padding(.trailing, Screen.width * 0.3)
}
Text(promptText)
Expand Down Expand Up @@ -112,8 +123,20 @@ struct MazePrompt: View {
.popUp(isActive: $isWrongAnswerPopup, title: "Oh tidak! Kamu pergi ke jalan yang salah", disableCancel: true) {
action()
}
.popUp(
isActive: $gameOverPopup,
title: "Waktu telah habis!",
cancelText: "Keluar",
confirmText: "Ulangi",
disableCancel: true,
type: .danger
) {
onRestart?()
} cancelHandler: {
navigate.popToRoot()
}
.onReceive(timer) { _ in
if updateTimer {
if updateTimer && mazePromptViewModel.isTutorialDone {
elapsedSecond += 1
}
}
Expand Down
45 changes: 27 additions & 18 deletions Moco/View/User/Maze/MazeScene.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ struct MazeAnswerAssets {

class MazeScene: SKScene, SKPhysicsContactDelegate, ObservableObject {
let motionViewModel = MotionViewModel.shared
let mazePromptViewModel = MazePromptViewModel.shared

var moco: SKSpriteNode!
var obj01: SKSpriteNode!
Expand Down Expand Up @@ -168,25 +169,25 @@ class MazeScene: SKScene, SKPhysicsContactDelegate, ObservableObject {
obj03?.texture = SKTexture(imageNamed: wrongAnswerAsset[1])
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
override func touchesBegan(_ touches: Set<UITouch>, with _: UIEvent?) {
if let touch = touches.first {
let location = touch.location(in: self)
lastTouchPosition = location
}
}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
override func touchesMoved(_ touches: Set<UITouch>, with _: UIEvent?) {
if let touch = touches.first {
let location = touch.location(in: self)
lastTouchPosition = location
}
}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
override func touchesEnded(_: Set<UITouch>, with _: UIEvent?) {
lastTouchPosition = nil
}

override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
override func touchesCancelled(_: Set<UITouch>, with _: UIEvent?) {
lastTouchPosition = nil
}

Expand All @@ -208,20 +209,27 @@ class MazeScene: SKScene, SKPhysicsContactDelegate, ObservableObject {
}
}

override func update(_ currentTime: TimeInterval) {
#if targetEnvironment(simulator)
if let currentTouch = lastTouchPosition {
let diff = CGPoint(x: currentTouch.x - player.position.x, y: currentTouch.y - player.position.y)
physicsWorld.gravity = CGVector(dx: diff.x / 100, dy: diff.y / 100)
}
#else
if let accelerometerData = motionViewModel.accelerometerData {
override func update(_: TimeInterval) {
if !mazePromptViewModel.canMove {
physicsWorld.gravity = CGVector(
dx: accelerometerData.acceleration.y * -2,
dy: accelerometerData.acceleration.x * 2
dx: 0,
dy: 0
)
return
}
#endif
#if targetEnvironment(simulator)
if let currentTouch = lastTouchPosition {
let diff = CGPoint(x: currentTouch.x - player.position.x, y: currentTouch.y - player.position.y)
physicsWorld.gravity = CGVector(dx: diff.x / 100, dy: diff.y / 100)
}
#else
if let accelerometerData = motionViewModel.accelerometerData {
physicsWorld.gravity = CGVector(
dx: accelerometerData.acceleration.y * -2,
dy: accelerometerData.acceleration.x * 2
)
}
#endif
}
}

Expand Down Expand Up @@ -257,7 +265,8 @@ extension MazeScene {
}
ground.physicsBody = SKPhysicsBody(rectangleOf: CGSize(
width: ground.size.width * 0.5,
height: ground.size.height * 0.5)
height: ground.size.height * 0.5
)
)
ground.physicsBody?.categoryBitMask = CollisionTypes.finish.rawValue
ground.physicsBody?.contactTestBitMask = CollisionTypes.player.rawValue
Expand All @@ -274,8 +283,8 @@ extension MazeScene {

// MARK: - Outer wall

if mazeModel.arrayPoint[index][jIndex] == 0 &&
[0, mazeModel.arrayPoint.indices.last].contains(index) {
if mazeModel.arrayPoint[index][jIndex] == 0,
[0, mazeModel.arrayPoint.indices.last].contains(index) {
let outerWall = SKSpriteNode()
outerWall.size = CGSize(width: tileSize, height: tileSize)
outerWall.name = "outer_wall"
Expand Down
130 changes: 62 additions & 68 deletions Moco/View/User/Maze/MazeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ struct MazeView: View {
@EnvironmentObject var motionViewModel: MotionViewModel
@EnvironmentObject var orientationInfo: OrientationInfo
@Environment(\.mazePromptViewModel) private var mazePromptViewModel
@State private var timerViewModel = TimerViewModel()

var answersAsset = ["Maze/answer_one", "Maze/answer_two"] {
didSet {
Expand Down Expand Up @@ -113,73 +112,6 @@ struct MazeView: View {
motionViewModel.startUpdates()
scene.correctAnswerAsset = correctAnswerAsset
scene.wrongAnswerAsset = answersAsset
timerViewModel.stopTimer("mazeTimer\(correctAnswerAsset)")
timerViewModel.setTimer(key: "mazeTimer\(correctAnswerAsset)", withInterval: 0.02) {
return
guard mazePromptViewModel.isTutorialDone else { return }
motionViewModel.updateMotion()
if orientationInfo.orientation == .landscapeLeft {
if abs(motionViewModel.rollNum) > abs(motionViewModel.pitchNum) {
if motionViewModel.rollNum > 0 {
switch motionViewModel.gravityDegree {
case -75 ... -10, 10 ... 80:
scene.move(.right)
case 100 ... 170, 190 ... 255:
scene.move(.left)
default:
scene.move(.up)
}
} else if motionViewModel.rollNum < 0 {
switch motionViewModel.gravityDegree {
case -75 ... -10, 105 ... 170:
scene.move(.right)
case 10 ... 75, 190 ... 255:
scene.move(.left)
default:
scene.move(.down)
}
}
} else {
if motionViewModel.pitchNum > 0 {
scene.move(.right)
} else if motionViewModel.pitchNum < 0 {
scene.move(.left)
}
}
} else if orientationInfo.orientation == .landscapeRight {
if abs(motionViewModel.rollNum) > abs(motionViewModel.pitchNum) {
if motionViewModel.rollNum > 0 {
switch motionViewModel.gravityDegree {
case -75 ... -10, 105 ... 170:
scene.move(.right)
case 10 ... 75, 190 ... 255:
scene.move(.left)
default:
scene.move(.down)
}
} else if motionViewModel.rollNum < 0 {
switch motionViewModel.gravityDegree {
case -75 ... -10, 10 ... 80:
scene.move(.left)
case 100 ... 170, 190 ... 255:
scene.move(.right)
default:
scene.move(.up)
}
}
} else {
if motionViewModel.pitchNum > 0 {
scene.move(.left)
} else if motionViewModel.pitchNum < 0 {
scene.move(.right)
}
}
}
}
}
.onDisappear {
// motionViewModel.stopUpdates()
timerViewModel.stopTimer("mazeTimer\(correctAnswerAsset)")
}
.onChange(of: scene.correctAnswer) {
if let sceneCorrectAnswer = scene.correctAnswer {
Expand All @@ -192,6 +124,68 @@ struct MazeView: View {
}
}
}

private func updateMazeControl() {

Check failure on line 128 in Moco/View/User/Maze/MazeView.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Cyclomatic Complexity Violation: Function should have complexity 10 or less; currently complexity is 25 (cyclomatic_complexity)
guard mazePromptViewModel.isTutorialDone else { return }
motionViewModel.updateMotion()
if orientationInfo.orientation == .landscapeLeft {
if abs(motionViewModel.rollNum) > abs(motionViewModel.pitchNum) {
if motionViewModel.rollNum > 0 {
switch motionViewModel.gravityDegree {
case -75 ... -10, 10 ... 80:
scene.move(.right)
case 100 ... 170, 190 ... 255:
scene.move(.left)
default:
scene.move(.up)
}
} else if motionViewModel.rollNum < 0 {
switch motionViewModel.gravityDegree {
case -75 ... -10, 105 ... 170:
scene.move(.right)
case 10 ... 75, 190 ... 255:
scene.move(.left)
default:
scene.move(.down)
}
}
} else {
if motionViewModel.pitchNum > 0 {
scene.move(.right)
} else if motionViewModel.pitchNum < 0 {
scene.move(.left)
}
}
} else if orientationInfo.orientation == .landscapeRight {
if abs(motionViewModel.rollNum) > abs(motionViewModel.pitchNum) {
if motionViewModel.rollNum > 0 {
switch motionViewModel.gravityDegree {
case -75 ... -10, 105 ... 170:
scene.move(.right)
case 10 ... 75, 190 ... 255:
scene.move(.left)
default:
scene.move(.down)
}
} else if motionViewModel.rollNum < 0 {
switch motionViewModel.gravityDegree {
case -75 ... -10, 10 ... 80:
scene.move(.left)
case 100 ... 170, 190 ... 255:
scene.move(.right)
default:
scene.move(.up)
}
}
} else {
if motionViewModel.pitchNum > 0 {
scene.move(.left)
} else if motionViewModel.pitchNum < 0 {
scene.move(.right)
}
}
}
}
}

struct MazeViewPreview: View {
Expand Down
5 changes: 4 additions & 1 deletion Moco/View/User/StoryView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,10 @@ struct StoryView: View {
promptId: mazePrompt.uid
) {
svvm.nextPage()
}.id(mazePrompt.id)
} onRestart: {
svvm.restart(true)
}
.id(mazePrompt.id)
}
case .ar:
if let ARPrompt = promptViewModel.prompts?[0] {
Expand Down
15 changes: 15 additions & 0 deletions Moco/ViewModel/MazePromptViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,21 @@ import SwiftUI
}
}

var canMove: Bool {
get {
isTutorialDone && !mazePromptModel.isGameOver
}
}

var isGameOver: Bool {
get {
mazePromptModel.isGameOver
}
set {
mazePromptModel.isGameOver = newValue
}
}

func playPrompt() {
mazePromptModel.isStarted = false
withAnimation(.easeInOut(duration: 3)) {
Expand Down
Loading

0 comments on commit cc11e2c

Please sign in to comment.