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.

Block.cs 8.2KB

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