Skip to content

Commit

Permalink
Merge pull request #201 from V1taS/feature/Artem_Pavlov/Add_a_tactile…
Browse files Browse the repository at this point in the history
…_response_when_the_cubes_land
  • Loading branch information
V1taS authored Sep 16, 2023
2 parents 7d80cdf + 0262eb4 commit ab68e3c
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ private extension CoinScreenView {

@objc
func generateButtonAction() {
output?.playHapticFeedbackAction()
output?.generateButtonAction()
resultLabel.text = ""
coinView.handleTap()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ protocol CubesScreenInteractorInput {
/// Кубики были подкинуты
/// - Parameter totalValue: Сумма всех кубиков
func diceAction(totalValue: Int)

/// Запустить обратную связь от моторчика
func playHapticFeedback()

/// Показать список генераций результатов
/// - Parameter isShow: показать список генераций результатов
Expand All @@ -52,8 +55,9 @@ final class CubesScreenInteractor: CubesScreenInteractorInput {

// MARK: - Private property

private var storageService: StorageService
private let storageService: StorageService
private let buttonCounterService: ButtonCounterService
private let hapticService: HapticService
private var cubesScreenModel: CubesScreenModel? {
get {
storageService.getData(from: CubesScreenModel.self)
Expand All @@ -69,6 +73,7 @@ final class CubesScreenInteractor: CubesScreenInteractorInput {
init(services: ApplicationServices) {
storageService = services.storageService
buttonCounterService = services.buttonCounterService
hapticService = services.hapticService
}

// MARK: - Internal func
Expand Down Expand Up @@ -139,6 +144,14 @@ final class CubesScreenInteractor: CubesScreenInteractorInput {
getContent()
output?.cleanButtonWasSelected()
}

func playHapticFeedback() {
DispatchQueue.main.async { [weak self] in
self?.hapticService.play(isRepeat: false,
patternType: .splash,
completion: {_ in })
}
}
}

// MARK: - Appearance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ final class CubesScreenViewController: CubesScreenModule {
// MARK: - CubesScreenViewOutput

extension CubesScreenViewController: CubesScreenViewOutput {
func playHapticFeedbackAction() {
interactor.playHapticFeedback()
}

func updateSelectedCountCubes(_ cubesType: CubesScreenModel.CubesType) {
interactor.updateSelectedCountCubes(cubesType)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ protocol CubesScreenViewOutput: AnyObject {
/// Кубики были подкинуты
/// - Parameter totalValue: Сумма всех кубиков
func diceAction(totalValue: Int)

/// Пользователь нажал на кнопку генерации
func playHapticFeedbackAction()
}

/// События которые отправляем от Presenter ко View
Expand Down Expand Up @@ -72,7 +75,7 @@ final class CubesScreenView: CubesScreenViewProtocol {
func updateContentWith(cubesType: CubesScreenModel.CubesType) {
cubesSegmentedControl.selectedSegmentIndex = cubesType.rawValue
cubesView.updateCubesWith(type: cubesType)
setButtinTitle()
setButtonTitle()
}

func updateContentWith(listResult: [String]) {
Expand Down Expand Up @@ -128,6 +131,10 @@ private extension CubesScreenView {
self.counter = .zero
}
}

cubesView.feedbackGeneratorAction = { [weak self] in
self?.output?.playHapticFeedbackAction()
}
}

func setupConstraints() {
Expand Down Expand Up @@ -165,7 +172,7 @@ private extension CubesScreenView {
])
}

func setButtinTitle() {
func setButtonTitle() {
let appearance = Appearance()
let cubeType = CubesScreenModel.CubesType(rawValue: cubesSegmentedControl.selectedSegmentIndex) ?? .cubesTwo
let buttonTitle = cubeType == .cubesOne ? appearance.buttonOneCubeTitle : appearance.buttonSomeCubeTitle
Expand All @@ -183,7 +190,7 @@ private extension CubesScreenView {

output?.updateSelectedCountCubes(cubeType)
cubesView.updateCubesWith(type: cubeType)
setButtinTitle()
setButtonTitle()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ final class CubesView: UIView {
// MARK: - Internal properties

var totalValueDiceAction: ((Int) -> Void)?
var feedbackGeneratorAction: (() -> Void)?

// MARK: - Private properties

Expand Down Expand Up @@ -60,6 +61,27 @@ final class CubesView: UIView {
}
}

// MARK: - SCNPhysicsContactDelegate

extension CubesView: SCNPhysicsContactDelegate {
func physicsWorld(_ world: SCNPhysicsWorld, didBegin contact: SCNPhysicsContact) {
let appearance = Appearance()

if (contact.nodeA.name == appearance.cubesNodeName && contact.nodeB.name == appearance.positionFloor) ||
(contact.nodeA.name == appearance.positionFloor && contact.nodeB.name == appearance.cubesNodeName) ||
(contact.nodeA.name == appearance.cubesNodeName && contact.nodeB.name == appearance.positionLeft) ||
(contact.nodeA.name == appearance.positionLeft && contact.nodeB.name == appearance.cubesNodeName) ||
(contact.nodeA.name == appearance.cubesNodeName && contact.nodeB.name == appearance.positionRight) ||
(contact.nodeA.name == appearance.positionRight && contact.nodeB.name == appearance.cubesNodeName) ||
(contact.nodeA.name == appearance.cubesNodeName && contact.nodeB.name == appearance.positionFront) ||
(contact.nodeA.name == appearance.positionFront && contact.nodeB.name == appearance.cubesNodeName) ||
(contact.nodeA.name == appearance.cubesNodeName && contact.nodeB.name == appearance.positionBack) ||
(contact.nodeA.name == appearance.positionBack && contact.nodeB.name == appearance.cubesNodeName) {
feedbackGeneratorAction?()
}
}
}

// MARK: - SCNSceneRendererDelegate

extension CubesView: SCNSceneRendererDelegate {
Expand Down Expand Up @@ -99,10 +121,10 @@ private extension SCNVector3 {

private extension CubesView {
func randomPosition() -> SCNVector3 {
let x = Float.random(in: -5...5)
let y = Float.random(in: 0...10)
let z = Float.random(in: -5...5)
return SCNVector3(x, y, z)
let x = Float.random(in: -5...5)
let y = Float.random(in: 0...10)
let z = Float.random(in: -5...5)
return SCNVector3(x, y, z)
}

func reposition(_ node: SCNNode, to position: SCNVector3, with normal: SCNVector3) {
Expand Down Expand Up @@ -132,12 +154,14 @@ private extension CubesView {
func wall(at position: SCNVector3,
with normal: SCNVector3,
sized size: CGSize,
name: String,
color: UIColor = .clear) -> SCNNode {
let geometry = SCNPlane(width: size.width, height: size.height)
geometry.materials.first?.diffuse.contents = color
geometry.materials.first?.isDoubleSided = true

let geometryNode = SCNNode(geometry: geometry)
geometryNode.name = name
geometryNode.physicsBody = SCNPhysicsBody(type: .static, shape: nil)
geometryNode.physicsBody?.collisionBitMask = 1
geometryNode.physicsBody?.contactTestBitMask = 1
Expand Down Expand Up @@ -229,6 +253,7 @@ private extension CubesView {
}

func setupScene() {
let appearance = Appearance()
scnView.scene = scnScene
setupCamera()
setupLight()
Expand All @@ -238,21 +263,33 @@ private extension CubesView {

let walls = [
// верхняя стена X,Y,Z
(position: SCNVector3(0, 13, 0), normal: SCNVector3Make(0, -1, 0)),
(position: SCNVector3(0, 13, 0),
normal: SCNVector3Make(0, -1, 0),
name: appearance.positionTop),
// нижняя стена X,Y,Z
(position: SCNVector3(0, -8, 0), normal: SCNVector3Make(0, 1, 0)),
(position: SCNVector3(0, -8, 0),
normal: SCNVector3Make(0, 1, 0),
name: appearance.positionFloor),
// правая стена X,Y,Z
(position: SCNVector3(7, -8, 0), normal: SCNVector3Make(-1, 0, 0)),
(position: SCNVector3(7, -8, 0),
normal: SCNVector3Make(-1, 0, 0),
name: appearance.positionRight),
// левая стена X,Y,Z
(position: SCNVector3(-7, -8, 0), normal: SCNVector3Make(1, 0, 0)),
(position: SCNVector3(-7, -8, 0),
normal: SCNVector3Make(1, 0, 0),
name: appearance.positionLeft),
// задняя стена X,Y,Z
(position: SCNVector3(0, -8, 11), normal: SCNVector3Make(0, 0, -1)),
(position: SCNVector3(0, -8, 11),
normal: SCNVector3Make(0, 0, -1),
name: appearance.positionBack),
// передняя стена X,Y,Z
(position: SCNVector3(0, -8, -11), normal: SCNVector3Make(0, 0, 1))
(position: SCNVector3(0, -8, -11),
normal: SCNVector3Make(0, 0, 1),
name: appearance.positionFront)
]

for wall in walls {
let panel = self.wall(at: wall.position, with: wall.normal, sized: wallSize)
let panel = self.wall(at: wall.position, with: wall.normal, sized: wallSize, name: wall.name)
scnScene.rootNode.addChildNode(panel)
}
}
Expand Down Expand Up @@ -407,5 +444,14 @@ private extension CubesView {
let numberFour: Int = 4
let numberFive: Int = 5
let numberSix: Int = 6

let positionTop = "Top"
let positionFloor = "Floor"
let positionRight = "Right"
let positionLeft = "Left"
let positionBack = "Back"
let positionFront = "Front"

let cubesNodeName = "Cube"
}
}

0 comments on commit ab68e3c

Please sign in to comment.