Skip to content

Commit 7f186c4

Browse files
author
Oleksandr Glagoliev
committed
Improve SK Demo
1 parent 93338ca commit 7f186c4

File tree

1 file changed

+23
-54
lines changed

1 file changed

+23
-54
lines changed

SpriteKitDemo/SpriteKitDemo/GameScene.swift

Lines changed: 23 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,13 @@ class GameScene: SKScene {
1818
var delaunayDiagramNode = SKShapeNode()
1919

2020
private var clippingRect: Rectangle {
21-
Rectangle(
22-
origin: Site(x: 50, y: 50),
23-
size: Size(width: Double(frame.width) - 100, height: Double(frame.height) - 100)
21+
let padding: Double = 50
22+
return Rectangle(
23+
origin: Site(x: padding, y: padding),
24+
size: Size(
25+
width: Double(frame.width) - padding * 2,
26+
height: Double(frame.height) - padding * 2
27+
)
2428
)
2529
}
2630

@@ -42,34 +46,27 @@ class GameScene: SKScene {
4246

4347
let numRepeats = 1
4448
let radius: CGFloat = 0
49+
let unitPadding: CGFloat = 4
4550
for i in 0..<numRepeats {
46-
let paddedHull = paddedPolygon(hullVertices, padding: CGFloat(-i) * 10)
51+
let paddedHull = paddedPolygon(hullVertices, padding: CGFloat(-i) * unitPadding)
4752
if let path = UIBezierPath.roundedCornersPath(paddedHull, radius) {
4853
voronoiPath.append(path)
4954
}
5055
}
5156

52-
// let centroid = polygonCentroid(hullVertices)
53-
//
54-
// for vtx in hullVertices {
55-
// voronoiPath.move(to: centroid)
56-
// voronoiPath.addLine(to: vtx)
57-
// }
58-
59-
6057
for n in cell.neighbours() {
6158
delaunayPath.move(to: cell.site.cgPoint)
6259
delaunayPath.addLine(to: n.site.cgPoint)
6360
}
6461
}
6562
voronoiDiagramNode.path = voronoiPath.cgPath
6663
voronoiDiagramNode.strokeColor = UIColor.black
67-
voronoiDiagramNode.lineWidth = 3.0
64+
voronoiDiagramNode.lineWidth = 2.0
6865
voronoiDiagramNode.fillColor = .clear
6966

7067
delaunayDiagramNode.path = delaunayPath.cgPath
7168
delaunayDiagramNode.strokeColor = UIColor.green
72-
delaunayDiagramNode.lineWidth = 1.0
69+
delaunayDiagramNode.lineWidth = 2.0
7370
delaunayDiagramNode.fillColor = .clear
7471
}
7572

@@ -82,23 +79,21 @@ class GameScene: SKScene {
8279
let lby = offset
8380
let ubx = Double(view.bounds.width) - 2 * offset
8481
let uby = Double(view.bounds.height) - 2 * offset
85-
let randomPoints = randomSites(50, xRange: lbx..<ubx, yRange: lby..<uby)
86-
let r: CGFloat = 20
82+
let randomPoints = randomSites(30, xRange: lbx..<ubx, yRange: lby..<uby)
83+
let r: CGFloat = 10
8784
balls = randomPoints.map {
88-
let point = SKShapeNode(circleOfRadius: r)
89-
point.position = $0.cgPoint
90-
point.fillColor = .red
85+
let node = SKShapeNode(circleOfRadius: r)
86+
node.position = $0.cgPoint
87+
node.fillColor = .red
9188

9289
let body = SKPhysicsBody(circleOfRadius: r)
9390
body.affectedByGravity = false
94-
body.linearDamping = 0.9
95-
body.mass = 2.0
9691

97-
point.physicsBody = body
98-
point.isHidden = false
92+
node.physicsBody = body
93+
node.isHidden = false
9994

100-
addChild(point)
101-
return point
95+
addChild(node)
96+
return node
10297
}
10398

10499
redraw(Set<Site>(balls.map { $0.position.point }))
@@ -127,44 +122,18 @@ class GameScene: SKScene {
127122
return set
128123
}
129124

130-
131-
var hexLike: [Site] {
132-
let num = 5
133-
let step: Double = 50
134-
var res = [Site]()
135-
for i in 0..<num {
136-
for j in 0..<3 {
137-
res.append(
138-
Site(
139-
x:Double(i) * step + Double(j) * step / 2 + 200,
140-
y:Double(j) * step + 200
141-
)
142-
)
143-
}
144-
}
145-
return res
146-
}
147-
148125
func touchDown(atPoint pos : CGPoint) {
149126
}
150127

151128
func touchMoved(toPoint pos : CGPoint) {
152129
let spriteAtPoint = atPoint(pos)
153-
if spriteAtPoint != voronoiDiagramNode {
154-
spriteAtPoint.position = CGPoint(x: pos.x.rounded(), y: pos.y.rounded())
155-
156-
}
130+
if spriteAtPoint == voronoiDiagramNode || spriteAtPoint == delaunayDiagramNode { return }
131+
spriteAtPoint.position = CGPoint(x: pos.x.rounded(), y: pos.y.rounded())
157132
}
158133

159134
func touchUp(atPoint pos : CGPoint) {
160135
let ball = atPoint(pos)
161-
if ball == voronoiDiagramNode { return }
162-
163-
let force = CGVector(
164-
dx: CGFloat.random(in: -300...300),
165-
dy: CGFloat.random(in: -300...300)
166-
)
167-
ball.physicsBody?.applyImpulse(force)
136+
if ball == voronoiDiagramNode || ball == delaunayDiagramNode { return }
168137
}
169138

170139
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

0 commit comments

Comments
 (0)