-
Notifications
You must be signed in to change notification settings - Fork 6
/
Shapes.swift
145 lines (109 loc) · 4.3 KB
/
Shapes.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
//
// Shapes.swift
// Asteroid
//
// Created by Daegeon Choi on 2022/04/08.
//
import Foundation
import SpriteKit
class ShipNode: SKShapeNode {
init(scale: CGFloat, position: CGPoint){
super.init()
let points = [
CGPoint(x: 0, y: 10),
CGPoint(x: 5, y: -5),
CGPoint(x: 0, y: -2),
CGPoint(x: -5, y: -5),
]
let size = CGPoint(x: scale, y: scale)
let scaledPoints = points.map { $0 * size }
let path = CGMutablePath()
path.move(to: scaledPoints[0])
for i in 1...scaledPoints.count-1 {
path.addLine(to: scaledPoints[i])
}
path.addLine(to: scaledPoints[0])
self.path = path
self.strokeColor = .white
self.lineWidth = kLineWidth
self.position = position
self.fillColor = kShipLoadedColor
self.name = kShipName
self.physicsBody = SKPhysicsBody(polygonFrom: path)
self.physicsBody!.affectedByGravity = false
self.physicsBody?.mass = 1.0
self.physicsBody?.isDynamic = true
self.physicsBody!.categoryBitMask = kShipCategory
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
class AsteroidNode: SKShapeNode {
var type = AsteroidType.A
var size = AsteroidSize.Big
var movingVector = CGPoint(x: 0, y: 0)
init(scaleType: AsteroidSize, position: CGPoint){
super.init()
let random = Int.random(in: 0...AsteroidType.allCases.count-1)
let points = AsteroidType(rawValue: random)?.points ?? AsteroidType.A.points
size = scaleType
let scale = CGPoint(x: scaleType.scale, y: scaleType.scale)
let scaledPoints = points.map { $0 * scale }
let path = CGMutablePath()
path.move(to: scaledPoints[0])
for i in 1...scaledPoints.count-1 {
path.addLine(to: scaledPoints[i])
}
path.addLine(to: scaledPoints[0])
self.path = path
self.strokeColor = .white
self.fillColor = .black
self.lineWidth = kLineWidth
self.position = position
self.name = kAsteroidName
self.physicsBody = SKPhysicsBody(polygonFrom: path)
self.physicsBody!.affectedByGravity = false
self.physicsBody?.isDynamic = false
self.physicsBody!.categoryBitMask = kAsteroidCategory
self.physicsBody!.contactTestBitMask = kShipCategory
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
public func getBulletNode(position: CGPoint) -> SKShapeNode {
let bullet = SKShapeNode(circleOfRadius: kBulletRadius)
bullet.position = position
bullet.fillColor = .white
bullet.zPosition = -10
bullet.name = kBulletName
bullet.physicsBody = SKPhysicsBody(circleOfRadius: kBulletRadius)
bullet.physicsBody!.affectedByGravity = false
bullet.physicsBody!.categoryBitMask = kBulletCategory
bullet.physicsBody!.contactTestBitMask = kAsteroidCategory
bullet.physicsBody!.usesPreciseCollisionDetection = true
return bullet
}
public func getLifeItemNode(position: CGPoint) -> SKSpriteNode {
let life = SKSpriteNode(imageNamed: "LifeItem")
life.size = CGSize(width: life.size.width * 2, height: life.size.height * 2)
life.position = position
life.name = kLifeItemName
life.physicsBody = SKPhysicsBody(rectangleOf: life.size)
life.physicsBody!.affectedByGravity = false
life.physicsBody!.categoryBitMask = kAsteroidCategory
life.physicsBody!.usesPreciseCollisionDetection = true
return life
}
public func getBulletItemNode(position: CGPoint) -> SKSpriteNode {
let bullet = SKSpriteNode(imageNamed: "BulletItem")
bullet.size = CGSize(width: bullet.size.width * 2, height: bullet.size.height * 2)
bullet.position = position
bullet.name = kGunItemName
bullet.physicsBody = SKPhysicsBody(rectangleOf: bullet.size)
bullet.physicsBody!.affectedByGravity = false
bullet.physicsBody!.categoryBitMask = kAsteroidCategory
bullet.physicsBody!.usesPreciseCollisionDetection = true
return bullet
}