-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomMapPlayground.swift
More file actions
155 lines (139 loc) · 4.36 KB
/
RandomMapPlayground.swift
File metadata and controls
155 lines (139 loc) · 4.36 KB
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
import UIKit
import PlaygroundSupport
import simd
let mapWidth: Int32 = 40
let mapHeight: Int32 = 40
var tileSize: Int = 10
var mapString: String = ""
var mapNodes: [SIMD2<Int32>: String] = [:]
struct Node: Hashable {
let gridPosition: SIMD2<Int32>
}
struct Rectangle {
let start: SIMD2<Int32>
var w: Int32
var h: Int32
}
final class Grid {
func makeRoom(at startPosition: SIMD2<Int32>) {
let roomSize: Int32 = 5
var rectangle = Rectangle(
start: startPosition,
w: (startPosition.x + roomSize),
h: (startPosition.y + roomSize)
)
// Out-of-bounds check. Clips the size of the room
// to the max size of the underlying map
if (startPosition.x + roomSize >= mapWidth) {
let difference = mapWidth - (startPosition.x + roomSize)
rectangle.w -= difference
}
if (startPosition.y + roomSize >= mapHeight) {
let difference = mapHeight - (startPosition.y + roomSize)
rectangle.h -= difference
}
for x in startPosition.x..<rectangle.w {
for y in startPosition.y..<rectangle.h {
let node = Node(gridPosition: SIMD2<Int32>(x,y))
mapNodes.updateValue(".", forKey: node.gridPosition)
}
}
}
// MAKE FLOOR
func makeFloor() {
for y in 0..<mapHeight {
for x in 0..<mapWidth {
let node = Node(gridPosition: SIMD2<Int32>(x,y))
mapNodes.updateValue("#", forKey: node.gridPosition)
}
}
}
// MAKE BOUNDARIES
func makeBoundaries() {
var position = SIMD2<Int32>(0,0)
// Upper boundary
for key in mapNodes.keys {
position.x = key.x
mapNodes[position] = "#"
}
// Lower boundary
for key in mapNodes.keys {
position.x = key.x
position.y = mapHeight - 1
mapNodes[position] = "#"
}
// Left boundary
for key in mapNodes.keys {
position.x = 0
position.y = key.y
mapNodes[position] = "#"
}
// Right boundary
for key in mapNodes.keys {
position.x = mapWidth - 1
position.y = key.y
mapNodes[position] = "#"
}
}
// RENDER NODES
func renderMap() {
for y in 0..<mapHeight {
for x in 0..<mapWidth {
let position = SIMD2<Int32>(x,y)
if let nodeFound = mapNodes[position] {
mapString.append(nodeFound)
}
}
mapString.append("\n")
}
}
// BUILD MAP
func buildBaseMap(numberOfRooms: Int){
makeFloor()
for _ in 0..<numberOfRooms {
let randX = Int32.random(in: 0..<mapWidth)
let randY = Int32.random(in: 0..<mapHeight)
makeRoom(at: SIMD2<Int32>(randX,randY))
}
makeBoundaries()
renderMap()
print("Nodes: \(mapNodes.count)")
print(mapString)
}
}
final class DisplayView: UIView {
convenience init() {
let frame = CGRect(
x: 0,
y: 0,
width: Int(mapWidth) * tileSize,
height: Int(mapHeight) * tileSize
)
self.init(frame: frame)
let grid = Grid()
grid.buildBaseMap(numberOfRooms: 10)
}
override func draw(_ rect: CGRect) {
// Reference to Core Graphics CGContext
let context = UIGraphicsGetCurrentContext()
context?.saveGState()
// Core Graphics & UIView draw() method:
// We're passing the drawing instructions to CGContext, telling it what to draw for every node
for node in mapNodes {
let rectangle = CGRect(
x: Int(node.key.x) * tileSize,
y: Int(node.key.y) * tileSize,
width: tileSize,
height: tileSize
)
let color = node.value == "." ? UIColor.white.cgColor : UIColor.black.cgColor
context?.addRect(rectangle)
context?.setFillColor(color)
context?.fill(rectangle)
}
// Set the CGContext changes, so that they can appear in the view
context?.restoreGState()
}
}
let view = DisplayView()
PlaygroundPage.current.liveView = view