|
2 | 2 | import UIKit |
3 | 3 | import XCPlayground |
4 | 4 |
|
5 | | -public class BoardView: UIView { |
6 | | - private let board: Board |
7 | | - public let gridSize: CGFloat |
8 | | - |
9 | | - public init(board: Board, gridSize: CGFloat = 20) { |
10 | | - self.board = board |
11 | | - self.gridSize = gridSize |
12 | | - super.init(frame: board.sizeForGridSize(gridSize)) |
13 | | - let gridLayer = CAShapeLayer() |
14 | | - gridLayer.frame = bounds |
15 | | - gridLayer.fillColor = UIColor.whiteColor().CGColor |
16 | | - gridLayer.strokeColor = UIColor.lightGrayColor().CGColor |
17 | | - layer.addSublayer(gridLayer) |
18 | | - gridLayer.path = boardPath() |
19 | | - } |
20 | | - |
21 | | - private func boardPath() -> CGPath { |
22 | | - |
23 | | - let playableSquares = board.squares().filter { $0.occupied == false } |
24 | | - let rects : [CGRect] = playableSquares.map { square in |
25 | | - let originX = CGFloat(square.column) * gridSize |
26 | | - let originY = CGFloat(square.row) * gridSize |
27 | | - return CGRect(x: originX, y: originY, width: gridSize, height: gridSize) |
28 | | - } |
29 | | - |
30 | | - let paths : [UIBezierPath] = rects.map { |
31 | | - return UIBezierPath(rect: $0) |
32 | | - } |
33 | | - let path = UIBezierPath() |
34 | | - paths.forEach { path.appendPath($0) } |
35 | | - return path.CGPath |
36 | | - } |
37 | | - |
38 | | - required public init?(coder aDecoder: NSCoder) { |
39 | | - fatalError("init(coder:) has not been implemented") |
40 | | - } |
41 | | -} |
42 | | - |
43 | | -let tileView = TileView(tile: Tile(shape: .X)) |
| 5 | +let tileView = TileView(tile: Tile(shape: .Z), color:.purpleColor(), gridSize: 30) |
| 6 | +tileView.lifted = true |
44 | 7 | let board = Board(size: .SixByTen) |
45 | | -let boardView = BoardView(board: board) |
| 8 | +let boardView = BoardView(board: board, gridSize: 30) |
46 | 9 | let shapes = (0..<11).map { Shape(rawValue:$0)! } |
47 | 10 | let tiles = shapes.map { Tile(shape: $0) } |
48 | 11 | for tile in tiles { |
49 | 12 | for square in board.squares() { |
50 | 13 | if board.canPositionTile(tile, atSquare: square) { |
51 | 14 | board.positionTile(tile, atSquare: square) |
52 | | - let tileView = TileView(tile: tile,color: randomColor()) |
| 15 | + let tileView = TileView(tile: tile,color: randomColor(), gridSize: boardView.gridSize) |
53 | 16 | boardView.addSubview(tileView) |
54 | 17 | tileView.frame.origin = board.pointAtOriginOfSquare(square, gridSize: boardView.gridSize) |
55 | 18 | break |
|
0 commit comments