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.1KB

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