Update music block and attempt to fix test

This commit is contained in:
Norbi Peti 2020-07-21 02:36:11 +02:00 committed by NGnius (Graham)
parent 15485481a2
commit 057a030c20
3 changed files with 81 additions and 74 deletions

View file

@ -123,6 +123,7 @@ namespace GamecraftModdingAPI
{ {
{typeof(ConsoleBlock), new[] {CommonExclusiveGroups.BUILD_CONSOLE_BLOCK_GROUP}}, {typeof(ConsoleBlock), new[] {CommonExclusiveGroups.BUILD_CONSOLE_BLOCK_GROUP}},
{typeof(Motor), new[] {CommonExclusiveGroups.BUILD_MOTOR_BLOCK_GROUP}}, {typeof(Motor), new[] {CommonExclusiveGroups.BUILD_MOTOR_BLOCK_GROUP}},
{typeof(MusicBlock), new[] {CommonExclusiveGroups.BUILD_MUSIC_BLOCK_GROUP}},
{typeof(Piston), new[] {CommonExclusiveGroups.BUILD_PISTON_BLOCK_GROUP}}, {typeof(Piston), new[] {CommonExclusiveGroups.BUILD_PISTON_BLOCK_GROUP}},
{typeof(Servo), new[] {CommonExclusiveGroups.BUILD_SERVO_BLOCK_GROUP}}, {typeof(Servo), new[] {CommonExclusiveGroups.BUILD_SERVO_BLOCK_GROUP}},
{ {
@ -198,10 +199,16 @@ namespace GamecraftModdingAPI
public Block(EGID id) public Block(EGID id)
{ {
Id = id; Id = id;
if (typeToGroup.TryGetValue(GetType(), out var groups) && groups.All(gr => gr != id.groupID)) var type = GetType();
if (typeToGroup.TryGetValue(type, out var groups))
{
if (groups.All(gr => gr != id.groupID))
throw new BlockTypeException("The block has the wrong group! The type is " + GetType() + throw new BlockTypeException("The block has the wrong group! The type is " + GetType() +
" while the group is " + id.groupID); " while the group is " + id.groupID);
} }
else if (type != typeof(Block))
Logging.LogWarning($"Unknown block type! Add {type} to the dictionary.");
}
/// <summary> /// <summary>
/// This overload searches for the correct group the block is in. /// This overload searches for the correct group the block is in.

View file

@ -74,8 +74,8 @@ namespace GamecraftModdingAPI.Blocks
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)] [APITestCase(TestType.Game)]
public static void TestMusicBlock() public static void TestMusicBlock1()
{ {
Block newBlock = Block.PlaceNew(BlockIDs.MusicBlock, Unity.Mathematics.float3.zero + 2); 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 MusicBlock b = null; // Note: the assignment operation is a lambda, which slightly confuses the compiler
@ -83,8 +83,19 @@ namespace GamecraftModdingAPI.Blocks
if (!Assert.NotNull(b, "Block.Specialize<MusicBlock>() returned null, possibly because it failed silently.", "Specialized MusicBlock is not null.")) return; 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.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; 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;
_musicBlock = b;
}
private static MusicBlock _musicBlock;
[APITestCase(TestType.EditMode)]
public static void TestMusicBlock2()
{
//Block newBlock = Block.GetLastPlacedBlock();
var b = _musicBlock;
if (!Assert.NotNull(b, "Block.Specialize<MusicBlock>() returned null, possibly because it failed silently.", "Specialized MusicBlock is not null.")) return;
b.IsPlaying = true; // play sfx 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.IsPlaying, true, $"MusicBlock.IsPlaying {b.IsPlaying} does not equal true, 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; 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()); //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; 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;

View file

@ -3,58 +3,38 @@ using System;
using FMOD.Studio; using FMOD.Studio;
using FMODUnity; using FMODUnity;
using Gamecraft.Wires; using Gamecraft.Wires;
using RobocraftX.Common;
using RobocraftX.Blocks; using RobocraftX.Blocks;
using Svelto.ECS; using Svelto.ECS;
using Unity.Mathematics; using Unity.Mathematics;
using GamecraftModdingAPI; using GamecraftModdingAPI;
using GamecraftModdingAPI.Tests;
using GamecraftModdingAPI.Utility; using GamecraftModdingAPI.Utility;
namespace GamecraftModdingAPI.Blocks namespace GamecraftModdingAPI.Blocks
{ {
public class MusicBlock : Block 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) 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) public MusicBlock(uint id) : base(new EGID(id, CommonExclusiveGroups.BUILD_MUSIC_BLOCK_GROUP))
{ {
if (!BlockEngine.GetBlockInfoExists<MusicBlockDataEntityStruct>(this.Id))
{
throw new BlockTypeException($"Block is not a {this.GetType().Name} block");
}
} }
public byte TrackIndex public byte TrackIndex
{ {
get get
{ {
return BlockEngine.GetBlockInfo<MusicBlockDataEntityStruct>(Id).trackIndx; return BlockEngine.GetBlockInfo(this, (MusicBlockDataEntityStruct st) => st.trackIndx);
} }
set set
{ {
ref MusicBlockDataEntityStruct msdes = ref BlockEngine.GetBlockInfo<MusicBlockDataEntityStruct>(Id); BlockEngine.SetBlockInfo(this,
msdes.trackIndx = value; (ref MusicBlockDataEntityStruct msdes, byte val) => msdes.trackIndx = val, value);
} }
} }
@ -62,22 +42,24 @@ namespace GamecraftModdingAPI.Blocks
{ {
get get
{ {
ref MusicBlockDataEntityStruct msdes = ref BlockEngine.GetBlockInfo<MusicBlockDataEntityStruct>(Id); return BlockEngine.GetBlockInfo(this,
return msdes.fmod2DEventPaths.Get<Guid>(msdes.trackIndx); (MusicBlockDataEntityStruct msdes) => msdes.fmod2DEventPaths.Get<Guid>(msdes.trackIndx));
} }
set set
{ {
ref MusicBlockDataEntityStruct msdes = ref BlockEngine.GetBlockInfo<MusicBlockDataEntityStruct>(Id); BlockEngine.SetBlockInfo(this, (ref MusicBlockDataEntityStruct msdes, Guid val) =>
{
for (byte i = 0; i < msdes.fmod2DEventPaths.Count<Guid>(); i++) for (byte i = 0; i < msdes.fmod2DEventPaths.Count<Guid>(); i++)
{ {
Guid track = msdes.fmod2DEventPaths.Get<Guid>(i); Guid track = msdes.fmod2DEventPaths.Get<Guid>(i);
if (track == value) if (track == val)
{ {
msdes.trackIndx = i; msdes.trackIndx = i;
break; break;
} }
} }
}, value);
} }
} }
@ -85,13 +67,15 @@ namespace GamecraftModdingAPI.Blocks
{ {
get get
{ {
ref MusicBlockDataEntityStruct msdes = ref BlockEngine.GetBlockInfo<MusicBlockDataEntityStruct>(Id); return BlockEngine.GetBlockInfo(this, (MusicBlockDataEntityStruct msdes) =>
{
Guid[] tracks = new Guid[msdes.fmod2DEventPaths.Count<Guid>()]; Guid[] tracks = new Guid[msdes.fmod2DEventPaths.Count<Guid>()];
for (byte i = 0; i < tracks.Length; i++) for (byte i = 0; i < tracks.Length; i++)
{ {
tracks[i] = msdes.fmod2DEventPaths.Get<Guid>(i); tracks[i] = msdes.fmod2DEventPaths.Get<Guid>(i);
} }
return tracks; return tracks;
});
} }
} }
@ -99,13 +83,13 @@ namespace GamecraftModdingAPI.Blocks
{ {
get get
{ {
return BlockEngine.GetBlockInfo<MusicBlockDataEntityStruct>(Id).tweakableVolume; return BlockEngine.GetBlockInfo(this, (MusicBlockDataEntityStruct msdes) => msdes.tweakableVolume);
} }
set set
{ {
ref MusicBlockDataEntityStruct msdes = ref BlockEngine.GetBlockInfo<MusicBlockDataEntityStruct>(Id); BlockEngine.SetBlockInfo(this,
msdes.tweakableVolume = value; (ref MusicBlockDataEntityStruct msdes, float val) => msdes.tweakableVolume = val, value);
} }
} }
@ -113,13 +97,15 @@ namespace GamecraftModdingAPI.Blocks
{ {
get get
{ {
return (ChannelType)BlockEngine.GetBlockInfo<MusicBlockDataEntityStruct>(Id).channelType; Assert.Log("Block exists: " + Exists);
return BlockEngine.GetBlockInfo(this,
(MusicBlockDataEntityStruct msdes) => (ChannelType) msdes.channelType);
} }
set set
{ {
ref MusicBlockDataEntityStruct msdes = ref BlockEngine.GetBlockInfo<MusicBlockDataEntityStruct>(Id); BlockEngine.SetBlockInfo(this,
msdes.channelType = (byte)value; (ref MusicBlockDataEntityStruct msdes, ChannelType val) => msdes.channelType = (byte) val, value);
} }
} }
@ -127,14 +113,16 @@ namespace GamecraftModdingAPI.Blocks
{ {
get get
{ {
return BlockEngine.GetBlockInfo<MusicBlockDataEntityStruct>(Id).isPlaying; return BlockEngine.GetBlockInfo(this,
(MusicBlockDataEntityStruct msdes) => msdes.isPlaying);
} }
set set
{ {
ref MusicBlockDataEntityStruct msdes = ref BlockEngine.GetBlockInfo<MusicBlockDataEntityStruct>(Id); BlockEngine.SetBlockInfo(this, (ref MusicBlockDataEntityStruct msdes, bool val) =>
if (msdes.isPlaying == value) return; {
if (value) if (msdes.isPlaying == val) return;
if (val)
{ {
// start playing // start playing
EventInstance inst = RuntimeManager.CreateInstance(msdes.fmod2DEventPaths.Get<Guid>(msdes.trackIndx)); EventInstance inst = RuntimeManager.CreateInstance(msdes.fmod2DEventPaths.Get<Guid>(msdes.trackIndx));
@ -150,7 +138,8 @@ namespace GamecraftModdingAPI.Blocks
inst.stop(FMOD.Studio.STOP_MODE.ALLOWFADEOUT); inst.stop(FMOD.Studio.STOP_MODE.ALLOWFADEOUT);
inst.release(); inst.release();
} }
msdes.isPlaying = value; msdes.isPlaying = val;
}, value);
} }
} }
} }