remove Ways as Component and add function to generate different types of ways

This commit is contained in:
Jakob Haag 2020-06-06 11:47:57 +02:00 committed by Marcel Schwarz
parent 79781ccdc9
commit ed764afc45
2 changed files with 46 additions and 11 deletions

View File

@ -59,6 +59,10 @@ class EntityManager {
scene.addChild(hudEntitiy.roundLabel)
}
if let wayEntity = entity as? Way {
scene.addChild(wayEntity.localWayComponent)
}
if let spriteNode = entity.component(ofType: DefaultBaseComponent.self) {
scene.addChild(spriteNode.labelNode)
scene.addChild(spriteNode.spriteNode)
@ -84,9 +88,6 @@ class EntityManager {
if let node = entity.component(ofType: SpinningLogoComponent.self)?.node {
scene.addChild(node)
}
if let wayNode = entity.component(ofType: DefaultWayComponent.self)?.shapeNode {
scene.addChild(wayNode)
}
}
func remove(_ entity: GKEntity) {

View File

@ -11,20 +11,54 @@ import SpriteKit
import GameplayKit
class Way: GKEntity {
var localWayComponent: SKShapeNode
required init(fromBase: Base, toBase: Base) {
super.init()
let x1 = fromBase.position.x
let y1 = fromBase.position.y
let x2 = toBase.position.x
let y2 = toBase.position.y
let xControll1 = Way.computeX(x1: x1, x2: x2, y1: y1, y2: y2, factor: 2/5)
let yControll1 = Way.computeY(x1: x1, x2: x2, y1: y1, y2: y2, factor: 2/5)
let xControll2 = Way.computeX(x1: x1, x2: x2, y1: y1, y2: y2, factor: 3/5)
let yControll2 = Way.computeY(x1: x1, x2: x2, y1: y1, y2: y2, factor: 3/5)
let pathToDraw = CGMutablePath()
pathToDraw.move(to: fromBase.position)
pathToDraw.addLine(to: toBase.position)
addComponent(DefaultWayComponent(pathToDraw: pathToDraw))
// pathToDraw.addLine(to: toBase.position)
pathToDraw.addCurve(
to: toBase.position,
control1: CGPoint(x: xControll1, y: yControll1),
control2: CGPoint(x: xControll2, y: yControll2)
)
self.localWayComponent = SKShapeNode()
self.localWayComponent.path = pathToDraw
self.localWayComponent.strokeColor = UIColor(red: 0.852, green: 0.649, blue: 0.123, alpha: 1)
self.localWayComponent.lineWidth = 10
self.localWayComponent.zPosition = 0
super.init()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
static var wayDelta: CGFloat = 5
static func computeX(x1: CGFloat, x2: CGFloat, y1: CGFloat, y2: CGFloat, factor: CGFloat) -> CGFloat {
let mix:CGFloat = Int.random(in: 0...1) == 1 ? -1/wayDelta : 1/wayDelta
let x = x1 + factor * (x2 - x1) + mix * -(y2-y1)
return x
}
static func computeY(x1: CGFloat, x2: CGFloat, y1: CGFloat, y2: CGFloat, factor: CGFloat) -> CGFloat {
let mix:CGFloat = Int.random(in: 0...1) == 1 ? -1/wayDelta : 1/wayDelta
let y = y1 + factor * (y2 - y1) + mix * (x2-x1)
return y
}
}