Skip to content

Commit

Permalink
抛弃音频引擎自带的音频计时器,使用自制音频计时器
Browse files Browse the repository at this point in the history
  • Loading branch information
MisaLiu committed Nov 28, 2022
1 parent 31d0004 commit 2323cde
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/game.js
@@ -1,4 +1,5 @@
import Judgement from './judgement';
import Timer from './timer';
import { Application, Container, Texture, Sprite, Graphics, Text, Rectangle } from 'pixi.js-legacy';

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

this._musicId = null;
this._audioTimer = new Timer(this._settings.speed);
this._audioOffset = 0;
this._animateStatus = NaN;
this._gameStartTime = NaN;
Expand Down Expand Up @@ -335,6 +337,8 @@ export default class Game
this.judgement.input._isPaused = this._isPaused;

if (!this._musicId) return;

this._audioTimer.pause();
if (this._isPaused)
{
this.chart.music.pause();
Expand All @@ -352,6 +356,7 @@ export default class Game

this.render.ticker.remove(this._calcTick);
this.chart.music.stop();
this._audioTimer.reset();
this._musicId = null;

this.chart.reset();
Expand Down Expand Up @@ -470,13 +475,12 @@ export default class Game
}
case 1:
{
let currentTime = (this.chart.music.seek() || 0) - this.chart.offset + this._settings.offset;
currentTime = currentTime > 0 ? currentTime : 0;
let currentTime = this._audioTimer.time - this.chart.offset + this._settings.offset;

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

this.sprites.progressBar.width = ((this.chart.music.seek() || 0) / this.chart.music._duration) * this.render.sizer.width;
this.sprites.progressBar.width = (this._audioTimer.time / this.chart.music._duration) * this.render.sizer.width;
break;
}
case 2:
Expand Down Expand Up @@ -527,6 +531,7 @@ export default class Game
setTimeout(async () =>
{
this._musicId = this.chart.music.play();
this._audioTimer.start();

for (const judgeline of this.chart.judgelines)
{
Expand All @@ -553,6 +558,7 @@ export default class Game
this._isPaused = true;
this._isEnded = true;
this._runCallback('end');
this._audioTimer.reset();
}
}
}
Expand Down
46 changes: 46 additions & 0 deletions src/timer.js
@@ -0,0 +1,46 @@
export default class Timer
{
constructor(speed = 1)
{
this.speed = !isNaN(parseFloat(speed)) ? parseFloat((speed).toFixed(2)) : 1;

this.reset();
}

reset()
{
this.startTime = NaN;
this.pauseTime = NaN;
this.isPaused = true;
}

start()
{
if (!isNaN(this.startTime)) return;

this.startTime = Date.now();
this.isPaused = false;
}

pause()
{
if (isNaN(this.startTime)) return;

this.isPaused = !this.isPaused;

if (this.isPaused)
{
this.pauseTime = Date.now();
}
else
{
this.startTime = Date.now() - (this.pauseTime - this.startTime);
this.pauseTime = NaN;
}
}

get time()
{
return (((!isNaN(this.pauseTime) ? this.pauseTime : Date.now()) - this.startTime) * this.speed) / 1000;
}
}

0 comments on commit 2323cde

Please sign in to comment.