Skip to content

Commit

Permalink
♻️ (GameEngineKit): Nodes now not draggable on gameplay completed
Browse files Browse the repository at this point in the history
  • Loading branch information
HPezz committed Nov 2, 2023
1 parent 5cd983b commit 34636f8
Showing 1 changed file with 36 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// Copyright 2023 APF France handicap
// SPDX-License-Identifier: Apache-2.0

import ContentKit
import Combine
import ContentKit
import SpriteKit
import SwiftUI

Expand All @@ -14,14 +14,17 @@ class DragAndDropBaseScene: SKScene {
var dropZoneB: DropZoneDetails?
var biggerSide: CGFloat = 130
var selectedNodes: [UITouch: DraggableImageAnswerNode] = [:]
var answerNodes: [DraggableImageAnswerNode] = []
var playedNode: DraggableImageAnswerNode?
var dropZoneNodes: [SKSpriteNode] = []
private var spacer: CGFloat = .zero
private var defaultPosition = CGPoint.zero
private var expectedItemsNodes: [String: [SKSpriteNode]] = [:]
private var cancellables: Set<AnyCancellable> = []

init(viewModel: DragAndDropViewViewModel, hints: Bool, dropZoneA: DropZoneDetails, dropZoneB: DropZoneDetails? = nil) {
init(
viewModel: DragAndDropViewViewModel, hints: Bool, dropZoneA: DropZoneDetails, dropZoneB: DropZoneDetails? = nil
) {
self.viewModel = viewModel
self.hints = hints
self.dropZoneA = dropZoneA
Expand All @@ -42,7 +45,6 @@ class DragAndDropBaseScene: SKScene {
self.removeAllChildren()
self.removeAllActions()


setFirstAnswerPosition()
if let dropZoneB = dropZoneB {
layoutDropZones(dropZones: dropZoneA, dropZoneB)
Expand All @@ -53,6 +55,13 @@ class DragAndDropBaseScene: SKScene {
layoutAnswers()
}

func exerciseCompletedBehavior() {
for node in answerNodes {
snapBack(node)
node.isDraggable = false
}
}

func subscribeToChoicesUpdates() {
self.viewModel.$choices
.receive(on: DispatchQueue.main)
Expand Down Expand Up @@ -82,6 +91,8 @@ class DragAndDropBaseScene: SKScene {
bindNodesToSafeArea([draggableImageAnswerNode, draggableImageShadowNode])
setNextAnswerPosition()

answerNodes.append(draggableImageAnswerNode)

addChild(draggableImageShadowNode)
addChild(draggableImageAnswerNode)
}
Expand Down Expand Up @@ -144,26 +155,13 @@ class DragAndDropBaseScene: SKScene {
onDropAction(node)
if viewModel.exercicesSharedData.state == .completed {
DispatchQueue.main.asyncAfter(deadline: .now() + 1.5) { [self] in
self.reset()
exerciseCompletedBehavior()
}
}
}

func wrongAnswerBehavior(_ node: DraggableImageAnswerNode) {
let moveAnimation: SKAction = SKAction.move(to: node.defaultPosition!, duration: 0.25)
.moveAnimation(.easeOut)
let group: DispatchGroup = DispatchGroup()
group.enter()
node.run(
moveAnimation,
completion: {
node.position = node.defaultPosition!
node.zPosition = 10
group.leave()
})
group.notify(queue: .main) {
self.onDropAction(node)
}
snapBack(node)
}

func onDragAnimation(_ node: SKSpriteNode) {
Expand All @@ -182,6 +180,24 @@ class DragAndDropBaseScene: SKScene {
selectedNodes = [:]
}

private func snapBack(_ node: DraggableImageAnswerNode) {
let moveAnimation: SKAction = SKAction.move(to: node.defaultPosition!, duration: 0.25)
.moveAnimation(.easeOut)
let group: DispatchGroup = DispatchGroup()
group.enter()
node.scaleForMax(sizeOf: biggerSide)
node.run(
moveAnimation,
completion: {
node.position = node.defaultPosition!
node.zPosition = 10
group.leave()
})
group.notify(queue: .main) {
self.onDropAction(node)
}
}

override func didMove(to view: SKView) {
self.reset()
}
Expand All @@ -191,7 +207,8 @@ class DragAndDropBaseScene: SKScene {
for touch in touches {
let location = touch.location(in: self)
if let node = self.atPoint(location) as? DraggableImageAnswerNode {
for gameplayChoiceModel in viewModel.choices where node.name == gameplayChoiceModel.choice.value && node.isDraggable {
for gameplayChoiceModel in viewModel.choices
where node.name == gameplayChoiceModel.choice.value && node.isDraggable {
selectedNodes[touch] = node
onDragAnimation(node)
node.zPosition += 100
Expand Down

0 comments on commit 34636f8

Please sign in to comment.