From 9e6edc19bdae238a43c18d457bee9e744fcb472f Mon Sep 17 00:00:00 2001 From: "NGnius (Graham)" Date: Wed, 23 Sep 2020 15:31:54 -0400 Subject: [PATCH] Implement SFX block API and bump version --- GamecraftModdingAPI/Block.cs | 8 + GamecraftModdingAPI/Blocks/MusicBlock.cs | 2 +- GamecraftModdingAPI/Blocks/SfxBlock.cs | 209 ++++++++++++++++++ .../GamecraftModdingAPI.csproj | 2 +- 4 files changed, 219 insertions(+), 2 deletions(-) create mode 100644 GamecraftModdingAPI/Blocks/SfxBlock.cs diff --git a/GamecraftModdingAPI/Block.cs b/GamecraftModdingAPI/Block.cs index 2e4161a..97386a3 100644 --- a/GamecraftModdingAPI/Block.cs +++ b/GamecraftModdingAPI/Block.cs @@ -135,6 +135,14 @@ namespace GamecraftModdingAPI CommonExclusiveGroups.BUILD_BUILDINGSPAWN_BLOCK_GROUP } }, + { + typeof(SfxBlock), + new[] + { + CommonExclusiveGroups.BUILD_SIMPLESFX_BLOCK_GROUP, + CommonExclusiveGroups.BUILD_LOOPEDSFX_BLOCK_GROUP + } + }, {typeof(TextBlock), new[] {CommonExclusiveGroups.BUILD_TEXT_BLOCK_GROUP}}, {typeof(Timer), new[] {CommonExclusiveGroups.BUILD_TIMER_BLOCK_GROUP}} }; diff --git a/GamecraftModdingAPI/Blocks/MusicBlock.cs b/GamecraftModdingAPI/Blocks/MusicBlock.cs index 185913b..fc3c880 100644 --- a/GamecraftModdingAPI/Blocks/MusicBlock.cs +++ b/GamecraftModdingAPI/Blocks/MusicBlock.cs @@ -97,7 +97,7 @@ namespace GamecraftModdingAPI.Blocks { get { - Assert.Log("Block exists: " + Exists); + //Assert.Log("Block exists: " + Exists); return BlockEngine.GetBlockInfo(this, (MusicBlockDataEntityStruct msdes) => (ChannelType) msdes.channelType); } diff --git a/GamecraftModdingAPI/Blocks/SfxBlock.cs b/GamecraftModdingAPI/Blocks/SfxBlock.cs new file mode 100644 index 0000000..f7efe6d --- /dev/null +++ b/GamecraftModdingAPI/Blocks/SfxBlock.cs @@ -0,0 +1,209 @@ +using System; +using FMOD.Studio; +using FMODUnity; +using Gamecraft.Wires; +using RobocraftX.Blocks; +using RobocraftX.Common; +using Svelto.ECS; + +namespace GamecraftModdingAPI.Blocks +{ + public class SfxBlock : SignalingBlock + { + public SfxBlock(EGID id) : base(id) + { + } + + public SfxBlock(uint id) : base(new EGID(id, CommonExclusiveGroups.BUILD_SIMPLESFX_BLOCK_GROUP /* This could also be BUILD_LOOPEDSFX_BLOCK_GROUP */)) + { + } + + public float Volume + { + get + { + return BlockEngine.GetBlockInfo(this, (SoundSfxBlockDataEntityStruct obj) => obj.tweakableVolume); + } + + set + { + BlockEngine.SetBlockInfo(this, + (ref SoundSfxBlockDataEntityStruct obj, float val) => obj.tweakableVolume = val, value); + } + } + + public float Pitch + { + get + { + return BlockEngine.GetBlockInfo(this, (SoundSfxBlockDataEntityStruct obj) => obj.tweakablePitch); + } + + set + { + BlockEngine.SetBlockInfo(this, + (ref SoundSfxBlockDataEntityStruct obj, float val) => obj.tweakablePitch = val, value); + } + } + + public bool Is3D + { + get + { + return BlockEngine.GetBlockInfo(this, (SoundSfxBlockDataEntityStruct obj) => obj.is3D); + } + + set + { + BlockEngine.SetBlockInfo(this, + (ref SoundSfxBlockDataEntityStruct obj, bool val) => obj.is3D = val, value); + } + } + + public ChannelType ChannelType + { + get + { + return BlockEngine.GetBlockInfo(this, (SoundSfxBlockDataEntityStruct obj) => (ChannelType)obj.channelType); + } + + set + { + BlockEngine.SetBlockInfo(this, + (ref SoundSfxBlockDataEntityStruct obj, ChannelType val) => obj.tweakableVolume = (byte) val, value); + } + } + + public byte TrackIndex + { + get + { + return BlockEngine.GetBlockInfo(this, (SoundSfxBlockDataEntityStruct obj) => obj.soundEffectIndex); + } + + set + { + BlockEngine.SetBlockInfo(this, + (ref SoundSfxBlockDataEntityStruct obj, byte val) => obj.soundEffectIndex = val, value); + } + } + + // track + public Guid Track + { + get + { + return BlockEngine.GetBlockInfo(this, + (SoundSfxBlockDataEntityStruct obj) => obj.is3D ? obj.fmod3DEventPaths.Get(obj.soundEffectIndex) : obj.fmod2DEventPaths.Get(obj.soundEffectIndex)); + } + + set + { + BlockEngine.SetBlockInfo(this, (ref SoundSfxBlockDataEntityStruct obj, Guid val) => + { + for (byte i = 0; i < obj.fmod2DEventPaths.Count(); i++) + { + Guid track = obj.fmod2DEventPaths.Get(i); + if (track == val) + { + obj.soundEffectIndex = i; + obj.is3D = false; + return; + } + } + for (byte i = 0; i < obj.fmod3DEventPaths.Count(); i++) + { + Guid track = obj.fmod3DEventPaths.Get(i); + if (track == val) + { + obj.soundEffectIndex = i; + obj.is3D = true; + return; + } + } + }, value); + } + } + + // all tracks + public Guid[] Tracks2D + { + get + { + return BlockEngine.GetBlockInfo(this, (SoundSfxBlockDataEntityStruct obj) => + { + Guid[] tracks = new Guid[obj.fmod2DEventPaths.Count()]; + for (byte i = 0; i < tracks.Length; i++) + { + tracks[i] = obj.fmod2DEventPaths.Get(i); + } + return tracks; + }); + } + } + + public Guid[] Tracks3D + { + get + { + return BlockEngine.GetBlockInfo(this, (SoundSfxBlockDataEntityStruct obj) => + { + Guid[] tracks = new Guid[obj.fmod3DEventPaths.Count()]; + for (byte i = 0; i < tracks.Length; i++) + { + tracks[i] = obj.fmod2DEventPaths.Get(i); + } + return tracks; + }); + } + } + + public bool IsLooped + { + get + { + return BlockEngine.GetBlockInfo(this, (SoundSfxBlockDataEntityStruct obj) => obj.isLoopedBlock); + } + + set + { + BlockEngine.SetBlockInfo(this, + (ref SoundSfxBlockDataEntityStruct obj, bool val) => obj.isLoopedBlock = val, value); + } + } + + public bool IsPlaying + { + get + { + return BlockEngine.GetBlockInfo(this, + (SoundSfxBlockDataEntityStruct obj) => obj.isPlaying); + } + + set + { + BlockEngine.SetBlockInfo(this, (ref SoundSfxBlockDataEntityStruct obj, bool val) => + { + if (obj.isPlaying == val) return; + if (val) + { + // start playing + EventInstance inst = RuntimeManager.CreateInstance(obj.is3D ? obj.fmod3DEventPaths.Get(obj.soundEffectIndex) : obj.fmod2DEventPaths.Get(obj.soundEffectIndex)); + inst.setVolume(obj.tweakableVolume / 100f); + inst.start(); + obj.eventHandle = inst.handle; + } + else + { + // stop playing + EventInstance inst = default(EventInstance); + inst.handle = obj.eventHandle; + inst.stop(FMOD.Studio.STOP_MODE.ALLOWFADEOUT); + inst.release(); + } + obj.isPlaying = val; + }, value); + } + } + } +} \ No newline at end of file diff --git a/GamecraftModdingAPI/GamecraftModdingAPI.csproj b/GamecraftModdingAPI/GamecraftModdingAPI.csproj index 7a7fe1d..193a14f 100644 --- a/GamecraftModdingAPI/GamecraftModdingAPI.csproj +++ b/GamecraftModdingAPI/GamecraftModdingAPI.csproj @@ -2,7 +2,7 @@ net472 true - 1.5.0-preview + 1.6.0-preview Exmods GNU General Public Licence 3+ https://git.exmods.org/modtainers/GamecraftModdingAPI