A stable modding interface between Techblox and mods https://mod.exmods.org/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
2.9KB

  1. using System.Collections.Generic;
  2. using RobocraftX.Blocks;
  3. using RobocraftX.Common;
  4. using RobocraftX.GUI.Hotbar.Colours;
  5. using Svelto.DataStructures;
  6. using Svelto.ECS;
  7. using GamecraftModdingAPI.Engines;
  8. namespace GamecraftModdingAPI.Blocks
  9. {
  10. public class BlockEngine : IApiEngine
  11. {
  12. public string Name { get; } = "GamecraftModdingAPIBlockGameEngine";
  13. public EntitiesDB entitiesDB { set; private get; }
  14. public bool isRemovable => false;
  15. public void Dispose()
  16. {
  17. }
  18. public void Ready()
  19. {
  20. }
  21. public Block[] GetConnectedBlocks(EGID blockID)
  22. {
  23. if (!BlockExists(blockID)) return new Block[0];
  24. Stack<uint> cubeStack = new Stack<uint>();
  25. FasterList<uint> cubesToProcess = new FasterList<uint>();
  26. ConnectedCubesUtility.TreeTraversal.GetConnectedCubes(entitiesDB, blockID.entityID, cubeStack, cubesToProcess, (in GridConnectionsEntityStruct g) => { return false; });
  27. var ret = new Block[cubesToProcess.count];
  28. for (int i = 0; i < cubesToProcess.count; i++)
  29. ret[i] = new Block(cubesToProcess[i]);
  30. return ret;
  31. }
  32. public void SetBlockColorFromPalette(ref ColourParameterEntityStruct color)
  33. {
  34. ref var paletteEntry = ref entitiesDB.QueryEntity<PaletteEntryEntityStruct>(color.indexInPalette,
  35. CommonExclusiveGroups.COLOUR_PALETTE_GROUP);
  36. color.paletteColour = paletteEntry.Colour;
  37. }
  38. /// <summary>
  39. /// Get a struct of a block. Can be used to set properties.
  40. /// Returns a default value if not found.
  41. /// </summary>
  42. /// <param name="blockID">The block's ID</param>
  43. /// <typeparam name="T">The struct to query</typeparam>
  44. /// <returns>An editable reference to the struct</returns>
  45. public ref T GetBlockInfo<T>(EGID blockID) where T : struct, IEntityComponent
  46. {
  47. if (entitiesDB.Exists<T>(blockID))
  48. return ref entitiesDB.QueryEntity<T>(blockID);
  49. T[] structHolder = new T[1]; //Create something that can be referenced
  50. return ref structHolder[0]; //Gets a default value automatically
  51. }
  52. /// <summary>
  53. /// Get a struct of a block. Can be used to set properties.
  54. /// Returns a default value if not found.
  55. /// </summary>
  56. /// <param name="blockID">The block's ID</param>
  57. /// <param name="exists">Whether the specified struct exists for the block</param>
  58. /// <typeparam name="T">The struct to query</typeparam>
  59. /// <returns>An editable reference to the struct</returns>
  60. public ref T GetBlockInfo<T>(EGID blockID, out bool exists) where T : struct, IEntityComponent
  61. {
  62. exists = entitiesDB.Exists<T>(blockID);
  63. if (exists)
  64. return ref entitiesDB.QueryEntity<T>(blockID);
  65. T[] structHolder = new T[1];
  66. //ref T defRef = ref structHolder[0];
  67. return ref structHolder[0];
  68. }
  69. public bool BlockExists(EGID id)
  70. {
  71. return entitiesDB.Exists<DBEntityStruct>(id);
  72. }
  73. public bool GetBlockInfoExists<T>(EGID blockID) where T : struct, IEntityComponent
  74. {
  75. return entitiesDB.Exists<T>(blockID);
  76. }
  77. #if DEBUG
  78. public EntitiesDB GetEntitiesDB()
  79. {
  80. return entitiesDB;
  81. }
  82. #endif
  83. }
  84. }