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.

Timer.cs 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using System;
  2. using RobocraftX.Blocks;
  3. using Gamecraft.Blocks.TimerBlock;
  4. using Svelto.ECS;
  5. using Unity.Mathematics;
  6. using GamecraftModdingAPI;
  7. using GamecraftModdingAPI.Utility;
  8. namespace GamecraftModdingAPI.Blocks
  9. {
  10. public class Timer : Block
  11. {
  12. /// <summary>
  13. /// Places a new timer block.
  14. /// </summary>
  15. public static Timer PlaceNew(float3 position,
  16. float3 rotation = default, BlockColors color = BlockColors.Default, byte darkness = 0,
  17. int uscale = 1, float3 scale = default, Player player = null)
  18. {
  19. if (PlacementEngine.IsInGame && GameState.IsBuildMode())
  20. {
  21. try
  22. {
  23. EGID id = PlacementEngine.PlaceBlock(BlockIDs.Timer, color, darkness,
  24. position, uscale, scale, player, rotation);
  25. Sync();
  26. return new Timer(id);
  27. }
  28. catch (Exception e)
  29. {
  30. Logging.MetaDebugLog(e);
  31. }
  32. }
  33. return null;
  34. }
  35. public Timer(EGID id) : base(id)
  36. {
  37. if (!BlockEngine.GetBlockInfoExists<TimerBlockDataStruct>(this.Id))
  38. {
  39. throw new BlockTypeException($"Block is not a {this.GetType().Name} block");
  40. }
  41. }
  42. public Timer(uint id) : base(id)
  43. {
  44. if (!BlockEngine.GetBlockInfoExists<TimerBlockDataStruct>(this.Id))
  45. {
  46. throw new BlockTypeException($"Block is not a {this.GetType().Name} block");
  47. }
  48. }
  49. // custom timer properties
  50. /// <summary>
  51. /// The player-specified start time.
  52. /// </summary>
  53. public float Start
  54. {
  55. get
  56. {
  57. return BlockEngine.GetBlockInfo<TimerBlockDataStruct>(Id).startTime;
  58. }
  59. set
  60. {
  61. ref TimerBlockDataStruct tbds = ref BlockEngine.GetBlockInfo<TimerBlockDataStruct>(Id);
  62. tbds.startTime = value;
  63. }
  64. }
  65. /// <summary>
  66. /// The player-specified end time.
  67. /// </summary>
  68. public float End
  69. {
  70. get
  71. {
  72. return BlockEngine.GetBlockInfo<TimerBlockDataStruct>(Id).endTime;
  73. }
  74. set
  75. {
  76. ref TimerBlockDataStruct tbds = ref BlockEngine.GetBlockInfo<TimerBlockDataStruct>(Id);
  77. tbds.endTime = value;
  78. }
  79. }
  80. /// <summary>
  81. /// Whether to display time with millisecond precision.
  82. /// </summary>
  83. public bool DisplayMilliseconds
  84. {
  85. get
  86. {
  87. return BlockEngine.GetBlockInfo<TimerBlockDataStruct>(Id).outputFormatHasMS;
  88. }
  89. set
  90. {
  91. ref TimerBlockDataStruct tbds = ref BlockEngine.GetBlockInfo<TimerBlockDataStruct>(Id);
  92. tbds.outputFormatHasMS = value;
  93. }
  94. }
  95. /// <summary>
  96. /// Current time (as of the last video frame), in milliseconds.
  97. /// </summary>
  98. public int CurrentTime
  99. {
  100. get
  101. {
  102. return BlockEngine.GetBlockInfo<TimerBlockLabelCacheEntityStruct>(Id).timeLastRenderFrameMS;
  103. }
  104. set
  105. {
  106. ref TimerBlockLabelCacheEntityStruct tblces = ref BlockEngine.GetBlockInfo<TimerBlockLabelCacheEntityStruct>(Id);
  107. tblces.timeLastRenderFrameMS = value;
  108. }
  109. }
  110. }
  111. }