Browse Source

Add new blocks and some blueprint/block group support

tags/v1.7.0
NorbiPeti 3 years ago
parent
commit
4f8feaa24b
6 changed files with 158 additions and 1 deletions
  1. +12
    -1
      GamecraftModdingAPI/Block.cs
  2. +46
    -0
      GamecraftModdingAPI/BlockGroup.cs
  3. +2
    -0
      GamecraftModdingAPI/Blocks/BlockIDs.cs
  4. +77
    -0
      GamecraftModdingAPI/Blocks/BlueprintEngine.cs
  5. +20
    -0
      GamecraftModdingAPI/Blueprint.cs
  6. +1
    -0
      GamecraftModdingAPI/Main.cs

+ 12
- 1
GamecraftModdingAPI/Block.cs View File

@@ -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.


+ 46
- 0
GamecraftModdingAPI/BlockGroup.cs 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);
}
}
}

+ 2
- 0
GamecraftModdingAPI/Blocks/BlockIDs.cs View File

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


+ 77
- 0
GamecraftModdingAPI/Blocks/BlueprintEngine.cs 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"));
}
}
}
}

+ 20
- 0
GamecraftModdingAPI/Blueprint.cs 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
});
}*/
}
}

+ 1
- 0
GamecraftModdingAPI/Main.cs View File

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


Loading…
Cancel
Save