From c948ecd587e40cec4e9e9bf8e895b2c33442d74a Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Tue, 15 Oct 2019 17:26:06 -0400 Subject: [PATCH] Tweak movement gesture --- Tetris/ContentView.swift | 27 ++++++++++----------------- TetrisKit/GamePiece.swift | 2 +- 2 files changed, 11 insertions(+), 18 deletions(-) diff --git a/Tetris/ContentView.swift b/Tetris/ContentView.swift index 386101e..defbd56 100644 --- a/Tetris/ContentView.swift +++ b/Tetris/ContentView.swift @@ -17,7 +17,7 @@ struct ContentView: View { return c }() @State var timer: Timer? - @State var prevHorizTranslation: CGFloat? + @State var initialXPosition: Int? var body: some View { GeometryReader { (geometry) in @@ -27,7 +27,7 @@ struct ContentView: View { .onAppear(perform: self.startTimer) .onDisappear(perform: self.stopTimer) .onTapGesture(perform: self.onTap) - .gesture(self.horizDragGesture(geometry: geometry)) + .gesture(self.horizDragGesture(geometry: geometry).simultaneously(with: self.verticalDragGesture)) // .gesture(ExclusiveGesture(horizDragGesture, verticalDragGesture)) // .gesture(horizDragGesture.simultaneously(with: verticalDragGesture)) @@ -57,32 +57,25 @@ struct ContentView: View { DragGesture(coordinateSpace: .global) .onChanged { (state) in guard let currentPiece = self.controller.currentPiece else { return } -// let position = (state.location.x) / geometry.size.width * 10 -// let moved = currentPiece.moved(by: (Int(position.rounded()) - currentPiece.topLeft.0, 0)) -// if !self.controller.overlapsAny(moved) { -// self.controller.currentPiece = moved -// } - if self.prevHorizTranslation == nil { - self.prevHorizTranslation = state.translation.width + if self.initialXPosition == nil { + self.initialXPosition = currentPiece.topLeft.0 } - let delta = state.translation.width - self.prevHorizTranslation! - let amount = Int((delta / 20).rounded()) - let moved = currentPiece.moved(by: (amount, 0)) + var moved = currentPiece + let xPosition = self.initialXPosition! + Int((state.translation.width / (geometry.size.width / 10)).rounded()) + moved.topLeft = (xPosition, currentPiece.topLeft.1) if !self.controller.overlapsAny(moved) { self.controller.currentPiece = moved - self.prevHorizTranslation = state.translation.width } }.onEnded { (state) in - self.prevHorizTranslation = nil + self.initialXPosition = nil } } var verticalDragGesture: some Gesture { DragGesture() .onEnded { (state) in - let deltaY = state.location.y - state.startLocation.y - if abs(deltaY) > 40 { - if deltaY > 0 { + if abs(state.translation.height) > 80 { + if state.translation.height > 0 { self.onSwipeDown() } else { self.onSwipeUp() diff --git a/TetrisKit/GamePiece.swift b/TetrisKit/GamePiece.swift index 45f287d..8484dbf 100644 --- a/TetrisKit/GamePiece.swift +++ b/TetrisKit/GamePiece.swift @@ -10,7 +10,7 @@ import Foundation public struct GamePiece { public let tetromino: Tetromino - public internal(set) var topLeft: (Int, Int) + public var topLeft: (Int, Int) public internal(set) var tiles: [[Bool]] public init(tetromino: Tetromino) {