-
Notifications
You must be signed in to change notification settings - Fork 0
/
World.swift
135 lines (121 loc) · 4.13 KB
/
World.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
//
// World.swift
// SwiftSnake
//
// Created by Chris on 6/25/14.
// Copyright (c) 2014 ChrisCombs. All rights reserved.
//
import UIKit
import QuartzCore
let gridColor = UIColor(red: 0, green: 1, blue: 0, alpha: 0.3)
protocol WorldDelegate {
func snakeDidEatApple()
func snakeDidHitEdge()
}
class World : UIView {
var linesArray:[UIView] = []
var shownSquares: [WorldSquare] = []
var snake = Snake()
var currentApple = Apple()
var delegate:WorldDelegate?
init() {
super.init(frame: CGRectZero)
layer.borderColor = UIColor.greenColor().CGColor
layer.borderWidth = 2.0
createLines()
updateSquareViews()
placeNewApple()
shownSquares += currentApple
}
func createLines() {
// Vertical lines
for index in 0..<gridSize {
var lineView:UIView = UIView(frame: CGRectZero)
lineView.frame = CGRectMake(0, 0, 1, 0)
lineView.backgroundColor = gridColor
linesArray += lineView
self.addSubview(lineView)
}
// Horizontal lines
for index in 0..<gridSize {
var lineView:UIView = UIView(frame: CGRectZero)
lineView.frame = CGRectMake(0, 0, 0, 1)
lineView.backgroundColor = gridColor
linesArray += lineView
self.addSubview(lineView)
}
}
override func layoutSubviews() {
super.layoutSubviews()
let fraction = self.frame.size.width/Double(gridSize)
// Vertical
for index in 1..<gridSize {
var view = linesArray[index-1]
view.frame.size.height = self.frame.size.height
view.frame.origin.x = fraction * Double(index)
}
// Horizontal
for index in gridSize..<gridSize*2 {
var view = linesArray[index-1]
view.frame.size.width = self.frame.size.width
view.frame.origin.y = fraction * Double(index-gridSize)
}
for square in shownSquares {
let (coordX, coordY) = square.coordinates
square.view.frame = CGRectMake(Double(coordX)*fraction, Double(coordY)*fraction, fraction, fraction)
}
}
func updateSquareViews() {
// Remove all existing squares
for square in shownSquares {
square.view.removeFromSuperview()
}
shownSquares = []
// Re-add the apple
shownSquares += currentApple
var view = Apple.genericView()
addSubview(view)
currentApple.view = view
let (x, y) = snake.tailSquares[0].coordinates
for index in 1..<snake.tailSquares.count-1 { // Check to see if snake is colliding with itself
println(snake.tailSquares.count-1)
let (tailX, tailY) = snake.tailSquares[index].coordinates
if (x == tailX && y == tailY) {
gameOver()
return
}
}
let (appleX, appleY) = currentApple.coordinates
if x < 0 || y < 0 || x >= gridSize || y >= gridSize { // Check to see if snake is off the grid
gameOver()
return
}
else if x == appleX && y == appleY { // Check to see if snake is at apple
snake.addLength()
delegate?.snakeDidEatApple()
placeNewApple()
}
// Draw the snake
for square in snake.tailSquares {
var view = WorldSquare.genericView()
addSubview(view)
square.view = view
shownSquares += square
}
}
func placeNewApple() {
let (randomX, randomY) = (Int(arc4random_uniform(UInt32(gridSize))), Int(arc4random_uniform(UInt32(gridSize))))
// Check to make sure you arent placing the apple on a part of the snake
for square in snake.tailSquares {
let (x, y) = square.coordinates
if randomX == x && randomY == y {
println("BREAK") // TODO
}
}
currentApple.coordinates = (randomX, randomY)
}
func gameOver() {
delegate?.snakeDidHitEdge()
snake = Snake()
}
}