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.

MusicBlock.cs 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. using System;
  2. using FMOD.Studio;
  3. using FMODUnity;
  4. using Gamecraft.Wires;
  5. using RobocraftX.Blocks;
  6. using Svelto.ECS;
  7. using Unity.Mathematics;
  8. using GamecraftModdingAPI;
  9. using GamecraftModdingAPI.Utility;
  10. namespace GamecraftModdingAPI.Blocks
  11. {
  12. public class MusicBlock : Block
  13. {
  14. public static MusicBlock PlaceNew(float3 position,
  15. float3 rotation = default, BlockColors color = BlockColors.Default, byte darkness = 0,
  16. int uscale = 1, float3 scale = default, Player player = null)
  17. {
  18. if (PlacementEngine.IsInGame && GameState.IsBuildMode())
  19. {
  20. EGID id = PlacementEngine.PlaceBlock(BlockIDs.MusicBlock, color, darkness,
  21. position, uscale, scale, player, rotation);
  22. return new MusicBlock(id);
  23. }
  24. return null;
  25. }
  26. public MusicBlock(EGID id) : base(id)
  27. {
  28. if (!BlockEngine.GetBlockInfoExists<MusicBlockDataEntityStruct>(this.Id))
  29. {
  30. throw new BlockTypeException($"Block is not a {this.GetType().Name} block");
  31. }
  32. }
  33. public MusicBlock(uint id) : base(id)
  34. {
  35. if (!BlockEngine.GetBlockInfoExists<MusicBlockDataEntityStruct>(this.Id))
  36. {
  37. throw new BlockTypeException($"Block is not a {this.GetType().Name} block");
  38. }
  39. }
  40. public byte TrackIndex
  41. {
  42. get
  43. {
  44. return BlockEngine.GetBlockInfo<MusicBlockDataEntityStruct>(Id).trackIndx;
  45. }
  46. set
  47. {
  48. ref MusicBlockDataEntityStruct msdes = ref BlockEngine.GetBlockInfo<MusicBlockDataEntityStruct>(Id);
  49. msdes.trackIndx = value;
  50. }
  51. }
  52. public Guid Track
  53. {
  54. get
  55. {
  56. ref MusicBlockDataEntityStruct msdes = ref BlockEngine.GetBlockInfo<MusicBlockDataEntityStruct>(Id);
  57. return msdes.fmod2DEventPaths.Get<Guid>(msdes.trackIndx);
  58. }
  59. set
  60. {
  61. ref MusicBlockDataEntityStruct msdes = ref BlockEngine.GetBlockInfo<MusicBlockDataEntityStruct>(Id);
  62. for (byte i = 0; i < msdes.fmod2DEventPaths.Count<Guid>(); i++)
  63. {
  64. Guid track = msdes.fmod2DEventPaths.Get<Guid>(i);
  65. if (track == value)
  66. {
  67. msdes.trackIndx = i;
  68. break;
  69. }
  70. }
  71. }
  72. }
  73. public Guid[] Tracks
  74. {
  75. get
  76. {
  77. ref MusicBlockDataEntityStruct msdes = ref BlockEngine.GetBlockInfo<MusicBlockDataEntityStruct>(Id);
  78. Guid[] tracks = new Guid[msdes.fmod2DEventPaths.Count<Guid>()];
  79. for (byte i = 0; i < tracks.Length; i++)
  80. {
  81. tracks[i] = msdes.fmod2DEventPaths.Get<Guid>(i);
  82. }
  83. return tracks;
  84. }
  85. }
  86. public float Volume
  87. {
  88. get
  89. {
  90. return BlockEngine.GetBlockInfo<MusicBlockDataEntityStruct>(Id).tweakableVolume;
  91. }
  92. set
  93. {
  94. ref MusicBlockDataEntityStruct msdes = ref BlockEngine.GetBlockInfo<MusicBlockDataEntityStruct>(Id);
  95. msdes.tweakableVolume = value;
  96. }
  97. }
  98. public ChannelType ChannelType
  99. {
  100. get
  101. {
  102. return (ChannelType)BlockEngine.GetBlockInfo<MusicBlockDataEntityStruct>(Id).channelType;
  103. }
  104. set
  105. {
  106. ref MusicBlockDataEntityStruct msdes = ref BlockEngine.GetBlockInfo<MusicBlockDataEntityStruct>(Id);
  107. msdes.channelType = (byte)value;
  108. }
  109. }
  110. public bool IsPlaying
  111. {
  112. get
  113. {
  114. return BlockEngine.GetBlockInfo<MusicBlockDataEntityStruct>(Id).isPlaying;
  115. }
  116. set
  117. {
  118. ref MusicBlockDataEntityStruct msdes = ref BlockEngine.GetBlockInfo<MusicBlockDataEntityStruct>(Id);
  119. if (msdes.isPlaying == value) return;
  120. if (value)
  121. {
  122. // start playing
  123. EventInstance inst = RuntimeManager.CreateInstance(msdes.fmod2DEventPaths.Get<Guid>(msdes.trackIndx));
  124. inst.setVolume(msdes.tweakableVolume / 100f);
  125. inst.start();
  126. msdes.eventHandle = inst.handle;
  127. }
  128. else
  129. {
  130. // stop playing
  131. EventInstance inst = default(EventInstance);
  132. inst.handle = msdes.eventHandle;
  133. inst.stop(FMOD.Studio.STOP_MODE.ALLOWFADEOUT);
  134. inst.release();
  135. }
  136. msdes.isPlaying = value;
  137. }
  138. }
  139. }
  140. }