Skip to content

Commit

Permalink
♻️ (GameEngineKit): Refactor the way to handle dropZones in BaseScene
Browse files Browse the repository at this point in the history
  • Loading branch information
HPezz committed Nov 3, 2023
1 parent 19210f3 commit 1f65ca1
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,21 @@ import ContentKit
import SpriteKit
import SwiftUI

struct DropZoneNode {
let details: DropZoneDetails
var node: SKSpriteNode = SKSpriteNode()
let zone: DragAndDropChoice.ChoiceDropZone
}

class DragAndDropBaseScene: SKScene {
var viewModel: DragAndDropViewViewModel
var hints: Bool
var dropZoneA: DropZoneDetails
var dropZoneB: DropZoneDetails?
var biggerSide: CGFloat = 130
var selectedNodes: [UITouch: DraggableImageAnswerNode] = [:]
var answerNodes: [DraggableImageAnswerNode] = []
var playedNode: DraggableImageAnswerNode?
var dropZoneNodes: [SKSpriteNode] = []
var dropZoneA: DropZoneNode
var dropZoneB: DropZoneNode?
private var hints: Bool
private var biggerSide: CGFloat = 130
private var selectedNodes: [UITouch: DraggableImageAnswerNode] = [:]
private var answerNodes: [DraggableImageAnswerNode] = []
private var playedNode: DraggableImageAnswerNode?
private var spacer: CGFloat = .zero
private var defaultPosition = CGPoint.zero
private var expectedItemsNodes: [String: [SKSpriteNode]] = [:]
Expand All @@ -27,8 +32,10 @@ class DragAndDropBaseScene: SKScene {
) {
self.viewModel = viewModel
self.hints = hints
self.dropZoneA = dropZoneA
self.dropZoneB = dropZoneB
self.dropZoneA = DropZoneNode(details: dropZoneA, zone: .zoneA)
if let dropZoneB = dropZoneB {
self.dropZoneB = DropZoneNode(details: dropZoneB, zone: .zoneB)
}
super.init(size: CGSize.zero)
self.spacer = size.width / CGFloat(viewModel.choices.count + 1)
self.defaultPosition = CGPoint(x: spacer, y: self.size.height)
Expand All @@ -46,11 +53,7 @@ class DragAndDropBaseScene: SKScene {
self.removeAllActions()

setFirstAnswerPosition()
if let dropZoneB = dropZoneB {
layoutDropZones(dropZones: dropZoneA, dropZoneB)
} else {
layoutDropZones(dropZones: dropZoneA)
}
layoutDropZones()
getExpectedItems()
layoutAnswers()
}
Expand Down Expand Up @@ -120,7 +123,7 @@ class DragAndDropBaseScene: SKScene {
self.defaultPosition.x += spacer
}

func layoutDropZones(dropZones: DropZoneDetails...) {
func layoutDropZones() {
fatalError("layoutDropZones(dropZones:) has not been implemented")
}

Expand All @@ -132,7 +135,7 @@ class DragAndDropBaseScene: SKScene {

guard hints else {
expectedNode.name = expectedItem
(expectedItemsNodes[dropZoneA.value, default: []]).append(expectedNode)
(expectedItemsNodes[dropZoneA.details.value, default: []]).append(expectedNode)
return
}
let texture = SKTexture(image: UIImage(named: expectedItem)!)
Expand All @@ -141,8 +144,8 @@ class DragAndDropBaseScene: SKScene {
expectedNode.name = expectedItem
expectedNode.texture = texture
expectedNode.scaleForMax(sizeOf: biggerSide * 0.8)
expectedNode.position = CGPoint(x: dropZoneNodes[0].position.x + 80, y: 110)
(expectedItemsNodes[dropZoneA.value, default: []]).append(expectedNode)
expectedNode.position = CGPoint(x: dropZoneA.node.position.x + 80, y: 110)
(expectedItemsNodes[dropZoneA.details.value, default: []]).append(expectedNode)

addChild(expectedNode)
}
Expand Down Expand Up @@ -245,14 +248,14 @@ class DragAndDropBaseScene: SKScene {
playedNode!.scaleForMax(sizeOf: biggerSide)
let gameplayChoiceModel = viewModel.choices.first(where: { $0.choice.value == playedNode!.name })

if playedNode!.fullyContains(bounds: dropZoneNodes[0].frame) {
viewModel.onChoiceTapped(choice: gameplayChoiceModel!, dropZone: .zoneA)
if playedNode!.fullyContains(bounds: dropZoneA.node.frame) {
viewModel.onChoiceTapped(choice: gameplayChoiceModel!, dropZone: dropZoneA.zone)
break
}

if dropZoneB != nil {
if playedNode!.fullyContains(bounds: dropZoneNodes[1].frame) {
viewModel.onChoiceTapped(choice: gameplayChoiceModel!, dropZone: .zoneB)
if let dropZoneB = dropZoneB {
if playedNode!.fullyContains(bounds: dropZoneB.node.frame) {
viewModel.onChoiceTapped(choice: gameplayChoiceModel!, dropZone: dropZoneB.zone)
break
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,19 @@
// Copyright 2023 APF France handicap
// SPDX-License-Identifier: Apache-2.0

import ContentKit
import SpriteKit
import SwiftUI

final class DragAndDropOneZoneScene: DragAndDropBaseScene {
override func layoutDropZones(dropZones: DropZoneDetails...) {
override func layoutDropZones() {
// TODO(@hugo): Add type declaration

Check failure on line 9 in Modules/GameEngineKit/Sources/_NewSystem/Views/DragAndDrop/OneZone/DragAndDropOneZoneScene.swift

View workflow job for this annotation

GitHub Actions / lint

TODOs should be resolved ((@hugo): Add type declaration) (todo)
let dropZoneNode = SKSpriteNode()
let dropZoneSize = CGSize(width: 380, height: 280)
dropZoneNode.size = dropZoneSize
dropZoneNode.texture = SKTexture(imageNamed: dropZones[0].value)
dropZoneNode.texture = SKTexture(imageNamed: dropZoneA.details.value)
dropZoneNode.position = CGPoint(x: size.width / 2, y: dropZoneSize.height / 2)
dropZoneNode.name = dropZones[0].value
dropZoneNode.name = dropZoneA.details.value
addChild(dropZoneNode)

dropZoneNodes.append(dropZoneNode)
dropZoneA.node = dropZoneNode
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,36 @@
// Copyright 2023 APF France handicap
// SPDX-License-Identifier: Apache-2.0

import ContentKit
import SpriteKit
import SwiftUI

final class DragAndDropTwoZonesScene: DragAndDropBaseScene {
override func layoutDropZones(dropZones: DropZoneDetails...) {
// TODO(@hugo): Add type declaration
let dropZoneSpacer = size.width / 4
var dropZonePosition = dropZoneSpacer
for dropZone in dropZones {
let dropZoneNode = SKSpriteNode()
// TODO(@hugo): Adapt size to final images given by the Design team
let dropZoneSize = CGSize(width: 280, height: 280)
dropZoneNode.size = dropZoneSize
dropZoneNode.texture = SKTexture(imageNamed: dropZone.value)
dropZoneNode.position = CGPoint(x: dropZonePosition, y: dropZoneSize.height / 2)
dropZoneNode.name = dropZone.value
addChild(dropZoneNode)

dropZoneNodes.append(dropZoneNode)

dropZonePosition += 2 * dropZoneSpacer
override func layoutDropZones() {
guard let unwrappedDropZoneB = dropZoneB else {
fatalError("No dropZoneB provided")
}

// TODO(@hugo): Add type declaration

Check failure on line 13 in Modules/GameEngineKit/Sources/_NewSystem/Views/DragAndDrop/TwoZones/DragAndDropTwoZonesScene.swift

View workflow job for this annotation

GitHub Actions / lint

TODOs should be resolved ((@hugo): Add type declaration) (todo)
let dropZoneNodeA = SKSpriteNode()
let dropZoneNodeB = SKSpriteNode()

// TODO(@hugo): Adapt size to final images given by the Design team

Check failure on line 17 in Modules/GameEngineKit/Sources/_NewSystem/Views/DragAndDrop/TwoZones/DragAndDropTwoZonesScene.swift

View workflow job for this annotation

GitHub Actions / lint

TODOs should be resolved ((@hugo): Adapt size to final i...) (todo)
let dropZoneSize = CGSize(width: 280, height: 280)
dropZoneNodeA.size = dropZoneSize
dropZoneNodeB.size = dropZoneSize

dropZoneNodeA.texture = SKTexture(imageNamed: dropZoneA.details.value)
dropZoneNodeB.texture = SKTexture(imageNamed: unwrappedDropZoneB.details.value)

let dropZonePosition = size.width / 4
dropZoneNodeA.position = CGPoint(x: dropZonePosition, y: dropZoneSize.height / 2)
dropZoneNodeB.position = CGPoint(x: dropZonePosition * 3, y: dropZoneSize.height / 2)

dropZoneNodeA.name = dropZoneA.details.value
dropZoneNodeB.name = unwrappedDropZoneB.details.value
addChild(dropZoneNodeA)
addChild(dropZoneNodeB)

dropZoneA.node = dropZoneNodeA
dropZoneB?.node = dropZoneNodeB
}
}

0 comments on commit 1f65ca1

Please sign in to comment.