1namespace stklib_kiki_a
2{
3    // Frame 0 to 109 is waving hand animation, used in stklib_carousel_a
4    // Frame 110 to 287 is painting animation, used in stklib_kikiPainter_a
5    void onStart(const string instID)
6    {
7        array<Track::TrackObject@>@ tl = Track::getTrackObjectList();
8        Track::TrackObject@ kiki_obj = null;
9        Track::Mesh@ kiki_mesh = null;
10        for (uint i = 0; i < tl.length(); i++)
11        {
12            if (tl[i].getName() == "stklib_kiki_a_main.spm")
13            {
14                // Originally kiki animation ends at 287 totally
15                if (tl[i].getMesh() !is null &&
16                    tl[i].getMesh().getAnimationSetFrames().length() == 2 &&
17                    tl[i].getMesh().getAnimationSetFrames()[0] == 0 &&
18                    tl[i].getMesh().getAnimationSetFrames()[1] == 287)
19                {
20                    @kiki_obj = @tl[i];
21                    @kiki_mesh = @tl[i].getMesh();
22                    break;
23                }
24            }
25        }
26        if (kiki_mesh !is null && kiki_obj.getParentLibrary() !is null)
27        {
28            // Try to see if this kiki is used by a meta library
29            // kiki_obj is the kiki .spm in kiki library, so 2
30            // getParentLibrary() will do the job
31            Track::TrackObject@ meta =
32                kiki_obj.getParentLibrary().getParentLibrary();
33            if (meta !is null)
34            {
35                if (meta.getName() == "stklib_carousel_a")
36                {
37                    kiki_mesh.removeAllAnimationSet();
38                    kiki_mesh.addAnimationSet(0, 109);
39                    kiki_mesh.useAnimationSet(0);
40                    return;
41                }
42                else if (meta.getName() == "stklib_kikiPainter_a")
43                {
44                    kiki_mesh.removeAllAnimationSet();
45                    kiki_mesh.addAnimationSet(110, 287);
46                    kiki_mesh.useAnimationSet(0);
47                    return;
48                }
49            }
50        }
51
52        if (kiki_mesh !is null)
53        {
54            // For now use painting animation if kiki is used alone
55            kiki_mesh.removeAllAnimationSet();
56            kiki_mesh.addAnimationSet(110, 287);
57            kiki_mesh.useAnimationSet(0);
58        }
59    }
60}
61