Tanoda
ToolPositioner.cs
Go to the documentation of this file.
1using System.Collections;
2using System.Collections.Generic;
3using System.Linq;
4using GILES;
5using UnityEngine;
6#if !UNITY_WEBGL
7using Valve.VR.InteractionSystem;
8
9#endif
10
11public class ToolPositioner : MonoBehaviour
12{
13 public static ToolPositioner instance;
14 public GameObject[] tools;
15 public Material RimLight;
16 private readonly Dictionary<string, GameObject> genericTools = new Dictionary<string, GameObject>();
17 public Transform ToolParent;
18
19 private void Awake()
20 {
21 if (!instance)
22 instance = this;
23
24 foreach (var tool in tools)
25 {
26 tool.SetActive(false);
27 }
28 }
29
30 public void hideAllTool()
31 {
32 if (!instance)
33 instance = this;
34
35 foreach (var tool in tools)
36 {
37 tool.SetActive(false);
38 }
39 }
40 public bool IsTool(GameObject tool)
41 {
42 return tools.Contains(tool) || genericTools.ContainsValue(tool) || tool.GetComponent<ToolGO>();
43 }
44
45 public void EditToolPositions()
46 {
48 pb_Scene.instance.EditToolsToggle(tools);
49 }
50
51 public GameObject GetToolByName(string tool)
52 {
53 foreach (var t in tools)
54 {
55 if (t.name == tool)
56 {
57 return t;
58 }
59 }
60
61 foreach (var item in genericTools)
62 {
63 if (item.Value.name == tool)
64 {
65 return item.Value;
66 }
67 }
68
69 return null;
70 }
71
72 public GameObject GetToolBlue(GameObject tool)
73 {
74 var allBlue = ToolParent.GetComponentsInChildren<CollisionEvents>();
75 foreach (var blue in allBlue)
76 {
77 if (blue.name.Contains(tool.name)) return blue.gameObject;
78 }
79 var fallback = GameObject.Find(tool.name + "_blue");
80 return null;
81 }
82
83 public GameObject GetGenericTool(string toolHash)
84 {
85 if (genericTools.ContainsKey(toolHash))
86 {
87 return genericTools[toolHash];
88 }
89 else
90 {
91 var tool = HashingManager.instance.GetGOFromHash(toolHash);
92 genericTools.Add(toolHash, tool);
93
94
95 var blue = Instantiate(tool);
96 RecursiveBlueJob(blue.transform);
97 blue.SetActive(false);
98 blue.name = tool.name + "_blue";
99 var ltm = blue.AddComponent<LerpToMe>();
100 var ce = blue.AddComponent<CollisionEvents>();
101 var coll = blue.AddComponent<BoxCollider>();
102
103 var bounds = new Bounds (transform.position, Vector3.one);
104 var renderers = GetComponentsInChildren<Renderer>();
105 foreach (Renderer renderer in renderers)
106 {
107 bounds.Encapsulate (renderer.bounds);
108 }
109
110 coll.center = bounds.center;
111 coll.size = bounds.size;
112
113 var isStatic = tool.GetComponent<StaticGO>();
114 var hasRigidbody = tool.GetComponent<Rigidbody>();
115 var hasOffsetter = tool.GetComponent<OffsetHolder>();
116#if !UNITY_WEBGL
117 var hasInteractable = tool.GetComponent<Interactable>();
118 var hasThrowable = tool.GetComponent<ThrowableCanDisable>();
119
120#endif
121 if (isStatic)
122 {
123 Destroy(isStatic);
124 }
125
126 if (!hasRigidbody)
127 {
128 tool.gameObject.AddComponent<Rigidbody>().isKinematic = true;
129 }
130 if (!hasOffsetter)
131 {
132 tool.gameObject.AddComponent<OffsetHolder>();
133 }
134
135#if !UNITY_WEBGL
136 if (!hasInteractable)
137 {
138 tool.gameObject.AddComponent<Interactable>();
139 }
140
141 if (!hasThrowable)
142 {
143 tool.gameObject.AddComponent<ThrowableCanDisable>();
144 }
145#endif
146 return tool;
147 }
148 }
149
150 private void RecursiveBlueJob(Transform parent)
151 {
152 var mr = parent.GetComponent<MeshRenderer>();
153 var coll = parent.GetComponent<Collider>();
154 if (mr)
155 {
156 var matnum = mr.materials.Length;
157 for (int i = 0; i < matnum; i++)
158 {
159 mr.materials[i] = RimLight;
160 }
161 }
162
163 if (coll)
164 {
165 Destroy(coll);
166 }
167 foreach (Transform c in parent)
168 {
169 RecursiveBlueJob(c);
170 }
171 }
172}
static void Clear()
Definition: pb_Selection.cs:61
Definition: ToolGO.cs:6
GameObject GetGenericTool(string toolHash)
Material RimLight
Transform ToolParent
GameObject GetToolByName(string tool)
GameObject[] tools
GameObject GetToolBlue(GameObject tool)
bool IsTool(GameObject tool)
static ToolPositioner instance
void EditToolPositions()