Browse Source

Add inventory select block support

tags/v0.2.0
NGnius 4 years ago
parent
commit
2149458d96
6 changed files with 130 additions and 2 deletions
  1. +0
    -2
      GamecraftModdingAPI/Blocks/SignalEngine.cs
  2. +3
    -0
      GamecraftModdingAPI/GamecraftModdingAPI.csproj
  3. +38
    -0
      GamecraftModdingAPI/Inventory/Hotbar.cs
  4. +55
    -0
      GamecraftModdingAPI/Inventory/HotbarEngine.cs
  5. +32
    -0
      GamecraftModdingAPI/Inventory/HotbarSlotSelectionHandlerEnginePatch.cs
  6. +2
    -0
      GamecraftModdingAPI/Main.cs

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

@@ -36,8 +36,6 @@ namespace GamecraftModdingAPI.Blocks

public bool IsInGame = false;

private System.Random random = new System.Random();

public void Dispose()
{
IsInGame = false;


+ 3
- 0
GamecraftModdingAPI/GamecraftModdingAPI.csproj View File

@@ -542,4 +542,7 @@
</ItemGroup>
<!--End Dependencies-->

<ItemGroup>
<Folder Include="Inventory\" />
</ItemGroup>
</Project>

+ 38
- 0
GamecraftModdingAPI/Inventory/Hotbar.cs View File

@@ -0,0 +1,38 @@
using System;

using RobocraftX.Common.Input;
using RobocraftX.Multiplayer.Input;

using GamecraftModdingAPI.Blocks;
using GamecraftModdingAPI.Utility;
using Harmony;

namespace GamecraftModdingAPI.Inventory
{
public static class Hotbar
{
private static HotbarEngine hotbarEngine = new HotbarEngine();

public static void EquipBlock(BlockIDs block, uint playerID = uint.MaxValue)
{
if (playerID == uint.MaxValue)
{
playerID = hotbarEngine.GetLocalPlayerID();
}
hotbarEngine.SelectBlock((int) block, playerID);
// cubeSelectedByPick = true will crash the game
// (this would be equivalent to mouse middle click pick block action)
// reason: the game expects a Dictionary entry for the tweaked stats
}

public static BlockIDs GetEquippedBlock()
{
return HotbarSlotSelectionHandlerEnginePatch.EquippedPartID;
}

public static void Init()
{
GameEngineManager.AddGameEngine(hotbarEngine);
}
}
}

+ 55
- 0
GamecraftModdingAPI/Inventory/HotbarEngine.cs View File

@@ -0,0 +1,55 @@
using System;

using RobocraftX.Character;
using RobocraftX.GUI.Hotbar;
using RobocraftX.Players;
using RobocraftX.Common;
using RobocraftX.Common.Input;
using RobocraftX.Common.Players;
using Svelto.ECS;

using GamecraftModdingAPI.Blocks;
using GamecraftModdingAPI.Utility;

namespace GamecraftModdingAPI.Inventory
{
public class HotbarEngine : IApiEngine
{
public string Name { get; } = "GamecraftModdingAPIHotbarGameEngine";

public EntitiesDB entitiesDB { set; private get; }

public bool IsInGame = false;

public void Dispose()
{
IsInGame = false;
}

public void Ready()
{
IsInGame = true;
}

public bool SelectBlock(int block, uint playerID, bool cubeSelectedByPick = false)
{
InputEntityStruct[] inputs = entitiesDB.QueryEntities<InputEntityStruct>(InputExclusiveGroups.LocalPlayers).ToFastAccess(out uint count);
if (count == 0) return false;
for (int i = 0; i < count; i++)
{
if (inputs[i].ID.entityID == playerID) {
inputs[i].cubeSelectedByPick = cubeSelectedByPick;
inputs[i].selectedCube = block;
return true;
}
}
// TODO: expose the rest of the input functionality
return false;
}

public uint GetLocalPlayerID()
{
return LocalPlayerIDUtility.GetLocalPlayerID(entitiesDB);
}
}
}

+ 32
- 0
GamecraftModdingAPI/Inventory/HotbarSlotSelectionHandlerEnginePatch.cs View File

@@ -0,0 +1,32 @@
using System;
using System.Reflection;

using RobocraftX.GUI;
using RobocraftX.GUI.Hotbar;
using Svelto.ECS;

using Harmony;
using GamecraftModdingAPI.Blocks;

namespace GamecraftModdingAPI.Inventory
{
[HarmonyPatch]
public class HotbarSlotSelectionHandlerEnginePatch
{
private static int selectedBlockInt = 0;

public static BlockIDs EquippedPartID { get => (BlockIDs)selectedBlockInt; }

private static MethodInfo PatchedMethod { get; } = AccessTools.Method(AccessTools.TypeByName("RobocraftX.GUI.Hotbar.HotbarSlotSelectionHandlerEngine"), "HandleEquippedCubeChanged", parameters: new Type[] { typeof(uint), typeof(int), typeof(ExclusiveGroupStruct) });

public static void Prefix(uint playerID, int selectedDBPartID, ExclusiveGroupStruct groupID)
{
selectedBlockInt = selectedDBPartID;
}

public static MethodBase TargetMethod(HarmonyInstance instance)
{
return PatchedMethod;
}
}
}

+ 2
- 0
GamecraftModdingAPI/Main.cs View File

@@ -65,6 +65,8 @@ namespace GamecraftModdingAPI
Blocks.Signals.Init();
Blocks.Placement.Init();
Blocks.Tweakable.Init();
// init inventory
Inventory.Hotbar.Init();
Logging.MetaLog($"{currentAssembly.GetName().Name} v{currentAssembly.GetName().Version} initialized");
}



Loading…
Cancel
Save