Setup logic for showing penguins and switching b/w good and evil
This commit is contained in:
parent
21741dab37
commit
c7f2c37ca0
|
@ -10,7 +10,26 @@ import SpriteKit
|
|||
import UIKit
|
||||
|
||||
class WhackSlot: SKNode {
|
||||
enum NodeName: String {
|
||||
case goodPenguin = "good-penguin"
|
||||
case evilPenguin = "evil-penguin"
|
||||
}
|
||||
|
||||
var penguinNode: SKSpriteNode!
|
||||
var isShowingPenguin: Bool = false
|
||||
var isWhacked: Bool = false
|
||||
|
||||
var penguinTextureName: String {
|
||||
return isPenguinGood ? "penguinGood" : "penguinEvil"
|
||||
}
|
||||
|
||||
var isPenguinGood = false {
|
||||
didSet {
|
||||
penguinNode.texture = SKTexture(imageNamed: penguinTextureName)
|
||||
penguinNode.name = isPenguinGood ? NodeName.goodPenguin.rawValue : NodeName.evilPenguin.rawValue
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func setup(at position: CGPoint) {
|
||||
self.position = position
|
||||
|
@ -22,6 +41,24 @@ class WhackSlot: SKNode {
|
|||
}
|
||||
|
||||
|
||||
func show(for duration: Double) {
|
||||
guard !isShowingPenguin else { return }
|
||||
|
||||
isPenguinGood = Double.random(in: 0...1) >= 0.3333
|
||||
|
||||
showPenguin(for: duration)
|
||||
}
|
||||
|
||||
|
||||
private func showPenguin(for duration: Double) {
|
||||
let showAction = SKAction.moveBy(x: 0, y: 80, duration: 0.05)
|
||||
|
||||
penguinNode.run(showAction)
|
||||
isShowingPenguin = true
|
||||
isWhacked = false
|
||||
}
|
||||
|
||||
|
||||
private func setupPenguin(at position: CGPoint) {
|
||||
let cropNode = SKCropNode()
|
||||
penguinNode = SKSpriteNode(imageNamed: "penguinGood")
|
||||
|
@ -31,12 +68,10 @@ class WhackSlot: SKNode {
|
|||
cropNode.maskNode = SKSpriteNode(imageNamed: "whackMask")
|
||||
|
||||
penguinNode.position = CGPoint(x: 0, y: -90)
|
||||
penguinNode.name = "penguin"
|
||||
penguinNode.name = NodeName.goodPenguin.rawValue
|
||||
|
||||
cropNode.addChild(penguinNode)
|
||||
|
||||
self.addChild(cropNode)
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue