From 1b9e697dd8913925c5fc4d2998fe64d69f34cee8 Mon Sep 17 00:00:00 2001 From: Lisergishnu Date: Mon, 10 Oct 2016 23:47:00 +0200 Subject: [PATCH] Now you can place and remove pieces from the board --- CHANGELOG.md | 10 ++++---- TetraVex/PieceView.swift | 37 +++++++++++++++++------------ TetraVex/TVGameViewController.swift | 9 +++++++ TetraVexKit/PieceModel.swift | 2 +- 4 files changed, 38 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e44e9d9..5607a25 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,9 +1,11 @@ # CHANGELOG -Here I will update any noteworthy changes to the TetraVex project +Here I will update any noteworthy changes to the TetraVex project. ## [Unreleased] ### Added -- Pretty basic board and pieces. -- Game allows for board sizes from 2x2 to 6x6 -- You can select the set of digits from 6 to 10 +- Pretty basic board and pieces.. +- Game allows for board sizes from 2x2 to 6x6. +- You can select the set of digits from 6 to 10. +- You can play without timer or score, but the board will only let you place pieces +correctly. diff --git a/TetraVex/PieceView.swift b/TetraVex/PieceView.swift index cfc41e6..fff8713 100644 --- a/TetraVex/PieceView.swift +++ b/TetraVex/PieceView.swift @@ -17,16 +17,21 @@ class PieceView : NSView { // MARK: - Dragging operations override func mouseDown(with event: NSEvent) { - isBeingDragged = true - lastDraggedPosition = self.convert(event.locationInWindow, to: self) - let i = superview?.subviews.index(of: self) - var svs = superview!.subviews - if (i != svs.count-1) { - swap(&svs[i!],&svs[svs.count-1]) - superview?.subviews = svs + if (pieceModel?.boltedInPlace)! == false { + isBeingDragged = true + lastDraggedPosition = self.convert(event.locationInWindow, to: self) + let i = superview?.subviews.index(of: self) + var svs = superview!.subviews + if (i != svs.count-1) { + swap(&svs[i!],&svs[svs.count-1]) + superview?.subviews = svs + } + if pieceModel!.isOnBoard { + controller?.removeFromBoard(piece: self) + } + NSCursor.closedHand().push() + self.needsDisplay = true } - NSCursor.closedHand().push() - self.needsDisplay = true } func offsetLocation(by dx:CGFloat, dy:CGFloat) { @@ -35,7 +40,7 @@ class PieceView : NSView { self.frame = self.frame.offsetBy(dx: dx, dy: dy*invertDeltaY) self.setNeedsDisplay(self.bounds) } - + override func mouseDragged(with event: NSEvent) { if (isBeingDragged) { let p : NSPoint = self.convert(event.locationInWindow, to: self) @@ -46,11 +51,13 @@ class PieceView : NSView { } override func mouseUp(with event: NSEvent) { - let dropPosition : NSPoint = superview!.convert(event.locationInWindow, to: nil) - controller?.checkPiece(with: self, at: dropPosition) - isBeingDragged = false - NSCursor.pop() - self.needsDisplay = true + if isBeingDragged { + let dropPosition : NSPoint = superview!.convert(event.locationInWindow, to: nil) + controller?.checkPiece(with: self, at: dropPosition) + isBeingDragged = false + NSCursor.pop() + self.needsDisplay = true + } } // MARK: - Drawing operations diff --git a/TetraVex/TVGameViewController.swift b/TetraVex/TVGameViewController.swift index f728980..8741f87 100644 --- a/TetraVex/TVGameViewController.swift +++ b/TetraVex/TVGameViewController.swift @@ -66,6 +66,14 @@ class TVGameViewController: NSViewController { } } + func removeFromBoard(piece p:PieceView) -> Bool { + if boardModel!.removePieceFromBoard(p.pieceModel!) { + p.pieceModel!.isOnBoard = false + return true + } + return false + } + func checkPiece(with pv:PieceView,at dropOffPosition:NSPoint) { /* Determine the position of view inside the grid */ if boardAreaBox.frame.contains(dropOffPosition) { @@ -75,6 +83,7 @@ class TVGameViewController: NSViewController { if boardModel!.addPieceToBoard(pv.pieceModel!, x: i, y: j) { pv.frame.origin.x = CGFloat(i)*pv.frame.width + boardAreaBox.frame.origin.x pv.frame.origin.y = CGFloat(j)*pv.frame.height + boardAreaBox.frame.origin.y + pv.pieceModel?.isOnBoard = true } else { pv.frame.origin.x = templatePieceView.frame.origin.x + CGFloat(Int.random(0...100)) pv.frame.origin.y = templatePieceView.frame.origin.y - CGFloat(Int.random(0...100)) diff --git a/TetraVexKit/PieceModel.swift b/TetraVexKit/PieceModel.swift index b3f6f11..0f15cd3 100644 --- a/TetraVexKit/PieceModel.swift +++ b/TetraVexKit/PieceModel.swift @@ -16,7 +16,7 @@ public struct PieceModel { public var bottomValue : Int public var rightValue : Int public var boltedInPlace : Bool = false - + public var isOnBoard : Bool = false // MARK: - Initializer public init(top: Int, left: Int, bottom: Int, right: Int) {