TechbloxModdingAPI/GamecraftModdingAPI/Blocks/BlockEngine.cs
NorbiPeti 1c5ce37fce Add debug interface API and improve block API
Added API for adding more information on the debug display (not object-oriented yet)
Removed the setter for block type to ensure stability
Made the block API return defaults if the block no longer exists
Added property to check if the block exists
Made a struct for the block's color property
Added missing block IDs
2020-05-17 23:46:31 +02:00

74 lines
No EOL
2.5 KiB
C#

using System.Collections.Generic;
using RobocraftX.Blocks;
using RobocraftX.Common;
using Svelto.DataStructures;
using Svelto.ECS;
using GamecraftModdingAPI.Engines;
namespace GamecraftModdingAPI.Blocks
{
public class BlockEngine : IApiEngine
{
public string Name { get; } = "GamecraftModdingAPIBlockGameEngine";
public EntitiesDB entitiesDB { set; private get; }
public bool isRemovable => false;
public void Dispose()
{
}
public void Ready()
{
}
public Block[] GetConnectedBlocks(EGID blockID)
{
if (!BlockExists(blockID)) return new Block[0];
Stack<uint> cubeStack = new Stack<uint>();
FasterList<uint> cubesToProcess = new FasterList<uint>();
ConnectedCubesUtility.TreeTraversal.GetConnectedCubes(entitiesDB, blockID.entityID, cubeStack, cubesToProcess, (in GridConnectionsEntityStruct g) => { return false; });
var ret = new Block[cubesToProcess.count];
for (int i = 0; i < cubesToProcess.count; i++)
ret[i] = new Block(cubesToProcess[i]);
return ret;
}
/// <summary>
/// Get a struct of a block. Can be used to set properties.
/// When only querying parameters, use the other overload for convenience.
/// </summary>
/// <param name="blockID"></param>
/// <param name="def"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public ref T GetBlockInfo<T>(EGID blockID, ref T def) where T : struct, IEntityComponent
{
if (entitiesDB.Exists<T>(blockID))
return ref entitiesDB.QueryEntity<T>(blockID);
return ref def;
}
/// <summary>
/// Get a struct of a block. Can only be used to retrieve information.
/// Use the overload with a default parameter to get the struct by reference to set values.
/// </summary>
/// <param name="blockID">The block's EGID</param>
/// <typeparam name="T">The struct's type to get</typeparam>
/// <returns>A copy of the struct or null</returns>
public T? GetBlockInfo<T>(EGID blockID) where T : struct, IEntityComponent
{
if (entitiesDB.Exists<T>(blockID))
return entitiesDB.QueryEntity<T>(blockID);
return null;
}
public bool BlockExists(EGID id)
{
return entitiesDB.Exists<DBEntityStruct>(id);
}
}
}