Improve GetBlockInfo(), block colors

Made GetBlockInfo() always return a reference without needing it as a parameter
Fixed Color property
Added CustomColor property for temporarily setting any color
This commit is contained in:
Norbi Peti 2020-05-20 00:08:02 +02:00
parent 97de5e606b
commit 3ff98f29bb
6 changed files with 106 additions and 51 deletions

View file

@ -4,7 +4,7 @@ using System.Reflection;
using Svelto.ECS;
using Svelto.ECS.EntityStructs;
using RobocraftX.Common;
using RobocraftX.Blocks.Scaling;
using RobocraftX.Blocks;
using Unity.Mathematics;
using GamecraftModdingAPI.Blocks;
@ -113,11 +113,10 @@ namespace GamecraftModdingAPI
/// </summary>
public float3 Scale
{
get => BlockEngine.GetBlockInfo<ScalingEntityStruct>(Id)?.scale ?? float3.zero;
get => BlockEngine.GetBlockInfo<ScalingEntityStruct>(Id).scale;
set
{
var def = new ScalingEntityStruct();
BlockEngine.GetBlockInfo(Id, ref def).scale = value;
BlockEngine.GetBlockInfo<ScalingEntityStruct>(Id).scale = value;
}
}
@ -126,13 +125,11 @@ namespace GamecraftModdingAPI
/// </summary>
public int UniformScale
{
get => BlockEngine.GetBlockInfo<BlockPlacementScaleEntityStruct>(Id)?.desiredScaleFactor ?? 0;
get => BlockEngine.GetBlockInfo<UniformBlockScaleEntityStruct>(Id).scaleFactor;
set
{
var def = new BlockPlacementScaleEntityStruct();
ref var scaleStruct = ref BlockEngine.GetBlockInfo(Id, ref def);
scaleStruct.blockPlacementHeight = scaleStruct.blockPlacementWidth =
scaleStruct.desiredScaleFactor = scaleStruct.snapGridScale = value;
ref var scaleStruct = ref BlockEngine.GetBlockInfo<UniformBlockScaleEntityStruct>(Id);
scaleStruct.scaleFactor = value;
Scale = new float3(value, value, value);
}
}
@ -140,7 +137,14 @@ namespace GamecraftModdingAPI
/// <summary>
/// The block's type (ID). Returns BlockIDs.Invalid if the block doesn't exist anymore.
/// </summary>
public BlockIDs Type => (BlockIDs)(BlockEngine.GetBlockInfo<DBEntityStruct>(Id)?.DBID ?? ushort.MaxValue);
public BlockIDs Type
{
get
{
var id = (BlockIDs) BlockEngine.GetBlockInfo<DBEntityStruct>(Id, out var exists).DBID;
return exists ? id : BlockIDs.Invalid;
}
}
/// <summary>
/// The block's color. Returns BlockColors.Default if the block no longer exists.
@ -149,15 +153,32 @@ namespace GamecraftModdingAPI
{
get
{
byte index = BlockEngine.GetBlockInfo<ColourParameterEntityStruct>(Id)?.indexInPalette ?? byte.MaxValue;
byte index = BlockEngine.GetBlockInfo<ColourParameterEntityStruct>(Id, out var exists).indexInPalette;
if (!exists) index = byte.MaxValue;
if (index == byte.MaxValue) return new BlockColor { Color = BlockColors.Default };
return new BlockColor { Color = (BlockColors)(index % 10), Darkness = (byte)(index / 10) };
}
set
{
var def = new ColourParameterEntityStruct();
ref var color = ref BlockEngine.GetBlockInfo(Id, ref def);
ref var color = ref BlockEngine.GetBlockInfo<ColourParameterEntityStruct>(Id);
color.indexInPalette = (byte)(value.Color + value.Darkness * 10);
color.overridePaletteColour = false;
color.needsUpdate = true;
BlockEngine.SetBlockColorFromPalette(ref color);
}
}
/// <summary>
/// The block's exact color. Gets reset to the palette color (Color property) after reentering the game.
/// </summary>
public float4 CustomColor
{
get => BlockEngine.GetBlockInfo<ColourParameterEntityStruct>(Id).overriddenColour;
set
{
ref var color = ref BlockEngine.GetBlockInfo<ColourParameterEntityStruct>(Id);
color.overriddenColour = value;
color.overridePaletteColour = true;
color.needsUpdate = true;
}
}

View file

@ -2,6 +2,7 @@ using System.Collections.Generic;
using RobocraftX.Blocks;
using RobocraftX.Common;
using RobocraftX.GUI.Hotbar.Colours;
using Svelto.DataStructures;
using Svelto.ECS;
@ -37,33 +38,44 @@ namespace GamecraftModdingAPI.Blocks
return ret;
}
/// <summary>
/// Get a struct of a block. Can be used to set properties.
/// When only querying parameters, use the other overload for convenience.
/// </summary>
/// <param name="blockID"></param>
/// <param name="def"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public ref T GetBlockInfo<T>(EGID blockID, ref T def) where T : struct, IEntityComponent
public void SetBlockColorFromPalette(ref ColourParameterEntityStruct color)
{
if (entitiesDB.Exists<T>(blockID))
return ref entitiesDB.QueryEntity<T>(blockID);
return ref def;
ref var paletteEntry = ref entitiesDB.QueryEntity<PaletteEntryEntityStruct>(color.indexInPalette,
CommonExclusiveGroups.COLOUR_PALETTE_GROUP);
color.paletteColour = paletteEntry.Colour;
}
/// <summary>
/// Get a struct of a block. Can only be used to retrieve information.
/// Use the overload with a default parameter to get the struct by reference to set values.
/// Get a struct of a block. Can be used to set properties.
/// Returns a default value if not found.
/// </summary>
/// <param name="blockID">The block's EGID</param>
/// <typeparam name="T">The struct's type to get</typeparam>
/// <returns>A copy of the struct or null</returns>
public T? GetBlockInfo<T>(EGID blockID) where T : struct, IEntityComponent
/// <param name="blockID">The block's ID</param>
/// <typeparam name="T">The struct to query</typeparam>
/// <returns>An editable reference to the struct</returns>
public ref T GetBlockInfo<T>(EGID blockID) where T : struct, IEntityComponent
{
if (entitiesDB.Exists<T>(blockID))
return entitiesDB.QueryEntity<T>(blockID);
return null;
return ref entitiesDB.QueryEntity<T>(blockID);
T[] structHolder = new T[1]; //Create something that can be referenced
return ref structHolder[0]; //Gets a default value automatically
}
/// <summary>
/// Get a struct of a block. Can be used to set properties.
/// Returns a default value if not found.
/// </summary>
/// <param name="blockID">The block's ID</param>
/// <param name="exists">Whether the specified struct exists for the block</param>
/// <typeparam name="T">The struct to query</typeparam>
/// <returns>An editable reference to the struct</returns>
public ref T GetBlockInfo<T>(EGID blockID, out bool exists) where T : struct, IEntityComponent
{
exists = entitiesDB.Exists<T>(blockID);
if (exists)
return ref entitiesDB.QueryEntity<T>(blockID);
T[] structHolder = new T[1];
ref T structRef = ref structHolder[0];
return ref structRef;
}
public bool BlockExists(EGID id)

View file

@ -5,6 +5,9 @@ namespace GamecraftModdingAPI.Blocks
/// </summary>
public enum BlockIDs : ushort
{
/// <summary>
/// A custom value for the API. Doesn't exist for Gamecraft.
/// </summary>
Invalid = ushort.MaxValue,
AluminiumCube = 0,
AxleS,

View file

@ -55,42 +55,42 @@ namespace GamecraftModdingAPI.Blocks
{
get
{
return BlockEngine.GetBlockInfo<ConsoleBlockEntityStruct>(Id)?.commandName ?? null;
return BlockEngine.GetBlockInfo<ConsoleBlockEntityStruct>(Id).commandName;
}
set
{
BlockEngine.GetBlockInfo<ConsoleBlockEntityStruct>(Id)?.commandName.Set(value);
BlockEngine.GetBlockInfo<ConsoleBlockEntityStruct>(Id).commandName.Set(value);
}
}
public string Arg1
{
get => BlockEngine.GetBlockInfo<ConsoleBlockEntityStruct>(Id)?.arg1 ?? null;
get => BlockEngine.GetBlockInfo<ConsoleBlockEntityStruct>(Id).arg1;
set
{
BlockEngine.GetBlockInfo<ConsoleBlockEntityStruct>(Id)?.arg1.Set(value);
BlockEngine.GetBlockInfo<ConsoleBlockEntityStruct>(Id).arg1.Set(value);
}
}
public string Arg2
{
get => BlockEngine.GetBlockInfo<ConsoleBlockEntityStruct>(Id)?.arg2 ?? null;
get => BlockEngine.GetBlockInfo<ConsoleBlockEntityStruct>(Id).arg2;
set
{
BlockEngine.GetBlockInfo<ConsoleBlockEntityStruct>(Id)?.arg2.Set(value);
BlockEngine.GetBlockInfo<ConsoleBlockEntityStruct>(Id).arg2.Set(value);
}
}
public string Arg3
{
get => BlockEngine.GetBlockInfo<ConsoleBlockEntityStruct>(Id)?.arg3 ?? null;
get => BlockEngine.GetBlockInfo<ConsoleBlockEntityStruct>(Id).arg3;
set
{
BlockEngine.GetBlockInfo<ConsoleBlockEntityStruct>(Id)?.arg3.Set(value);
BlockEngine.GetBlockInfo<ConsoleBlockEntityStruct>(Id).arg3.Set(value);
}
}
}

View file

@ -59,16 +59,15 @@ namespace GamecraftModdingAPI.Blocks
{
get
{
return BlockEngine.GetBlockInfo<TextBlockDataStruct>(Id)?.textCurrent ?? null;
return BlockEngine.GetBlockInfo<TextBlockDataStruct>(Id).textCurrent;
}
set
{
TextBlockDataStruct def = default;
ref TextBlockDataStruct tbds = ref BlockEngine.GetBlockInfo<TextBlockDataStruct>(Id, ref def);
ref TextBlockDataStruct tbds = ref BlockEngine.GetBlockInfo<TextBlockDataStruct>(Id);
tbds.textCurrent.Set(value);
tbds.textStored.Set(value);
BlockEngine.GetBlockInfo<TextBlockNetworkDataStruct>(Id)?.newTextBlockStringContent.Set(value);
BlockEngine.GetBlockInfo<TextBlockNetworkDataStruct>(Id).newTextBlockStringContent.Set(value);
}
}
@ -79,13 +78,13 @@ namespace GamecraftModdingAPI.Blocks
{
get
{
return BlockEngine.GetBlockInfo<TextBlockDataStruct>(Id)?.textBlockID ?? null;
return BlockEngine.GetBlockInfo<TextBlockDataStruct>(Id).textBlockID;
}
set
{
BlockEngine.GetBlockInfo<TextBlockDataStruct>(Id)?.textBlockID.Set(value);
BlockEngine.GetBlockInfo<TextBlockNetworkDataStruct>(Id)?.newTextBlockID.Set(value);
BlockEngine.GetBlockInfo<TextBlockDataStruct>(Id).textBlockID.Set(value);
BlockEngine.GetBlockInfo<TextBlockNetworkDataStruct>(Id).newTextBlockID.Set(value);
}
}
}

View file

@ -8,12 +8,14 @@ using Svelto.ECS;
using RobocraftX.Blocks;
using RobocraftX.Common;
using RobocraftX.SimulationModeState;
using RobocraftX.FrontEnd;
using Unity.Mathematics;
using GamecraftModdingAPI.Commands;
using GamecraftModdingAPI.Events;
using GamecraftModdingAPI.Utility;
using GamecraftModdingAPI.Blocks;
using RobocraftX.FrontEnd;
using GamecraftModdingAPI.Players;
namespace GamecraftModdingAPI.Tests
{
@ -180,6 +182,24 @@ namespace GamecraftModdingAPI.Tests
.Action(() => { throw new Exception("Error Command always throws an error"); })
.Build();
CommandBuilder.Builder("ColorBlock",
"Change color of the block looked at if there's any.")
.Action<string>(str =>
{
if (!Enum.TryParse(str, out BlockColors color))
{
Logging.CommandLog("Color " + str + " not found! Interpreting as 4 color values.");
var s = str.Split(' ');
new Player(PlayerType.Local).GetBlockLookedAt().CustomColor = new float4(float.Parse(s[0]),
float.Parse(s[1]), float.Parse(s[2]), float.Parse(s[3]));
return;
}
new Player(PlayerType.Local).GetBlockLookedAt().Color =
new BlockColor {Color = color};
Logging.CommandLog("Colored block to " + color);
}).Build();
GameClient.SetDebugInfo("lookedAt", LookedAt);
/*