Browse Source

Update music block and attempt to fix test

tags/v2.2.0
NorbiPeti 4 years ago
parent
commit
47126d2d79
Signed by: NorbiPeti <szatmari.norbert.peter@gmail.com> GPG Key ID: DBA4C4549A927E56
3 changed files with 81 additions and 74 deletions
  1. +11
    -4
      GamecraftModdingAPI/Block.cs
  2. +14
    -3
      GamecraftModdingAPI/Blocks/BlockTests.cs
  3. +56
    -67
      GamecraftModdingAPI/Blocks/MusicBlock.cs

+ 11
- 4
GamecraftModdingAPI/Block.cs View File

@@ -29,7 +29,7 @@ namespace GamecraftModdingAPI
protected static readonly SignalEngine SignalEngine = new SignalEngine();
protected static readonly BlockEventsEngine BlockEventsEngine = new BlockEventsEngine();
protected static readonly ScalingEngine ScalingEngine = new ScalingEngine();
protected internal static readonly BlockEngine BlockEngine = new BlockEngine();

/// <summary>
@@ -123,6 +123,7 @@ namespace GamecraftModdingAPI
{
{typeof(ConsoleBlock), new[] {CommonExclusiveGroups.BUILD_CONSOLE_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(Servo), new[] {CommonExclusiveGroups.BUILD_SERVO_BLOCK_GROUP}},
{
@@ -198,9 +199,15 @@ namespace GamecraftModdingAPI
public Block(EGID id)
{
Id = id;
if (typeToGroup.TryGetValue(GetType(), out var groups) && groups.All(gr => gr != id.groupID))
throw new BlockTypeException("The block has the wrong group! The type is " + GetType() +
" while the group is " + 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() +
" while the group is " + id.groupID);
}
else if (type != typeof(Block))
Logging.LogWarning($"Unknown block type! Add {type} to the dictionary.");
}

/// <summary>


+ 14
- 3
GamecraftModdingAPI/Blocks/BlockTests.cs 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;
}
[APITestCase(TestType.EditMode)]
public static void TestMusicBlock()
[APITestCase(TestType.Game)]
public static void TestMusicBlock1()
{
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
@@ -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.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;
_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
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;
//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;


+ 56
- 67
GamecraftModdingAPI/Blocks/MusicBlock.cs View File

@@ -3,58 +3,38 @@ using System;
using FMOD.Studio;
using FMODUnity;
using Gamecraft.Wires;
using RobocraftX.Common;
using RobocraftX.Blocks;
using Svelto.ECS;
using Unity.Mathematics;

using GamecraftModdingAPI;
using GamecraftModdingAPI.Tests;
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)
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
{
get
{
return BlockEngine.GetBlockInfo<MusicBlockDataEntityStruct>(Id).trackIndx;
return BlockEngine.GetBlockInfo(this, (MusicBlockDataEntityStruct st) => st.trackIndx);
}

set
{
ref MusicBlockDataEntityStruct msdes = ref BlockEngine.GetBlockInfo<MusicBlockDataEntityStruct>(Id);
msdes.trackIndx = value;
BlockEngine.SetBlockInfo(this,
(ref MusicBlockDataEntityStruct msdes, byte val) => msdes.trackIndx = val, value);
}
}

@@ -62,22 +42,24 @@ namespace GamecraftModdingAPI.Blocks
{
get
{
ref MusicBlockDataEntityStruct msdes = ref BlockEngine.GetBlockInfo<MusicBlockDataEntityStruct>(Id);
return msdes.fmod2DEventPaths.Get<Guid>(msdes.trackIndx);
return BlockEngine.GetBlockInfo(this,
(MusicBlockDataEntityStruct msdes) => 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++)
BlockEngine.SetBlockInfo(this, (ref MusicBlockDataEntityStruct msdes, Guid val) =>
{
Guid track = msdes.fmod2DEventPaths.Get<Guid>(i);
if (track == value)
for (byte i = 0; i < msdes.fmod2DEventPaths.Count<Guid>(); i++)
{
msdes.trackIndx = i;
break;
Guid track = msdes.fmod2DEventPaths.Get<Guid>(i);
if (track == val)
{
msdes.trackIndx = i;
break;
}
}
}
}, value);
}
}

@@ -85,13 +67,15 @@ namespace GamecraftModdingAPI.Blocks
{
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++)
return BlockEngine.GetBlockInfo(this, (MusicBlockDataEntityStruct msdes) =>
{
tracks[i] = msdes.fmod2DEventPaths.Get<Guid>(i);
}
return tracks;
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;
});
}
}

@@ -99,13 +83,13 @@ namespace GamecraftModdingAPI.Blocks
{
get
{
return BlockEngine.GetBlockInfo<MusicBlockDataEntityStruct>(Id).tweakableVolume;
return BlockEngine.GetBlockInfo(this, (MusicBlockDataEntityStruct msdes) => msdes.tweakableVolume);
}

set
{
ref MusicBlockDataEntityStruct msdes = ref BlockEngine.GetBlockInfo<MusicBlockDataEntityStruct>(Id);
msdes.tweakableVolume = value;
BlockEngine.SetBlockInfo(this,
(ref MusicBlockDataEntityStruct msdes, float val) => msdes.tweakableVolume = val, value);
}
}

@@ -113,13 +97,15 @@ namespace GamecraftModdingAPI.Blocks
{
get
{
return (ChannelType)BlockEngine.GetBlockInfo<MusicBlockDataEntityStruct>(Id).channelType;
Assert.Log("Block exists: " + Exists);
return BlockEngine.GetBlockInfo(this,
(MusicBlockDataEntityStruct msdes) => (ChannelType) msdes.channelType);
}
set
{
ref MusicBlockDataEntityStruct msdes = ref BlockEngine.GetBlockInfo<MusicBlockDataEntityStruct>(Id);
msdes.channelType = (byte)value;
BlockEngine.SetBlockInfo(this,
(ref MusicBlockDataEntityStruct msdes, ChannelType val) => msdes.channelType = (byte) val, value);
}
}

@@ -127,30 +113,33 @@ namespace GamecraftModdingAPI.Blocks
{
get
{
return BlockEngine.GetBlockInfo<MusicBlockDataEntityStruct>(Id).isPlaying;
return BlockEngine.GetBlockInfo(this,
(MusicBlockDataEntityStruct msdes) => msdes.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
BlockEngine.SetBlockInfo(this, (ref MusicBlockDataEntityStruct msdes, bool val) =>
{
// stop playing
EventInstance inst = default(EventInstance);
inst.handle = msdes.eventHandle;
inst.stop(FMOD.Studio.STOP_MODE.ALLOWFADEOUT);
inst.release();
}
msdes.isPlaying = value;
if (msdes.isPlaying == val) return;
if (val)
{
// 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 = val;
}, value);
}
}
}

Loading…
Cancel
Save