Fix move/rotate during init, add blueprint properties

This commit is contained in:
Norbi Peti 2020-11-14 18:29:48 +01:00
parent f30dcd251f
commit 1f911b1d32
5 changed files with 54 additions and 21 deletions

View file

@ -115,7 +115,7 @@ namespace GamecraftModdingAPI
/// <summary> /// <summary>
/// Creates a new block group consisting of a single block. /// Creates a new block group consisting of a single block.
/// You can add more blocks using the Add() method or by setting the BlockGroup property of the blocks.<br /> /// You can add more blocks using the Add() method or by setting the BlockGroup property of the blocks.<br />
/// Note that only newly placed blocks should be added to groups. /// Note that only newly placed blocks can be added to groups.
/// </summary> /// </summary>
/// <param name="block">The block to add</param> /// <param name="block">The block to add</param>
/// <returns>A new block group containing the given block</returns> /// <returns>A new block group containing the given block</returns>
@ -155,7 +155,7 @@ namespace GamecraftModdingAPI
IEnumerator IEnumerable.GetEnumerator() => blocks.GetEnumerator(); IEnumerator IEnumerable.GetEnumerator() => blocks.GetEnumerator();
/// <summary> /// <summary>
/// Adds a block to the group. You should only add newly placed blocks /// Adds a block to the group. You can only add newly placed blocks
/// so that the game initializes the group membership properly. /// so that the game initializes the group membership properly.
/// </summary> /// </summary>
/// <param name="item"></param> /// <param name="item"></param>
@ -170,7 +170,7 @@ namespace GamecraftModdingAPI
/// <summary> /// <summary>
/// Removes all blocks from this group. /// Removes all blocks from this group.
/// You should not remove blocks that have been initialized, only those that you placed recently. /// You cannot remove blocks that have been initialized, only those that you placed recently.
/// </summary> /// </summary>
public void Clear() public void Clear()
{ {
@ -183,7 +183,7 @@ namespace GamecraftModdingAPI
/// <summary> /// <summary>
/// Removes a block from this group. /// Removes a block from this group.
/// You should not remove blocks that have been initialized, only those that you placed recently. /// You cannot remove blocks that have been initialized, only those that you placed recently.
/// </summary> /// </summary>
/// <param name="item"></param> /// <param name="item"></param>
/// <returns></returns> /// <returns></returns>

View file

@ -209,6 +209,18 @@ namespace GamecraftModdingAPI.Blocks
return blocks; return blocks;
} }
public void GetBlueprintInfo(uint blueprintID, out float3 pos, out quaternion rot, out uint selectionSize)
{
var serializationData = clipboardManager.GetSerializationData(blueprintID);
var blueprintData = serializationData.blueprintData;
blueprintData.dataPos = 0U;
BoxSelectSerializationUtilities.ReadClipboardHeader(blueprintData, out selectionSize, out var posst,
out var rotst, out _);
blueprintData.dataPos = 0U; //Just to be sure, it gets reset when it's read anyway
pos = posst.position;
rot = rotst.rotation;
}
public void InitBlueprint(uint blueprintID) public void InitBlueprint(uint blueprintID)
{ {
clipboardManager.IncrementRefCount(blueprintID); clipboardManager.IncrementRefCount(blueprintID);
@ -280,7 +292,7 @@ namespace GamecraftModdingAPI.Blocks
} }
public static MethodBase TargetMethod() public static MethodBase TargetMethod()
{ {RobocraftX.CR.MachineEditing.BuildGhostChildForMultiblockPickEngine
return AccessTools.GetDeclaredConstructors(AccessTools.TypeByName("RobocraftX.CR.MachineEditing.BuildGhostChildForMultiblockPickEngine"))[0]; return AccessTools.GetDeclaredConstructors(AccessTools.TypeByName("RobocraftX.CR.MachineEditing.BuildGhostChildForMultiblockPickEngine"))[0];
} }
} }

View file

@ -41,9 +41,9 @@ namespace GamecraftModdingAPI.Blocks
{ {
if (data.Group == null) return float3.zero; if (data.Group == null) return float3.zero;
var init = new EntityComponentInitializer(blockID, data.Group); var init = new EntityComponentInitializer(blockID, data.Group);
init.Init(new PositionEntityStruct {position = vector}); init.GetOrCreate<PositionEntityStruct>().position = vector;
init.Init(new GridRotationStruct {position = vector}); init.GetOrCreate<GridRotationStruct>().position = vector;
init.Init(new LocalTransformEntityStruct {position = vector}); init.GetOrCreate<LocalTransformEntityStruct>().position = vector;
return vector; return vector;
} }
ref PositionEntityStruct posStruct = ref this.entitiesDB.QueryEntity<PositionEntityStruct>(blockID); ref PositionEntityStruct posStruct = ref this.entitiesDB.QueryEntity<PositionEntityStruct>(blockID);

View file

@ -41,9 +41,9 @@ namespace GamecraftModdingAPI.Blocks
{ {
if (data.Group == null) return float3.zero; if (data.Group == null) return float3.zero;
var init = new EntityComponentInitializer(blockID, data.Group); var init = new EntityComponentInitializer(blockID, data.Group);
init.Init(new RotationEntityStruct {rotation = new Quaternion {eulerAngles = vector}}); init.GetOrCreate<RotationEntityStruct>().rotation = Quaternion.Euler(vector);
init.Init(new GridRotationStruct {rotation = new Quaternion {eulerAngles = vector}}); init.GetOrCreate<GridRotationStruct>().rotation = Quaternion.Euler(vector);
init.Init(new LocalTransformEntityStruct {rotation = new Quaternion {eulerAngles = vector}}); init.GetOrCreate<LocalTransformEntityStruct>().rotation = Quaternion.Euler(vector);
return vector; return vector;
} }
ref RotationEntityStruct rotStruct = ref this.entitiesDB.QueryEntity<RotationEntityStruct>(blockID); ref RotationEntityStruct rotStruct = ref this.entitiesDB.QueryEntity<RotationEntityStruct>(blockID);

View file

@ -15,15 +15,23 @@ namespace GamecraftModdingAPI
{ {
Id = id; Id = id;
BlockGroup._engine.InitBlueprint(id); BlockGroup._engine.InitBlueprint(id);
Refresh();
} }
/*public static void SelectBlueprint(Blueprint blueprint) /// <summary>
{ /// The center of the blueprint. Can only be set using the StoreBlocks() method.
BlueprintUtil.SelectBlueprint(null, new BlueprintInventoryItemEntityStruct /// </summary>
{ public float3 Position { get; private set; }
blueprintResourceId = blueprint.Id
}); /// <summary>
}*/ /// The rotation of the blueprint. Can only be set using the StoreBlocks() method.
/// </summary>
public float3 Rotation { get; private set; }
/// <summary>
/// The amount of blocks in the blueprint. Gan only be set using the StoreBlocks() method.
/// </summary>
public uint BlockCount { get; private set; }
/// <summary> /// <summary>
/// Creates a new, empty blueprint. It will be deleted on disposal unless the game holds a reference to it. /// Creates a new, empty blueprint. It will be deleted on disposal unless the game holds a reference to it.
@ -39,12 +47,13 @@ namespace GamecraftModdingAPI
/// Use the BlockGroup overload for automatically calculated position and rotation. /// Use the BlockGroup overload for automatically calculated position and rotation.
/// </summary> /// </summary>
/// <param name="blocks">The array of blocks to use</param> /// <param name="blocks">The array of blocks to use</param>
/// <param name="position">The anchor position of the blueprint</param> /// <param name="position">The anchor (center) position of the blueprint</param>
/// <param name="rotation">The base rotation of the blueprint</param> /// <param name="rotation">The base rotation of the blueprint</param>
public void StoreBlocks(Block[] blocks, float3 position, float3 rotation) public void StoreBlocks(Block[] blocks, float3 position, float3 rotation)
{ {
BlockGroup._engine.ReplaceBlueprint(Player.LocalPlayer.Id, Id, blocks, position, BlockGroup._engine.ReplaceBlueprint(Player.LocalPlayer.Id, Id, blocks, position,
quaternion.Euler(rotation)); quaternion.Euler(rotation));
Refresh();
} }
/// <summary> /// <summary>
@ -55,6 +64,7 @@ namespace GamecraftModdingAPI
{ {
BlockGroup._engine.ReplaceBlueprint(Player.LocalPlayer.Id, Id, group, group.Position, BlockGroup._engine.ReplaceBlueprint(Player.LocalPlayer.Id, Id, group, group.Position,
Quaternion.Euler(group.Rotation)); Quaternion.Euler(group.Rotation));
Refresh();
} }
/// <summary> /// <summary>
@ -68,6 +78,17 @@ namespace GamecraftModdingAPI
return BlockGroup._engine.PlaceBlueprintBlocks(Id, Player.LocalPlayer.Id, position, rotation); return BlockGroup._engine.PlaceBlueprintBlocks(Id, Player.LocalPlayer.Id, position, rotation);
} }
/// <summary>
/// Updates the properties based on the blueprint data. Only necessary if the blueprint is changed from the game.
/// </summary>
public void Refresh()
{
BlockGroup._engine.GetBlueprintInfo(Id, out var pos, out var rot, out uint count);
Position = pos;
Rotation = ((Quaternion) rot).eulerAngles;
BlockCount = count;
}
public void Dispose() public void Dispose()
{ {
BlockGroup._engine.DisposeBlueprint(Id); BlockGroup._engine.DisposeBlueprint(Id);
@ -75,7 +96,7 @@ namespace GamecraftModdingAPI
public override string ToString() public override string ToString()
{ {
return $"{nameof(Id)}: {Id}"; return $"{nameof(Id)}: {Id}, {nameof(Position)}: {Position}, {nameof(Rotation)}: {Rotation}, {nameof(BlockCount)}: {BlockCount}";
} }
} }
} }