From 9a560d2fa02cc7ea3eb7b5b105386f68b78e266e Mon Sep 17 00:00:00 2001 From: Niko Jochim Date: Mon, 20 Apr 2020 12:33:57 +0200 Subject: [PATCH] changed base to SKSpriteNode and add effects touch/move --- GoldWars/GoldWars/Base/Base.swift | 70 ++++++++++++++++++++++--------- 1 file changed, 50 insertions(+), 20 deletions(-) diff --git a/GoldWars/GoldWars/Base/Base.swift b/GoldWars/GoldWars/Base/Base.swift index 393c427..595ba31 100644 --- a/GoldWars/GoldWars/Base/Base.swift +++ b/GoldWars/GoldWars/Base/Base.swift @@ -9,33 +9,63 @@ import SpriteKit import GameplayKit -class Base { - var spriteNode: BaseNode! +class Base : SKSpriteNode{ + public static let colorSelected = SKColor.yellow + var availableBases = [Base]() + var standartColor = SKColor.green - init(frame: CGRect, color: UIColor, position: CGPoint) { - spriteNode = BaseNode(texture: SKTexture(imageNamed: "Base"), color: color, size: CGSize(width: 50.0, height: 50.0)) - spriteNode.colorBlendFactor = 1 - spriteNode.name = "Base" - spriteNode.position = position - spriteNode.isUserInteractionEnabled = true + init(color: UIColor, position: CGPoint, name: String) { + super.init(texture: SKTexture(imageNamed: "Base"), color: color, size: CGSize(width: 50.0, height: 50.0)) + + standartColor = color + self.colorBlendFactor = 1 + self.name = name + self.position = position + self.zPosition = 2 + self.isUserInteractionEnabled = true + } + + required init?(coder aDecoder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + + func addAvailableBase(base: Base) { + availableBases.append(base) } -} -class BaseNode: SKSpriteNode { override func touchesBegan(_ touches: Set, with event: UIEvent?) { - print("\(self.name!) was touched and is located at \(self.position)") - self.run( - SKAction.sequence( - [ - SKAction.resize(byWidth: 20, height: 20, duration: 0.5), - SKAction.resize(byWidth: -20, height: -20, duration: 0.5) - ] - ) - ) - + print("\(self.name!) was touched and is located at \(self.position)") + self.run( + SKAction.sequence( + [ + SKAction.resize(byWidth: 20, height: 20, duration: 0.5), + SKAction.resize(byWidth: -20, height: -20, duration: 0.5) + ] + ) + ) + + } + + override func touchesMoved(_ touches: Set, with event: UIEvent?) { + self.color = SKColor.yellow + + for base in availableBases { + base.color = Base.colorSelected + base.size = CGSize(width: 60, height: 60) + } } + + override func touchesEnded(_ touches: Set, with event: UIEvent?) { + self.color = standartColor + for base in availableBases { + base.color = base.standartColor + base.size = CGSize(width: 50, height: 50) + } + } + } + enum PlayerColors { static let colors = [ UIColor(red: 231/255, green: 76/255, blue: 60/255, alpha: 1.0),