2020-11-10 18:28:36 +00:00
|
|
|
|
using System;
|
|
|
|
|
using Unity.Mathematics;
|
2020-11-12 01:39:58 +00:00
|
|
|
|
using UnityEngine;
|
2020-11-10 15:37:20 +00:00
|
|
|
|
|
|
|
|
|
namespace GamecraftModdingAPI
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Represents a blueprint in the inventory. When placed it becomes a block group.
|
|
|
|
|
/// </summary>
|
2020-11-10 18:28:36 +00:00
|
|
|
|
public class Blueprint : IDisposable
|
2020-11-10 15:37:20 +00:00
|
|
|
|
{
|
|
|
|
|
public uint Id { get; }
|
2020-11-10 18:28:36 +00:00
|
|
|
|
|
|
|
|
|
internal Blueprint(uint id)
|
|
|
|
|
{
|
|
|
|
|
Id = id;
|
|
|
|
|
BlockGroup._engine.InitBlueprint(id);
|
2020-11-14 17:29:48 +00:00
|
|
|
|
Refresh();
|
2020-11-10 18:28:36 +00:00
|
|
|
|
}
|
2020-11-14 17:29:48 +00:00
|
|
|
|
|
|
|
|
|
/// <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; }
|
2020-11-10 18:28:36 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates a new, empty blueprint. It will be deleted on disposal unless the game holds a reference to it.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>A blueprint that doesn't have any blocks</returns>
|
|
|
|
|
public static Blueprint Create()
|
|
|
|
|
{
|
|
|
|
|
return new Blueprint(BlockGroup._engine.CreateBlueprint());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Set the blocks that the blueprint contains.
|
2020-11-12 01:39:58 +00:00
|
|
|
|
/// Use the BlockGroup overload for automatically calculated position and rotation.
|
2020-11-10 18:28:36 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="blocks">The array of blocks to use</param>
|
2020-11-14 17:29:48 +00:00
|
|
|
|
/// <param name="position">The anchor (center) position of the blueprint</param>
|
2020-11-12 01:39:58 +00:00
|
|
|
|
/// <param name="rotation">The base rotation of the blueprint</param>
|
|
|
|
|
public void StoreBlocks(Block[] blocks, float3 position, float3 rotation)
|
2020-11-10 18:28:36 +00:00
|
|
|
|
{
|
|
|
|
|
BlockGroup._engine.ReplaceBlueprint(Player.LocalPlayer.Id, Id, blocks, position,
|
|
|
|
|
quaternion.Euler(rotation));
|
2020-11-14 17:29:48 +00:00
|
|
|
|
Refresh();
|
2020-11-10 18:28:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-12 01:39:58 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Store the blocks from the given group in the blueprint with correct position and rotation for the blueprint.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="group">The block group to store</param>
|
|
|
|
|
public void StoreBlocks(BlockGroup group)
|
|
|
|
|
{
|
|
|
|
|
BlockGroup._engine.ReplaceBlueprint(Player.LocalPlayer.Id, Id, group, group.Position,
|
|
|
|
|
Quaternion.Euler(group.Rotation));
|
2020-11-14 17:29:48 +00:00
|
|
|
|
Refresh();
|
2020-11-12 01:39:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-10 18:28:36 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Places the blocks the blueprint contains at the specified position and rotation.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="position">The position of the blueprint</param>
|
|
|
|
|
/// <param name="rotation">The rotation of the blueprint</param>
|
|
|
|
|
/// <returns>An array of the placed blocks</returns>
|
|
|
|
|
public Block[] PlaceBlocks(float3 position, float3 rotation)
|
|
|
|
|
{
|
|
|
|
|
return BlockGroup._engine.PlaceBlueprintBlocks(Id, Player.LocalPlayer.Id, position, rotation);
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-14 17:29:48 +00:00
|
|
|
|
/// <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;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-10 18:28:36 +00:00
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
BlockGroup._engine.DisposeBlueprint(Id);
|
|
|
|
|
}
|
2020-11-14 01:52:16 +00:00
|
|
|
|
|
|
|
|
|
public override string ToString()
|
|
|
|
|
{
|
2020-11-14 17:29:48 +00:00
|
|
|
|
return $"{nameof(Id)}: {Id}, {nameof(Position)}: {Position}, {nameof(Rotation)}: {Rotation}, {nameof(BlockCount)}: {BlockCount}";
|
2020-11-14 01:52:16 +00:00
|
|
|
|
}
|
2020-11-10 15:37:20 +00:00
|
|
|
|
}
|
|
|
|
|
}
|