Sonolus Wiki

05. 音符生命周期

在本章中,我们将实现音符的生命周期逻辑。

音符原型

让我们先设置一个音符原型:

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

当我们设置时,让我们加上生命设置:

export class Note extends Archetype {
    globalPreprocess() {
        this.life.set({
            perfect: 10,
            great: 0,
            good: 0,
            miss: -100,
        })
    }
}
export class Note extends Archetype {
    globalPreprocess() {
        this.life.set({
            perfect: 10,
            great: 0,
            good: 0,
            miss: -100,
        })
    }
}

目标时间与可视时间

与游玩模式类似,我们可以使用音符的节拍来计算目标时间与可视时间:

export class Note extends Archetype {
    import = this.defineImport({
        beat: { name: EngineArchetypeDataName.Beat, type: Number },
    })

    targetTime = this.entityMemory(Number)

    visualTime = this.entityMemory({
        min: Number,
        max: Number,
    })

    // ...

    preprocess() {
        this.targetTime = bpmChanges.at(this.import.beat).time

        this.visualTime.max = timeScaleChanges.at(this.targetTime).scaledTime
        this.visualTime.min = this.visualTime.max - 120 / bpmChanges.at(this.import.beat).bpm
    }
}
export class Note extends Archetype {
    import = this.defineImport({
        beat: { name: EngineArchetypeDataName.Beat, type: Number },
    })

    targetTime = this.entityMemory(Number)

    visualTime = this.entityMemory({
        min: Number,
        max: Number,
    })

    // ...

    preprocess() {
        this.targetTime = bpmChanges.at(this.import.beat).time

        this.visualTime.max = timeScaleChanges.at(this.targetTime).scaledTime
        this.visualTime.min = this.visualTime.max - 120 / bpmChanges.at(this.import.beat).bpm
    }
}

生命周期

音符的生命周期就是它的可视时间:

export class Note extends Archetype {
    // ...

    spawnTime() {
        return this.visualTime.min
    }

    despawnTime() {
        return this.visualTime.max
    }
}
export class Note extends Archetype {
    // ...

    spawnTime() {
        return this.visualTime.min
    }

    despawnTime() {
        return this.visualTime.max
    }
}