using System; using FMOD.Studio; using FMODUnity; using Gamecraft.Wires; using RobocraftX.Blocks; using Svelto.ECS; using Unity.Mathematics; using GamecraftModdingAPI; using GamecraftModdingAPI.Utility; namespace GamecraftModdingAPI.Blocks { public class MusicBlock : Block { public static MusicBlock PlaceNew(float3 position, float3 rotation = default, BlockColors color = BlockColors.Default, byte darkness = 0, int uscale = 1, float3 scale = default, Player player = null) { if (PlacementEngine.IsInGame && GameState.IsBuildMode()) { EGID id = PlacementEngine.PlaceBlock(BlockIDs.MusicBlock, color, darkness, position, uscale, scale, player, rotation); return new MusicBlock(id); } return null; } public MusicBlock(EGID id) : base(id) { if (!BlockEngine.GetBlockInfoExists(this.Id)) { throw new BlockTypeException($"Block is not a {this.GetType().Name} block"); } } public MusicBlock(uint id) : base(id) { if (!BlockEngine.GetBlockInfoExists(this.Id)) { throw new BlockTypeException($"Block is not a {this.GetType().Name} block"); } } public byte TrackIndex { get { return BlockEngine.GetBlockInfo(Id).trackIndx; } set { ref MusicBlockDataEntityStruct msdes = ref BlockEngine.GetBlockInfo(Id); msdes.trackIndx = value; } } public Guid Track { get { ref MusicBlockDataEntityStruct msdes = ref BlockEngine.GetBlockInfo(Id); return msdes.fmod2DEventPaths.Get(msdes.trackIndx); } set { ref MusicBlockDataEntityStruct msdes = ref BlockEngine.GetBlockInfo(Id); for (byte i = 0; i < msdes.fmod2DEventPaths.Count(); i++) { Guid track = msdes.fmod2DEventPaths.Get(i); if (track == value) { msdes.trackIndx = i; break; } } } } public Guid[] Tracks { get { ref MusicBlockDataEntityStruct msdes = ref BlockEngine.GetBlockInfo(Id); Guid[] tracks = new Guid[msdes.fmod2DEventPaths.Count()]; for (byte i = 0; i < tracks.Length; i++) { tracks[i] = msdes.fmod2DEventPaths.Get(i); } return tracks; } } public float Volume { get { return BlockEngine.GetBlockInfo(Id).tweakableVolume; } set { ref MusicBlockDataEntityStruct msdes = ref BlockEngine.GetBlockInfo(Id); msdes.tweakableVolume = value; } } public ChannelType ChannelType { get { return (ChannelType)BlockEngine.GetBlockInfo(Id).channelType; } set { ref MusicBlockDataEntityStruct msdes = ref BlockEngine.GetBlockInfo(Id); msdes.channelType = (byte)value; } } public bool IsPlaying { get { return BlockEngine.GetBlockInfo(Id).isPlaying; } set { ref MusicBlockDataEntityStruct msdes = ref BlockEngine.GetBlockInfo(Id); if (msdes.isPlaying == value) return; if (value) { // start playing EventInstance inst = RuntimeManager.CreateInstance(msdes.fmod2DEventPaths.Get(msdes.trackIndx)); inst.setVolume(msdes.tweakableVolume / 100f); inst.start(); msdes.eventHandle = inst.handle; } else { // stop playing EventInstance inst = default(EventInstance); inst.handle = msdes.eventHandle; inst.stop(FMOD.Studio.STOP_MODE.ALLOWFADEOUT); inst.release(); } msdes.isPlaying = value; } } } }