Skip to content

Commit

Permalink
更新播放网络文件的实现逻辑,以符合规范要求 (#30)
Browse files Browse the repository at this point in the history
* 更新播放网络文件的实现逻辑,以符合规范要求

* 难绷注释

* 轻度改造

---------

Co-authored-by: 永安404 <[email protected]>
  • Loading branch information
ShirakamiAme and YongAn404 authored Dec 17, 2024
1 parent 921b54d commit a185740
Showing 1 changed file with 71 additions and 4 deletions.
75 changes: 71 additions & 4 deletions Roles/MusicManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
using SCPSLAudioApi.AudioCore;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using UnityEngine;
using YongAnFrame.Players;
using static SCPSLAudioApi.AudioCore.AudioPlayerBase;
Expand Down Expand Up @@ -172,10 +175,74 @@ public AudioPlayerBase Play(string musicFile, string npcName, TrackEvent? trackE
}
return audioPlayerBase;
}

public readonly struct TrackEvent(TrackLoaded trackLoaded)

/// <summary>
/// 播放音频(Url)
/// </summary>
/// <param name="musicUrl">音频文件</param>
/// <param name="npcName">NPC名称</param>
/// <param name="trackEvent">播放事件(可null)</param>
/// <param name="source">传播距离检测源头玩家(可null,null时是NPC)</param>
/// <param name="distance">传播距离(-1时是全部玩家,0时是源头玩家)</param>
/// <param name="extraPlay">额外可接收音频的玩家(可null)</param>
/// <param name="isSole">[弃用]是否覆盖播放</param>
/// <param name="volume">音量大小</param>
/// <param name="isLoop">是否循环</param>
/// <returns></returns>
public AudioPlayerBase PlayUrl(string musicUrl, string npcName, TrackEvent? trackEvent, FramePlayer source, float distance, FramePlayer[] extraPlay, bool isSole = false, float volume = 80, bool isLoop = false)
{
public TrackLoaded TrackLoaded { get; } = trackLoaded;
AudioPlayerBase audioPlayerBase = null;
try
{
if (trackEvent.HasValue)
{
OnTrackLoaded += trackEvent.Value.TrackLoaded;
}

ReferenceHub npc = CreateMusicNpc(npcName);
audioPlayerBase = Get(npc);

if (distance != -1)
{
if (source != null)
{
if (distance == 0)
{
audioPlayerBase.BroadcastTo.Add(npc.PlayerId);
}
else
{
audioPlayerBase.BroadcastTo = FramePlayer.List.Where(p => Vector3.Distance(p.ExPlayer.Position, source.ExPlayer.Position) <= distance).Select((s) => s.ExPlayer.Id).ToList();
}
}

if (extraPlay != null)
{
foreach (var player in extraPlay)
{
if (!audioPlayerBase.BroadcastTo.Contains(player.ExPlayer.Id))
{
audioPlayerBase.BroadcastTo.Add(player.ExPlayer.Id);
}
}
}
}

audioPlayerBase.CurrentPlay = musicUrl;
audioPlayerBase.Volume = volume;
audioPlayerBase.Loop = isLoop;
audioPlayerBase.AllowUrl = true;
audioPlayerBase.Play(-1);
}
catch (Exception)
{
Stop(audioPlayerBase);
}
return audioPlayerBase;
}
}
}
public readonly struct TrackEvent(TrackLoaded trackLoaded)
{
public TrackLoaded TrackLoaded { get; } = trackLoaded;
}
}

0 comments on commit a185740

Please sign in to comment.