Add support for getting the RGB of block colors

Only works if the constructors are used
This commit is contained in:
Norbi Peti 2020-09-28 03:10:59 +02:00
parent 4dfa7b0f4e
commit c9e71d84b4
3 changed files with 24 additions and 16 deletions

View file

@ -36,16 +36,11 @@ namespace GamecraftModdingAPI
/// Place a new block at the given position. If scaled, position means the center of the block. The default block size is 0.2 in terms of position. /// Place a new block at the given position. If scaled, position means the center of the block. The default block size is 0.2 in terms of position.
/// Place blocks next to each other to connect them. /// Place blocks next to each other to connect them.
/// The placed block will be a complete block with a placement grid and collision which will be saved along with the game. /// The placed block will be a complete block with a placement grid and collision which will be saved along with the game.
/// <para></para>
/// <para>When placing multiple blocks, do not access properties immediately after creation as this
/// triggers a sync each time which can affect performance and may cause issues with the game.
/// You may either use AsyncUtils.WaitForSubmission() after placing all of the blocks
/// or simply access the block properties which will trigger the synchronization the first time a property is used.</para>
/// </summary> /// </summary>
/// <param name="block">The block's type</param> /// <param name="block">The block's type</param>
/// <param name="color">The block's color</param> /// <param name="color">The block's color</param>
/// <param name="darkness">The block color's darkness (0-9) - 0 is default color</param> /// <param name="darkness">The block color's darkness (0-9) - 0 is default color</param>
/// <param name="position">The block's position in the grid - default block size is 0.2</param> /// <param name="position">The block's position - default block size is 0.2</param>
/// <param name="rotation">The block's rotation in degrees</param> /// <param name="rotation">The block's rotation in degrees</param>
/// <param name="uscale">The block's uniform scale - default scale is 1 (with 0.2 width)</param> /// <param name="uscale">The block's uniform scale - default scale is 1 (with 0.2 width)</param>
/// <param name="scale">The block's non-uniform scale - 0 means <paramref name="uscale"/> is used</param> /// <param name="scale">The block's non-uniform scale - 0 means <paramref name="uscale"/> is used</param>
@ -66,7 +61,7 @@ namespace GamecraftModdingAPI
/// <param name="block">The block's type</param> /// <param name="block">The block's type</param>
/// <param name="color">The block's color</param> /// <param name="color">The block's color</param>
/// <param name="darkness">The block color's darkness (0-9) - 0 is default color</param> /// <param name="darkness">The block color's darkness (0-9) - 0 is default color</param>
/// <param name="position">The block's position in the grid - default block size is 0.2</param> /// <param name="position">The block's position - default block size is 0.2</param>
/// <param name="rotation">The block's rotation in degrees</param> /// <param name="rotation">The block's rotation in degrees</param>
/// <param name="uscale">The block's uniform scale - default scale is 1 (with 0.2 width)</param> /// <param name="uscale">The block's uniform scale - default scale is 1 (with 0.2 width)</param>
/// <param name="scale">The block's non-uniform scale - 0 means <paramref name="uscale"/> is used</param> /// <param name="scale">The block's non-uniform scale - 0 means <paramref name="uscale"/> is used</param>
@ -192,7 +187,7 @@ namespace GamecraftModdingAPI
type); type);
ILGenerator il = dynamic.GetILGenerator(); ILGenerator il = dynamic.GetILGenerator();
il.DeclareLocal(type); //il.DeclareLocal(type);
il.Emit(OpCodes.Ldarg_0); //Load EGID and pass to constructor il.Emit(OpCodes.Ldarg_0); //Load EGID and pass to constructor
il.Emit(OpCodes.Newobj, ctor); //Call constructor il.Emit(OpCodes.Newobj, ctor); //Call constructor
//il.Emit(OpCodes.Stloc_0); - doesn't seem like we need these //il.Emit(OpCodes.Stloc_0); - doesn't seem like we need these
@ -317,7 +312,7 @@ namespace GamecraftModdingAPI
color.indexInPalette = (byte) (val.Color + val.Darkness * 10); color.indexInPalette = (byte) (val.Color + val.Darkness * 10);
color.overridePaletteColour = false; color.overridePaletteColour = false;
color.needsUpdate = true; color.needsUpdate = true;
BlockEngine.SetBlockColorFromPalette(ref color); color.paletteColour = BlockEngine.ConvertBlockColor(color.indexInPalette);
}, value); }, value);
} }
} }

View file

@ -1,9 +1,13 @@
namespace GamecraftModdingAPI.Blocks using System;
using Unity.Mathematics;
namespace GamecraftModdingAPI.Blocks
{ {
public struct BlockColor public struct BlockColor
{ {
public BlockColors Color; public BlockColors Color;
public byte Darkness; public byte Darkness;
public byte Index;
public BlockColor(byte index) public BlockColor(byte index)
{ {
@ -14,17 +18,27 @@
} }
else else
{ {
if (index > 99)
throw new ArgumentOutOfRangeException(nameof(index), "Invalid color index. Must be 0-90 or 255.");
Color = (BlockColors) (index % 10); Color = (BlockColors) (index % 10);
Darkness = (byte) (index / 10); Darkness = (byte) (index / 10);
} }
Index = index;
} }
public BlockColor(BlockColors color, byte darkness) public BlockColor(BlockColors color, byte darkness)
{ {
if (darkness > 9)
throw new ArgumentOutOfRangeException(nameof(darkness), "Darkness must be 0-9 where 0 is default.");
Color = color; Color = color;
Darkness = darkness; Darkness = darkness;
if (color == BlockColors.Default) Index = byte.MaxValue;
else Index = (byte) (darkness * 10 + color);
} }
public float4 RGBA => Block.BlockEngine.ConvertBlockColor(Index);
public override string ToString() public override string ToString()
{ {
return $"{nameof(Color)}: {Color}, {nameof(Darkness)}: {Darkness}"; return $"{nameof(Color)}: {Color}, {nameof(Darkness)}: {Darkness}";

View file

@ -13,6 +13,7 @@ using Svelto.ECS;
using Svelto.ECS.Hybrid; using Svelto.ECS.Hybrid;
using GamecraftModdingAPI.Engines; using GamecraftModdingAPI.Engines;
using Unity.Mathematics;
namespace GamecraftModdingAPI.Blocks namespace GamecraftModdingAPI.Blocks
{ {
@ -54,12 +55,10 @@ namespace GamecraftModdingAPI.Blocks
return ret; return ret;
} }
public void SetBlockColorFromPalette(ref ColourParameterEntityStruct color) public float4 ConvertBlockColor(byte index) => index == byte.MaxValue
{ ? new float4(-1f, -1f, -1f, -1f)
ref var paletteEntry = ref entitiesDB.QueryEntity<PaletteEntryEntityStruct>(color.indexInPalette, : entitiesDB.QueryEntity<PaletteEntryEntityStruct>(index,
CommonExclusiveGroups.COLOUR_PALETTE_GROUP); CommonExclusiveGroups.COLOUR_PALETTE_GROUP).Colour;
color.paletteColour = paletteEntry.Colour;
}
public ref T GetBlockInfo<T>(EGID blockID) where T : unmanaged, IEntityComponent public ref T GetBlockInfo<T>(EGID blockID) where T : unmanaged, IEntityComponent
{ {