Add new blocks and some blueprint/block group support

This commit is contained in:
Norbi Peti 2020-11-10 16:37:20 +01:00
parent d891f12701
commit 1a986056a1
6 changed files with 158 additions and 1 deletions

View file

@ -3,12 +3,12 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Reflection.Emit; using System.Reflection.Emit;
using Gamecraft.Blocks.BlockGroups;
using Svelto.ECS; using Svelto.ECS;
using Svelto.ECS.EntityStructs; using Svelto.ECS.EntityStructs;
using RobocraftX.Common; using RobocraftX.Common;
using RobocraftX.Blocks; using RobocraftX.Blocks;
using Unity.Mathematics; using Unity.Mathematics;
using Unity.Entities;
using Gamecraft.Blocks.GUI; using Gamecraft.Blocks.GUI;
using GamecraftModdingAPI.Blocks; 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> /// <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

@ -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);
}
}
}

View file

@ -327,6 +327,8 @@ namespace GamecraftModdingAPI.Blocks
SpotLightInvisible, SpotLightInvisible,
UnlitSlope, UnlitSlope,
UnlitGlowSlope, UnlitGlowSlope,
Fog,
Sky,
MagmaRockCube = 777, MagmaRockCube = 777,
MagmaRockCubeSliced, MagmaRockCubeSliced,
MagmaRockSlope, MagmaRockSlope,

View file

@ -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"));
}
}
}
}

View file

@ -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
});
}*/
}
}

View file

@ -83,6 +83,7 @@ namespace GamecraftModdingAPI
// init object-oriented classes // init object-oriented classes
Player.Init(); Player.Init();
Block.Init(); Block.Init();
BlockGroup.Init();
Wire.Init(); Wire.Init();
GameClient.Init(); GameClient.Init();
AsyncUtils.Init(); AsyncUtils.Init();