-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameManager.swift
163 lines (131 loc) · 4.68 KB
/
GameManager.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
//
// GameManager.swift
// DropTetris
//
// Created by Kevin Yue on 11/20/14.
// Copyright (c) 2014 Kevin Yue. All rights reserved.
//
import SpriteKit
class GameManager: SKNode {
let marginRatio:CGFloat = 0.1
var mainGrid:MainGrid
var subGrid:SmallGrid
var scoreLabel:SKLabelNode = SKLabelNode()
override init(){
subGrid = SmallGrid()
mainGrid = MainGrid(i: Indicator(occupied: subGrid.occupied()))
super.init()
addChild(mainGrid)
addChild(subGrid)
addScoreLabel()
Score.resetScore()
Difficulty.resetDifficulty()
}
/////////////////////TOUCH HANDLING////////////////////////
func newTouch(touch:UITouch){
var loc = touch.locationInNode(self)
TouchManager.touchBegan(touch.hash, loc: loc)
}
func movedTouch(touch:UITouch){
var loc = touch.locationInNode(self)
TouchManager.touchMoved(touch.hash, loc: loc)
}
func touchEnded(touch:UITouch){
var loc:CGPoint = touch.locationInNode(self)
TouchManager.touchEnded(touch.hash, loc: loc)
parseGesture()
}
private func getTouchedSquare(loc:CGPoint) -> Coordinate{
return mainGrid.getTouchedSq(loc)
}
private func parseGesture(){
var g:Gesture = TouchManager.popGesture()
switch g.type{
case GestureType.CLICK:
parseClick(g)
case GestureType.SWIPE:
parseSwipe(g)
case GestureType.ROTATE:
parseRotate(g)
default:
return
}
}
private func parseClick(g:Gesture){
if(subGrid.frame.contains(g.loc)){} //TODO: Send click to subGrid: Rotate subGrid, rotate mainGrid.indicator
else if(mainGrid.frame.contains(g.loc)){} //TODO: Send click to mainGrid: move Indicator, drop if there already
else{} //TODO: Nothing?
}
private func parseSwipe(g:Gesture){
//TODO: Send click to mainGrid, move Indicator
}
private func parseRotate(g:Gesture){
//TODO: Send click to subGrid: Rotate subGrid, rotate mainGrid.indicator
}
//IMPORTANT TODO NOTE!!!
// There are actually only 3 cases, rotate, translate, and drop. You can design accordingly
private func attemptTransferTo(coord:Coordinate){
if(canTransferTo(coord)){
transferTo(coord)
subGrid.populate()
clearRowsAndCols()
Difficulty.increaseSpawnProb()
mainGrid.newIndicator(Indicator(occupied: subGrid.occupied()))
}
}
private func clearRowsAndCols(){
var toClear:Array<GridSq> = mainGrid.getSqToClear()
Score.incrementScore(toClear.count * toClear.count)
mainGrid.clearSquares(toClear)
}
func hasValidMove() -> Bool{
for(var i = 0; i<4; i++){
for coord:Coordinate in mainGrid.sqDict.keys{
if(canTransferTo(coord)){
return true
}
}
subGrid.rotate(Rotation.CW)
}
return false
}
private func canTransferTo(coord:Coordinate) -> Bool{
for sq:GridSq in subGrid.getAllOccupied(){
if(!mainGrid.canPlace(sq.coord + coord)){return false}
}
return true
}
private func transferTo(coord: Coordinate){
for sq:GridSq in subGrid.getAllOccupied(){
movePiece(sq.coord, mainGridCoord: sq.coord + coord)
}
}
private func movePiece(subGridCoord: Coordinate, mainGridCoord:Coordinate){
var p:Piece = subGrid.pieceAtSq(subGridCoord)
subGrid.clearSq(subGridCoord)
mainGrid.addPiece(p, coord: mainGridCoord)
}
func tick(){
mainGrid.tick()
subGrid.tick()
updateScore()
updateDifficulty()
}
private func addScoreLabel(){
scoreLabel = SKLabelNode(fontNamed:Constants.font)
scoreLabel.fontSize = Constants.labelFontSize
scoreLabel.position = CGPoint.bottomRight + convertRatioToSize(CGVector(dx: marginRatio / Layout.widthToHeight, dy: marginRatio).half()).toPoint() * CGVector(dx: -1, dy: 1)
scoreLabel.horizontalAlignmentMode = SKLabelHorizontalAlignmentMode.Right
scoreLabel.fontColor = Constants.labelFontColor
addChild(scoreLabel)
}
func updateScore(){
scoreLabel.text = Score.getScore()
}
func updateDifficulty(){
Difficulty.update(Double(mainGrid.occupied().count) / mainGrid.dims.numCols / mainGrid.dims.numRows);
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}