From fe25b11af4cad31ec2f8c41c8c7de76461268f46 Mon Sep 17 00:00:00 2001 From: Burrito Date: Mon, 2 Dec 2024 00:47:04 +0800 Subject: [PATCH] Refactor to use range (#31) --- .../notes/visibleNotes/VisibleNote.mts | 34 ++++------------- .../sliderConnectors/SlideConnector.mts | 7 ++-- shared/src/engine/data/windows.mts | 37 +++++++++++++++++-- .../notes/visibleNotes/VisibleNote.mts | 34 ++++------------- .../sliderConnectors/SlideConnector.mts | 7 ++-- 5 files changed, 56 insertions(+), 63 deletions(-) diff --git a/play/src/engine/playData/archetypes/notes/visibleNotes/VisibleNote.mts b/play/src/engine/playData/archetypes/notes/visibleNotes/VisibleNote.mts index baa66a3..93e80e4 100644 --- a/play/src/engine/playData/archetypes/notes/visibleNotes/VisibleNote.mts +++ b/play/src/engine/playData/archetypes/notes/visibleNotes/VisibleNote.mts @@ -1,5 +1,10 @@ import { approach } from '../../../../../../../shared/src/engine/data/note.mjs' import { perspectiveLayout } from '../../../../../../../shared/src/engine/data/utils.mjs' +import { + DualWindows, + toBucketWindows, + toWindows, +} from '../../../../../../../shared/src/engine/data/windows.mjs' import { options } from '../../../../configuration/options.mjs' import { lane } from '../../../lane.mjs' import { note } from '../../../note.mjs' @@ -12,10 +17,7 @@ export abstract class VisibleNote extends Note { accuracyDiff: { name: 'accuracyDiff', type: Number }, }) - abstract dualWindows: { - normal: JudgmentWindows - strict: JudgmentWindows - } + abstract dualWindows: DualWindows abstract bucket: Bucket @@ -29,16 +31,7 @@ export abstract class VisibleNote extends Note { y = this.entityMemory(Number) globalPreprocess() { - const toMs = ({ min, max }: RangeLike) => ({ - min: Math.round(min * 1000), - max: Math.round(max * 1000), - }) - - this.bucket.set({ - perfect: toMs(this.windows.perfect), - great: toMs(this.windows.great), - good: toMs(this.windows.good), - }) + this.bucket.set(toBucketWindows(this.windows)) this.life.miss = -100 } @@ -77,18 +70,7 @@ export abstract class VisibleNote extends Note { } get windows() { - const dualWindows = this.dualWindows - - const toWindow = (key: 'perfect' | 'great' | 'good') => ({ - min: options.strictJudgment ? dualWindows.strict[key].min : dualWindows.normal[key].min, - max: options.strictJudgment ? dualWindows.strict[key].max : dualWindows.normal[key].max, - }) - - return { - perfect: toWindow('perfect'), - great: toWindow('great'), - good: toWindow('good'), - } + return toWindows(this.dualWindows, options.strictJudgment) } get shouldScheduleSFX() { diff --git a/play/src/engine/playData/archetypes/sliderConnectors/SlideConnector.mts b/play/src/engine/playData/archetypes/sliderConnectors/SlideConnector.mts index 3b8fc45..9aacc00 100644 --- a/play/src/engine/playData/archetypes/sliderConnectors/SlideConnector.mts +++ b/play/src/engine/playData/archetypes/sliderConnectors/SlideConnector.mts @@ -36,8 +36,8 @@ export abstract class SlideConnector extends Archetype { visualTime = this.entityMemory({ min: Number, - hidden: Number, }) + hiddenTime = this.entityMemory(Number) spawnTime = this.entityMemory(Number) @@ -82,8 +82,7 @@ export abstract class SlideConnector extends Archetype { this.tail.l = this.tail.lane - w this.tail.r = this.tail.lane + w - if (options.hidden > 0) - this.visualTime.hidden = this.tail.time - note.duration * options.hidden + if (options.hidden > 0) this.hiddenTime = this.tail.time - note.duration * options.hidden this.connector.z = getZ(layer.note.connector, this.head.time, this.headImport.lane) @@ -151,7 +150,7 @@ export abstract class SlideConnector extends Archetype { } renderConnector() { - if (options.hidden > 0 && time.now > this.visualTime.hidden) return + if (options.hidden > 0 && time.now > this.hiddenTime) return const hiddenDuration = options.hidden > 0 ? note.duration * options.hidden : 0 diff --git a/shared/src/engine/data/windows.mts b/shared/src/engine/data/windows.mts index 536e546..7ea5179 100644 --- a/shared/src/engine/data/windows.mts +++ b/shared/src/engine/data/windows.mts @@ -1,10 +1,41 @@ +type Windows = { + perfect: Range + great: Range + good: Range +} + +export type DualWindows = { + normal: Windows + strict: Windows +} + +export const toWindows = (dualWindows: DualWindows, strictJudgment: boolean) => { + const toWindow = (key: 'perfect' | 'great' | 'good') => + new Range( + strictJudgment ? dualWindows.strict[key].min : dualWindows.normal[key].min, + strictJudgment ? dualWindows.strict[key].max : dualWindows.normal[key].max, + ) + + return { + perfect: toWindow('perfect'), + great: toWindow('great'), + good: toWindow('good'), + } +} + +const toMs = ({ min, max }: Range) => new Range(Math.round(min * 1000), Math.round(max * 1000)) + +export const toBucketWindows = (windows: Windows) => ({ + perfect: toMs(windows.perfect), + great: toMs(windows.great), + good: toMs(windows.good), +}) + type Seconds = number | [min: number, max: number] const fromSeconds = (perfect: Seconds, great: Seconds, good: Seconds) => { const toWindow = (seconds: Seconds) => - typeof seconds === 'number' - ? { min: -seconds, max: seconds } - : { min: seconds[0], max: seconds[1] } + typeof seconds === 'number' ? Range.one.mul(seconds) : new Range(...seconds) return { perfect: toWindow(perfect), diff --git a/watch/src/engine/watchData/archetypes/notes/visibleNotes/VisibleNote.mts b/watch/src/engine/watchData/archetypes/notes/visibleNotes/VisibleNote.mts index a96dda9..0013f8f 100644 --- a/watch/src/engine/watchData/archetypes/notes/visibleNotes/VisibleNote.mts +++ b/watch/src/engine/watchData/archetypes/notes/visibleNotes/VisibleNote.mts @@ -1,6 +1,11 @@ import { lane } from '../../../../../../../shared/src/engine/data/lane.mjs' import { approach } from '../../../../../../../shared/src/engine/data/note.mjs' import { perspectiveLayout } from '../../../../../../../shared/src/engine/data/utils.mjs' +import { + DualWindows, + toBucketWindows, + toWindows, +} from '../../../../../../../shared/src/engine/data/windows.mjs' import { options } from '../../../../configuration/options.mjs' import { note } from '../../../note.mjs' import { particle } from '../../../particle.mjs' @@ -8,10 +13,7 @@ import { getZ, layer } from '../../../skin.mjs' import { Note } from '../Note.mjs' export abstract class VisibleNote extends Note { - abstract dualWindows: { - normal: JudgmentWindows - strict: JudgmentWindows - } + abstract dualWindows: DualWindows abstract bucket: Bucket @@ -33,16 +35,7 @@ export abstract class VisibleNote extends Note { y = this.entityMemory(Number) globalPreprocess() { - const toMs = ({ min, max }: RangeLike) => ({ - min: Math.round(min * 1000), - max: Math.round(max * 1000), - }) - - this.bucket.set({ - perfect: toMs(this.windows.perfect), - great: toMs(this.windows.great), - good: toMs(this.windows.good), - }) + this.bucket.set(toBucketWindows(this.windows)) this.life.miss = -100 } @@ -102,18 +95,7 @@ export abstract class VisibleNote extends Note { } get windows() { - const dualWindows = this.dualWindows - - const toWindow = (key: 'perfect' | 'great' | 'good') => ({ - min: options.strictJudgment ? dualWindows.strict[key].min : dualWindows.normal[key].min, - max: options.strictJudgment ? dualWindows.strict[key].max : dualWindows.normal[key].max, - }) - - return { - perfect: toWindow('perfect'), - great: toWindow('great'), - good: toWindow('good'), - } + return toWindows(this.dualWindows, options.strictJudgment) } get hitTime() { diff --git a/watch/src/engine/watchData/archetypes/sliderConnectors/SlideConnector.mts b/watch/src/engine/watchData/archetypes/sliderConnectors/SlideConnector.mts index c83543d..b39866d 100644 --- a/watch/src/engine/watchData/archetypes/sliderConnectors/SlideConnector.mts +++ b/watch/src/engine/watchData/archetypes/sliderConnectors/SlideConnector.mts @@ -36,8 +36,8 @@ export abstract class SlideConnector extends Archetype { visualTime = this.entityMemory({ min: Number, - hidden: Number, }) + hiddenTime = this.entityMemory(Number) connector = this.entityMemory({ z: Number, @@ -165,8 +165,7 @@ export abstract class SlideConnector extends Archetype { this.tail.l = this.tail.lane - w this.tail.r = this.tail.lane + w - if (options.hidden > 0) - this.visualTime.hidden = this.tail.time - note.duration * options.hidden + if (options.hidden > 0) this.hiddenTime = this.tail.time - note.duration * options.hidden this.connector.z = getZ(layer.note.connector, this.head.time, this.headImport.lane) @@ -192,7 +191,7 @@ export abstract class SlideConnector extends Archetype { } renderConnector() { - if (options.hidden > 0 && time.now > this.visualTime.hidden) return + if (options.hidden > 0 && time.now > this.hiddenTime) return const hiddenDuration = options.hidden > 0 ? note.duration * options.hidden : 0