Browse Source

Add API to remove blocks

tags/v0.2.1^2
NorbiPeti 4 years ago
parent
commit
81f2f613f7
Signed by: NorbiPeti <szatmari.norbert.peter@gmail.com> GPG Key ID: DBA4C4549A927E56
5 changed files with 130 additions and 8 deletions
  1. +6
    -7
      GamecraftModdingAPI/Blocks/PlacementEngine.cs
  2. +28
    -0
      GamecraftModdingAPI/Blocks/Removal.cs
  3. +94
    -0
      GamecraftModdingAPI/Blocks/RemovalEngine.cs
  4. +1
    -1
      GamecraftModdingAPI/Blocks/RotationEngine.cs
  5. +1
    -0
      GamecraftModdingAPI/Main.cs

+ 6
- 7
GamecraftModdingAPI/Blocks/PlacementEngine.cs View File

@@ -113,12 +113,11 @@ namespace GamecraftModdingAPI.Blocks
{
scaleFactor = placementScale.desiredScaleFactor
});
structInitializer.Init(
new BlockPlacementInfoStruct()
{
loadedFromDisk = false,
placedBy = playerId
});
structInitializer.Init(new BlockPlacementInfoStruct()
{
loadedFromDisk = false,
placedBy = playerId
});
PrimaryRotationUtility.InitialisePrimaryDirection(rotation.rotation, ref structInitializer);
EGID playerEGID = new EGID(playerId, CharacterExclusiveGroups.OnFootGroup);
ref PickedBlockExtraDataStruct pickedBlock = ref entitiesDB.QueryEntity<PickedBlockExtraDataStruct>(playerEGID);
@@ -139,7 +138,7 @@ namespace GamecraftModdingAPI.Blocks

static MethodBase TargetMethod(HarmonyInstance instance)
{
return AccessTools.TypeByName("RobocraftX.CR.MachineEditing.PlaceBlockEngine").GetConstructors()[0];
return AccessTools.TypeByName("RobocraftX.CR.MachineEditing.PlaceBlockEngine").GetConstructors()[0];
}
}
}

+ 28
- 0
GamecraftModdingAPI/Blocks/Removal.cs View File

@@ -0,0 +1,28 @@
using Svelto.ECS;

using GamecraftModdingAPI.Utility;

namespace GamecraftModdingAPI.Blocks
{
public class Removal
{
private static RemovalEngine _removalEngine = new RemovalEngine();

/// <summary>
/// Removes the block with the given ID. Returns false if the block doesn't exist or the game isn't in build mode.
/// </summary>
/// <param name="targetBlock">The block to remove</param>
/// <returns>Whether the block was successfully removed</returns>
public static bool RemoveBlock(EGID targetBlock)
{
if (GameState.IsBuildMode())
return _removalEngine.RemoveBlock(targetBlock);
return false;
}

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

+ 94
- 0
GamecraftModdingAPI/Blocks/RemovalEngine.cs View File

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

using Harmony;
using RobocraftX.Blocks;
using RobocraftX.Blocks.Ghost;
using RobocraftX.Character.Camera;
using RobocraftX.Character.Factories;
using RobocraftX.Common;
using RobocraftX.Players;
using Svelto.ECS;
using uREPL;

using GamecraftModdingAPI.Commands;
using GamecraftModdingAPI.Utility;

namespace GamecraftModdingAPI.Blocks
{
public class RemovalEngine : IApiEngine
{
private static IEntityFunctions _entityFunctions;
private static MachineGraphConnectionEntityFactory _connectionFactory;

public bool RemoveBlock(EGID target)
{
if (!entitiesDB.Exists<MachineGraphConnectionsEntityStruct>(target))
return false;
var connections = entitiesDB.QueryEntity<MachineGraphConnectionsEntityStruct>(target);
for (int i = connections.connections.Length - 1; i >= 0; i--)
_connectionFactory.RemoveConnection(connections, i, entitiesDB);
_entityFunctions.RemoveEntity<BlockEntityDescriptor>(target);
return true;
}

/// <summary>
/// Returns the block the player is currently looking at.
/// </summary>
/// <param name="playerId">The player's ID</param>
/// <param name="maxDistance">The maximum distance from the player (default is the player's building reach)</param>
/// <returns>The block's EGID or null if not found</returns>
public EGID? GetBlockUnderCursor(uint playerId, float maxDistance = -1f)
{
if (!entitiesDB.TryQueryMappedEntities<CharacterCameraRayCastEntityStruct>(
CameraExclusiveGroups.CameraGroup, out var mapper))
return null;
mapper.TryGetEntity(playerId, out CharacterCameraRayCastEntityStruct rayCast);
float distance = maxDistance < 0
? GhostBlockUtils.GetBuildInteractionDistance(entitiesDB, rayCast)
: maxDistance;
if (rayCast.hit && rayCast.distance <= distance)
return rayCast.hitEgid;
return null;
}

public void Ready()
{
CommandManager.AddCommand(new SimpleCustomCommandEngine(() =>
{
var block = GetBlockUnderCursor(LocalPlayerIDUtility.GetLocalPlayerID(entitiesDB));
if (block.HasValue)
{
RemoveBlock(block.Value);
Log.Output("Removed block.");
}
else
Log.Output("No block found where you're looking at.");
}, "removeCube", "Removes the cube you're looking at."));
}

public EntitiesDB entitiesDB { get; set; }

public void Dispose()
{
}

public string Name { get; } = "GamecraftModdingAPIRemovalGameEngine";

[HarmonyPatch]
public class FactoryObtainerPatch
{
static void Postfix(IEntityFunctions entityFunctions,
MachineGraphConnectionEntityFactory machineGraphConnectionEntityFactory)
{
_entityFunctions = entityFunctions;
_connectionFactory = machineGraphConnectionEntityFactory;
Logging.MetaDebugLog("Requirements injected.");
}

static MethodBase TargetMethod(HarmonyInstance instance)
{
return AccessTools.TypeByName("RobocraftX.CR.MachineEditing.RemoveBlockEngine").GetConstructors()[0];
}
}
}
}

+ 1
- 1
GamecraftModdingAPI/Blocks/RotationEngine.cs View File

@@ -44,7 +44,7 @@ namespace GamecraftModdingAPI.Blocks
IsInGame = true;
}

// implementations for Movement static class
// implementations for Rotation static class

public float3 RotateBlock(uint blockID, Vector3 vector)
{


+ 1
- 0
GamecraftModdingAPI/Main.cs View File

@@ -65,6 +65,7 @@ namespace GamecraftModdingAPI
Blocks.Signals.Init();
Blocks.Placement.Init();
Blocks.Tweakable.Init();
Blocks.Removal.Init();
// init inventory
Inventory.Hotbar.Init();
// init input


Loading…
Cancel
Save