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.

202 lines
5.9KB

  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 TechbloxModdingAPI.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.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<SoundSfxBlockDataEntityStruct>(this).tweakableVolume;
  23. }
  24. set
  25. {
  26. BlockEngine.GetBlockInfo<SoundSfxBlockDataEntityStruct>(this).tweakableVolume = value;
  27. }
  28. }
  29. public float Pitch
  30. {
  31. get
  32. {
  33. return BlockEngine.GetBlockInfo<SoundSfxBlockDataEntityStruct>(this).tweakablePitch;
  34. }
  35. set
  36. {
  37. BlockEngine.GetBlockInfo<SoundSfxBlockDataEntityStruct>(this).tweakablePitch = value;
  38. }
  39. }
  40. public bool Is3D
  41. {
  42. get
  43. {
  44. return BlockEngine.GetBlockInfo<SoundSfxBlockDataEntityStruct>(this).is3D;
  45. }
  46. set
  47. {
  48. BlockEngine.GetBlockInfo<SoundSfxBlockDataEntityStruct>(this).is3D = value;
  49. }
  50. }
  51. public ChannelType ChannelType
  52. {
  53. get
  54. {
  55. return (ChannelType) BlockEngine.GetBlockInfo<SoundSfxBlockDataEntityStruct>(this).channelType;
  56. }
  57. set
  58. {
  59. BlockEngine.GetBlockInfo<SoundSfxBlockDataEntityStruct>(this).channelType = (byte) value;
  60. }
  61. }
  62. public byte TrackIndex
  63. {
  64. get
  65. {
  66. return BlockEngine.GetBlockInfo<SoundSfxBlockDataEntityStruct>(this).soundEffectIndex;
  67. }
  68. set
  69. {
  70. BlockEngine.GetBlockInfo<SoundSfxBlockDataEntityStruct>(this).soundEffectIndex = value;
  71. }
  72. }
  73. // track
  74. public Guid Track
  75. {
  76. get
  77. {
  78. var obj = BlockEngine.GetBlockInfo<SoundSfxBlockDataEntityStruct>(this);
  79. return obj.is3D
  80. ? obj.fmod3DEventPaths.Get<Guid>(obj.soundEffectIndex)
  81. : obj.fmod2DEventPaths.Get<Guid>(obj.soundEffectIndex);
  82. }
  83. set
  84. {
  85. var obj = BlockEngine.GetBlockInfo<SoundSfxBlockDataEntityStruct>(this);
  86. for (byte i = 0; i < obj.fmod2DEventPaths.Count<Guid>(); i++)
  87. {
  88. Guid track = obj.fmod2DEventPaths.Get<Guid>(i);
  89. if (track == value)
  90. {
  91. obj.soundEffectIndex = i;
  92. obj.is3D = false;
  93. return;
  94. }
  95. }
  96. for (byte i = 0; i < obj.fmod3DEventPaths.Count<Guid>(); i++)
  97. {
  98. Guid track = obj.fmod3DEventPaths.Get<Guid>(i);
  99. if (track == value)
  100. {
  101. obj.soundEffectIndex = i;
  102. obj.is3D = true;
  103. return;
  104. }
  105. }
  106. }
  107. }
  108. // all tracks
  109. public Guid[] Tracks2D
  110. {
  111. get
  112. {
  113. var obj = BlockEngine.GetBlockInfo<SoundSfxBlockDataEntityStruct>(this);
  114. Guid[] tracks = new Guid[obj.fmod2DEventPaths.Count<Guid>()];
  115. for (byte i = 0; i < tracks.Length; i++)
  116. {
  117. tracks[i] = obj.fmod2DEventPaths.Get<Guid>(i);
  118. }
  119. return tracks;
  120. }
  121. }
  122. public Guid[] Tracks3D
  123. {
  124. get
  125. {
  126. var obj = BlockEngine.GetBlockInfo<SoundSfxBlockDataEntityStruct>(this);
  127. Guid[] tracks = new Guid[obj.fmod3DEventPaths.Count<Guid>()];
  128. for (byte i = 0; i < tracks.Length; i++)
  129. {
  130. tracks[i] = obj.fmod2DEventPaths.Get<Guid>(i);
  131. }
  132. return tracks;
  133. }
  134. }
  135. public bool IsLooped
  136. {
  137. get
  138. {
  139. return BlockEngine.GetBlockInfo<SoundSfxBlockDataEntityStruct>(this).isLoopedBlock;
  140. }
  141. set
  142. {
  143. BlockEngine.GetBlockInfo<SoundSfxBlockDataEntityStruct>(this).isLoopedBlock = value;
  144. }
  145. }
  146. public bool IsPlaying
  147. {
  148. get
  149. {
  150. return BlockEngine.GetBlockInfo<SoundSfxBlockDataEntityStruct>(this).isPlaying;
  151. }
  152. set
  153. {
  154. var obj = BlockEngine.GetBlockInfo<SoundSfxBlockDataEntityStruct>(this);
  155. if (obj.isPlaying == value) return;
  156. if (value)
  157. {
  158. // start playing
  159. EventInstance inst = RuntimeManager.CreateInstance(obj.is3D
  160. ? obj.fmod3DEventPaths.Get<Guid>(obj.soundEffectIndex)
  161. : obj.fmod2DEventPaths.Get<Guid>(obj.soundEffectIndex));
  162. inst.setVolume(obj.tweakableVolume / 100f);
  163. inst.start();
  164. obj.eventHandle = inst.handle;
  165. }
  166. else
  167. {
  168. // stop playing
  169. EventInstance inst = default(EventInstance);
  170. inst.handle = obj.eventHandle;
  171. inst.stop(FMOD.Studio.STOP_MODE.ALLOWFADEOUT);
  172. inst.release();
  173. }
  174. obj.isPlaying = value;
  175. }
  176. }
  177. }
  178. }