141 lines
No EOL
6 KiB
C#
141 lines
No EOL
6 KiB
C#
using System;
|
|
using System.Reflection;
|
|
|
|
using DataLoader;
|
|
using Gamecraft.Wires;
|
|
using HarmonyLib;
|
|
using RobocraftX.Blocks;
|
|
using RobocraftX.Blocks.Scaling;
|
|
using RobocraftX.Character;
|
|
using RobocraftX.Common;
|
|
using RobocraftX.CR.MachineEditing;
|
|
using Svelto.ECS;
|
|
using Svelto.ECS.EntityStructs;
|
|
using Unity.Mathematics;
|
|
using UnityEngine;
|
|
|
|
using GamecraftModdingAPI.Utility;
|
|
using GamecraftModdingAPI.Engines;
|
|
using GamecraftModdingAPI.Players;
|
|
using RobocraftX.Rendering.GPUI;
|
|
|
|
namespace GamecraftModdingAPI.Blocks
|
|
{
|
|
/// <summary>
|
|
/// Engine which executes block placement actions
|
|
/// </summary>
|
|
public class PlacementEngine : IApiEngine
|
|
{
|
|
public bool IsInGame;
|
|
|
|
public void Dispose()
|
|
{
|
|
IsInGame = false;
|
|
}
|
|
|
|
public void Ready()
|
|
{
|
|
IsInGame = true;
|
|
}
|
|
|
|
public EntitiesDB entitiesDB { get; set; }
|
|
private static BlockEntityFactory _blockEntityFactory; //Injected from PlaceSingleBlockEngine
|
|
|
|
public EGID PlaceBlock(BlockIDs block, BlockColor color, BlockMaterial materialId, float3 position, int uscale,
|
|
float3 scale, Player player, float3 rotation, bool isFlipped, bool autoWire, out EntityInitializer initializer)
|
|
{ //It appears that only the non-uniform scale has any visible effect, but if that's not given here it will be set to the uniform one
|
|
if (color.Darkness > 9)
|
|
throw new Exception("That is too dark. Make sure to use 0-9 as darkness. (0 is default.)");
|
|
initializer = BuildBlock((ushort) block, color.Index, (byte) materialId, position, uscale, scale, rotation,
|
|
isFlipped, autoWire, (player ?? new Player(PlayerType.Local)).Id);
|
|
return initializer.EGID;
|
|
}
|
|
|
|
private EntityInitializer BuildBlock(ushort block, byte color, byte materialId, float3 position, int uscale, float3 scale, float3 rot, bool isFlipped, bool autoWire, uint playerId)
|
|
{
|
|
if (_blockEntityFactory == null)
|
|
throw new BlockException("The factory is null.");
|
|
if (uscale < 1)
|
|
throw new BlockException("Scale needs to be at least 1");
|
|
if (scale.x < 4e-5) scale.x = uscale;
|
|
if (scale.y < 4e-5) scale.y = uscale;
|
|
if (scale.z < 4e-5) scale.z = uscale;
|
|
uint resourceId = (uint) PrefabsID.GenerateResourceID(0, block);
|
|
if (!PrefabsID.PrefabIDByResourceIDMap.ContainsKey(resourceId))
|
|
throw new BlockException("Block with ID " + block + " not found!");
|
|
//RobocraftX.CR.MachineEditing.PlaceSingleBlockEngine
|
|
ScalingEntityStruct scaling = new ScalingEntityStruct {scale = scale * (isFlipped ? -1 : 1)};
|
|
Quaternion rotQ = Quaternion.Euler(rot);
|
|
RotationEntityStruct rotation = new RotationEntityStruct {rotation = rotQ};
|
|
GridRotationStruct gridRotation = new GridRotationStruct
|
|
{position = position, rotation = rotQ};
|
|
DBEntityStruct dbEntity = new DBEntityStruct {DBID = block};
|
|
BlockPlacementScaleEntityStruct placementScale = new BlockPlacementScaleEntityStruct
|
|
{
|
|
blockPlacementHeight = uscale, blockPlacementWidth = uscale, desiredScaleFactor = uscale
|
|
};
|
|
|
|
EntityInitializer structInitializer = _blockEntityFactory.Build(CommonExclusiveGroups.nextBlockEntityID, block); //The ghost block index is only used for triggers
|
|
if (color != byte.MaxValue)
|
|
structInitializer.Init(new ColourParameterEntityStruct
|
|
{
|
|
indexInPalette = color,
|
|
hasNetworkChange = true
|
|
});
|
|
if (materialId != byte.MaxValue)
|
|
structInitializer.Init(new CubeMaterialStruct
|
|
{
|
|
materialId = materialId
|
|
});
|
|
uint prefabId = PrefabsID.GetOrCreatePrefabID(block, materialId, 0, isFlipped);
|
|
structInitializer.Init(new GFXPrefabEntityStructGPUI(prefabId));
|
|
structInitializer.Init(new PhysicsPrefabEntityStruct(prefabId));
|
|
structInitializer.Init(dbEntity);
|
|
structInitializer.Init(new PositionEntityStruct {position = position});
|
|
structInitializer.Init(rotation);
|
|
structInitializer.Init(scaling);
|
|
structInitializer.Init(gridRotation);
|
|
structInitializer.Init(new UniformBlockScaleEntityStruct
|
|
{
|
|
scaleFactor = placementScale.desiredScaleFactor
|
|
});
|
|
structInitializer.Init(new BlockPlacementInfoStruct()
|
|
{
|
|
loadedFromDisk = false,
|
|
placedBy = playerId,
|
|
triggerAutoWiring = autoWire && structInitializer.Has<BlockPortsStruct>()
|
|
});
|
|
|
|
/*structInitializer.Init(new CollisionFilterOverride
|
|
{
|
|
belongsTo = 32U,
|
|
collidesWith = 239532U
|
|
});*/
|
|
|
|
EGID playerEGID = new EGID(playerId, CharacterExclusiveGroups.OnFootGroup);
|
|
ref PickedBlockExtraDataStruct pickedBlock = ref entitiesDB.QueryEntity<PickedBlockExtraDataStruct>(playerEGID);
|
|
pickedBlock.placedBlockEntityID = structInitializer.EGID;
|
|
pickedBlock.placedBlockWasAPickedBlock = false;
|
|
return structInitializer;
|
|
}
|
|
|
|
public string Name { get; } = "GamecraftModdingAPIPlacementGameEngine";
|
|
|
|
public bool isRemovable => false;
|
|
|
|
[HarmonyPatch]
|
|
public class FactoryObtainerPatch
|
|
{
|
|
static void Postfix(BlockEntityFactory blockEntityFactory)
|
|
{
|
|
_blockEntityFactory = blockEntityFactory;
|
|
Logging.MetaDebugLog("Block entity factory injected.");
|
|
}
|
|
|
|
static MethodBase TargetMethod(Harmony instance)
|
|
{
|
|
return AccessTools.TypeByName("RobocraftX.CR.MachineEditing.PlaceSingleBlockEngine").GetConstructors()[0];
|
|
}
|
|
}
|
|
}
|
|
} |