Skip to content

Commit 2323cde

Browse files
committed
抛弃音频引擎自带的音频计时器,使用自制音频计时器
1 parent 31d0004 commit 2323cde

File tree

2 files changed

+55
-3
lines changed

2 files changed

+55
-3
lines changed

src/game.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Judgement from './judgement';
2+
import Timer from './timer';
23
import { Application, Container, Texture, Sprite, Graphics, Text, Rectangle } from 'pixi.js-legacy';
34

45
const ProgressBarCache = (() =>
@@ -128,6 +129,7 @@ export default class Game
128129
this._watermarkText = params.watermark && params.watermark != '' ? params.watermark : 'github/MisaLiu/phi-chart-render';
129130

130131
this._musicId = null;
132+
this._audioTimer = new Timer(this._settings.speed);
131133
this._audioOffset = 0;
132134
this._animateStatus = NaN;
133135
this._gameStartTime = NaN;
@@ -335,6 +337,8 @@ export default class Game
335337
this.judgement.input._isPaused = this._isPaused;
336338

337339
if (!this._musicId) return;
340+
341+
this._audioTimer.pause();
338342
if (this._isPaused)
339343
{
340344
this.chart.music.pause();
@@ -352,6 +356,7 @@ export default class Game
352356

353357
this.render.ticker.remove(this._calcTick);
354358
this.chart.music.stop();
359+
this._audioTimer.reset();
355360
this._musicId = null;
356361

357362
this.chart.reset();
@@ -470,13 +475,12 @@ export default class Game
470475
}
471476
case 1:
472477
{
473-
let currentTime = (this.chart.music.seek() || 0) - this.chart.offset + this._settings.offset;
474-
currentTime = currentTime > 0 ? currentTime : 0;
478+
let currentTime = this._audioTimer.time - this.chart.offset + this._settings.offset;
475479

476480
this.chart.calcTime(currentTime);
477481
if (!this._isPaused) this.judgement.calcTick();
478482

479-
this.sprites.progressBar.width = ((this.chart.music.seek() || 0) / this.chart.music._duration) * this.render.sizer.width;
483+
this.sprites.progressBar.width = (this._audioTimer.time / this.chart.music._duration) * this.render.sizer.width;
480484
break;
481485
}
482486
case 2:
@@ -527,6 +531,7 @@ export default class Game
527531
setTimeout(async () =>
528532
{
529533
this._musicId = this.chart.music.play();
534+
this._audioTimer.start();
530535

531536
for (const judgeline of this.chart.judgelines)
532537
{
@@ -553,6 +558,7 @@ export default class Game
553558
this._isPaused = true;
554559
this._isEnded = true;
555560
this._runCallback('end');
561+
this._audioTimer.reset();
556562
}
557563
}
558564
}

src/timer.js

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
export default class Timer
2+
{
3+
constructor(speed = 1)
4+
{
5+
this.speed = !isNaN(parseFloat(speed)) ? parseFloat((speed).toFixed(2)) : 1;
6+
7+
this.reset();
8+
}
9+
10+
reset()
11+
{
12+
this.startTime = NaN;
13+
this.pauseTime = NaN;
14+
this.isPaused = true;
15+
}
16+
17+
start()
18+
{
19+
if (!isNaN(this.startTime)) return;
20+
21+
this.startTime = Date.now();
22+
this.isPaused = false;
23+
}
24+
25+
pause()
26+
{
27+
if (isNaN(this.startTime)) return;
28+
29+
this.isPaused = !this.isPaused;
30+
31+
if (this.isPaused)
32+
{
33+
this.pauseTime = Date.now();
34+
}
35+
else
36+
{
37+
this.startTime = Date.now() - (this.pauseTime - this.startTime);
38+
this.pauseTime = NaN;
39+
}
40+
}
41+
42+
get time()
43+
{
44+
return (((!isNaN(this.pauseTime) ? this.pauseTime : Date.now()) - this.startTime) * this.speed) / 1000;
45+
}
46+
}

0 commit comments

Comments
 (0)