-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainGrid.swift
166 lines (135 loc) · 4.17 KB
/
MainGrid.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
//
// MainGrid.swift
// DropTetris
//
// Created by Kevin Yue on 12/1/14.
// Copyright (c) 2014 Kevin Yue. All rights reserved.
//
import Foundation
import SpriteKit
class MainGrid: Grid {
let myGridDims:GridDimension
private var indicator:Indicator
convenience init(){
var i = Indicator(occupied: Array<Coordinate>())
self.init(i: i)
}
init(i:Indicator){
indicator = i
let mainGridSize = Layout.gridSqSize * Rules.mainGridN
myGridDims = GridDimension(numRows: Rules.mainGridN, numCols: Rules.mainGridN,
gridSize: mainGridSize,
origin: (mainGridSize.toVector().half() + Layout.screenMarginSize.toVector()).toPoint());
super.init(d: myGridDims)
updateIndicator()
}
func getTouchedSq(loc:CGPoint) -> Coordinate{
for sq : GridSq in sqDict.values{
if(CGRectContainsPoint(sq.frame, loc)){return sq.coord}
}
return Coordinate.null
}
func canPlace(coord:Coordinate) -> Bool{
if(sqDict[coord] == nil){return false}
var sq:GridSq = sqDict[coord]!
if(sq.occupied()){return false}
return true
}
func canPlace(sq: GridSq) -> Bool{
if(sq.occupied()){return false}
if(sqDict[sq.coord] == nil){return false}
return true
}
func getSqToClear() -> Array<GridSq>{
var toClear:Array<GridSq> = Array<GridSq>()
for(var x = dims.colMin(); x <= dims.colMax(); x++){
var col:Array<GridSq> = getCol(x)
if (isFull(col)){toClear.extend(col)}
}
for(var y:Int = dims.colMin(); y <= dims.colMax(); y++){
var row:Array<GridSq> = getRow(y)
if(isFull(row)){toClear.extend(row)}
}
return toClear
}
private func getRow(y:Int) -> Array<GridSq>{
var a:Array<GridSq> = Array<GridSq>()
for sq:GridSq in sqDict.values{
if(sq.coord.y == y){a.append(sq)}
}
return a;
}
private func getCol(x:Int) -> Array<GridSq>{
var a:Array<GridSq> = Array<GridSq>()
for sq:GridSq in sqDict.values{
if(sq.coord.x == x){a.append(sq)}
}
return a;
}
private func isFull(a:Array<GridSq>) -> Bool{
for sq:GridSq in a{
if(!sq.occupied()){return false}
}
return true;
}
func clearSquares(sqA:Array<GridSq>){
for sq:GridSq in sqA{
sq.removePiece()
sq.changeColor()
}
}
/////////////////////INDICATOR FUNCTIONS///////////////////
func newIndicator(i:Indicator){
indicator = i
updateIndicator()
}
func moveIndicator(loc:CGPoint){
indicator.moveTo(getTouchedSq(loc))
updateIndicator()
}
func moveIndicator(d:Direction){
indicator.moveTo(d)
updateIndicator()
}
func newIndicatorContents(c:Array<Coordinate>){
indicator.newContents(c)
updateIndicator()
}
func newIndicatorContents(c:Array<GridSq>){
indicator.newContents(c)
updateIndicator()
}
private func updateIndicator(){
removeAllIndicators()
addIndicators()
}
private func removeAllIndicators(){
for sq:GridSq in sqDict.values{
sq.removeIndicator()
}
}
private func addIndicators(){
if(indicator.indicatorInBounds(myGridDims.colMin(),
xMax: myGridDims.colMax(),
yMin: myGridDims.rowMin(),
yMax: myGridDims.rowMax())){
addValidIndicator()
}
else{
addInvalidIndicator()
}
}
private func addValidIndicator(){
for c:Coordinate in indicator.getContents(){
if let sq = sqDict[c]{sq.addIndicator()}
}
}
private func addInvalidIndicator(){
for c:Coordinate in indicator.getContents(){
if let sq = sqDict[c]{sq.addInvalidIndicator()}
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}