Finish project 14

- functionality for detecting touches and "whacking" a penguin
- logic for ending the game
This commit is contained in:
CypherPoet 2019-01-29 00:21:35 -05:00
parent 2e2246134c
commit 098d96e9e9
2 changed files with 90 additions and 4 deletions

View File

@ -15,6 +15,11 @@ class GameScene: SKScene {
var currentScoreLabel: SKLabelNode!
var slots = [WhackSlot]()
var popupTime = 0.85
var currentRound = 0
var remainingRounds: Int {
return 30 - currentRound
}
var currentScore = 0 {
didSet {
@ -42,7 +47,16 @@ class GameScene: SKScene {
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let touch = touches.first else { return }
let location = touch.location(in: self)
let tappedNodes = nodes(at: location)
for node in tappedNodes {
if let slot = WhackSlot.getPenguinSlot(from: node) {
whackPenguin(inSlot: slot)
}
}
}
@ -109,10 +123,14 @@ class GameScene: SKScene {
}
@objc func createEnemy() {
currentRound += 1
guard remainingRounds > 0 else { return endGame() }
popupTime *= 0.991
for slot in getSlotsToShow() {
slot.show(for: popupTime)
slot.show(for: popupTime)
}
DispatchQueue.main.asyncAfter(deadline: .now() + createEnemyDelay) { [unowned self] in
@ -133,4 +151,36 @@ class GameScene: SKScene {
return slotsToShow
}
func whackPenguin(inSlot slot: WhackSlot) {
if slot.penguinNode.name == WhackSlot.NodeName.goodPenguin.rawValue {
guard !slot.isWhacked && slot.isShowingPenguin else { return }
slot.whack(andShrink: true)
currentScore += 1
run(SKAction.playSoundFileNamed("whack.caf", waitForCompletion: false))
} else if slot.penguinNode.name == WhackSlot.NodeName.evilPenguin.rawValue {
guard !slot.isWhacked && slot.isShowingPenguin else { return }
slot.whack()
currentScore -= 5
run(SKAction.playSoundFileNamed("whackBad.caf", waitForCompletion: false))
}
}
func endGame() {
for slot in slots { slot.hide() }
let gameOverText = SKSpriteNode(imageNamed: "gameOver")
gameOverText.position = CGPoint(x: sceneWidth / 2, y: sceneHeight / 2)
gameOverText.zPosition = 1
addChild(gameOverText)
}
}

View File

@ -30,6 +30,26 @@ class WhackSlot: SKNode {
}
}
var showAction: SKAction {
return SKAction.moveBy(x: 0, y: 80, duration: 0.05)
}
var hideAction: SKAction {
return SKAction.moveBy(x: 0, y: -80, duration: 0.05)
}
static func getPenguinSlot(from node: SKNode) -> WhackSlot? {
if (
node.name == WhackSlot.NodeName.evilPenguin.rawValue ||
node.name == WhackSlot.NodeName.goodPenguin.rawValue
) {
return node.parent?.parent as? WhackSlot
}
return nil
}
func setup(at position: CGPoint) {
self.position = position
@ -46,8 +66,10 @@ class WhackSlot: SKNode {
isPenguinGood = Double.random(in: 0...1) >= 0.3333
penguinNode.run(SKAction.moveBy(x: 0, y: 80, duration: 0.05))
penguinNode.xScale = 1
penguinNode.yScale = 1
penguinNode.run(showAction)
isShowingPenguin = true
isWhacked = false
@ -59,12 +81,26 @@ class WhackSlot: SKNode {
func hide() {
guard isShowingPenguin else { return }
penguinNode.run(SKAction.moveBy(x: 0, y: -80, duration: 0.05))
penguinNode.run(hideAction)
isShowingPenguin = false
}
func whack(andShrink: Bool = false) {
guard isShowingPenguin && !isWhacked else { return }
let delay = SKAction.wait(forDuration: 0.25)
let turnOffVisibility = SKAction.run({ [unowned self] in self.isShowingPenguin = false })
isWhacked = true
penguinNode.xScale = 0.85
penguinNode.yScale = 0.85
penguinNode.run(SKAction.sequence([delay, hideAction, turnOffVisibility]))
}
private func setupPenguin(at position: CGPoint) {
let cropNode = SKCropNode()
penguinNode = SKSpriteNode(imageNamed: "penguinGood")