A stable modding interface between Techblox and mods https://mod.exmods.org/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SfxBlock.cs 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. using System;
  2. using FMOD.Studio;
  3. using FMODUnity;
  4. using Gamecraft.Wires;
  5. using RobocraftX.Blocks;
  6. using RobocraftX.Common;
  7. using Svelto.ECS;
  8. namespace GamecraftModdingAPI.Blocks
  9. {
  10. public class SfxBlock : SignalingBlock
  11. {
  12. public SfxBlock(EGID id) : base(id)
  13. {
  14. }
  15. public SfxBlock(uint id) : base(new EGID(id, CommonExclusiveGroups.BUILD_SIMPLESFX_BLOCK_GROUP /* This could also be BUILD_LOOPEDSFX_BLOCK_GROUP */))
  16. {
  17. }
  18. public float Volume
  19. {
  20. get
  21. {
  22. return BlockEngine.GetBlockInfo(this, (SoundSfxBlockDataEntityStruct obj) => obj.tweakableVolume);
  23. }
  24. set
  25. {
  26. BlockEngine.SetBlockInfo(this,
  27. (ref SoundSfxBlockDataEntityStruct obj, float val) => obj.tweakableVolume = val, value);
  28. }
  29. }
  30. public float Pitch
  31. {
  32. get
  33. {
  34. return BlockEngine.GetBlockInfo(this, (SoundSfxBlockDataEntityStruct obj) => obj.tweakablePitch);
  35. }
  36. set
  37. {
  38. BlockEngine.SetBlockInfo(this,
  39. (ref SoundSfxBlockDataEntityStruct obj, float val) => obj.tweakablePitch = val, value);
  40. }
  41. }
  42. public bool Is3D
  43. {
  44. get
  45. {
  46. return BlockEngine.GetBlockInfo(this, (SoundSfxBlockDataEntityStruct obj) => obj.is3D);
  47. }
  48. set
  49. {
  50. BlockEngine.SetBlockInfo(this,
  51. (ref SoundSfxBlockDataEntityStruct obj, bool val) => obj.is3D = val, value);
  52. }
  53. }
  54. public ChannelType ChannelType
  55. {
  56. get
  57. {
  58. return BlockEngine.GetBlockInfo(this, (SoundSfxBlockDataEntityStruct obj) => (ChannelType)obj.channelType);
  59. }
  60. set
  61. {
  62. BlockEngine.SetBlockInfo(this,
  63. (ref SoundSfxBlockDataEntityStruct obj, ChannelType val) => obj.tweakableVolume = (byte) val, value);
  64. }
  65. }
  66. public byte TrackIndex
  67. {
  68. get
  69. {
  70. return BlockEngine.GetBlockInfo(this, (SoundSfxBlockDataEntityStruct obj) => obj.soundEffectIndex);
  71. }
  72. set
  73. {
  74. BlockEngine.SetBlockInfo(this,
  75. (ref SoundSfxBlockDataEntityStruct obj, byte val) => obj.soundEffectIndex = val, value);
  76. }
  77. }
  78. // track
  79. public Guid Track
  80. {
  81. get
  82. {
  83. return BlockEngine.GetBlockInfo(this,
  84. (SoundSfxBlockDataEntityStruct obj) => obj.is3D ? obj.fmod3DEventPaths.Get<Guid>(obj.soundEffectIndex) : obj.fmod2DEventPaths.Get<Guid>(obj.soundEffectIndex));
  85. }
  86. set
  87. {
  88. BlockEngine.SetBlockInfo(this, (ref SoundSfxBlockDataEntityStruct obj, Guid val) =>
  89. {
  90. for (byte i = 0; i < obj.fmod2DEventPaths.Count<Guid>(); i++)
  91. {
  92. Guid track = obj.fmod2DEventPaths.Get<Guid>(i);
  93. if (track == val)
  94. {
  95. obj.soundEffectIndex = i;
  96. obj.is3D = false;
  97. return;
  98. }
  99. }
  100. for (byte i = 0; i < obj.fmod3DEventPaths.Count<Guid>(); i++)
  101. {
  102. Guid track = obj.fmod3DEventPaths.Get<Guid>(i);
  103. if (track == val)
  104. {
  105. obj.soundEffectIndex = i;
  106. obj.is3D = true;
  107. return;
  108. }
  109. }
  110. }, value);
  111. }
  112. }
  113. // all tracks
  114. public Guid[] Tracks2D
  115. {
  116. get
  117. {
  118. return BlockEngine.GetBlockInfo(this, (SoundSfxBlockDataEntityStruct obj) =>
  119. {
  120. Guid[] tracks = new Guid[obj.fmod2DEventPaths.Count<Guid>()];
  121. for (byte i = 0; i < tracks.Length; i++)
  122. {
  123. tracks[i] = obj.fmod2DEventPaths.Get<Guid>(i);
  124. }
  125. return tracks;
  126. });
  127. }
  128. }
  129. public Guid[] Tracks3D
  130. {
  131. get
  132. {
  133. return BlockEngine.GetBlockInfo(this, (SoundSfxBlockDataEntityStruct obj) =>
  134. {
  135. Guid[] tracks = new Guid[obj.fmod3DEventPaths.Count<Guid>()];
  136. for (byte i = 0; i < tracks.Length; i++)
  137. {
  138. tracks[i] = obj.fmod2DEventPaths.Get<Guid>(i);
  139. }
  140. return tracks;
  141. });
  142. }
  143. }
  144. public bool IsLooped
  145. {
  146. get
  147. {
  148. return BlockEngine.GetBlockInfo(this, (SoundSfxBlockDataEntityStruct obj) => obj.isLoopedBlock);
  149. }
  150. set
  151. {
  152. BlockEngine.SetBlockInfo(this,
  153. (ref SoundSfxBlockDataEntityStruct obj, bool val) => obj.isLoopedBlock = val, value);
  154. }
  155. }
  156. public bool IsPlaying
  157. {
  158. get
  159. {
  160. return BlockEngine.GetBlockInfo(this,
  161. (SoundSfxBlockDataEntityStruct obj) => obj.isPlaying);
  162. }
  163. set
  164. {
  165. BlockEngine.SetBlockInfo(this, (ref SoundSfxBlockDataEntityStruct obj, bool val) =>
  166. {
  167. if (obj.isPlaying == val) return;
  168. if (val)
  169. {
  170. // start playing
  171. EventInstance inst = RuntimeManager.CreateInstance(obj.is3D ? obj.fmod3DEventPaths.Get<Guid>(obj.soundEffectIndex) : obj.fmod2DEventPaths.Get<Guid>(obj.soundEffectIndex));
  172. inst.setVolume(obj.tweakableVolume / 100f);
  173. inst.start();
  174. obj.eventHandle = inst.handle;
  175. }
  176. else
  177. {
  178. // stop playing
  179. EventInstance inst = default(EventInstance);
  180. inst.handle = obj.eventHandle;
  181. inst.stop(FMOD.Studio.STOP_MODE.ALLOWFADEOUT);
  182. inst.release();
  183. }
  184. obj.isPlaying = val;
  185. }, value);
  186. }
  187. }
  188. }
  189. }