Browse Source

Fix move/rotate during init, add blueprint properties

tags/v1.7.0
NorbiPeti 3 years ago
parent
commit
1f911b1d32
5 changed files with 54 additions and 21 deletions
  1. +4
    -4
      GamecraftModdingAPI/BlockGroup.cs
  2. +13
    -1
      GamecraftModdingAPI/Blocks/BlueprintEngine.cs
  3. +3
    -3
      GamecraftModdingAPI/Blocks/MovementEngine.cs
  4. +3
    -3
      GamecraftModdingAPI/Blocks/RotationEngine.cs
  5. +31
    -10
      GamecraftModdingAPI/Blueprint.cs

+ 4
- 4
GamecraftModdingAPI/BlockGroup.cs View File

@@ -115,7 +115,7 @@ namespace GamecraftModdingAPI
/// <summary>
/// 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 />
/// Note that only newly placed blocks should be added to groups.
/// Note that only newly placed blocks can be added to groups.
/// </summary>
/// <param name="block">The block to add</param>
/// <returns>A new block group containing the given block</returns>
@@ -155,7 +155,7 @@ namespace GamecraftModdingAPI
IEnumerator IEnumerable.GetEnumerator() => blocks.GetEnumerator();

/// <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.
/// </summary>
/// <param name="item"></param>
@@ -170,7 +170,7 @@ namespace GamecraftModdingAPI

/// <summary>
/// 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>
public void Clear()
{
@@ -183,7 +183,7 @@ namespace GamecraftModdingAPI

/// <summary>
/// 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>
/// <param name="item"></param>
/// <returns></returns>


+ 13
- 1
GamecraftModdingAPI/Blocks/BlueprintEngine.cs View File

@@ -209,6 +209,18 @@ namespace GamecraftModdingAPI.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)
{
clipboardManager.IncrementRefCount(blueprintID);
@@ -280,7 +292,7 @@ namespace GamecraftModdingAPI.Blocks
}

public static MethodBase TargetMethod()
{
{RobocraftX.CR.MachineEditing.BuildGhostChildForMultiblockPickEngine
return AccessTools.GetDeclaredConstructors(AccessTools.TypeByName("RobocraftX.CR.MachineEditing.BuildGhostChildForMultiblockPickEngine"))[0];
}
}


+ 3
- 3
GamecraftModdingAPI/Blocks/MovementEngine.cs View File

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


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

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


+ 31
- 10
GamecraftModdingAPI/Blueprint.cs View File

@@ -15,15 +15,23 @@ namespace GamecraftModdingAPI
{
Id = id;
BlockGroup._engine.InitBlueprint(id);
Refresh();
}
/*public static void SelectBlueprint(Blueprint blueprint)
{
BlueprintUtil.SelectBlueprint(null, new BlueprintInventoryItemEntityStruct
{
blueprintResourceId = blueprint.Id
});
}*/

/// <summary>
/// The center of the blueprint. Can only be set using the StoreBlocks() method.
/// </summary>
public float3 Position { get; private set; }

/// <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>
/// 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.
/// </summary>
/// <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>
public void StoreBlocks(Block[] blocks, float3 position, float3 rotation)
{
BlockGroup._engine.ReplaceBlueprint(Player.LocalPlayer.Id, Id, blocks, position,
quaternion.Euler(rotation));
Refresh();
}

/// <summary>
@@ -55,6 +64,7 @@ namespace GamecraftModdingAPI
{
BlockGroup._engine.ReplaceBlueprint(Player.LocalPlayer.Id, Id, group, group.Position,
Quaternion.Euler(group.Rotation));
Refresh();
}

/// <summary>
@@ -68,6 +78,17 @@ namespace GamecraftModdingAPI
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()
{
BlockGroup._engine.DisposeBlueprint(Id);
@@ -75,7 +96,7 @@ namespace GamecraftModdingAPI

public override string ToString()
{
return $"{nameof(Id)}: {Id}";
return $"{nameof(Id)}: {Id}, {nameof(Position)}: {Position}, {nameof(Rotation)}: {Rotation}, {nameof(BlockCount)}: {BlockCount}";
}
}
}

Loading…
Cancel
Save