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;
using Svelto.ECS.EntityStructs; using Svelto.ECS.EntityStructs;
using RobocraftX.Common; using RobocraftX.Common;
using RobocraftX.Blocks.Scaling; using RobocraftX.Blocks;
using Unity.Mathematics; using Unity.Mathematics;
using GamecraftModdingAPI.Blocks; using GamecraftModdingAPI.Blocks;
@ -113,11 +113,10 @@ namespace GamecraftModdingAPI
/// </summary> /// </summary>
public float3 Scale public float3 Scale
{ {
get => BlockEngine.GetBlockInfo<ScalingEntityStruct>(Id)?.scale ?? float3.zero; get => BlockEngine.GetBlockInfo<ScalingEntityStruct>(Id).scale;
set set
{ {
var def = new ScalingEntityStruct(); BlockEngine.GetBlockInfo<ScalingEntityStruct>(Id).scale = value;
BlockEngine.GetBlockInfo(Id, ref def).scale = value;
} }
} }
@ -126,13 +125,11 @@ namespace GamecraftModdingAPI
/// </summary> /// </summary>
public int UniformScale public int UniformScale
{ {
get => BlockEngine.GetBlockInfo<BlockPlacementScaleEntityStruct>(Id)?.desiredScaleFactor ?? 0; get => BlockEngine.GetBlockInfo<UniformBlockScaleEntityStruct>(Id).scaleFactor;
set set
{ {
var def = new BlockPlacementScaleEntityStruct(); ref var scaleStruct = ref BlockEngine.GetBlockInfo<UniformBlockScaleEntityStruct>(Id);
ref var scaleStruct = ref BlockEngine.GetBlockInfo(Id, ref def); scaleStruct.scaleFactor = value;
scaleStruct.blockPlacementHeight = scaleStruct.blockPlacementWidth =
scaleStruct.desiredScaleFactor = scaleStruct.snapGridScale = value;
Scale = new float3(value, value, value); Scale = new float3(value, value, value);
} }
} }
@ -140,7 +137,14 @@ namespace GamecraftModdingAPI
/// <summary> /// <summary>
/// The block's type (ID). Returns BlockIDs.Invalid if the block doesn't exist anymore. /// The block's type (ID). Returns BlockIDs.Invalid if the block doesn't exist anymore.
/// </summary> /// </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> /// <summary>
/// The block's color. Returns BlockColors.Default if the block no longer exists. /// The block's color. Returns BlockColors.Default if the block no longer exists.
@ -149,15 +153,32 @@ namespace GamecraftModdingAPI
{ {
get 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 }; if (index == byte.MaxValue) return new BlockColor { Color = BlockColors.Default };
return new BlockColor { Color = (BlockColors)(index % 10), Darkness = (byte)(index / 10) }; return new BlockColor { Color = (BlockColors)(index % 10), Darkness = (byte)(index / 10) };
} }
set set
{ {
var def = new ColourParameterEntityStruct(); ref var color = ref BlockEngine.GetBlockInfo<ColourParameterEntityStruct>(Id);
ref var color = ref BlockEngine.GetBlockInfo(Id, ref def);
color.indexInPalette = (byte)(value.Color + value.Darkness * 10); 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; color.needsUpdate = true;
} }
} }

View file

@ -2,6 +2,7 @@ using System.Collections.Generic;
using RobocraftX.Blocks; using RobocraftX.Blocks;
using RobocraftX.Common; using RobocraftX.Common;
using RobocraftX.GUI.Hotbar.Colours;
using Svelto.DataStructures; using Svelto.DataStructures;
using Svelto.ECS; using Svelto.ECS;
@ -37,33 +38,44 @@ namespace GamecraftModdingAPI.Blocks
return ret; return ret;
} }
/// <summary> public void SetBlockColorFromPalette(ref ColourParameterEntityStruct color)
/// 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
{ {
if (entitiesDB.Exists<T>(blockID)) ref var paletteEntry = ref entitiesDB.QueryEntity<PaletteEntryEntityStruct>(color.indexInPalette,
return ref entitiesDB.QueryEntity<T>(blockID); CommonExclusiveGroups.COLOUR_PALETTE_GROUP);
return ref def; color.paletteColour = paletteEntry.Colour;
} }
/// <summary> /// <summary>
/// Get a struct of a block. Can only be used to retrieve information. /// Get a struct of a block. Can be used to set properties.
/// Use the overload with a default parameter to get the struct by reference to set values. /// Returns a default value if not found.
/// </summary> /// </summary>
/// <param name="blockID">The block's EGID</param> /// <param name="blockID">The block's ID</param>
/// <typeparam name="T">The struct's type to get</typeparam> /// <typeparam name="T">The struct to query</typeparam>
/// <returns>A copy of the struct or null</returns> /// <returns>An editable reference to the struct</returns>
public T? GetBlockInfo<T>(EGID blockID) where T : struct, IEntityComponent public ref T GetBlockInfo<T>(EGID blockID) where T : struct, IEntityComponent
{ {
if (entitiesDB.Exists<T>(blockID)) if (entitiesDB.Exists<T>(blockID))
return entitiesDB.QueryEntity<T>(blockID); return ref entitiesDB.QueryEntity<T>(blockID);
return null; 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) public bool BlockExists(EGID id)

View file

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

View file

@ -55,42 +55,42 @@ namespace GamecraftModdingAPI.Blocks
{ {
get get
{ {
return BlockEngine.GetBlockInfo<ConsoleBlockEntityStruct>(Id)?.commandName ?? null; return BlockEngine.GetBlockInfo<ConsoleBlockEntityStruct>(Id).commandName;
} }
set set
{ {
BlockEngine.GetBlockInfo<ConsoleBlockEntityStruct>(Id)?.commandName.Set(value); BlockEngine.GetBlockInfo<ConsoleBlockEntityStruct>(Id).commandName.Set(value);
} }
} }
public string Arg1 public string Arg1
{ {
get => BlockEngine.GetBlockInfo<ConsoleBlockEntityStruct>(Id)?.arg1 ?? null; get => BlockEngine.GetBlockInfo<ConsoleBlockEntityStruct>(Id).arg1;
set set
{ {
BlockEngine.GetBlockInfo<ConsoleBlockEntityStruct>(Id)?.arg1.Set(value); BlockEngine.GetBlockInfo<ConsoleBlockEntityStruct>(Id).arg1.Set(value);
} }
} }
public string Arg2 public string Arg2
{ {
get => BlockEngine.GetBlockInfo<ConsoleBlockEntityStruct>(Id)?.arg2 ?? null; get => BlockEngine.GetBlockInfo<ConsoleBlockEntityStruct>(Id).arg2;
set set
{ {
BlockEngine.GetBlockInfo<ConsoleBlockEntityStruct>(Id)?.arg2.Set(value); BlockEngine.GetBlockInfo<ConsoleBlockEntityStruct>(Id).arg2.Set(value);
} }
} }
public string Arg3 public string Arg3
{ {
get => BlockEngine.GetBlockInfo<ConsoleBlockEntityStruct>(Id)?.arg3 ?? null; get => BlockEngine.GetBlockInfo<ConsoleBlockEntityStruct>(Id).arg3;
set 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 get
{ {
return BlockEngine.GetBlockInfo<TextBlockDataStruct>(Id)?.textCurrent ?? null; return BlockEngine.GetBlockInfo<TextBlockDataStruct>(Id).textCurrent;
} }
set set
{ {
TextBlockDataStruct def = default; ref TextBlockDataStruct tbds = ref BlockEngine.GetBlockInfo<TextBlockDataStruct>(Id);
ref TextBlockDataStruct tbds = ref BlockEngine.GetBlockInfo<TextBlockDataStruct>(Id, ref def);
tbds.textCurrent.Set(value); tbds.textCurrent.Set(value);
tbds.textStored.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 get
{ {
return BlockEngine.GetBlockInfo<TextBlockDataStruct>(Id)?.textBlockID ?? null; return BlockEngine.GetBlockInfo<TextBlockDataStruct>(Id).textBlockID;
} }
set set
{ {
BlockEngine.GetBlockInfo<TextBlockDataStruct>(Id)?.textBlockID.Set(value); BlockEngine.GetBlockInfo<TextBlockDataStruct>(Id).textBlockID.Set(value);
BlockEngine.GetBlockInfo<TextBlockNetworkDataStruct>(Id)?.newTextBlockID.Set(value); BlockEngine.GetBlockInfo<TextBlockNetworkDataStruct>(Id).newTextBlockID.Set(value);
} }
} }
} }

View file

@ -8,12 +8,14 @@ using Svelto.ECS;
using RobocraftX.Blocks; using RobocraftX.Blocks;
using RobocraftX.Common; using RobocraftX.Common;
using RobocraftX.SimulationModeState; using RobocraftX.SimulationModeState;
using RobocraftX.FrontEnd;
using Unity.Mathematics;
using GamecraftModdingAPI.Commands; using GamecraftModdingAPI.Commands;
using GamecraftModdingAPI.Events; using GamecraftModdingAPI.Events;
using GamecraftModdingAPI.Utility; using GamecraftModdingAPI.Utility;
using GamecraftModdingAPI.Blocks; using GamecraftModdingAPI.Blocks;
using RobocraftX.FrontEnd; using GamecraftModdingAPI.Players;
namespace GamecraftModdingAPI.Tests namespace GamecraftModdingAPI.Tests
{ {
@ -177,8 +179,26 @@ namespace GamecraftModdingAPI.Tests
.Action(() => uREPL.Log.Output(new Player(Players.PlayerType.Local).GetBlockLookedAt()+"")).Build(); .Action(() => uREPL.Log.Output(new Player(Players.PlayerType.Local).GetBlockLookedAt()+"")).Build();
CommandBuilder.Builder("Error", "Throw an error to make sure SimpleCustomCommandEngine's wrapper catches it.") CommandBuilder.Builder("Error", "Throw an error to make sure SimpleCustomCommandEngine's wrapper catches it.")
.Action(() => { throw new Exception("Error Command always throws an error"); }) .Action(() => { throw new Exception("Error Command always throws an error"); })
.Build(); .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); GameClient.SetDebugInfo("lookedAt", LookedAt);