TechbloxModdingAPI/GamecraftModdingAPI/Blocks/MusicBlock.cs
2020-07-19 16:39:35 -04:00

157 lines
No EOL
4.9 KiB
C#

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<MusicBlockDataEntityStruct>(this.Id))
{
throw new BlockTypeException($"Block is not a {this.GetType().Name} block");
}
}
public MusicBlock(uint id) : base(id)
{
if (!BlockEngine.GetBlockInfoExists<MusicBlockDataEntityStruct>(this.Id))
{
throw new BlockTypeException($"Block is not a {this.GetType().Name} block");
}
}
public byte TrackIndex
{
get
{
return BlockEngine.GetBlockInfo<MusicBlockDataEntityStruct>(Id).trackIndx;
}
set
{
ref MusicBlockDataEntityStruct msdes = ref BlockEngine.GetBlockInfo<MusicBlockDataEntityStruct>(Id);
msdes.trackIndx = value;
}
}
public Guid Track
{
get
{
ref MusicBlockDataEntityStruct msdes = ref BlockEngine.GetBlockInfo<MusicBlockDataEntityStruct>(Id);
return msdes.fmod2DEventPaths.Get<Guid>(msdes.trackIndx);
}
set
{
ref MusicBlockDataEntityStruct msdes = ref BlockEngine.GetBlockInfo<MusicBlockDataEntityStruct>(Id);
for (byte i = 0; i < msdes.fmod2DEventPaths.Count<Guid>(); i++)
{
Guid track = msdes.fmod2DEventPaths.Get<Guid>(i);
if (track == value)
{
msdes.trackIndx = i;
break;
}
}
}
}
public Guid[] Tracks
{
get
{
ref MusicBlockDataEntityStruct msdes = ref BlockEngine.GetBlockInfo<MusicBlockDataEntityStruct>(Id);
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<MusicBlockDataEntityStruct>(Id).tweakableVolume;
}
set
{
ref MusicBlockDataEntityStruct msdes = ref BlockEngine.GetBlockInfo<MusicBlockDataEntityStruct>(Id);
msdes.tweakableVolume = value;
}
}
public ChannelType ChannelType
{
get
{
return (ChannelType)BlockEngine.GetBlockInfo<MusicBlockDataEntityStruct>(Id).channelType;
}
set
{
ref MusicBlockDataEntityStruct msdes = ref BlockEngine.GetBlockInfo<MusicBlockDataEntityStruct>(Id);
msdes.channelType = (byte)value;
}
}
public bool IsPlaying
{
get
{
return BlockEngine.GetBlockInfo<MusicBlockDataEntityStruct>(Id).isPlaying;
}
set
{
ref MusicBlockDataEntityStruct msdes = ref BlockEngine.GetBlockInfo<MusicBlockDataEntityStruct>(Id);
if (msdes.isPlaying == value) return;
if (value)
{
// 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 = value;
}
}
}
}