Skip to content

Commit

Permalink
let beatmaps set the background
Browse files Browse the repository at this point in the history
  • Loading branch information
konekowo committed Sep 28, 2024
1 parent ab227ee commit 7b43521
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 20 deletions.
15 changes: 6 additions & 9 deletions src/Audio/AudioEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class AudioEngine {
private readonly _playingAudios: PlayingAudios;
private _musicQueue: MapAudio[] = [];
private _audioIdTicker: number = 0;
private _changeCallbacks: (() => void)[] = [];
private _changeCallbacks: ((mapAudio: MapAudio) => void)[] = [];
private silentMusic = this.createSilentMusic();
public useSilentMusic = true;

Expand All @@ -26,12 +26,12 @@ export class AudioEngine {
if (this._musicQueue[0]) {
if (!this._musicQueue[0].fadingOut && this._musicQueue[0].GetCurrentTime() == 0) {
this._play(this._musicQueue[0]);
this._changeCallbacks.forEach((cb) => cb());
this._changeCallbacks.forEach((cb) => cb(this._musicQueue[0]));
}
if (this._musicQueue[0].fadingOut && this._musicQueue[1]) {
if (this._musicQueue[1]) {
this._play(this._musicQueue[1]);
this._changeCallbacks.forEach((cb) => cb());
this._changeCallbacks.forEach((cb) => cb(this._musicQueue[1]));
}
}
}
Expand All @@ -56,11 +56,11 @@ export class AudioEngine {
return mapAudio;
}

public addMusicChangeEventListener(cb: () => void) {
public addMusicChangeEventListener(cb: (() => void) | ((mapAudio: MapAudio) => void)) {
this._changeCallbacks.push(cb);
}

public removeMusicChangeEventListener(cb: () => void) {
public removeMusicChangeEventListener(cb: (() => void) | ((mapAudio: MapAudio) => void)) {
this._changeCallbacks = this._changeCallbacks.filter(callback => callback != cb);
}

Expand Down Expand Up @@ -97,13 +97,10 @@ export class AudioEngine {

public PlayMusicImmediately(mapAudio: string, beatMapData: BeatmapData, musicPlayingCallback?: () => void) {
let currentPlaying = this.GetCurrentPlayingMusicNoSilent();
this._musicQueue = [];
if (currentPlaying) {
this._musicQueue = [currentPlaying];
currentPlaying.FadeOut();
}
else {
this._musicQueue = []
}
this.AddToMusicQueue(mapAudio, beatMapData, musicPlayingCallback);
}

Expand Down
13 changes: 11 additions & 2 deletions src/Elements/RandomBackground/RandomBackground.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,17 @@ export class RandomBackground extends Screen {
(Main.mousePos.y - (this.getScreenHeight() / 2)) / this.parallaxMultiplier);
this.addChild(this.bgContainer);
this.newRandomBG();
Main.AudioEngine.addMusicChangeEventListener(() => {
this.newRandomBG();
Main.AudioEngine.addMusicChangeEventListener((audio) => {
if (audio.beatmap.background) {
let url = URL.createObjectURL(audio.beatmap.background);
let texture = PIXI.Assets.load({src: url, loadParser: 'loadTextures'});
texture.then((texture) => {
this.setBG(texture);
})
}
else {
this.newRandomBG();
}
});
this.zIndex = -100;
}
Expand Down
2 changes: 2 additions & 0 deletions src/Util/Beatmap/Data/BeatmapData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {ColorsData} from "./Sections/Colors/ColorsData";
* <a href="https://osu.ppy.sh/wiki/en/Client/File_formats/osu_%28file_format%29">.osu (file format)</a>
*/
export class BeatmapData {
public background: Blob | null = null;

/**
* General information about the beatmap
*/
Expand Down
2 changes: 2 additions & 0 deletions src/Util/Beatmap/Parser/BeatmapParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {GeneralParser} from "./GeneralParser";
import {EditorParser} from "./EditorParser";
import {MetadataParser} from "./MetadataParser";
import {DifficultyParser} from "./DifficultyParser";
import {EventsParser} from "./EventsParser";

export class BeatmapParser {
public static Parse(osuFileContent: string, storyBoardFileContent?: string): BeatmapData {
Expand All @@ -13,6 +14,7 @@ export class BeatmapParser {
EditorParser.ParseEditor(beatMapData, BeatmapParser.GetSection("Editor", osuFileContentLines));
MetadataParser.ParseMetadata(beatMapData, BeatmapParser.GetSection("Metadata", osuFileContentLines));
DifficultyParser.ParseDifficulty(beatMapData, BeatmapParser.GetSection("Difficulty", osuFileContentLines));
EventsParser.ParseEvents(beatMapData, BeatmapParser.GetSection("Events", osuFileContentLines));
TimingPointsParser.ParseTimingPoints(beatMapData, BeatmapParser.GetSection("TimingPoints", osuFileContentLines));
return beatMapData
}
Expand Down
22 changes: 22 additions & 0 deletions src/Util/Beatmap/Parser/EventsParser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {BeatmapData} from "../Data/BeatmapData";
import {EventTypes} from "../Data/Sections/Events/EventTypes";
import {EventBackground} from "../Data/Sections/Events/EventBackground";

export class EventsParser {
public static ParseEvents(beatmapData: BeatmapData, section: string[]) {
section.forEach((str) => {
let values = str.split(",");
let event;
if (parseInt(values[0]) == EventTypes.BACKGROUND) {
event = new EventBackground();
event.filename = values[2].replaceAll('"', "");
event.xOffset = parseInt(values[3]);
event.yOffset = parseInt(values[3]);
event.startTime = 0;
}
if (event) {
beatmapData.Events.Events.push(event);
}
});
}
}
30 changes: 21 additions & 9 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import * as TWEEN from "@tweenjs/tween.js";
import {SettingsPane} from "./Elements/Settings/SettingsPane";
import {unzip} from "unzipit";
import {BeatmapParser} from "./Util/Beatmap/Parser/BeatmapParser";
import {EventTypes} from "./Util/Beatmap/Data/Sections/Events/EventTypes";
import {EventBackground} from "./Util/Beatmap/Data/Sections/Events/EventBackground";

export class Main {
public static app: Application;
Expand All @@ -35,18 +37,28 @@ export class Main {
unzip(file).then(({entries}) => {
for (const [name, entry] of Object.entries(entries)) {
if (name.endsWith(".osu")) {
entry.text().then((osuFile) => {
entry.text().then(async (osuFile) => {
let beatmapData = BeatmapParser.Parse(osuFile);
console.log(beatmapData);
let bgEvent: EventBackground | undefined =
beatmapData.Events.Events.find((event) => {
if (event instanceof EventBackground) return event;
}) as EventBackground;
if (bgEvent) {
for (const [name, entry] of Object.entries(entries)) {
if (name == bgEvent.filename) {
beatmapData.background = await entry.blob();
}
}
}
for (const [name, entry] of Object.entries(entries)) {
if (name == beatmapData.General.AudioFilename) {
entry.blob().then(blob => {
let url = URL.createObjectURL(blob);
Main.AudioEngine.PlayMusicImmediately(url, beatmapData, () => {
console.log("Now playing " + beatmapData.Metadata.TitleUnicode + " - " +beatmapData.Metadata.ArtistUnicode +
" ("+beatmapData.Metadata.Title + " - " + beatmapData.Metadata.Artist + ")");
});
let blob = await entry.blob()
let url = URL.createObjectURL(blob);
Main.AudioEngine.PlayMusicImmediately(url, beatmapData, () => {
console.log("Now playing " + beatmapData.Metadata.TitleUnicode + " - " + beatmapData.Metadata.ArtistUnicode +
" (" + beatmapData.Metadata.Title + " - " + beatmapData.Metadata.Artist + ")");
});
console.log(beatmapData);
}
}
});
Expand All @@ -65,7 +77,7 @@ export class Main {
Main.app.canvas.setAttribute("ondragover", "window.onDragEvent(event);");

document.body.appendChild(Main.app.canvas);

Main.settingsPane = new SettingsPane();
Main.settingsPane.zIndex = 999998;
Main.app.stage.addChild(Main.settingsPane);
Expand Down

0 comments on commit 7b43521

Please sign in to comment.