TechbloxModdingAPI/GamecraftModdingAPI/Blocks/BlockEngine.cs

86 lines
2.3 KiB
C#
Raw Normal View History

using System.Collections.Generic;
2020-05-13 14:52:21 +00:00
using RobocraftX.Blocks;
using RobocraftX.Common;
using Svelto.DataStructures;
using Svelto.ECS;
2020-05-13 14:52:21 +00:00
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;
}
2020-05-13 14:52:21 +00:00
/// <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);
}
public bool GetBlockInfoExists<T>(EGID blockID) where T : struct, IEntityComponent
{
return entitiesDB.Exists<T>(blockID);
}
#if DEBUG
public EntitiesDB GetEntitiesDB()
{
return entitiesDB;
}
#endif
}
}