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

View file

@ -248,11 +248,14 @@ namespace TechbloxModdingAPI
} }
set 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); ref var color = ref BlockEngine.GetBlockInfo<ColourParameterEntityStruct>(this);
color.indexInPalette = value.Index; color.indexInPalette = value.Index;
color.hasNetworkChange = true; 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 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!"); throw new BlockException($"Block material {value} does not exist!");
BlockEngine.GetBlockInfo<CubeMaterialStruct>(this).materialId = (byte) value; BlockEngine.GetBlockInfo<CubeMaterialStruct>(this).materialId = val;
BlockEngine.UpdatePrefab(this, (byte) value, Flipped); //TODO: Test default 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> /// <summary>
/// Whether the block exists. The other properties will return a default value if the block doesn't exist. /// 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. /// 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>() triggerAutoWiring = autoWire && structInitializer.Has<BlockPortsStruct>()
}); });
/*structInitializer.Init(new OverrideStaticComponent()
{ //TODO
staticIfUnconnected = true
});*/
int nextFilterId = BlockGroupUtility.NextFilterId; int nextFilterId = BlockGroupUtility.NextFilterId;
structInitializer.Init(new BlockGroupEntityComponent structInitializer.Init(new BlockGroupEntityComponent
{ {

View file

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

View file

@ -194,6 +194,15 @@ namespace TechbloxModdingAPI.Tests
Game.CurrentGame().ToggleTimeMode(); Game.CurrentGame().ToggleTimeMode();
}).Build(); }).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); Game.AddPersistentDebugInfo("InstalledMods", InstalledMods);
Block.Placed += (sender, args) => Block.Placed += (sender, args) =>
Logging.MetaDebugLog("Placed block " + args.Block); Logging.MetaDebugLog("Placed block " + args.Block);