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.

220 lines
7.1KB

  1. using System;
  2. using System.Reflection;
  3. using Svelto.ECS;
  4. using Svelto.ECS.EntityStructs;
  5. using RobocraftX.Common;
  6. using RobocraftX.Blocks.Scaling;
  7. using Unity.Mathematics;
  8. using GamecraftModdingAPI.Blocks;
  9. using GamecraftModdingAPI.Utility;
  10. namespace GamecraftModdingAPI
  11. {
  12. /// <summary>
  13. /// A single (perhaps scaled) block. Properties may return default values if the block is removed and then setting them is ignored.
  14. /// </summary>
  15. public class Block
  16. {
  17. protected static readonly PlacementEngine PlacementEngine = new PlacementEngine();
  18. protected static readonly MovementEngine MovementEngine = new MovementEngine();
  19. protected static readonly RotationEngine RotationEngine = new RotationEngine();
  20. protected static readonly RemovalEngine RemovalEngine = new RemovalEngine();
  21. protected static readonly BlockEngine BlockEngine = new BlockEngine();
  22. /// <summary>
  23. /// 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.
  24. /// Place blocks next to each other to connect them.
  25. /// The placed block will be a complete block with a placement grid and collision which will be saved along with the game.
  26. /// </summary>
  27. /// <param name="block">The block's type</param>
  28. /// <param name="color">The block's color</param>
  29. /// <param name="darkness">The block color's darkness (0-9) - 0 is default color</param>
  30. /// <param name="position">The block's position in the grid - default block size is 0.2</param>
  31. /// <param name="rotation">The block's rotation in degrees</param>
  32. /// <param name="uscale">The block's uniform scale - default scale is 1 (with 0.2 width)</param>
  33. /// <param name="scale">The block's non-uniform scale - 0 means <paramref name="uscale"/> is used</param>
  34. /// <param name="player">The player who placed the block</param>
  35. /// <returns>The placed block or null if failed</returns>
  36. public static Block PlaceNew(BlockIDs block, float3 position,
  37. float3 rotation = default, BlockColors color = BlockColors.Default, byte darkness = 0,
  38. int uscale = 1, float3 scale = default, Player player = null)
  39. {
  40. if (PlacementEngine.IsInGame && GameState.IsBuildMode())
  41. {
  42. try
  43. {
  44. return new Block(PlacementEngine.PlaceBlock(block, color, darkness,
  45. position, uscale, scale, player, rotation));
  46. }
  47. catch (Exception e)
  48. {
  49. Logging.MetaDebugLog(e);
  50. }
  51. }
  52. return null;
  53. }
  54. /// <summary>
  55. /// Returns the most recently placed block.
  56. /// </summary>
  57. /// <returns>The block object</returns>
  58. public static Block GetLastPlacedBlock()
  59. {
  60. return new Block(BlockIdentifiers.LatestBlockID);
  61. }
  62. public Block(EGID id)
  63. {
  64. Id = id;
  65. }
  66. public Block(uint id)
  67. {
  68. Id = new EGID(id, CommonExclusiveGroups.OWNED_BLOCKS_GROUP);
  69. }
  70. protected static void Sync()
  71. {
  72. DeterministicStepCompositionRootPatch.SubmitEntitiesNow();
  73. }
  74. public EGID Id { get; }
  75. /// <summary>
  76. /// The block's current position or zero if the block no longer exists.
  77. /// A block is 0.2 wide by default in terms of position.
  78. /// </summary>
  79. public float3 Position
  80. {
  81. get => Exists ? MovementEngine.GetPosition(Id.entityID) : float3.zero;
  82. set
  83. {
  84. if (Exists) MovementEngine.MoveBlock(Id.entityID, value);
  85. }
  86. }
  87. /// <summary>
  88. /// The block's current rotation in degrees or zero if the block doesn't exist.
  89. /// </summary>
  90. public float3 Rotation
  91. {
  92. get => Exists ? RotationEngine.GetRotation(Id.entityID) : float3.zero;
  93. set
  94. {
  95. if (Exists) RotationEngine.RotateBlock(Id.entityID, value);
  96. }
  97. }
  98. /// <summary>
  99. /// The block's non-uniform scale or zero if the block's invalid. Independent of the uniform scaling.
  100. /// </summary>
  101. public float3 Scale
  102. {
  103. get => BlockEngine.GetBlockInfo<ScalingEntityStruct>(Id)?.scale ?? float3.zero;
  104. set
  105. {
  106. var def = new ScalingEntityStruct();
  107. BlockEngine.GetBlockInfo(Id, ref def).scale = value;
  108. }
  109. }
  110. /// <summary>
  111. /// The block's uniform scale or zero if the block's invalid. Also sets the non-uniform scale.
  112. /// </summary>
  113. public int UniformScale
  114. {
  115. get => BlockEngine.GetBlockInfo<BlockPlacementScaleEntityStruct>(Id)?.desiredScaleFactor ?? 0;
  116. set
  117. {
  118. var def = new BlockPlacementScaleEntityStruct();
  119. ref var scaleStruct = ref BlockEngine.GetBlockInfo(Id, ref def);
  120. scaleStruct.blockPlacementHeight = scaleStruct.blockPlacementWidth =
  121. scaleStruct.desiredScaleFactor = scaleStruct.snapGridScale = value;
  122. Scale = new float3(value, value, value);
  123. }
  124. }
  125. /// <summary>
  126. /// The block's type (ID). Returns BlockIDs.Invalid if the block doesn't exist anymore.
  127. /// </summary>
  128. public BlockIDs Type => (BlockIDs)(BlockEngine.GetBlockInfo<DBEntityStruct>(Id)?.DBID ?? ushort.MaxValue);
  129. /// <summary>
  130. /// The block's color. Returns BlockColors.Default if the block no longer exists.
  131. /// </summary>
  132. public BlockColor Color
  133. {
  134. get
  135. {
  136. byte index = BlockEngine.GetBlockInfo<ColourParameterEntityStruct>(Id)?.indexInPalette ?? byte.MaxValue;
  137. if (index == byte.MaxValue) return new BlockColor { Color = BlockColors.Default };
  138. return new BlockColor { Color = (BlockColors)(index % 10), Darkness = (byte)(index / 10) };
  139. }
  140. set
  141. {
  142. var def = new ColourParameterEntityStruct();
  143. ref var color = ref BlockEngine.GetBlockInfo(Id, ref def);
  144. color.indexInPalette = (byte)(value.Color + value.Darkness * 10);
  145. color.needsUpdate = true;
  146. }
  147. }
  148. /// <summary>
  149. /// Whether the block exists. The other properties will return a default value if the block doesn't exist.
  150. /// </summary>
  151. public bool Exists => BlockEngine.BlockExists(Id);
  152. /// <summary>
  153. /// Returns an array of blocks that are connected to this one. Returns an empty array if the block doesn't exist.
  154. /// </summary>
  155. public Block[] GetConnectedCubes() => BlockEngine.GetConnectedBlocks(Id);
  156. /// <summary>
  157. /// Removes this block.
  158. /// </summary>
  159. /// <returns>True if the block exists and could be removed.</returns>
  160. public bool Remove() => RemovalEngine.RemoveBlock(Id);
  161. public override string ToString()
  162. {
  163. return $"{nameof(Id)}: {Id}, {nameof(Position)}: {Position}, {nameof(Rotation)}: {Rotation}";
  164. }
  165. public static void Init()
  166. {
  167. GameEngineManager.AddGameEngine(PlacementEngine);
  168. GameEngineManager.AddGameEngine(MovementEngine);
  169. GameEngineManager.AddGameEngine(RotationEngine);
  170. GameEngineManager.AddGameEngine(RemovalEngine);
  171. GameEngineManager.AddGameEngine(BlockEngine);
  172. }
  173. public T Specialise<T>(bool sameTick = true) where T : Block
  174. {
  175. // What have I gotten myself into?
  176. // C# can't cast to a child of Block unless the object was originally that child type
  177. // And C# doesn't let me make implicit cast operators for child types
  178. // So thanks to Microsoft, we've got this horrible implementation using reflection
  179. ConstructorInfo ctor = typeof(T).GetConstructor(types: new System.Type[] { typeof(EGID) });
  180. if (ctor == null)
  181. {
  182. throw new BlockSpecializationException("Specialized block constructor does not accept an EGID");
  183. }
  184. if (sameTick) Sync();
  185. return (T)ctor.Invoke(new object[] { Id });
  186. }
  187. #if DEBUG
  188. public static EntitiesDB entitiesDB
  189. {
  190. get
  191. {
  192. return BlockEngine.GetEntitiesDB();
  193. }
  194. }
  195. #endif
  196. }
  197. }