TechbloxModdingAPI/GamecraftModdingAPI/Blocks/MusicBlock.cs
2020-11-09 16:18:25 -05:00

146 lines
No EOL
4.4 KiB
C#

using System;
using FMOD.Studio;
using FMODUnity;
using Gamecraft.Wires;
using RobocraftX.Common;
using RobocraftX.Blocks;
using Svelto.ECS;
using Unity.Mathematics;
using GamecraftModdingAPI;
using GamecraftModdingAPI.Tests;
using GamecraftModdingAPI.Utility;
namespace GamecraftModdingAPI.Blocks
{
public class MusicBlock : SignalingBlock
{
public MusicBlock(EGID id) : base(id)
{
}
public MusicBlock(uint id) : base(new EGID(id, CommonExclusiveGroups.MUSIC_BLOCK_GROUP))
{
}
public byte TrackIndex
{
get
{
return BlockEngine.GetBlockInfo(this, (MusicBlockDataEntityStruct st) => st.trackIndx);
}
set
{
BlockEngine.SetBlockInfo(this,
(ref MusicBlockDataEntityStruct msdes, byte val) => msdes.trackIndx = val, value);
}
}
public Guid Track
{
get
{
return BlockEngine.GetBlockInfo(this,
(MusicBlockDataEntityStruct msdes) => msdes.fmod2DEventPaths.Get<Guid>(msdes.trackIndx));
}
set
{
BlockEngine.SetBlockInfo(this, (ref MusicBlockDataEntityStruct msdes, Guid val) =>
{
for (byte i = 0; i < msdes.fmod2DEventPaths.Count<Guid>(); i++)
{
Guid track = msdes.fmod2DEventPaths.Get<Guid>(i);
if (track == val)
{
msdes.trackIndx = i;
break;
}
}
}, value);
}
}
public Guid[] Tracks
{
get
{
return BlockEngine.GetBlockInfo(this, (MusicBlockDataEntityStruct msdes) =>
{
Guid[] tracks = new Guid[msdes.fmod2DEventPaths.Count<Guid>()];
for (byte i = 0; i < tracks.Length; i++)
{
tracks[i] = msdes.fmod2DEventPaths.Get<Guid>(i);
}
return tracks;
});
}
}
public float Volume
{
get
{
return BlockEngine.GetBlockInfo(this, (MusicBlockDataEntityStruct msdes) => msdes.tweakableVolume);
}
set
{
BlockEngine.SetBlockInfo(this,
(ref MusicBlockDataEntityStruct msdes, float val) => msdes.tweakableVolume = val, value);
}
}
public ChannelType ChannelType
{
get
{
//Assert.Log("Block exists: " + Exists);
return BlockEngine.GetBlockInfo(this,
(MusicBlockDataEntityStruct msdes) => (ChannelType) msdes.channelType);
}
set
{
BlockEngine.SetBlockInfo(this,
(ref MusicBlockDataEntityStruct msdes, ChannelType val) => msdes.channelType = (byte) val, value);
}
}
public bool IsPlaying
{
get
{
return BlockEngine.GetBlockInfo(this,
(MusicBlockDataEntityStruct msdes) => msdes.isPlaying);
}
set
{
BlockEngine.SetBlockInfo(this, (ref MusicBlockDataEntityStruct msdes, bool val) =>
{
if (msdes.isPlaying == val) return;
if (val)
{
// start playing
EventInstance inst = RuntimeManager.CreateInstance(msdes.fmod2DEventPaths.Get<Guid>(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 = val;
}, value);
}
}
}
}