Skip to content

Commit

Permalink
Now you can place and remove pieces from the board
Browse files Browse the repository at this point in the history
  • Loading branch information
Lisergishnu committed Oct 10, 2016
1 parent fcc8dda commit 1b9e697
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 20 deletions.
10 changes: 6 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.

37 changes: 22 additions & 15 deletions TetraVex/PieceView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
Expand All @@ -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
Expand Down
9 changes: 9 additions & 0 deletions TetraVex/TVGameViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion TetraVexKit/PieceModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 1b9e697

Please sign in to comment.