Skip to content

Commit 517170a

Browse files
committed
optimize and reformat
1 parent 323e745 commit 517170a

15 files changed

+568
-466
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//
2+
// Array+Extensions.swift
3+
// CherrystudiosGameKit
4+
//
5+
import CoreGraphics
6+
7+
extension Array where Element == CGVector {
8+
/// Calculate the average of the giving vectors
9+
func average() -> CGVector {
10+
guard !isEmpty else {
11+
return .zero
12+
}
13+
14+
let count = CGFloat(self.count)
15+
16+
let sum = reduce(CGVector.zero) { result, vector in
17+
CGVector(dx: result.dx + vector.dx, dy: result.dy + vector.dy)
18+
}
19+
20+
return CGVector(dx: sum.dx / count, dy: sum.dy / count)
21+
}
22+
}

Sources/CherrystudiosGameKit/CGFloat+Extensions.swift

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,17 @@ let π = CGFloat(Double.pi)
55

66
public extension CGFloat {
77
// MARK: - Normal Methods
8+
89
/// Converts an angle in degrees to radians.
910
func degreesToRadians() -> CGFloat {
1011
return π * self / 180.0
1112
}
12-
13+
1314
/// Converts an angle in radians to degrees.
1415
func radiansToDegrees() -> CGFloat {
1516
return self * 180.0 / π
1617
}
17-
18+
1819
/// Ensures that the float value stays between the given values, inclusive.
1920
func clamped(_ v1: CGFloat, _ v2: CGFloat) -> CGFloat {
2021
let min = v1 < v2 ? v1 : v2
@@ -26,41 +27,45 @@ public extension CGFloat {
2627
func sign() -> CGFloat {
2728
return (self >= 0.0) ? 1.0 : -1.0
2829
}
29-
30+
3031
// MARK: - Mutating Methods
32+
3133
/// Ensures that the float value stays between the given values, inclusive.
32-
mutating func clamp(_ v1: CGFloat, _ v2: CGFloat) -> CGFloat {
34+
@discardableResult
35+
mutating func clamp(_ v1: CGFloat, _ v2: CGFloat) -> Self {
3336
self = clamped(v1, v2)
3437
return self
3538
}
36-
39+
3740
// MARK: - Static Methods
41+
3842
/// Returns a random floating point number between 0.0 and 1.0, inclusive.
3943
static func random() -> CGFloat {
40-
return CGFloat.random(in: 0.0...1.0)
44+
return CGFloat.random(in: 0.0 ... 1.0)
4145
}
42-
46+
4347
/// Returns a random floating point number in the range min...max, inclusive.
4448
static func random(min: CGFloat, max: CGFloat) -> CGFloat {
4549
assert(min < max)
4650
return CGFloat.random() * (max - min) + min
4751
}
48-
52+
4953
/// Randomly returns either 1.0 or -1.0.
5054
static func randomSign() -> CGFloat {
5155
return (arc4random_uniform(2) == 0) ? 1.0 : -1.0
5256
}
5357
}
5458

5559
// MARK: - Associated Functions
60+
5661
/// Returns the shortest angle between two angles. The result is always between -π and π.
5762
public func shortestAngleBetween(_ angle1: CGFloat, angle2: CGFloat) -> CGFloat {
5863
let twoπ = π * 2.0
5964
var angle = (angle2 - angle1).truncatingRemainder(dividingBy: twoπ)
60-
if (angle >= π) {
65+
if angle >= π {
6166
angle = angle - twoπ
6267
}
63-
if (angle <= -π) {
68+
if angle <= -π {
6469
angle = angle + twoπ
6570
}
6671
return angle

Sources/CherrystudiosGameKit/CGPoint+Extensions.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ import SpriteKit
33

44
public extension CGPoint {
55
// MARK: - Computed Property
6+
67
/// Returns the angle in radians of the vector described by the CGPoint.
78
/// The range of the angle is -π to π; an angle of 0 points to the right.
89
var angle: CGFloat {
910
return atan2(y, x)
1011
}
1112

1213
// MARK: - Initializers
14+
1315
/// Creates a new CGPoint given a CGVector.
1416
init(vector: CGVector) {
1517
self.init(x: vector.dx, y: vector.dy)
@@ -22,20 +24,24 @@ public extension CGPoint {
2224
}
2325

2426
// MARK: - Mutating Methods
27+
2528
/// Normalizes the vector described by the CGPoint to length 1.0.
26-
mutating func normalize() -> CGPoint {
29+
@discardableResult
30+
mutating func normalize() -> Self {
2731
self = normalized()
2832
return self
2933
}
3034

3135
/// Adds (dx, dy) to the point.
32-
mutating func offset(dx: CGFloat, dy: CGFloat) -> CGPoint {
36+
@discardableResult
37+
mutating func offset(dx: CGFloat, dy: CGFloat) -> Self {
3338
x += dx
3439
y += dy
3540
return self
3641
}
3742

3843
// MARK: - Normal Methods
44+
3945
/// Returns the length (magnitude) of the vector described by the CGPoint.
4046
func length() -> CGFloat {
4147
return sqrt(x * x + y * y)
@@ -57,10 +63,10 @@ public extension CGPoint {
5763
func distanceTo(_ point: CGPoint) -> CGFloat {
5864
return (self - point).length()
5965
}
60-
6166
}
6267

6368
// MARK: - Associated Functions
69+
6470
/// Adds two CGPoint values and returns the result as a new CGPoint.
6571
public func + (left: CGPoint, right: CGPoint) -> CGPoint {
6672
return CGPoint(x: left.x + right.x, y: left.y + right.y)

Sources/CherrystudiosGameKit/CGVector+Extensions.swift

Lines changed: 54 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ import SpriteKit
33

44
public extension CGVector {
55
// MARK: - Computed Property
6+
67
/// Returns the angle in radians of the vector described by the CGVector. The range of the angle is -π to π; an angle of 0 points to the right.
78
var angle: CGFloat {
89
return atan2(dy, dx)
910
}
10-
11+
1112
// MARK: - Initializers
13+
1214
/// Creates a new CGVector given a CGPoint.
1315
init(point: CGPoint) {
1416
self.init(dx: point.x, dy: point.y)
@@ -20,57 +22,68 @@ public extension CGVector {
2022
}
2123

2224
// MARK: - Mutating Methods
25+
2326
// Adds (dx, dy) to the vector.
24-
mutating func offset(dx: CGFloat, dy: CGFloat) -> CGVector {
27+
@discardableResult
28+
mutating func offset(dx: CGFloat, dy: CGFloat) -> Self {
2529
self.dx += dx
2630
self.dy += dy
2731
return self
2832
}
2933

3034
/// Normalizes the vector described by the CGVector to length 1.0.
31-
mutating func normalize() -> CGVector {
35+
@discardableResult
36+
mutating func normalize() -> Self {
3237
self = normalized()
3338
return self
3439
}
3540

36-
mutating func setMag(_ mag: CGFloat) {
37-
let normalizedVector = normalized()
38-
self = CGVector(dx: normalizedVector.dx * mag, dy: normalizedVector.dy * mag)
41+
/// Set Vector's Magnititude
42+
@discardableResult
43+
mutating func setMag(_ mag: CGFloat) -> Self {
44+
let normal = normalized()
45+
self = CGVector(dx: normal.dx * mag, dy: normal.dy * mag)
46+
return self
3947
}
40-
41-
mutating func div(_ mag: CGFloat) {
42-
if mag == 0 {
43-
return
48+
49+
/// Calculate average vector into N
50+
@discardableResult
51+
mutating func div(_ n: CGFloat) -> Self {
52+
if n == 0 {
53+
return self
4454
}
45-
46-
self = CGVector(dx: self.dx/mag, dy: dy/mag)
47-
}
48-
49-
mutating func limit(_ mag: CGFloat) {
50-
let currentMag = sqrt(dx * dx + dy * dy)
51-
if currentMag > mag {
52-
let ratio = mag / currentMag
55+
self = CGVector(dx: dx / n, dy: dy / n)
56+
return self
57+
}
58+
59+
/// Limit the vector's length
60+
@discardableResult
61+
mutating func limit(_ length: CGFloat) -> Self {
62+
let len = self.length()
63+
if len > length {
64+
let ratio = length / len
5365
self = CGVector(dx: dx * ratio, dy: dy * ratio)
5466
}
67+
68+
return self
5569
}
5670

57-
mutating func add(_ vector: CGVector) {
71+
/// Add a vector to self
72+
@discardableResult
73+
mutating func add(_ vector: CGVector) -> Self {
5874
self = CGVector(dx: dx + vector.dx, dy: dy + vector.dy)
75+
return self
5976
}
60-
61-
mutating func sub(_ vector: CGVector) {
77+
78+
/// Subtraction a vector from self
79+
@discardableResult
80+
mutating func sub(_ vector: CGVector) -> Self {
6281
self = CGVector(dx: dx - vector.dx, dy: dy - vector.dy)
63-
}
64-
65-
static func add(_ a:CGVector, _ b: CGVector) -> CGVector {
66-
return CGVector(dx: a.dx + b.dx, dy: a.dy + b.dy)
67-
}
68-
69-
static func sub(_ a:CGVector, _ b: CGVector) -> CGVector {
70-
return CGVector(dx: a.dx - b.dx, dy: a.dy - b.dy)
82+
return self
7183
}
7284

7385
// MARK: - Normal Methods
86+
7487
/// Returns a copy of the vector.
7588
func copy() -> CGVector {
7689
return CGVector(dx: dx, dy: dy)
@@ -97,10 +110,21 @@ public extension CGVector {
97110
return (self - vector).length()
98111
}
99112

100-
// MARK: - Static Methods
113+
// MARK: - Static Methods, those methods can be replaced with opertator functions
114+
115+
/// Calculates the Sum of two vectors
116+
static func add(_ a: CGVector, _ b: CGVector) -> CGVector {
117+
return CGVector(dx: a.dx + b.dx, dy: a.dy + b.dy)
118+
}
119+
120+
/// Calculate the Subtraction of two vectors
121+
static func sub(_ a: CGVector, _ b: CGVector) -> CGVector {
122+
return CGVector(dx: a.dx - b.dx, dy: a.dy - b.dy)
123+
}
101124
}
102125

103126
// MARK: - Associated Functions
127+
104128
/// Adds two CGVector values and returns the result as a new CGVector.
105129
public func + (left: CGVector, right: CGVector) -> CGVector {
106130
return CGVector(dx: left.dx + right.dx, dy: left.dy + right.dy)

Sources/CherrystudiosGameKit/Int+Extensions.swift

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,60 +2,66 @@ import CoreGraphics
22

33
public extension Int {
44
// MARK: - Normal Methods
5+
56
/// Ensures that the integer value stays with the specified range.
67
func clamped(_ range: Range<Int>) -> Int {
7-
return (self < range.lowerBound) ? range.lowerBound : ((self >= range.upperBound) ? range.upperBound - 1: self)
8+
return (self < range.lowerBound) ? range.lowerBound : ((self >= range.upperBound) ? range.upperBound - 1 : self)
89
}
9-
10+
1011
func clamped(_ range: ClosedRange<Int>) -> Int {
11-
return (self < range.lowerBound) ? range.lowerBound : ((self > range.upperBound) ? range.upperBound: self)
12+
return (self < range.lowerBound) ? range.lowerBound : ((self > range.upperBound) ? range.upperBound : self)
1213
}
13-
14+
1415
// MARK: - Mutating Methods
16+
1517
/// Ensures that the integer value stays with the specified range.
16-
mutating func clamp(_ range: Range<Int>) -> Int {
18+
@discardableResult
19+
mutating func clamp(_ range: Range<Int>) -> Self {
1720
self = clamped(range)
1821
return self
1922
}
20-
21-
mutating func clamp(_ range: ClosedRange<Int>) -> Int {
23+
24+
@discardableResult
25+
mutating func clamp(_ range: ClosedRange<Int>) -> Self {
2226
self = clamped(range)
2327
return self
2428
}
25-
29+
2630
/// Ensures that the integer value stays between the given values, inclusive.
27-
mutating func clamp(_ v1: Int, _ v2: Int) -> Int {
31+
@discardableResult
32+
mutating func clamp(_ v1: Int, _ v2: Int) -> Self {
2833
self = clamped(v1, v2)
2934
return self
3035
}
3136

3237
// MARK: - Normal Methods
38+
3339
/// Ensures that the integer value stays between the given values, inclusive.
3440
func clamped(_ v1: Int, _ v2: Int) -> Int {
3541
let min = v1 < v2 ? v1 : v2
3642
let max = v1 > v2 ? v1 : v2
3743
return self < min ? min : (self > max ? max : self)
3844
}
3945

40-
// MARK: - Static Methods
46+
// MARK: - Static Methods
47+
4148
/// Returns a random integer in the specified range.
4249
static func random(_ range: Range<Int>) -> Int {
4350
return Int(arc4random_uniform(UInt32(range.upperBound - range.lowerBound - 1))) + range.lowerBound
4451
}
45-
52+
4653
static func random(_ range: ClosedRange<Int>) -> Int {
4754
return Int(arc4random_uniform(UInt32(range.upperBound - range.lowerBound))) + range.lowerBound
4855
}
49-
56+
5057
/// Returns a random integer between 0 and n-1.
5158
static func random(_ n: Int) -> Int {
5259
return Int(arc4random_uniform(UInt32(n)))
5360
}
54-
61+
5562
/// Returns a random integer in the range min...max, inclusive.
5663
static func random(min: Int, max: Int) -> Int {
5764
assert(min < max)
5865
return Int(arc4random_uniform(UInt32(max - min + 1))) + min
5966
}
60-
61-
}
67+
}

0 commit comments

Comments
 (0)