RecipePage.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. namespace RecipeMenuCore.API
  4. {
  5. /// <summary>
  6. /// Defines a custom RecipePage. Make sure to call Register on it to enable your RecipePage and to add at least one RecipePageEntry using AddRecipePageEntry.
  7. /// </summary>
  8. public class RecipePage
  9. {
  10. /// <summary>
  11. /// The RecipePageType of this RecipePage
  12. /// </summary>
  13. public readonly RecipePageType Type;
  14. /// <summary>
  15. /// The id of this crafter for RecipePageType = Custom
  16. /// </summary>
  17. public readonly string CrafterId;
  18. /// <summary>
  19. /// The Title displayed on top of this RecipePage.
  20. /// </summary>
  21. public readonly string Title;
  22. /// <summary>
  23. /// The Texture associated with this RecipePage. May be null.
  24. /// </summary>
  25. public Texture Tex { get; protected set; }
  26. /// <summary>
  27. /// The Material associated with this RecipePage. May be null.
  28. /// </summary>
  29. public Material Mat { get; protected set; }
  30. internal List<RecipePageEntry> Rows = new List<RecipePageEntry>();
  31. /// <summary>
  32. /// Use to create a new RecipePage. Make sure to call Register on it to enable your RecipePage.
  33. /// </summary>
  34. public RecipePage(RecipePageType Type, string Title, Texture Tex)
  35. {
  36. if (Type == RecipePageType.Custom)
  37. throw new System.Exception("RecipePageType.Custom requires a crafter name to be set.");
  38. this.Type = Type;
  39. this.Title = Title;
  40. this.Tex = Tex;
  41. }
  42. /// <summary>
  43. /// Use to create a new RecipePage. Make sure to call Register on it to enable your RecipePage.
  44. /// </summary>
  45. public RecipePage(RecipePageType Type, string Title, Material Mat)
  46. {
  47. if (Type == RecipePageType.Custom)
  48. throw new System.Exception("RecipePageType.Custom requires a crafter name to be set.");
  49. this.Type = Type;
  50. this.Title = Title;
  51. this.Mat = Mat;
  52. }
  53. /// <summary>
  54. /// Use to create a new RecipePage. Make sure to call Register on it to enable your RecipePage.
  55. /// </summary>
  56. public RecipePage(RecipePageType Type, string Title, Texture Tex, string CrafterId)
  57. {
  58. if (Type != RecipePageType.Custom)
  59. throw new System.Exception("CrafterId is only used for RecipePageType = Custom.");
  60. this.Type = Type;
  61. this.Title = Title;
  62. this.Tex = Tex;
  63. this.CrafterId = CrafterId;
  64. }
  65. /// <summary>
  66. /// Use to create a new RecipePage. Make sure to call Register on it to enable your RecipePage.
  67. /// </summary>
  68. public RecipePage(RecipePageType Type, string Title, Material Mat, string CrafterId)
  69. {
  70. if (Type != RecipePageType.Custom)
  71. throw new System.Exception("CrafterId is only used for RecipePageType = Custom.");
  72. this.Type = Type;
  73. this.Title = Title;
  74. this.Mat = Mat;
  75. this.CrafterId = CrafterId;
  76. }
  77. /// <summary>
  78. /// Used to register the page and make it show.
  79. /// </summary>
  80. public virtual RecipePage Register()
  81. {
  82. PostRegister();
  83. switch (Type)
  84. {
  85. case RecipePageType.GearForge:
  86. Core.pageGearForgeInfoList.Add(this);
  87. break;
  88. case RecipePageType.AlchemyStation:
  89. Core.pageAlchemyStationInfoList.Add(this);
  90. break;
  91. case RecipePageType.UltimateForge:
  92. Core.pageUltimateForgeInfoList.Add(this);
  93. break;
  94. case RecipePageType.UniversalCrafter:
  95. Core.pageUniversalCrafterInfoList.Add(this);
  96. break;
  97. case RecipePageType.Custom:
  98. if (!Core.pageCustomCrafterInfoLists.ContainsKey(CrafterId))
  99. Core.pageCustomCrafterInfoLists.Add(CrafterId, new List<RecipePage>());
  100. Core.pageCustomCrafterInfoLists[CrafterId].Add(this);
  101. break;
  102. }
  103. return this;
  104. }
  105. /// <summary>
  106. /// Used to register the page to repalce a vanilla one and make it show.
  107. /// </summary>
  108. public virtual RecipePage RegisterAsVanilla(int i)
  109. {
  110. if (Type != RecipePageType.GearForge && Type != RecipePageType.AlchemyStation && Type != RecipePageType.UltimateForge)
  111. throw new System.Exception("RegisterAsVanilla can only be used for vanilla crafters.");
  112. PostRegister();
  113. switch (Type)
  114. {
  115. case RecipePageType.GearForge:
  116. Core.pageGearForgeInfoListVanilla[i] = this;
  117. break;
  118. case RecipePageType.AlchemyStation:
  119. Core.pageAlchemyStationInfoListVanilla[i] = this;
  120. break;
  121. case RecipePageType.UltimateForge:
  122. Core.pageUltimateForgeInfoListVanilla[i] = this;
  123. break;
  124. }
  125. return this;
  126. }
  127. protected void PostRegister()
  128. {
  129. if (Mat == null)
  130. {
  131. Mat = new Material(Shader.Find("Unlit/Transparent"))
  132. {
  133. mainTexture = Tex
  134. };
  135. }
  136. else
  137. {
  138. Tex = Mat.mainTexture;
  139. }
  140. }
  141. /// <summary>
  142. /// Used to register the RecipePage to make it show up.
  143. /// </summary>
  144. public RecipePageType GetEntryType()
  145. {
  146. return Type;
  147. }
  148. /// <summary>
  149. /// Adds a new RecipePageEntry to the RecipePage.
  150. /// </summary>
  151. public void AddRecipePageEntry(RecipePageEntry row)
  152. {
  153. if (Type != RecipePageType.Custom && row.IsReverse)
  154. throw new System.Exception("Reverse recipes are only supported in custom crafting stations.");
  155. Rows.Add(row);
  156. }
  157. /// <summary>
  158. /// Returns the list of avalible RecipePageEntry on this RecipePage.
  159. /// </summary>
  160. public RecipePageEntry[] GetRecipePageEntries()
  161. {
  162. return Rows.ToArray();
  163. }
  164. }
  165. /// <summary>
  166. /// This indicates what types of recipes are avalible.
  167. /// </summary>
  168. public enum RecipePageType
  169. {
  170. /// <summary>
  171. /// This recipe is for the Gear Forge.
  172. /// </summary>
  173. GearForge,
  174. /// <summary>
  175. /// This recipe is for the Alchemy Station.
  176. /// </summary>
  177. AlchemyStation,
  178. /// <summary>
  179. /// This recipe is for the Ultimate Forge.
  180. /// </summary>
  181. UltimateForge,
  182. /// <summary>
  183. /// This recipe is for the Gadget Core Universal Crafter.
  184. /// </summary>
  185. UniversalCrafter,
  186. /// <summary>
  187. /// This recipe is for a custom Crafter.
  188. /// </summary>
  189. Custom
  190. }
  191. }