Advertisement
Guest User

Untitled

a guest
Dec 5th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.08 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.SceneManagement;
  6. // Justin BIttner
  7. // 12/1/2019 2335
  8. // Captains Logs...
  9. // Compiles Interactable Items Into A List With Required Items To Interact With
  10. // And Gives The Effect To Be Triggered
  11.  
  12. // Also Removes Items From Inventory
  13. public class ItemReqList : MonoBehaviour
  14. {
  15. // Ini Format
  16. // [X, 0] Object Interacted With's Name
  17. // [X, 1] Scene Name For This Item Use
  18. // [X, 2] Object Required To Interact With Object In 0.
  19. // [X, 3] Name Of Function To Trigger Upon Completion
  20. // For [X,3] Attach Script With Function To Same Game Object As This Script
  21. // Failure To Do So May Result In Complete System Failure
  22. // Or it just won't work...
  23. // [X, 4] Remove Item After This Specific Use? (y,n)
  24. // Ini Example Below
  25. // Interactable name!Scene Name@Required Item's Name#Name Of Function$Remove After This Use(y,n)
  26. // Hull Breach!Barracks@Arc Welder#RepairHullFunction$y
  27.  
  28. // Global Declaration
  29. int foundOne;
  30.  
  31. // Public Declaration
  32. public string[,] itemList;
  33. public int objectsInList;
  34. void Start()
  35. {
  36. // Declarations
  37. int countItems = 0;
  38. string readLine;
  39. string path = Application.dataPath + "/Resources/itemReqList.ini";// Location Of Ini File
  40. System.IO.StreamReader listCompiler;
  41.  
  42. // Checks Amount Of Lines(Items)
  43. // Also has 1000 item limit in case of error unity won't freeze
  44. listCompiler = new System.IO.StreamReader(path);
  45. while ((readLine = listCompiler.ReadLine()) != null && countItems < 1000)
  46. {
  47. countItems++;
  48. }
  49. Debug.Log(countItems);
  50.  
  51. // Total Objects Count
  52. objectsInList = countItems;
  53.  
  54. // Define Array Size
  55. itemList = new string[countItems, 5];
  56.  
  57. // Reset Count Var And StreamReader
  58. countItems = 0;
  59. listCompiler.Close();
  60. listCompiler = new System.IO.StreamReader(path);
  61.  
  62. // Populate Array
  63. while ((readLine = listCompiler.ReadLine()) != null && countItems < 1000)
  64. {
  65. // Set Indexes
  66. int first = readLine.IndexOf('!');
  67. int second = readLine.IndexOf('@');
  68. int third = readLine.IndexOf('#');
  69. int last = readLine.IndexOf('$');
  70.  
  71. // Set [x, 0]
  72. // Interactive Name
  73. string intName = readLine.Substring(0, first);
  74. itemList[countItems, 0] = intName;
  75.  
  76. // Set [x,1]
  77. // Required Scene(Room) Name
  78. string sceneName = readLine.Substring(first + 1, second - first - 1);
  79. itemList[countItems, 1] = sceneName;
  80.  
  81. // Set [x,2]
  82. // Item Required To Complete Puzzle
  83. string reqItem = readLine.Substring(second + 1, third - second - 1);
  84. itemList[countItems, 2] = reqItem;
  85.  
  86. // Set [x,3]
  87. // Function Called To Complete Puzzle
  88. string funcCall = readLine.Substring(third + 1, last - third - 1);
  89. itemList[countItems, 3] = funcCall;
  90.  
  91. // Set [x,4]
  92. // Remove Item After This Use
  93. string itemRemove = readLine.Substring(last + 1);
  94. itemList[countItems, 4] = itemRemove;
  95.  
  96. countItems++;
  97. }
  98. listCompiler.Close();
  99. }
  100.  
  101. // Use Item
  102. void OnTriggerStay(Collider other)
  103. {
  104. GameObject gameStatus = GameObject.Find("GameStatus");
  105.  
  106. string nameOne = "";
  107. string nameTwo = "";
  108. string funcToCall;
  109.  
  110. int timesSpotted = 0;
  111.  
  112. if (other.tag == "Player" && Input.GetKeyDown(KeyCode.F))
  113. {
  114. timesSpotted = 0;
  115. // Scans For Object In The List
  116. for (int i = 0; i < objectsInList; i++)
  117. {
  118. if (timesSpotted == 0)
  119. {
  120. // Continues To Next Check
  121. if (itemList[i, 0] == gameObject.name)
  122. {
  123. timesSpotted = 1;
  124. nameOne = itemList[i, 2];
  125. foundOne = i;
  126. for (int j = i + 1; j < objectsInList; j++)
  127. {
  128. if (itemList[j, 0] == gameObject.name)
  129. {
  130. nameTwo = itemList[j, 2];
  131. Debug.Log(nameOne);
  132. Debug.Log(nameTwo);
  133. timesSpotted = 2;
  134. j = objectsInList;
  135. }
  136. }
  137. }
  138. }
  139. }
  140.  
  141. // Calls Check Function
  142. // Checks If Item Is To Be Removed
  143. // Then Removes Item
  144. if (timesSpotted == 1)
  145. {
  146. if (InInventory(nameOne))
  147. {
  148. funcToCall = itemList[foundOne, 3];
  149. gameObject.SendMessage(funcToCall, itemList[foundOne, 2]);
  150. removeItem(nameOne);
  151. }
  152. }
  153.  
  154. else if (timesSpotted == 2)
  155. {
  156. if (InInventory(nameOne, nameTwo))
  157. {
  158. Debug.Log("True");
  159. funcToCall = itemList[foundOne, 3];
  160. gameObject.SendMessage(funcToCall);
  161. Debug.Log("Hit");
  162. removeItem(nameOne);
  163. Debug.Log("Hit2");
  164. removeItem(nameTwo);
  165.  
  166. }
  167. }
  168. }
  169. }
  170. void removeItem(string removeThis)
  171. {
  172. bool removed;
  173. removed = false;
  174.  
  175. for (int i = 0; i < GameStatus.inventory.Count; i++)
  176. {
  177. if (!removed)
  178. {
  179. if (GameStatus.inventory[i].name == removeThis)
  180. {
  181. GameStatus.inventory.Remove(GameStatus.inventory[i].gameObject);
  182. removed = true;
  183. Debug.Log("Removed " + GameStatus.inventory[i].gameObject.name);
  184. }
  185. }
  186. }
  187. }
  188.  
  189. // Inventory Scans
  190. // One Item Scan
  191. public bool InInventory(string item1)
  192. {
  193. for (int i = 0; i < GameStatus.inventory.Count; i++)
  194. {
  195. if (GameStatus.inventory[i].gameObject.name.ToString() == item1)
  196. {
  197. return true;
  198. }
  199. }
  200. return false;
  201. }
  202.  
  203. // Two Item Scan
  204. public bool InInventory(string item1, string item2)
  205. {
  206. foreach (GameObject i in GameStatus.inventory)
  207. {
  208. if (i.gameObject.name.ToString() == item1)
  209. {
  210. foreach (GameObject j in GameStatus.inventory)
  211. {
  212. if (j.gameObject.name.ToString() == item2)
  213. {
  214. return true;
  215. }
  216. }
  217. }
  218. }
  219. return false;
  220. }
  221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement