Sonolus Wiki

06. Note Drawing

In this chapter, we will implement drawing logic of Note.

Declaring

Declare note's skin sprite:

export const skin = defineSkin({
    sprites: {
        // ...
        note: SkinSpriteName.NoteHeadCyan,
    },
})
export const skin = defineSkin({
    sprites: {
        // ...
        note: SkinSpriteName.NoteHeadCyan,
    },
})

Drawing

Similar to play mode, we calculate the z value in initialize callback, and draw the note sprite in updateParallel callback.

However, notes can spawn and despawn multiple times due to time skip by players, we should only calculate z value once. We can use an extra variable to skip unnecessary recalculation.

export class Note extends Archetype {
    // ...

    initialized = this.entityMemory(Boolean)

    // ...

    z = this.entityMemory(Number)

    // ...

    initialize() {
        if (this.initialized) return
        this.initialized = true

        this.z = 1000 - this.targetTime
    }

    updateParallel() {
        const y = Math.unlerp(this.visualTime.min, this.visualTime.max, time.scaled)

        const layout = Rect.one.mul(note.radius).scale(1, -1).translate(0, y)

        skin.sprites.note.draw(layout, this.z, 1)
    }
}
export class Note extends Archetype {
    // ...

    initialized = this.entityMemory(Boolean)

    // ...

    z = this.entityMemory(Number)

    // ...

    initialize() {
        if (this.initialized) return
        this.initialized = true

        this.z = 1000 - this.targetTime
    }

    updateParallel() {
        const y = Math.unlerp(this.visualTime.min, this.visualTime.max, time.scaled)

        const layout = Rect.one.mul(note.radius).scale(1, -1).translate(0, y)

        skin.sprites.note.draw(layout, this.z, 1)
    }
}