import { MinecraftDimensionTypes, world } from '@minecraft/server';
import ScriptingModule from '../scriptingModule';
import { isLocationInfected } from '../infectWorld/main';
import WitherbossModule from '../witherboss/witherbossModule';
import WBVector3 from '../util/wbVector3';
export default class MusicModule extends ScriptingModule {
    constructor() {
        super('MusicModule');
        // The set of players that are currently playing music
        this.activeMusicPlayers = [];
    }
    on3Seconds() {
        // Remove all players that are not online
        this.activeMusicPlayers = this.activeMusicPlayers.filter((player) => player.isValid());
        // Find all players that are in an infected area
        const infectedPlayers = world.getAllPlayers().filter((player) => {
            if (player.dimension.id !== MinecraftDimensionTypes.overworld) {
                return false;
            }
            if (isLocationInfected(player.location)) {
                return true;
            }
            const nearbyWitherBosses = WitherbossModule.WITHER_BOSSES.filter((witherBoss) => {
                const boss = witherBoss.getEntity();
                if (boss && WBVector3.distanceSquared(player.location, boss.location) < 60 * 60) {
                    return true;
                }
                return false;
            });
            return nearbyWitherBosses.length > 0;
        });
        // Find all infected players not playing music and start playing music for them
        infectedPlayers.filter((player) => !this.activeMusicPlayers.includes(player)).forEach((player) => {
            this.activeMusicPlayers.push(player);
            player.runCommand('/music stop 5');
            player.playMusic('twb_wisu:music.infected.ambience', { volume: 0.25, fade: 5, loop: true });
        });
        // Find all players that are playing music but are not in an infected area
        this.activeMusicPlayers.filter((player) => !infectedPlayers.includes(player)).forEach((player) => {
            this.activeMusicPlayers = this.activeMusicPlayers.filter((p) => p !== player);
            player.runCommand('/music stop 5');
        });
    }
}
