@@ -3,12 +3,12 @@ using System.Collections.Generic; | |||
using System.Linq; | |||
using System.Reflection.Emit; | |||
using Gamecraft.Blocks.BlockGroups; | |||
using Svelto.ECS; | |||
using Svelto.ECS.EntityStructs; | |||
using RobocraftX.Common; | |||
using RobocraftX.Blocks; | |||
using Unity.Mathematics; | |||
using Unity.Entities; | |||
using Gamecraft.Blocks.GUI; | |||
using GamecraftModdingAPI.Blocks; | |||
@@ -354,6 +354,17 @@ namespace GamecraftModdingAPI | |||
} | |||
} | |||
/// <summary> | |||
/// Returns the block group this block is a part of. Block groups can be placed using blueprints. | |||
/// Returns null if not part of a group. | |||
/// </summary> | |||
public BlockGroup BlockGroup | |||
{ | |||
get => BlockEngine.GetBlockInfo(this, | |||
(BlockGroupEntityComponent bgec) => | |||
bgec.currentBlockGroup == -1 ? null : new BlockGroup(bgec.currentBlockGroup, this)); | |||
} | |||
/// <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. | |||
@@ -0,0 +1,46 @@ | |||
using Gamecraft.Blocks.BlockGroups; | |||
using GamecraftModdingAPI.Blocks; | |||
using GamecraftModdingAPI.Utility; | |||
namespace GamecraftModdingAPI | |||
{ | |||
/// <summary> | |||
/// A group of blocks that can be selected together. The placed version of blueprints. | |||
/// </summary> | |||
public class BlockGroup | |||
{ | |||
internal static BlueprintEngine _engine = new BlueprintEngine(); | |||
public int Id { get; } | |||
private Block _sourceBlock; | |||
internal BlockGroup(int id, Block block) | |||
{ | |||
if (id == BlockGroupUtility.GROUP_UNASSIGNED) | |||
throw new BlockException("Cannot create a block group for blocks without a group!"); | |||
Id = id; | |||
_sourceBlock = block; | |||
} | |||
/// <summary> | |||
/// Collects each block that is a part of this group. | |||
/// </summary> | |||
/// <returns>An array of blocks</returns> | |||
public Block[] GetBlocks() | |||
{ | |||
return _engine.GetBlocksFromGroup(_sourceBlock.Id); | |||
} | |||
/// <summary> | |||
/// Removes all of the blocks in this group from the world. | |||
/// </summary> | |||
public void Remove() | |||
{ | |||
_engine.RemoveBlockGroup(Id); | |||
} | |||
public static void Init() | |||
{ | |||
GameEngineManager.AddGameEngine(_engine); | |||
} | |||
} | |||
} |
@@ -327,6 +327,8 @@ namespace GamecraftModdingAPI.Blocks | |||
SpotLightInvisible, | |||
UnlitSlope, | |||
UnlitGlowSlope, | |||
Fog, | |||
Sky, | |||
MagmaRockCube = 777, | |||
MagmaRockCubeSliced, | |||
MagmaRockSlope, | |||
@@ -0,0 +1,77 @@ | |||
using System.Reflection; | |||
using Gamecraft.Blocks.BlockGroups; | |||
using Gamecraft.GUI.Blueprints; | |||
using GamecraftModdingAPI.Engines; | |||
using HarmonyLib; | |||
using RobocraftX.Blocks; | |||
using Svelto.DataStructures; | |||
using Svelto.ECS; | |||
using Svelto.ECS.DataStructures; | |||
using Unity.Collections; | |||
namespace GamecraftModdingAPI.Blocks | |||
{ | |||
public class BlueprintEngine : IApiEngine | |||
{ | |||
private readonly MethodInfo getBlocksFromGroup = | |||
AccessTools.Method("RobocraftX.CR.MachineEditing.PlaceBlockUtility:GetBlocksSharingBlockgroup"); | |||
private readonly NativeDynamicArray selectedBlocksInGroup = new NativeDynamicArray(); | |||
private readonly NativeHashSet<ulong> removedConnections = new NativeHashSet<ulong>(); | |||
private static NativeEntityRemove nativeRemove; | |||
private static MachineGraphConnectionEntityFactory connectionFactory; | |||
public void Ready() | |||
{ | |||
} | |||
public EntitiesDB entitiesDB { get; set; } | |||
public void Dispose() | |||
{ | |||
} | |||
public Block[] GetBlocksFromGroup(EGID blockID) | |||
{ | |||
var list = new FasterList<Block>(); | |||
object blockPos = null, blockRot = null; | |||
getBlocksFromGroup.Invoke(null, new[] {blockID, selectedBlocksInGroup, entitiesDB, blockPos, blockRot}); | |||
for (uint i = 0; i < selectedBlocksInGroup.Count<EGID>(); i++) | |||
list.Add(new Block(selectedBlocksInGroup.Get<EGID>(i))); | |||
selectedBlocksInGroup.FastClear(); | |||
return list.ToArray(); | |||
} | |||
public void RemoveBlockGroup(int id) | |||
{ | |||
BlockGroupUtility.RemoveAllBlocksInBlockGroup(id, entitiesDB, removedConnections, nativeRemove, | |||
connectionFactory, default).Complete(); | |||
} | |||
public void SelectBlueprint(uint resourceID) | |||
{ | |||
BlueprintUtil.SelectBlueprint(null, new BlueprintInventoryItemEntityStruct | |||
{ | |||
blueprintResourceId = resourceID, | |||
}); | |||
} | |||
public string Name { get; } = "GamecraftModdingAPIBlueprintGameEngine"; | |||
public bool isRemovable { get; } | |||
[HarmonyPatch] | |||
private static class RemoveEnginePatch | |||
{ | |||
public static void Prefix(IEntityFunctions entityFunctions, | |||
MachineGraphConnectionEntityFactory machineGraphConnectionEntityFactory) | |||
{ | |||
nativeRemove = entityFunctions.ToNativeRemove<BlockEntityDescriptor>("GCAPI" + nameof(BlueprintEngine)); | |||
connectionFactory = machineGraphConnectionEntityFactory; | |||
} | |||
public static MethodBase TargetMethod() | |||
{ | |||
return AccessTools.Constructor(AccessTools.TypeByName("RobocraftX.CR.MachineEditing.RemoveBlockEngine")); | |||
} | |||
} | |||
} | |||
} |
@@ -0,0 +1,20 @@ | |||
using Gamecraft.GUI.Blueprints; | |||
namespace GamecraftModdingAPI | |||
{ | |||
/// <summary> | |||
/// Represents a blueprint in the inventory. When placed it becomes a block group. | |||
/// </summary> | |||
public class Blueprint | |||
{ | |||
public uint Id { get; } | |||
/*public static void SelectBlueprint(Blueprint blueprint) | |||
{ | |||
BlueprintUtil.SelectBlueprint(null, new BlueprintInventoryItemEntityStruct | |||
{ | |||
blueprintResourceId = blueprint.Id | |||
}); | |||
}*/ | |||
} | |||
} |
@@ -83,6 +83,7 @@ namespace GamecraftModdingAPI | |||
// init object-oriented classes | |||
Player.Init(); | |||
Block.Init(); | |||
BlockGroup.Init(); | |||
Wire.Init(); | |||
GameClient.Init(); | |||
AsyncUtils.Init(); | |||