Added support for setting default color/material and static blocks

Also fixed setting game name/description

When setting the block color or material to default it will look up the default value to use
Blocks can now be set to be static so they don't fall or move at all
This commit is contained in:
Norbi Peti 2021-06-04 23:07:06 +02:00
parent 06cb911ea3
commit c1c226ef2a
5 changed files with 41 additions and 28 deletions

View file

@ -92,7 +92,7 @@ namespace TechbloxModdingAPI.App
{
if (!ExistsGameInfo(id)) return false;
GetGameInfo(id).GameName.Set(name);
//GetGameViewInfo(id).MyGamesSlotComponent.GameName = StringUtil.SanitiseString(name); - TODO: Input field struct
GetGameViewInfo(id).MyGamesSlotComponent.GameName = StringUtil.SanitiseString(name);
return true;
}
@ -100,7 +100,7 @@ namespace TechbloxModdingAPI.App
{
if (!ExistsGameInfo(id)) return false;
GetGameInfo(id).GameDescription.Set(name);
//GetGameViewInfo(id).MyGamesSlotComponent.GameDescription = StringUtil.SanitiseString(name); - TODO
GetGameViewInfo(id).MyGamesSlotComponent.GameDescription = StringUtil.SanitiseString(name);
return true;
}
@ -114,21 +114,14 @@ namespace TechbloxModdingAPI.App
return ref GetComponent<MyGameDataEntityStruct>(id);
}
/*public ref MyGamesSlotEntityViewStruct GetGameViewInfo(EGID id)
public dynamic GetGameViewInfo(EGID id)
{
EntityCollection<MyGamesSlotEntityViewStruct> entities =
entitiesDB.QueryEntities<MyGamesSlotEntityViewStruct>(MyGamesScreenExclusiveGroups.GameSlotGuiEntities);
var entitiesB = entities.ToBuffer().buffer;
for (int i = 0; i < entities.count; i++)
{
if (entitiesB[i].ID.entityID == id.entityID)
{
return ref entitiesB[i];
}
}
MyGamesSlotEntityViewStruct[] defRef = new MyGamesSlotEntityViewStruct[1];
return ref defRef[0]; - TODO: The struct is internal now
}*/
dynamic structOptional = AccessTools.Method("TechbloxModdingAPI.Utility.NativeApiExtensions:QueryEntityOptional", new []{typeof(EntitiesDB), typeof(EGID)})
.MakeGenericMethod(AccessTools.TypeByName("RobocraftX.GUI.MyGamesScreen.MyGamesSlotEntityViewStruct"))
.Invoke(null, new object[] {entitiesDB, new EGID(id.entityID, MyGamesScreenExclusiveGroups.GameSlotGuiEntities)});
if (structOptional == null) throw new Exception("Could not get game slot entity");
return structOptional ? structOptional : null;
}
public ref T GetComponent<T>(EGID id) where T: unmanaged, IEntityComponent
{

View file

@ -248,11 +248,14 @@ namespace TechbloxModdingAPI
}
set
{
//TODO: Check if setting to 255 works
if (value.Color == BlockColors.Default)
value = new BlockColor(FullGameFields._dataDb.TryGetValue((int) Type, out CubeListData cld)
? cld.DefaultColour
: throw new BlockTypeException("Unknown block type! Could not set default color."));
ref var color = ref BlockEngine.GetBlockInfo<ColourParameterEntityStruct>(this);
color.indexInPalette = value.Index;
color.hasNetworkChange = true;
color.paletteColour = BlockEngine.ConvertBlockColor(color.indexInPalette);
color.paletteColour = BlockEngine.ConvertBlockColor(color.indexInPalette); //Setting to 255 results in black
}
}
@ -282,10 +285,15 @@ namespace TechbloxModdingAPI
}
set
{
if (!FullGameFields._dataDb.ContainsKey<MaterialPropertiesData>((byte) value))
byte val = (byte) value;
if (value == BlockMaterial.Default)
val = FullGameFields._dataDb.TryGetValue((int) Type, out CubeListData cld)
? cld.DefaultMaterialID
: throw new BlockTypeException("Unknown block type! Could not set default material.");
if (!FullGameFields._dataDb.ContainsKey<MaterialPropertiesData>(val))
throw new BlockException($"Block material {value} does not exist!");
BlockEngine.GetBlockInfo<CubeMaterialStruct>(this).materialId = (byte) value;
BlockEngine.UpdatePrefab(this, (byte) value, Flipped); //TODO: Test default
BlockEngine.GetBlockInfo<CubeMaterialStruct>(this).materialId = val;
BlockEngine.UpdatePrefab(this, val, Flipped); //The default causes the screen to go black
}
}
@ -336,6 +344,15 @@ namespace TechbloxModdingAPI
}
}
/// <summary>
/// Whether the block should be static in simulation. If set, it cannot be moved. The effect is temporary, it will not be saved with the block.
/// </summary>
public bool Static
{
get => BlockEngine.GetBlockInfo<OverrideStaticComponent>(this).staticIfUnconnected;
set => BlockEngine.GetBlockInfo<OverrideStaticComponent>(this).staticIfUnconnected = value;
}
/// <summary>
/// Whether the block exists. The other properties will return a default value if the block doesn't exist.
/// If the block was just placed, then this will also return false but the properties will work correctly.

View file

@ -77,12 +77,6 @@ namespace TechbloxModdingAPI.Blocks.Engines
triggerAutoWiring = autoWire && structInitializer.Has<BlockPortsStruct>()
});
/*structInitializer.Init(new OverrideStaticComponent()
{ //TODO
staticIfUnconnected = true
});*/
int nextFilterId = BlockGroupUtility.NextFilterId;
structInitializer.Init(new BlockGroupEntityComponent
{

View file

@ -24,7 +24,7 @@ namespace TechbloxModdingAPI.Interface.IMGUI
/// <summary>
/// Best-effort imitation of Techblox's UI style.
/// </summary>
public static GUISkin Default //TODO: Update to Techblox style
public static GUISkin Default
{
get
{

View file

@ -194,6 +194,15 @@ namespace TechbloxModdingAPI.Tests
Game.CurrentGame().ToggleTimeMode();
}).Build();
CommandBuilder.Builder("testColorBlock", "Tests coloring a block to default color")
.Action(() => Player.LocalPlayer.GetBlockLookedAt().Color = BlockColors.Default).Build();
CommandBuilder.Builder("testMaterialBlock", "Tests materialing a block to default material")
.Action(() => Player.LocalPlayer.GetBlockLookedAt().Material = BlockMaterial.Default).Build();
CommandBuilder.Builder("testGameName", "Tests changing the game name")
.Action(() => Game.CurrentGame().Name = "Test").Build();
CommandBuilder.Builder("makeBlockStatic", "Makes a block you look at static")
.Action(() => Player.LocalPlayer.GetBlockLookedAt().Static = true).Build();
Game.AddPersistentDebugInfo("InstalledMods", InstalledMods);
Block.Placed += (sender, args) =>
Logging.MetaDebugLog("Placed block " + args.Block);