2020-05-13 12:02:36 +00:00
|
|
|
using System;
|
2020-05-18 03:19:16 +00:00
|
|
|
using System.Reflection;
|
2020-05-22 01:00:33 +00:00
|
|
|
using System.Threading.Tasks;
|
2020-05-13 14:52:21 +00:00
|
|
|
|
2020-05-13 12:02:36 +00:00
|
|
|
using Svelto.ECS;
|
2020-05-17 21:23:06 +00:00
|
|
|
using Svelto.ECS.EntityStructs;
|
2020-05-13 14:52:21 +00:00
|
|
|
using RobocraftX.Common;
|
2020-05-19 22:08:02 +00:00
|
|
|
using RobocraftX.Blocks;
|
2020-05-13 12:02:36 +00:00
|
|
|
using Unity.Mathematics;
|
2020-05-24 17:29:02 +00:00
|
|
|
using Gamecraft.Blocks.GUI;
|
2020-05-13 12:02:36 +00:00
|
|
|
|
2020-05-13 14:52:21 +00:00
|
|
|
using GamecraftModdingAPI.Blocks;
|
|
|
|
using GamecraftModdingAPI.Utility;
|
|
|
|
|
2020-05-13 12:02:36 +00:00
|
|
|
namespace GamecraftModdingAPI
|
|
|
|
{
|
2020-05-18 03:19:16 +00:00
|
|
|
/// <summary>
|
|
|
|
/// A single (perhaps scaled) block. Properties may return default values if the block is removed and then setting them is ignored.
|
2020-05-21 00:42:02 +00:00
|
|
|
/// For specific block type operations, use the specialised block classes in the GamecraftModdingAPI.Blocks namespace.
|
2020-05-18 03:19:16 +00:00
|
|
|
/// </summary>
|
|
|
|
public class Block
|
|
|
|
{
|
|
|
|
protected static readonly PlacementEngine PlacementEngine = new PlacementEngine();
|
|
|
|
protected static readonly MovementEngine MovementEngine = new MovementEngine();
|
|
|
|
protected static readonly RotationEngine RotationEngine = new RotationEngine();
|
|
|
|
protected static readonly RemovalEngine RemovalEngine = new RemovalEngine();
|
2020-05-21 19:04:55 +00:00
|
|
|
protected static readonly SignalEngine SignalEngine = new SignalEngine();
|
2020-05-22 22:06:49 +00:00
|
|
|
|
|
|
|
protected internal static readonly BlockEngine BlockEngine = new BlockEngine();
|
2020-05-18 03:19:16 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Place a new block at the given position. If scaled, position means the center of the block. The default block size is 0.2 in terms of position.
|
|
|
|
/// Place blocks next to each other to connect them.
|
|
|
|
/// The placed block will be a complete block with a placement grid and collision which will be saved along with the game.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="block">The block's type</param>
|
|
|
|
/// <param name="color">The block's color</param>
|
|
|
|
/// <param name="darkness">The block color's darkness (0-9) - 0 is default color</param>
|
|
|
|
/// <param name="position">The block's position in the grid - default block size is 0.2</param>
|
|
|
|
/// <param name="rotation">The block's rotation in degrees</param>
|
|
|
|
/// <param name="uscale">The block's uniform scale - default scale is 1 (with 0.2 width)</param>
|
|
|
|
/// <param name="scale">The block's non-uniform scale - 0 means <paramref name="uscale"/> is used</param>
|
|
|
|
/// <param name="player">The player who placed the block</param>
|
|
|
|
/// <returns>The placed block or null if failed</returns>
|
|
|
|
public static Block PlaceNew(BlockIDs block, float3 position,
|
|
|
|
float3 rotation = default, BlockColors color = BlockColors.Default, byte darkness = 0,
|
|
|
|
int uscale = 1, float3 scale = default, Player player = null)
|
|
|
|
{
|
|
|
|
if (PlacementEngine.IsInGame && GameState.IsBuildMode())
|
|
|
|
{
|
2020-05-22 22:06:49 +00:00
|
|
|
return new Block(PlacementEngine.PlaceBlock(block, color, darkness,
|
|
|
|
position, uscale, scale, player, rotation));
|
2020-05-18 03:19:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-05-22 01:00:33 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Place a new block at the given position. If scaled, position means the center of the block. The default block size is 0.2 in terms of position.
|
|
|
|
/// Place blocks next to each other to connect them.
|
|
|
|
/// The placed block will be a complete block with a placement grid and collision which will be saved along with the game.
|
|
|
|
/// <para></para>
|
|
|
|
/// <para>This method waits for the block to be constructed in the game.</para>
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="block">The block's type</param>
|
|
|
|
/// <param name="color">The block's color</param>
|
|
|
|
/// <param name="darkness">The block color's darkness (0-9) - 0 is default color</param>
|
|
|
|
/// <param name="position">The block's position in the grid - default block size is 0.2</param>
|
|
|
|
/// <param name="rotation">The block's rotation in degrees</param>
|
|
|
|
/// <param name="uscale">The block's uniform scale - default scale is 1 (with 0.2 width)</param>
|
|
|
|
/// <param name="scale">The block's non-uniform scale - 0 means <paramref name="uscale"/> is used</param>
|
|
|
|
/// <param name="player">The player who placed the block</param>
|
|
|
|
/// <returns>The placed block or null if failed</returns>
|
|
|
|
public static async Task<Block> PlaceNewAsync(BlockIDs block, float3 position,
|
|
|
|
float3 rotation = default, BlockColors color = BlockColors.Default, byte darkness = 0,
|
|
|
|
int uscale = 1, float3 scale = default, Player player = null)
|
|
|
|
{
|
|
|
|
if (PlacementEngine.IsInGame && GameState.IsBuildMode())
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
var ret = new Block(PlacementEngine.PlaceBlock(block, color, darkness,
|
|
|
|
position, uscale, scale, player, rotation));
|
|
|
|
await AsyncUtils.WaitForSubmission();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
Logging.MetaDebugLog(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-05-18 03:19:16 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Returns the most recently placed block.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>The block object</returns>
|
|
|
|
public static Block GetLastPlacedBlock()
|
|
|
|
{
|
|
|
|
return new Block(BlockIdentifiers.LatestBlockID);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Block(EGID id)
|
|
|
|
{
|
|
|
|
Id = id;
|
2020-05-22 00:00:31 +00:00
|
|
|
if (!BlockEngine.BlockExists(Id))
|
|
|
|
{
|
|
|
|
Sync();
|
|
|
|
if (!BlockEngine.BlockExists(Id))
|
|
|
|
{
|
|
|
|
throw new BlockDoesNotExistException($"Block {Id.entityID} must be placed using PlaceNew(...) since it does not exist yet");
|
|
|
|
}
|
|
|
|
}
|
2020-05-18 03:19:16 +00:00
|
|
|
}
|
|
|
|
|
2020-05-22 22:06:49 +00:00
|
|
|
public Block(uint id) : this(new EGID(id, CommonExclusiveGroups.OWNED_BLOCKS_GROUP))
|
2020-05-18 03:19:16 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-05-22 00:00:31 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Synchronize newly created entity components with entities DB.
|
|
|
|
/// This forces a partial game tick, so it may be slow.
|
|
|
|
/// This also has the potential to make Gamecraft unstable.
|
|
|
|
/// Use this sparingly.
|
|
|
|
/// </summary>
|
2020-05-18 03:19:16 +00:00
|
|
|
protected static void Sync()
|
|
|
|
{
|
|
|
|
DeterministicStepCompositionRootPatch.SubmitEntitiesNow();
|
|
|
|
}
|
|
|
|
|
2020-05-21 00:42:02 +00:00
|
|
|
public EGID Id { get; protected set; }
|
2020-05-18 03:19:16 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The block's current position or zero if the block no longer exists.
|
|
|
|
/// A block is 0.2 wide by default in terms of position.
|
|
|
|
/// </summary>
|
|
|
|
public float3 Position
|
|
|
|
{
|
|
|
|
get => Exists ? MovementEngine.GetPosition(Id.entityID) : float3.zero;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
if (Exists) MovementEngine.MoveBlock(Id.entityID, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The block's current rotation in degrees or zero if the block doesn't exist.
|
|
|
|
/// </summary>
|
|
|
|
public float3 Rotation
|
|
|
|
{
|
|
|
|
get => Exists ? RotationEngine.GetRotation(Id.entityID) : float3.zero;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
if (Exists) RotationEngine.RotateBlock(Id.entityID, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The block's non-uniform scale or zero if the block's invalid. Independent of the uniform scaling.
|
|
|
|
/// </summary>
|
|
|
|
public float3 Scale
|
|
|
|
{
|
2020-05-19 22:08:02 +00:00
|
|
|
get => BlockEngine.GetBlockInfo<ScalingEntityStruct>(Id).scale;
|
2020-05-18 03:19:16 +00:00
|
|
|
set
|
|
|
|
{
|
2020-05-19 22:08:02 +00:00
|
|
|
BlockEngine.GetBlockInfo<ScalingEntityStruct>(Id).scale = value;
|
2020-05-18 03:19:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The block's uniform scale or zero if the block's invalid. Also sets the non-uniform scale.
|
|
|
|
/// </summary>
|
|
|
|
public int UniformScale
|
|
|
|
{
|
2020-05-19 22:08:02 +00:00
|
|
|
get => BlockEngine.GetBlockInfo<UniformBlockScaleEntityStruct>(Id).scaleFactor;
|
2020-05-18 03:19:16 +00:00
|
|
|
set
|
|
|
|
{
|
2020-05-19 22:08:02 +00:00
|
|
|
ref var scaleStruct = ref BlockEngine.GetBlockInfo<UniformBlockScaleEntityStruct>(Id);
|
|
|
|
scaleStruct.scaleFactor = value;
|
2020-05-18 03:19:16 +00:00
|
|
|
Scale = new float3(value, value, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The block's type (ID). Returns BlockIDs.Invalid if the block doesn't exist anymore.
|
|
|
|
/// </summary>
|
2020-05-19 22:08:02 +00:00
|
|
|
public BlockIDs Type
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
var id = (BlockIDs) BlockEngine.GetBlockInfo<DBEntityStruct>(Id, out var exists).DBID;
|
|
|
|
return exists ? id : BlockIDs.Invalid;
|
|
|
|
}
|
|
|
|
}
|
2020-05-18 03:19:16 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The block's color. Returns BlockColors.Default if the block no longer exists.
|
|
|
|
/// </summary>
|
|
|
|
public BlockColor Color
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
2020-05-19 22:08:02 +00:00
|
|
|
byte index = BlockEngine.GetBlockInfo<ColourParameterEntityStruct>(Id, out var exists).indexInPalette;
|
|
|
|
if (!exists) index = byte.MaxValue;
|
2020-05-18 03:19:16 +00:00
|
|
|
if (index == byte.MaxValue) return new BlockColor { Color = BlockColors.Default };
|
|
|
|
return new BlockColor { Color = (BlockColors)(index % 10), Darkness = (byte)(index / 10) };
|
|
|
|
}
|
|
|
|
set
|
|
|
|
{
|
2020-05-19 22:08:02 +00:00
|
|
|
ref var color = ref BlockEngine.GetBlockInfo<ColourParameterEntityStruct>(Id);
|
2020-05-18 03:19:16 +00:00
|
|
|
color.indexInPalette = (byte)(value.Color + value.Darkness * 10);
|
2020-05-19 22:08:02 +00:00
|
|
|
color.overridePaletteColour = false;
|
|
|
|
color.needsUpdate = true;
|
|
|
|
BlockEngine.SetBlockColorFromPalette(ref color);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The block's exact color. Gets reset to the palette color (Color property) after reentering the game.
|
|
|
|
/// </summary>
|
|
|
|
public float4 CustomColor
|
|
|
|
{
|
|
|
|
get => BlockEngine.GetBlockInfo<ColourParameterEntityStruct>(Id).overriddenColour;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
ref var color = ref BlockEngine.GetBlockInfo<ColourParameterEntityStruct>(Id);
|
|
|
|
color.overriddenColour = value;
|
|
|
|
color.overridePaletteColour = true;
|
2020-05-18 03:19:16 +00:00
|
|
|
color.needsUpdate = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-24 17:29:02 +00:00
|
|
|
/// <summary>
|
|
|
|
/// The short text displayed on the block if applicable, or null.
|
|
|
|
/// Setting it is temporary to the session, it won't be saved.
|
|
|
|
/// </summary>
|
|
|
|
public string Label
|
|
|
|
{
|
|
|
|
get => BlockEngine.GetBlockInfo<TextLabelEntityViewStruct>(Id).textLabelComponent?.text;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
ref var text = ref BlockEngine.GetBlockInfo<TextLabelEntityViewStruct>(Id);
|
|
|
|
if (text.textLabelComponent != null) text.textLabelComponent.text = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-18 03:19:16 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Whether the block exists. The other properties will return a default value if the block doesn't exist.
|
|
|
|
/// </summary>
|
|
|
|
public bool Exists => BlockEngine.BlockExists(Id);
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Returns an array of blocks that are connected to this one. Returns an empty array if the block doesn't exist.
|
|
|
|
/// </summary>
|
|
|
|
public Block[] GetConnectedCubes() => BlockEngine.GetConnectedBlocks(Id);
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Removes this block.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>True if the block exists and could be removed.</returns>
|
|
|
|
public bool Remove() => RemovalEngine.RemoveBlock(Id);
|
|
|
|
|
2020-05-22 22:06:49 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Returns the rigid body of the cluster of blocks this one belongs to during simulation.
|
|
|
|
/// Can be used to apply forces or move the block around while the simulation is running.
|
|
|
|
/// </summary>
|
2020-05-24 17:29:02 +00:00
|
|
|
/// <returns>The SimBody of the cluster</returns>
|
|
|
|
public SimBody GetSimBody()
|
2020-05-22 22:06:49 +00:00
|
|
|
{
|
|
|
|
uint id = BlockEngine.GetBlockInfo<GridConnectionsEntityStruct>(Id).machineRigidBodyId;
|
|
|
|
return new SimBody(id);
|
|
|
|
}
|
|
|
|
|
2020-05-18 03:19:16 +00:00
|
|
|
public override string ToString()
|
|
|
|
{
|
2020-05-24 17:29:02 +00:00
|
|
|
return $"{nameof(Id)}: {Id}, {nameof(Position)}: {Position}, {nameof(Type)}: {Type}, {nameof(Color)}: {Color}, {nameof(Exists)}: {Exists}";
|
2020-05-18 03:19:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static void Init()
|
|
|
|
{
|
|
|
|
GameEngineManager.AddGameEngine(PlacementEngine);
|
|
|
|
GameEngineManager.AddGameEngine(MovementEngine);
|
|
|
|
GameEngineManager.AddGameEngine(RotationEngine);
|
|
|
|
GameEngineManager.AddGameEngine(RemovalEngine);
|
|
|
|
GameEngineManager.AddGameEngine(BlockEngine);
|
|
|
|
}
|
|
|
|
|
2020-05-21 00:42:02 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Convert the block to a specialised block class.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>The block.</returns>
|
|
|
|
/// <typeparam name="T">The specialised block type.</typeparam>
|
2020-05-22 00:00:31 +00:00
|
|
|
public T Specialise<T>() where T : Block
|
2020-05-18 03:19:16 +00:00
|
|
|
{
|
|
|
|
// What have I gotten myself into?
|
|
|
|
// C# can't cast to a child of Block unless the object was originally that child type
|
|
|
|
// And C# doesn't let me make implicit cast operators for child types
|
|
|
|
// So thanks to Microsoft, we've got this horrible implementation using reflection
|
|
|
|
ConstructorInfo ctor = typeof(T).GetConstructor(types: new System.Type[] { typeof(EGID) });
|
|
|
|
if (ctor == null)
|
|
|
|
{
|
|
|
|
throw new BlockSpecializationException("Specialized block constructor does not accept an EGID");
|
|
|
|
}
|
|
|
|
return (T)ctor.Invoke(new object[] { Id });
|
|
|
|
}
|
|
|
|
|
|
|
|
#if DEBUG
|
|
|
|
public static EntitiesDB entitiesDB
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return BlockEngine.GetEntitiesDB();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2020-05-13 12:02:36 +00:00
|
|
|
}
|