ToastManager.ts
3.15 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
import { NodePoolFactory } from "../../utils/NodePoolFactory";
import { AnimationUtils } from "../../utils/AnimationUtils";
import SpecialToast, { SpecialToastType } from "../../../game/ui/SpecialToast";
export namespace ToastManager {
const path = "prefab/ui/Toast";
const specialPath = "prefab/ui/SpecialToast";
let nodeCount = 0;
let initialized = false;
export async function init() {
if (!initialized) {
await NodePoolFactory.createPool(path, 3);
await NodePoolFactory.createPool(specialPath, 3);
initialized = true;
}
}
export function addToast(parent: cc.Node, content: string | cc.Node, time: number = 0.6) {
let node = typeof content === "string" ? NodePoolFactory.getNode(path) : content;
if (node) {
nodeCount++;
if (typeof content === "string") {
let textComp = node.getChildByName("text").getComponent(cc.RichText);
textComp.string = content;
node.height = textComp.node.height + 10;
}
node.stopAllActions();
node.x = 0;
node.y = 0;
node.scaleX = 0;
node.scaleY = 0;
node.opacity = 255;
node.parent = parent;
AnimationUtils.runAction(node, cc.sequence(
cc.scaleTo(0.2, 1, 1),
cc.delayTime(0.2),
cc.spawn(
cc.moveBy(0.8, 0, 100),
cc.sequence(
cc.delayTime(time),
cc.fadeOut(0.2)
)
)
)).then(() => {
if (node) {
if ((node as any)._path) {
NodePoolFactory.putNode(node);
} else {
node.removeFromParent(true);
}
}
nodeCount--;
});
}
}
export function addSpecialToast(parent: cc.Node, type: SpecialToastType, time: number = 0.6) {
let node = NodePoolFactory.getNode(specialPath);
if (node) {
node.getComponent(SpecialToast).setData(type);;
node.stopAllActions();
node.x = 0;
node.y = 0;
node.scaleX = 0;
node.scaleY = 0;
node.opacity = 255;
node.parent = parent;
AnimationUtils.runAction(node, cc.sequence(
cc.scaleTo(0.2, 1, 1),
cc.delayTime(0.2),
cc.spawn(
cc.moveBy(0.8, 0, 100),
cc.sequence(
cc.delayTime(time),
cc.fadeOut(0.2)
)
)
)).then(() => {
if (node) {
if ((node as any)._path) {
NodePoolFactory.putNode(node);
} else {
node.removeFromParent(true);
}
}
nodeCount--;
});
}
}
}