From ee6a0e3af638efcf2c5df9b397e86fd17052a829 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Mon, 28 Sep 2020 03:10:59 +0200 Subject: [PATCH] Add support for getting the RGB of block colors Only works if the constructors are used --- GamecraftModdingAPI/Block.cs | 13 ++++--------- GamecraftModdingAPI/Blocks/BlockColor.cs | 16 +++++++++++++++- GamecraftModdingAPI/Blocks/BlockEngine.cs | 11 +++++------ 3 files changed, 24 insertions(+), 16 deletions(-) diff --git a/GamecraftModdingAPI/Block.cs b/GamecraftModdingAPI/Block.cs index 97386a3..f827e09 100644 --- a/GamecraftModdingAPI/Block.cs +++ b/GamecraftModdingAPI/Block.cs @@ -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 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. - /// - /// 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. /// /// The block's type /// The block's color /// The block color's darkness (0-9) - 0 is default color - /// The block's position in the grid - default block size is 0.2 + /// The block's position - default block size is 0.2 /// The block's rotation in degrees /// The block's uniform scale - default scale is 1 (with 0.2 width) /// The block's non-uniform scale - 0 means is used @@ -66,7 +61,7 @@ namespace GamecraftModdingAPI /// The block's type /// The block's color /// The block color's darkness (0-9) - 0 is default color - /// The block's position in the grid - default block size is 0.2 + /// The block's position - default block size is 0.2 /// The block's rotation in degrees /// The block's uniform scale - default scale is 1 (with 0.2 width) /// The block's non-uniform scale - 0 means is used @@ -192,7 +187,7 @@ namespace GamecraftModdingAPI type); ILGenerator il = dynamic.GetILGenerator(); - il.DeclareLocal(type); + //il.DeclareLocal(type); il.Emit(OpCodes.Ldarg_0); //Load EGID and pass to constructor il.Emit(OpCodes.Newobj, ctor); //Call constructor //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.overridePaletteColour = false; color.needsUpdate = true; - BlockEngine.SetBlockColorFromPalette(ref color); + color.paletteColour = BlockEngine.ConvertBlockColor(color.indexInPalette); }, value); } } diff --git a/GamecraftModdingAPI/Blocks/BlockColor.cs b/GamecraftModdingAPI/Blocks/BlockColor.cs index d7ada0d..46a5dc4 100644 --- a/GamecraftModdingAPI/Blocks/BlockColor.cs +++ b/GamecraftModdingAPI/Blocks/BlockColor.cs @@ -1,9 +1,13 @@ -namespace GamecraftModdingAPI.Blocks +using System; +using Unity.Mathematics; + +namespace GamecraftModdingAPI.Blocks { public struct BlockColor { public BlockColors Color; public byte Darkness; + public byte Index; public BlockColor(byte index) { @@ -14,17 +18,27 @@ } else { + if (index > 99) + throw new ArgumentOutOfRangeException(nameof(index), "Invalid color index. Must be 0-90 or 255."); Color = (BlockColors) (index % 10); Darkness = (byte) (index / 10); } + + Index = index; } 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; 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() { return $"{nameof(Color)}: {Color}, {nameof(Darkness)}: {Darkness}"; diff --git a/GamecraftModdingAPI/Blocks/BlockEngine.cs b/GamecraftModdingAPI/Blocks/BlockEngine.cs index 2622d41..3a979fe 100644 --- a/GamecraftModdingAPI/Blocks/BlockEngine.cs +++ b/GamecraftModdingAPI/Blocks/BlockEngine.cs @@ -13,6 +13,7 @@ using Svelto.ECS; using Svelto.ECS.Hybrid; using GamecraftModdingAPI.Engines; +using Unity.Mathematics; namespace GamecraftModdingAPI.Blocks { @@ -54,12 +55,10 @@ namespace GamecraftModdingAPI.Blocks return ret; } - public void SetBlockColorFromPalette(ref ColourParameterEntityStruct color) - { - ref var paletteEntry = ref entitiesDB.QueryEntity(color.indexInPalette, - CommonExclusiveGroups.COLOUR_PALETTE_GROUP); - color.paletteColour = paletteEntry.Colour; - } + public float4 ConvertBlockColor(byte index) => index == byte.MaxValue + ? new float4(-1f, -1f, -1f, -1f) + : entitiesDB.QueryEntity(index, + CommonExclusiveGroups.COLOUR_PALETTE_GROUP).Colour; public ref T GetBlockInfo(EGID blockID) where T : unmanaged, IEntityComponent {