Add first block children ConsoleBlock and TextBlock, I hope they're not bullied at school for those names

This commit is contained in:
NGnius (Graham) 2020-05-17 23:19:16 -04:00
parent 6dce87fb66
commit 97de5e606b
7 changed files with 514 additions and 214 deletions

View file

@ -1,4 +1,5 @@
using System;
using System.Reflection;
using Svelto.ECS;
using Svelto.ECS.EntityStructs;
@ -16,11 +17,11 @@ namespace GamecraftModdingAPI
/// </summary>
public class Block
{
private static readonly PlacementEngine PlacementEngine = new PlacementEngine();
private static readonly MovementEngine MovementEngine = new MovementEngine();
private static readonly RotationEngine RotationEngine = new RotationEngine();
private static readonly RemovalEngine RemovalEngine = new RemovalEngine();
private static readonly BlockEngine BlockEngine = new BlockEngine();
protected static readonly PlacementEngine PlacementEngine = new PlacementEngine();
protected static readonly MovementEngine MovementEngine = new MovementEngine();
protected static readonly RotationEngine RotationEngine = new RotationEngine();
protected static readonly RemovalEngine RemovalEngine = new RemovalEngine();
protected static readonly BlockEngine BlockEngine = new BlockEngine();
/// <summary>
/// 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.
@ -75,6 +76,11 @@ namespace GamecraftModdingAPI
Id = new EGID(id, CommonExclusiveGroups.OWNED_BLOCKS_GROUP);
}
protected static void Sync()
{
DeterministicStepCompositionRootPatch.SubmitEntitiesNow();
}
public EGID Id { get; }
/// <summary>
@ -134,7 +140,7 @@ 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 => (BlockIDs)(BlockEngine.GetBlockInfo<DBEntityStruct>(Id)?.DBID ?? ushort.MaxValue);
/// <summary>
/// The block's color. Returns BlockColors.Default if the block no longer exists.
@ -144,14 +150,14 @@ namespace GamecraftModdingAPI
get
{
byte index = BlockEngine.GetBlockInfo<ColourParameterEntityStruct>(Id)?.indexInPalette ?? byte.MaxValue;
if (index == byte.MaxValue) return new BlockColor {Color = BlockColors.Default};
return new BlockColor {Color = (BlockColors) (index % 10), Darkness = (byte) (index / 10)};
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);
color.indexInPalette = (byte) (value.Color + value.Darkness * 10);
color.indexInPalette = (byte)(value.Color + value.Darkness * 10);
color.needsUpdate = true;
}
}
@ -185,5 +191,30 @@ namespace GamecraftModdingAPI
GameEngineManager.AddGameEngine(RemovalEngine);
GameEngineManager.AddGameEngine(BlockEngine);
}
public T Specialise<T>(bool sameTick = true) where T : Block
{
// What have I gotten myself into?
// C# can't cast to a child of Block unless the object was originally that child type
// And C# doesn't let me make implicit cast operators for child types
// So thanks to Microsoft, we've got this horrible implementation using reflection
ConstructorInfo ctor = typeof(T).GetConstructor(types: new System.Type[] { typeof(EGID) });
if (ctor == null)
{
throw new BlockSpecializationException("Specialized block constructor does not accept an EGID");
}
if (sameTick) Sync();
return (T)ctor.Invoke(new object[] { Id });
}
#if DEBUG
public static EntitiesDB entitiesDB
{
get
{
return BlockEngine.GetEntitiesDB();
}
}
#endif
}
}

View file

@ -70,5 +70,17 @@ namespace GamecraftModdingAPI.Blocks
{
return entitiesDB.Exists<DBEntityStruct>(id);
}
public bool GetBlockInfoExists<T>(EGID blockID) where T : struct, IEntityComponent
{
return entitiesDB.Exists<T>(blockID);
}
#if DEBUG
public EntitiesDB GetEntitiesDB()
{
return entitiesDB;
}
#endif
}
}

View file

@ -0,0 +1,43 @@
using System;
using GamecraftModdingAPI;
namespace GamecraftModdingAPI.Blocks
{
public class BlockException : GamecraftModdingAPIException
{
public BlockException()
{
}
public BlockException(System.String message) : base(message)
{
}
public BlockException(System.String message, Exception innerException) : base(message, innerException)
{
}
}
public class BlockTypeException : BlockException
{
public BlockTypeException()
{
}
public BlockTypeException(string message) : base(message)
{
}
}
public class BlockSpecializationException : BlockException
{
public BlockSpecializationException()
{
}
public BlockSpecializationException(string message) : base(message)
{
}
}
}

View file

@ -0,0 +1,97 @@
using System;
using RobocraftX.Blocks;
using Svelto.ECS;
using Unity.Mathematics;
using GamecraftModdingAPI;
using GamecraftModdingAPI.Utility;
namespace GamecraftModdingAPI.Blocks
{
public class ConsoleBlock : Block
{
public static ConsoleBlock PlaceNew(float3 position,
float3 rotation = default, BlockColors color = BlockColors.Default, byte darkness = 0,
int uscale = 1, float3 scale = default, Player player = null)
{
if (PlacementEngine.IsInGame && GameState.IsBuildMode())
{
try
{
EGID id = PlacementEngine.PlaceBlock(BlockIDs.ConsoleBlock, color, darkness,
position, uscale, scale, player, rotation);
Sync();
return new ConsoleBlock(id);
}
catch (Exception e)
{
Logging.MetaDebugLog(e);
}
}
return null;
}
public ConsoleBlock(EGID id): base(id)
{
if (!BlockEngine.GetBlockInfoExists<ConsoleBlockEntityStruct>(this.Id))
{
throw new BlockTypeException($"Block is not a {this.GetType().Name} block");
}
}
public ConsoleBlock(uint id): base(id)
{
if (!BlockEngine.GetBlockInfoExists<ConsoleBlockEntityStruct>(this.Id))
{
throw new BlockTypeException($"Block is not a {this.GetType().Name} block");
}
}
// custom console block properties
public string Command
{
get
{
return BlockEngine.GetBlockInfo<ConsoleBlockEntityStruct>(Id)?.commandName ?? null;
}
set
{
BlockEngine.GetBlockInfo<ConsoleBlockEntityStruct>(Id)?.commandName.Set(value);
}
}
public string Arg1
{
get => BlockEngine.GetBlockInfo<ConsoleBlockEntityStruct>(Id)?.arg1 ?? null;
set
{
BlockEngine.GetBlockInfo<ConsoleBlockEntityStruct>(Id)?.arg1.Set(value);
}
}
public string Arg2
{
get => BlockEngine.GetBlockInfo<ConsoleBlockEntityStruct>(Id)?.arg2 ?? null;
set
{
BlockEngine.GetBlockInfo<ConsoleBlockEntityStruct>(Id)?.arg2.Set(value);
}
}
public string Arg3
{
get => BlockEngine.GetBlockInfo<ConsoleBlockEntityStruct>(Id)?.arg3 ?? null;
set
{
BlockEngine.GetBlockInfo<ConsoleBlockEntityStruct>(Id)?.arg3.Set(value);
}
}
}
}

View file

@ -0,0 +1,92 @@
using System;
using Gamecraft.Blocks.GUI;
using Svelto.ECS;
using Unity.Mathematics;
using GamecraftModdingAPI;
using GamecraftModdingAPI.Utility;
namespace GamecraftModdingAPI.Blocks
{
public class TextBlock : Block
{
public static TextBlock PlaceNew(float3 position,
float3 rotation = default, BlockColors color = BlockColors.Default, byte darkness = 0,
int uscale = 1, float3 scale = default, Player player = null)
{
if (PlacementEngine.IsInGame && GameState.IsBuildMode())
{
try
{
EGID id = PlacementEngine.PlaceBlock(BlockIDs.TextBlock, color, darkness,
position, uscale, scale, player, rotation);
Sync();
return new TextBlock(id);
}
catch (Exception e)
{
Logging.MetaDebugLog(e);
}
}
return null;
}
public TextBlock(EGID id) : base(id)
{
if (!BlockEngine.GetBlockInfoExists<TextBlockDataStruct>(this.Id))
{
throw new BlockTypeException($"Block is not a {this.GetType().Name} block");
}
}
public TextBlock(uint id) : base(id)
{
if (!BlockEngine.GetBlockInfoExists<TextBlockDataStruct>(this.Id))
{
throw new BlockTypeException($"Block is not a {this.GetType().Name} block");
}
}
// custom text block properties
/// <summary>
/// The text block's current text.
/// </summary>
public string Text
{
get
{
return BlockEngine.GetBlockInfo<TextBlockDataStruct>(Id)?.textCurrent ?? null;
}
set
{
TextBlockDataStruct def = default;
ref TextBlockDataStruct tbds = ref BlockEngine.GetBlockInfo<TextBlockDataStruct>(Id, ref def);
tbds.textCurrent.Set(value);
tbds.textStored.Set(value);
BlockEngine.GetBlockInfo<TextBlockNetworkDataStruct>(Id)?.newTextBlockStringContent.Set(value);
}
}
/// <summary>
/// The text block's current text block ID (used in ChangeTextBlockCommand).
/// </summary>
public string TextBlockId
{
get
{
return BlockEngine.GetBlockInfo<TextBlockDataStruct>(Id)?.textBlockID ?? null;
}
set
{
BlockEngine.GetBlockInfo<TextBlockDataStruct>(Id)?.textBlockID.Set(value);
BlockEngine.GetBlockInfo<TextBlockNetworkDataStruct>(Id)?.newTextBlockID.Set(value);
}
}
}
}

View file

@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>net472</TargetFramework>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Version>1.0.2</Version>
<Version>1.1.0</Version>
<Authors>Exmods</Authors>
<PackageLicenseExpression>GNU General Public Licence 3+</PackageLicenseExpression>
<PackageProjectUrl>https://git.exmods.org/modtainers/GamecraftModdingAPI</PackageProjectUrl>

View file

@ -0,0 +1,25 @@
using System;
using RobocraftX.StateSync;
using Svelto.ECS;
using HarmonyLib;
namespace GamecraftModdingAPI.Utility
{
[HarmonyPatch(typeof(DeterministicStepCompositionRoot), "ResetWorld")]
public static class DeterministicStepCompositionRootPatch
{
private static SimpleEntitiesSubmissionScheduler engineRootScheduler;
public static void Postfix(SimpleEntitiesSubmissionScheduler scheduler)
{
engineRootScheduler = scheduler;
}
internal static void SubmitEntitiesNow()
{
if (engineRootScheduler != null)
engineRootScheduler.SubmitEntities();
}
}
}