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 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. /// </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;
  104. set
  105. {
  106. BlockEngine.GetBlockInfo<ScalingEntityStruct>(Id).scale = value;
  107. }
  108. }
  109. /// <summary>
  110. /// The block's uniform scale or zero if the block's invalid. Also sets the non-uniform scale.
  111. /// </summary>
  112. public int UniformScale
  113. {
  114. get => BlockEngine.GetBlockInfo<UniformBlockScaleEntityStruct>(Id).scaleFactor;
  115. set
  116. {
  117. ref var scaleStruct = ref BlockEngine.GetBlockInfo<UniformBlockScaleEntityStruct>(Id);
  118. scaleStruct.scaleFactor = value;
  119. Scale = new float3(value, value, value);
  120. }
  121. }
  122. /// <summary>
  123. /// The block's type (ID). Returns BlockIDs.Invalid if the block doesn't exist anymore.
  124. /// </summary>
  125. public BlockIDs Type
  126. {
  127. get
  128. {
  129. var id = (BlockIDs) BlockEngine.GetBlockInfo<DBEntityStruct>(Id, out var exists).DBID;
  130. return exists ? id : BlockIDs.Invalid;
  131. }
  132. }
  133. /// <summary>
  134. /// The block's color. Returns BlockColors.Default if the block no longer exists.
  135. /// </summary>
  136. public BlockColor Color
  137. {
  138. get
  139. {
  140. byte index = BlockEngine.GetBlockInfo<ColourParameterEntityStruct>(Id, out var exists).indexInPalette;
  141. if (!exists) index = byte.MaxValue;
  142. if (index == byte.MaxValue) return new BlockColor { Color = BlockColors.Default };
  143. return new BlockColor { Color = (BlockColors)(index % 10), Darkness = (byte)(index / 10) };
  144. }
  145. set
  146. {
  147. ref var color = ref BlockEngine.GetBlockInfo<ColourParameterEntityStruct>(Id);
  148. color.indexInPalette = (byte)(value.Color + value.Darkness * 10);
  149. color.overridePaletteColour = false;
  150. color.needsUpdate = true;
  151. BlockEngine.SetBlockColorFromPalette(ref color);
  152. }
  153. }
  154. /// <summary>
  155. /// The block's exact color. Gets reset to the palette color (Color property) after reentering the game.
  156. /// </summary>
  157. public float4 CustomColor
  158. {
  159. get => BlockEngine.GetBlockInfo<ColourParameterEntityStruct>(Id).overriddenColour;
  160. set
  161. {
  162. ref var color = ref BlockEngine.GetBlockInfo<ColourParameterEntityStruct>(Id);
  163. color.overriddenColour = value;
  164. color.overridePaletteColour = true;
  165. color.needsUpdate = true;
  166. }
  167. }
  168. /// <summary>
  169. /// Whether the block exists. The other properties will return a default value if the block doesn't exist.
  170. /// </summary>
  171. public bool Exists => BlockEngine.BlockExists(Id);
  172. /// <summary>
  173. /// Returns an array of blocks that are connected to this one. Returns an empty array if the block doesn't exist.
  174. /// </summary>
  175. public Block[] GetConnectedCubes() => BlockEngine.GetConnectedBlocks(Id);
  176. /// <summary>
  177. /// Removes this block.
  178. /// </summary>
  179. /// <returns>True if the block exists and could be removed.</returns>
  180. public bool Remove() => RemovalEngine.RemoveBlock(Id);
  181. public override string ToString()
  182. {
  183. return $"{nameof(Id)}: {Id}, {nameof(Position)}: {Position}, {nameof(Rotation)}: {Rotation}";
  184. }
  185. public static void Init()
  186. {
  187. GameEngineManager.AddGameEngine(PlacementEngine);
  188. GameEngineManager.AddGameEngine(MovementEngine);
  189. GameEngineManager.AddGameEngine(RotationEngine);
  190. GameEngineManager.AddGameEngine(RemovalEngine);
  191. GameEngineManager.AddGameEngine(BlockEngine);
  192. }
  193. public T Specialise<T>(bool sameTick = true) where T : Block
  194. {
  195. // What have I gotten myself into?
  196. // C# can't cast to a child of Block unless the object was originally that child type
  197. // And C# doesn't let me make implicit cast operators for child types
  198. // So thanks to Microsoft, we've got this horrible implementation using reflection
  199. ConstructorInfo ctor = typeof(T).GetConstructor(types: new System.Type[] { typeof(EGID) });
  200. if (ctor == null)
  201. {
  202. throw new BlockSpecializationException("Specialized block constructor does not accept an EGID");
  203. }
  204. if (sameTick) Sync();
  205. return (T)ctor.Invoke(new object[] { Id });
  206. }
  207. #if DEBUG
  208. public static EntitiesDB entitiesDB
  209. {
  210. get
  211. {
  212. return BlockEngine.GetEntitiesDB();
  213. }
  214. }
  215. #endif
  216. }
  217. }