implemented ButtonEntity

This commit is contained in:
Niko 2020-05-02 23:12:37 +02:00
parent fcf49e656d
commit 94d7be35ff
2 changed files with 39 additions and 36 deletions

View File

@ -11,39 +11,48 @@ import SpriteKit
class ButtonNode : SKSpriteNode { class ButtonNode : SKSpriteNode {
var isEnabled:Bool var isEnabled:Bool{
didSet{
if isEnabled {
self.alpha = 1
self.childNode(withName: "label")?.alpha = 1
} else {
self.alpha = 0.3
self.childNode(withName: "label")?.alpha = 0.3
}
}
}
let onButtonPress: () -> () let onButtonPress: () -> ()
init(iconName: String, text: String,isEnabled:Bool, position: CGPoint, onButtonPress: @escaping () -> ()) { init(iconName: String, text: String,isEnabled:Bool, position: CGPoint, onButtonPress: @escaping () -> ()) {
self.onButtonPress = onButtonPress
self.isEnabled = isEnabled
let texture = SKTexture(imageNamed: "yellow_button04")
super.init(texture: texture, color: SKColor.white, size: texture.size())
self.position = position
self.onButtonPress = onButtonPress let label = SKLabelNode(fontNamed: "Courier-Bold")
self.isEnabled = isEnabled
let texture = SKTexture(imageNamed: "yellow_button04")
super.init(texture: texture, color: SKColor.white, size: texture.size())
self.position = position
let label = SKLabelNode(fontNamed: "Courier-Bold") label.fontSize = 30
label.fontColor = SKColor.black
label.fontSize = 30 label.zPosition = 1
label.fontColor = SKColor.black label.verticalAlignmentMode = .center
label.text = text
label.name = "label"
label.zPosition = 1 if !iconName.isEmpty {
label.verticalAlignmentMode = .center label.position = CGPoint(x: size.width * 0.25, y: 0)
label.text = text let icon = SKSpriteNode(imageNamed: iconName)
label.name = "label" icon.position = CGPoint(x: -size.width * 0.25, y: 0)
icon.zPosition = 1
if !iconName.isEmpty { self.addChild(icon)
label.position = CGPoint(x: size.width * 0.25, y: 0) } else {
let icon = SKSpriteNode(imageNamed: iconName) label.position = CGPoint(x: 0, y: 0)
icon.position = CGPoint(x: -size.width * 0.25, y: 0) }
icon.zPosition = 1 self.addChild(label)
self.addChild(icon) isUserInteractionEnabled = true
} else {
label.position = CGPoint(x: 0, y: 0)
}
self.addChild(label)
isUserInteractionEnabled = true
} }
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

View File

@ -12,7 +12,7 @@ class MenuScene: SKScene {
var entityManager: EntityManager! var entityManager: EntityManager!
override func sceneDidLoad() { override func sceneDidLoad() {
entityManager = EntityManager(scene: self) entityManager = EntityManager(scene: self)
let midX = self.size.width / 2 let midX = self.size.width / 2
let midY = self.size.height / 2 let midY = self.size.height / 2
@ -23,18 +23,14 @@ class MenuScene: SKScene {
onButtonPress: { onButtonPress: {
self.loadScene(scene: GameScene(size: self.size)) self.loadScene(scene: GameScene(size: self.size))
})) }))
entityManager.add(Button(name: "settingsButton", entityManager.add(Button(name: "settingsButton",
iconName: "", iconName: "",
text: "Settings", text: "Settings",
position: CGPoint(x: midX, y: midY - 80 ), position: CGPoint(x: midX, y: midY - 80 ),
onButtonPress: { onButtonPress: {
//TODO: create Settings Scene
})) }))
}
}
func backgroundInit(){ func backgroundInit(){
for i in 0...2 { for i in 0...2 {
@ -43,7 +39,6 @@ class MenuScene: SKScene {
sky.zPosition = -1 sky.zPosition = -1
sky.size = CGSize(width: self.size.width, height: self.size.height) sky.size = CGSize(width: self.size.width, height: self.size.height)
sky.position = CGPoint(x: CGFloat(i) * sky.size.width , y: (self.frame.size.height / 2)) sky.position = CGPoint(x: CGFloat(i) * sky.size.width , y: (self.frame.size.height / 2))
self.addChild(sky) self.addChild(sky)
} }
} }
@ -61,7 +56,6 @@ class MenuScene: SKScene {
node.position.x += (self.scene?.size.width)! * 3 node.position.x += (self.scene?.size.width)! * 3
} }
})) }))
entityManager.getButtonByName(buttonName: "startGameButton").component(ofType: ButtonComponent.self)?.buttonNode.isEnabled = GameCenterHelper.isAuthenticated
entityManager.getButtonByName(buttonName: "startGameButton").isEnabled = GameCenterHelper.isAuthenticated
} }
} }