-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDialogueManager.swift
More file actions
152 lines (110 loc) · 3.97 KB
/
DialogueManager.swift
File metadata and controls
152 lines (110 loc) · 3.97 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
//
// DialogueManager.swift
// WWDC24
//
// Created by Gustavo Binder on 24/02/24.
//
import SpriteKit
class DialogueManager {
private var notes : [SKSpriteNode] = []
private var currentAmount : Int = 0
private var iterable : Int = 0
private let firstNum : Int
private let lastNum : Int
public var nextTextNum : Int
private var currentNum : Int = 0
private var sets : [Int]
private var current_set = 0
private var scene : SKScene
init(_ firstNum: Int, _ lastNum : Int, sets: [Int], _ scene: SKScene) {
self.firstNum = firstNum
self.lastNum = lastNum
self.sets = sets
self.scene = scene
self.nextTextNum = firstNum
}
public func createNotes(_ positions: [CGPoint], _ quant: Int) {
var actualQuant : Int = quant
if nextTextNum + quant - 1 > lastNum {
actualQuant = lastNum - (nextTextNum + quant - 1)
}
currentAmount = actualQuant
for i in 0..<actualQuant {
let actualName = "Text\(nextTextNum + i)"
let note = SKSpriteNode(imageNamed: actualName)
note.size = doubleSize(note.size)
let texture = SKTexture(imageNamed: "Paper")
// let sizen = CGSize(width: note.size.width * 1.2, height: note.size.height * 1.5)
// noteBox.addChild(note)
note.position = CGPoint(x: note.size.width/2, y: -note.size.height/2)
note.anchorPoint = CGPoint(x: 0, y: 1)
note.position = positions[i]
note.isHidden = true
note.setScale(0)
notes.append(note)
scene.addChild(note)
}
nextTextNum += actualQuant
print(notes.count)
}
public func showNextNote() -> Bool {
if iterable == currentAmount {
return true
}
let pop = SKAction.scale(to: 1.2, duration: 0.05)
let mini_shrink = SKAction.scale(to: 1, duration: 0.05)
let sequence = SKAction.sequence([pop, mini_shrink])
notes[iterable].isHidden = false
notes[iterable].run(sequence)
iterable += 1
return false
}
public func destroyAllNotes() {
if notes.isEmpty {
return
}
for note in notes {
let grow_a_bit = SKAction.scale(to: 1.2, duration: 0.05)
let shrink_a_bit = SKAction.scale(to: 0, duration: 0.1)
let wait = SKAction.wait(forDuration: 0.15)
let destroy = SKAction.run {
note.removeFromParent()
}
let sequence = SKAction.sequence([grow_a_bit, shrink_a_bit, wait, destroy])
note.run(sequence)
}
iterable = 0
notes.removeAll()
}
public func isOver() -> Bool {
if nextTextNum >= lastNum {
return true
}
return false
}
public func nextDialogue(_ positions: [[CGPoint]]) -> Bool {
if showNextNote() {
if isOver() {
return true
}
scene.isUserInteractionEnabled = false
destroyAllNotes()
current_set += 1
let next = SKAction.run {
self.createNotes(positions[self.current_set], self.sets[self.current_set])
if self.showNextNote() {
return
}
self.scene.isUserInteractionEnabled = true
}
let wait = SKAction.wait(forDuration: 0.2)
let sequence = SKAction.sequence([wait, next])
scene.run(sequence)
}
currentNum += 1
return false
}
public func getNum() -> Int {
return currentNum
}
}