-
Notifications
You must be signed in to change notification settings - Fork 0
/
WhackSlot.swift
88 lines (68 loc) · 2.42 KB
/
WhackSlot.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//
// WhackSlot.swift
/// Day56 - Project14
//
// Created by Jean-Yves Garcin on 21/06/2023.
//
import UIKit
import SpriteKit
class WhackSlot: SKNode {
var charNode: SKSpriteNode!
var isVisible = false
var isHit = false
func configure(at position: CGPoint) {
self.position = position
let sprite = SKSpriteNode(imageNamed: "whackHole")
addChild(sprite)
let cropNode = SKCropNode()
cropNode.position = CGPoint(x: 0, y: 15)
cropNode.zPosition = 1
cropNode.maskNode = SKSpriteNode(imageNamed: "whackMask")
charNode = SKSpriteNode(imageNamed: "penguinGood")
charNode.position = CGPoint(x: 0, y: -90) // right off the screen
charNode.name = "character"
cropNode.addChild(charNode)
addChild(cropNode)
}
func show(hideTime: Double) {
if isVisible { return }
charNode.xScale = 1
charNode.yScale = 1
charNode.run(SKAction.moveBy(x: 0, y: 80, duration: 0.05))
isVisible = true
isHit = false
if Int.random(in: 0...2) == 0 {
charNode.texture = SKTexture(imageNamed: "penguinGood")
charNode.name = "charFriend"
} else {
charNode.texture = SKTexture(imageNamed: "penguinEvil")
charNode.name = "charEnemy"
}
if let mudPartice = SKEmitterNode(fileNamed: "mud") {
let mudPos = CGPoint(x: 0, y: 0)
mudPartice.position = mudPos
addChild(mudPartice)
}
DispatchQueue.main.asyncAfter(deadline: .now() + (hideTime * 3.5)) {
[weak self] in
self?.hide()
}
}
func hide() {
if !isVisible { return }
charNode.run(SKAction.moveBy(x: 0, y: -80, duration: 0.05))
isVisible = false
}
func hit() {
isHit = true
let delay = SKAction.wait(forDuration: 0.25)
let hide = SKAction.moveBy(x: 0, y: -80, duration: 0.5)
let notVisible = SKAction.run { [weak self] in self?.isVisible = false }
let sequence = SKAction.sequence([delay, hide, notVisible])
charNode.run(sequence)
if let smokePartice = SKEmitterNode(fileNamed: "smoke") {
smokePartice.position = charNode.position
addChild(smokePartice)
}
}
}