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.

101 lines
3.0KB

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