Sonolus Wiki

07. Note and Entity Data

In this chapter, we will set up Note and look at how to integrate Entity Data into it.

Note Archetype

Let's first set up Note:

export class Note extends Archetype {}
export class Note extends Archetype {}
export const archetypes = defineArchetypes({
    // ...
    Note,
})
export const archetypes = defineArchetypes({
    // ...
    Note,
})

Entity Data

Until now we only have Initialization and Stage, both should behave the same across all levels.

However that is not the case for notes: in one level maybe the first note is at 5 second mark, while in another level it could be at 2; one level could have 200 notes, while another could have 30.

How would engine be able to handle varying amount of notes with different information provided by the level? That's where Entity Data comes into play. Each level can specify all the entities and also inject data into them.

Let's define that Note has a time data, which is simply time of the note in seconds. We can import the data using its name:

export class Note extends Archetype {
    import = this.defineImport({
        time: { name: 'time', type: Number },
    })
}
export class Note extends Archetype {
    import = this.defineImport({
        time: { name: 'time', type: Number },
    })
}

Now we can simply access it. To test it out, let's log it:

export class Note extends Archetype {
    // ...

    updateParallel() {
        debug.log(this.import.time)
    }
}
export class Note extends Archetype {
    // ...

    updateParallel() {
        debug.log(this.import.time)
    }
}

Lastly, let's add a note entity to our level, also giving it time data:

export const data: LevelData = {
    // ...
    entities: [
        // ...
        {
            archetype: 'Note',
            data: [
                {
                    name: 'time',
                    value: 2,
                },
            ],
        },
    ],
}
export const data = {
    // ...
    entities: [
        // ...
        {
            archetype: 'Note',
            data: [
                {
                    name: 'time',
                    value: 2,
                },
            ],
        },
    ],
}