Add sfx block support

This commit is contained in:
NGnius (Graham) 2020-07-19 16:39:35 -04:00
parent 4a9ceecc29
commit cda57afade
3 changed files with 179 additions and 4 deletions

View file

@ -1,5 +1,7 @@
using System; using System;
using Gamecraft.Wires;
using GamecraftModdingAPI; using GamecraftModdingAPI;
using GamecraftModdingAPI.Tests; using GamecraftModdingAPI.Tests;
@ -72,6 +74,22 @@ namespace GamecraftModdingAPI.Blocks
if (!Assert.CloseTo(b.MinimumAngle, -180f, $"Servo.MinimumAngle {b.MinimumAngle} does not equal default value, possibly because it failed silently.", "Servo.MinimumAngle is close enough to default.")) return; if (!Assert.CloseTo(b.MinimumAngle, -180f, $"Servo.MinimumAngle {b.MinimumAngle} does not equal default value, possibly because it failed silently.", "Servo.MinimumAngle is close enough to default.")) return;
if (!Assert.CloseTo(b.MaximumForce, 750f, $"Servo.MaximumForce {b.MaximumForce} does not equal default value, possibly because it failed silently.", "Servo.MaximumForce is close enough to default.")) return; if (!Assert.CloseTo(b.MaximumForce, 750f, $"Servo.MaximumForce {b.MaximumForce} does not equal default value, possibly because it failed silently.", "Servo.MaximumForce is close enough to default.")) return;
} }
[APITestCase(TestType.EditMode)]
public static void TestMusicBlock()
{
Block newBlock = Block.PlaceNew(BlockIDs.MusicBlock, Unity.Mathematics.float3.zero + 2);
MusicBlock b = null; // Note: the assignment operation is a lambda, which slightly confuses the compiler
Assert.Errorless(() => { b = newBlock.Specialise<MusicBlock>(); }, "Block.Specialize<MusicBlock>() raised an exception: ", "Block.Specialize<MusicBlock>() completed without issue.");
if (!Assert.NotNull(b, "Block.Specialize<MusicBlock>() returned null, possibly because it failed silently.", "Specialized MusicBlock is not null.")) return;
if (!Assert.CloseTo(b.Volume, 100f, $"MusicBlock.Volume {b.Volume} does not equal default value, possibly because it failed silently.", "MusicBlock.Volume is close enough to default.")) return;
if (!Assert.Equal(b.TrackIndex, 0, $"MusicBlock.TrackIndex {b.TrackIndex} does not equal default value, possibly because it failed silently.", "MusicBlock.TrackIndex is equal to default.")) return;
b.IsPlaying = true; // play sfx
if (!Assert.Equal(b.IsPlaying, true, $"MusicBlock.IsPlaying {b.IsPlaying} does not equal default value, possibly because it failed silently.", "MusicBlock.IsPlaying is set properly.")) return;
if (!Assert.Equal(b.ChannelType, ChannelType.Character, $"MusicBlock.ChannelType {b.ChannelType} does not equal default value, possibly because it failed silently.", "MusicBlock.ChannelType is equal to default.")) return;
//Assert.Log(b.Track.ToString());
if (!Assert.Equal(b.Track.ToString(), new Guid("3237ff8f-f5f2-4f84-8144-496ca280f8c0").ToString(), $"MusicBlock.Track {b.Track} does not equal default value, possibly because it failed silently.", "MusicBlock.Track is equal to default.")) return;
}
} }
#endif #endif
} }

View file

@ -0,0 +1,157 @@
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;
}
}
}
}

View file

@ -130,7 +130,7 @@ namespace GamecraftModdingAPI.Tests
private static IEnumerator<TaskContract> GoToGameTests() private static IEnumerator<TaskContract> GoToGameTests()
{ {
/*Client app = new Client(); Client app = new Client();
int oldLength = 0; int oldLength = 0;
while (app.MyGames.Length == 0 || oldLength != app.MyGames.Length) while (app.MyGames.Length == 0 || oldLength != app.MyGames.Length)
{ {
@ -138,10 +138,10 @@ namespace GamecraftModdingAPI.Tests
yield return new WaitForSecondsEnumerator(1).Continue(); yield return new WaitForSecondsEnumerator(1).Continue();
} }
yield return Yield.It; yield return Yield.It;
app.MyGames[0].EnterGame();*/ app.MyGames[0].EnterGame();
Game newGame = Game.NewGame(); /*Game newGame = Game.NewGame();
yield return new WaitForSecondsEnumerator(5).Continue(); // wait for sync yield return new WaitForSecondsEnumerator(5).Continue(); // wait for sync
newGame.EnterGame(); newGame.EnterGame();*/
} }
private static IEnumerator<TaskContract> GameTests() private static IEnumerator<TaskContract> GameTests()