Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEditor;
- using System.Linq;
- public class ItemList : MonoBehaviour
- {
- public static class fileName
- {
- public static string i = "Items";
- public static string iv = "ItemsVars";
- }
- public static class path
- {
- public static string ip = "Assets/Resources/Text/" + fileName.i + ".txt";
- public static string ivp = "Assets/Resources/Text/" + fileName.iv + ".txt";
- }
- public static class Rpath
- {
- public static string ir = "Text/" + fileName.i;
- public static string ivr = "Text/" + fileName.iv;
- }
- public static string ItemsPatch = "Items/";
- public class itemD
- {
- public string name;
- public object value;
- public object[] array;
- public bool String;
- public bool Int;
- public bool Float;
- public bool Double;
- public bool Bool;
- public bool stringArray;
- public bool intArray;
- public bool floatArray;
- public bool doubleArray;
- public bool boolArray;
- public bool find;
- }
- public class dVar
- {
- public string name;
- public int i;
- }
- private static string[] v = {
- "string", "int", "float", "double", "bool",
- "string array", "int array", "float array", "double array", "bool array"
- };
- public static List<dVar> LoadVars()
- {
- List<dVar> dList1 = new List<dVar>();
- TextAsset t = Resources.Load<TextAsset>(Rpath.ivr);
- if (t == null)
- return dList1;
- string[] lines = t.text.Split('\n');
- for (int i = 0; i < lines.Length - 1; i++)
- {
- string[] args = lines[i].Split('*');
- dVar n = new dVar();
- n.name = args[0];
- n.i = int.Parse(args[1]);
- dList1.Add(n);
- }
- return dList1;
- }
- public static List<List<itemD>> Load()
- {
- List<List<itemD>> dItemList = new List<List<itemD>>();
- List<dVar> dList = ItemList.LoadVars();
- if (dList == null)
- {
- Debug.LogError("Cant load items DB!");
- return dItemList;
- }
- TextAsset ta = Resources.Load<TextAsset>(Rpath.ir);
- if (ta == null)
- {
- Debug.LogError("Cant find file '" + Rpath.ir + "'!");
- return dItemList;
- }
- string[] lines = ta.text.Split('\n');
- for (int i = 0; i < lines.Length - 1; i++)
- {
- List<itemD> n = new List<itemD>();
- string[] args = lines[i].Split(';');
- for (int x = 0; x < dList.Count; x++)
- {
- n.Add(new itemD());
- n[n.Count - 1].name = dList[x].name;
- }
- for (int ii = 0; ii < args.Length - 1; ii++)
- {
- //itemD idd = new itemD();
- string[] args2 = args[ii].Split('=');
- string[] nargs = args2[0].Split('*');
- string name = nargs[0];
- string typ = nargs[1];
- string value = args2[1];
- for (int x = 0; x < n.Count; x++)
- {
- if (v[dList[x].i] == "string")
- {
- n[x].String = true;
- if (name == n[x].name)
- n[x].value = value;
- }
- else if (v[dList[x].i] == "int")
- {
- n[x].Int = true;
- if (name == n[x].name)
- n[x].value = int.Parse(value);
- }
- else if (v[dList[x].i] == "float")
- {
- n[x].Float = true;
- if (name == n[x].name)
- n[x].value = float.Parse(value);
- }
- else if (v[dList[x].i] == "double")
- {
- n[x].Double = true;
- if (name == n[x].name)
- n[x].value = double.Parse(value);
- }
- else if (v[dList[x].i] == "bool")
- {
- n[x].Bool = true;
- if (name == n[x].name)
- n[x].value = bool.Parse(value);
- }
- else if (v[dList[x].i] == "string array")
- {
- n[x].stringArray = true;
- if (name == n[x].name)
- {
- string[] s = value.Split(',');
- int iii = 0;
- foreach (string a in s)
- {
- n[x].array[iii] = a;
- }
- }
- }
- else if (v[dList[x].i] == "int array")
- {
- n[x].intArray = true;
- if (name == n[x].name)
- {
- string[] s = value.Split(',');
- int iii = 0;
- foreach (string a in s)
- {
- n[x].array[iii] = int.Parse(a);
- }
- }
- }
- else if (v[dList[x].i] == "float array")
- {
- n[x].floatArray = true;
- if (name == n[x].name)
- {
- string[] s = value.Split(',');
- int iii = 0;
- foreach (string a in s)
- {
- n[x].array[iii] = float.Parse(a);
- }
- }
- }
- else if (v[dList[x].i] == "double array")
- {
- n[x].doubleArray = true;
- if (name == n[x].name)
- {
- string[] s = value.Split(',');
- int iii = 0;
- foreach (string a in s)
- {
- n[x].array[iii] = double.Parse(a);
- }
- }
- }
- else if (v[dList[x].i] == "bool array")
- {
- n[x].boolArray = true;
- if (name == n[x].name)
- {
- string[] s = value.Split(',');
- int iii = 0;
- foreach (string a in s)
- {
- n[x].array[iii] = bool.Parse(a);
- }
- }
- }
- }
- }
- dItemList.Add(n);
- }
- return dItemList;
- }
- private static Dictionary<string, List<itemD>> dItemList;
- private List<string> saling = new List<string>();
- private void Awake()
- {
- dItemList = LoadDic();
- saling = new List<string>();
- List<itemD>[] dil = dItemList.Values.ToArray();
- string[] dils = dItemList.Keys.ToArray();
- for (int i = 0; i < dItemList.Count; i++)
- {
- List<itemD> liD = dil[i];
- foreach (itemD dliD in liD)
- {
- if (dliD.name == "sale")
- {
- if ((bool)dliD.value == true)
- {
- saling.Add(dils[i]);
- break;
- }
- }
- }
- }
- }
- private static Dictionary<string, List<itemD>> LoadDic()
- {
- Dictionary<string, List<itemD>> dItemListD = new Dictionary<string, List<itemD>>();
- string code = "";
- List<dVar> dList = ItemList.LoadVars();
- if (dList == null)
- {
- Debug.LogError("Cant load items DB!");
- return dItemListD;
- }
- TextAsset ta = Resources.Load<TextAsset>(Rpath.ir);
- if (ta == null)
- {
- Debug.LogError("Cant find file '" + Rpath.ir + "'!");
- return dItemListD;
- }
- string[] lines = ta.text.Split('\n');
- for (int i = 0; i < lines.Length - 1; i++)
- {
- List<itemD> n = new List<itemD>();
- string[] args = lines[i].Split(';');
- for (int x = 0; x < dList.Count; x++)
- {
- n.Add(new itemD());
- n[n.Count - 1].name = dList[x].name;
- }
- for (int ii = 0; ii < args.Length - 1; ii++)
- {
- //itemD idd = new itemD();
- string[] args2 = args[ii].Split('=');
- string[] nargs = args2[0].Split('*');
- string name = nargs[0];
- string typ = nargs[1];
- string value = args2[1];
- if (name == "code")
- {
- code = value;
- }
- for (int x = 0; x < n.Count; x++)
- {
- if (v[dList[x].i] == "string")
- {
- n[x].String = true;
- if (name == n[x].name)
- n[x].value = value;
- }
- else if (v[dList[x].i] == "int")
- {
- n[x].Int = true;
- if (name == n[x].name)
- n[x].value = int.Parse(value);
- }
- else if (v[dList[x].i] == "float")
- {
- n[x].Float = true;
- if (name == n[x].name)
- n[x].value = float.Parse(value);
- }
- else if (v[dList[x].i] == "double")
- {
- n[x].Double = true;
- if (name == n[x].name)
- n[x].value = double.Parse(value);
- }
- else if (v[dList[x].i] == "bool")
- {
- n[x].Bool = true;
- if (name == n[x].name)
- n[x].value = bool.Parse(value);
- }
- else if (v[dList[x].i] == "string array")
- {
- n[x].stringArray = true;
- if (name == n[x].name)
- {
- string[] s = value.Split(',');
- int iii = 0;
- foreach (string a in s)
- {
- n[x].array[iii] = a;
- }
- }
- }
- else if (v[dList[x].i] == "int array")
- {
- n[x].intArray = true;
- if (name == n[x].name)
- {
- string[] s = value.Split(',');
- int iii = 0;
- foreach (string a in s)
- {
- n[x].array[iii] = int.Parse(a);
- }
- }
- }
- else if (v[dList[x].i] == "float array")
- {
- n[x].floatArray = true;
- if (name == n[x].name)
- {
- string[] s = value.Split(',');
- int iii = 0;
- foreach (string a in s)
- {
- n[x].array[iii] = float.Parse(a);
- }
- }
- }
- else if (v[dList[x].i] == "double array")
- {
- n[x].doubleArray = true;
- if (name == n[x].name)
- {
- string[] s = value.Split(',');
- int iii = 0;
- foreach (string a in s)
- {
- n[x].array[iii] = double.Parse(a);
- }
- }
- }
- else if (v[dList[x].i] == "bool array")
- {
- n[x].boolArray = true;
- if (name == n[x].name)
- {
- string[] s = value.Split(',');
- int iii = 0;
- foreach (string a in s)
- {
- n[x].array[iii] = bool.Parse(a);
- }
- }
- }
- }
- }
- dItemListD.Add(code, n);
- }
- return dItemListD;
- }
- public Transform SpawnItem(string code, Vector3 pos, Quaternion rot)
- {
- code = code.ToLower();
- Transform o = null;
- if (dItemList.ContainsKey(code))
- {
- List<itemD> ob = dItemList[code];
- foreach (itemD id in ob)
- {
- if (o != null)
- {
- if (id.name == "name")
- o.name = (string)id.value;
- }
- else
- {
- if (id.name == "prefab")
- o = Instantiate(Resources.Load<Transform>(ItemsPatch + (string)id.value), pos, rot);
- }
- }
- Item d = o.gameObject.AddComponent<Item>();
- d.Send(ob);
- }
- else
- {
- Debug.LogError("Item '" + code + "' dont exists in DB!");
- }
- return o;
- }
- public Transform SpawnRandomSalingItem(Vector3 pos, Quaternion rot)
- {
- string code = saling[Random.Range(0, saling.Count)];
- Transform o = null;
- List<itemD> ob = dItemList[code];
- foreach (itemD id in ob)
- {
- if (o != null)
- {
- if (id.name == "name")
- o.name = (string)id.value;
- }
- else
- {
- if (id.name == "prefab")
- {
- o = Instantiate(Resources.Load<Transform>(ItemsPatch + (string)id.value), pos, rot);
- break;
- }
- }
- }
- Item d = o.gameObject.AddComponent<Item>();
- d.Send(ob);
- return o;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement