Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.SceneManagement;
- // Justin BIttner
- // 12/1/2019 2335
- // Captains Logs...
- // Compiles Interactable Items Into A List With Required Items To Interact With
- // And Gives The Effect To Be Triggered
- // Also Removes Items From Inventory
- public class ItemReqList : MonoBehaviour
- {
- // Ini Format
- // [X, 0] Object Interacted With's Name
- // [X, 1] Scene Name For This Item Use
- // [X, 2] Object Required To Interact With Object In 0.
- // [X, 3] Name Of Function To Trigger Upon Completion
- // For [X,3] Attach Script With Function To Same Game Object As This Script
- // Failure To Do So May Result In Complete System Failure
- // Or it just won't work...
- // [X, 4] Remove Item After This Specific Use? (y,n)
- // Ini Example Below
- // Interactable name!Scene Name@Required Item's Name#Name Of Function$Remove After This Use(y,n)
- // Hull Breach!Barracks@Arc Welder#RepairHullFunction$y
- // Global Declaration
- int foundOne;
- // Public Declaration
- public string[,] itemList;
- public int objectsInList;
- void Start()
- {
- // Declarations
- int countItems = 0;
- string readLine;
- string path = Application.dataPath + "/Resources/itemReqList.ini";// Location Of Ini File
- System.IO.StreamReader listCompiler;
- // Checks Amount Of Lines(Items)
- // Also has 1000 item limit in case of error unity won't freeze
- listCompiler = new System.IO.StreamReader(path);
- while ((readLine = listCompiler.ReadLine()) != null && countItems < 1000)
- {
- countItems++;
- }
- Debug.Log(countItems);
- // Total Objects Count
- objectsInList = countItems;
- // Define Array Size
- itemList = new string[countItems, 5];
- // Reset Count Var And StreamReader
- countItems = 0;
- listCompiler.Close();
- listCompiler = new System.IO.StreamReader(path);
- // Populate Array
- while ((readLine = listCompiler.ReadLine()) != null && countItems < 1000)
- {
- // Set Indexes
- int first = readLine.IndexOf('!');
- int second = readLine.IndexOf('@');
- int third = readLine.IndexOf('#');
- int last = readLine.IndexOf('$');
- // Set [x, 0]
- // Interactive Name
- string intName = readLine.Substring(0, first);
- itemList[countItems, 0] = intName;
- // Set [x,1]
- // Required Scene(Room) Name
- string sceneName = readLine.Substring(first + 1, second - first - 1);
- itemList[countItems, 1] = sceneName;
- // Set [x,2]
- // Item Required To Complete Puzzle
- string reqItem = readLine.Substring(second + 1, third - second - 1);
- itemList[countItems, 2] = reqItem;
- // Set [x,3]
- // Function Called To Complete Puzzle
- string funcCall = readLine.Substring(third + 1, last - third - 1);
- itemList[countItems, 3] = funcCall;
- // Set [x,4]
- // Remove Item After This Use
- string itemRemove = readLine.Substring(last + 1);
- itemList[countItems, 4] = itemRemove;
- countItems++;
- }
- listCompiler.Close();
- }
- // Use Item
- void OnTriggerStay(Collider other)
- {
- GameObject gameStatus = GameObject.Find("GameStatus");
- string nameOne = "";
- string nameTwo = "";
- string funcToCall;
- int timesSpotted = 0;
- if (other.tag == "Player" && Input.GetKeyDown(KeyCode.F))
- {
- timesSpotted = 0;
- // Scans For Object In The List
- for (int i = 0; i < objectsInList; i++)
- {
- if (timesSpotted == 0)
- {
- // Continues To Next Check
- if (itemList[i, 0] == gameObject.name)
- {
- timesSpotted = 1;
- nameOne = itemList[i, 2];
- foundOne = i;
- for (int j = i + 1; j < objectsInList; j++)
- {
- if (itemList[j, 0] == gameObject.name)
- {
- nameTwo = itemList[j, 2];
- Debug.Log(nameOne);
- Debug.Log(nameTwo);
- timesSpotted = 2;
- j = objectsInList;
- }
- }
- }
- }
- }
- // Calls Check Function
- // Checks If Item Is To Be Removed
- // Then Removes Item
- if (timesSpotted == 1)
- {
- if (InInventory(nameOne))
- {
- funcToCall = itemList[foundOne, 3];
- gameObject.SendMessage(funcToCall, itemList[foundOne, 2]);
- removeItem(nameOne);
- }
- }
- else if (timesSpotted == 2)
- {
- if (InInventory(nameOne, nameTwo))
- {
- Debug.Log("True");
- funcToCall = itemList[foundOne, 3];
- gameObject.SendMessage(funcToCall);
- Debug.Log("Hit");
- removeItem(nameOne);
- Debug.Log("Hit2");
- removeItem(nameTwo);
- }
- }
- }
- }
- void removeItem(string removeThis)
- {
- bool removed;
- removed = false;
- for (int i = 0; i < GameStatus.inventory.Count; i++)
- {
- if (!removed)
- {
- if (GameStatus.inventory[i].name == removeThis)
- {
- GameStatus.inventory.Remove(GameStatus.inventory[i].gameObject);
- removed = true;
- Debug.Log("Removed " + GameStatus.inventory[i].gameObject.name);
- }
- }
- }
- }
- // Inventory Scans
- // One Item Scan
- public bool InInventory(string item1)
- {
- for (int i = 0; i < GameStatus.inventory.Count; i++)
- {
- if (GameStatus.inventory[i].gameObject.name.ToString() == item1)
- {
- return true;
- }
- }
- return false;
- }
- // Two Item Scan
- public bool InInventory(string item1, string item2)
- {
- foreach (GameObject i in GameStatus.inventory)
- {
- if (i.gameObject.name.ToString() == item1)
- {
- foreach (GameObject j in GameStatus.inventory)
- {
- if (j.gameObject.name.ToString() == item2)
- {
- return true;
- }
- }
- }
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement