Tanoda
WeldAction.cs
Go to the documentation of this file.
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using System.Linq;
5using System.Runtime.Serialization;
6using JetBrains.Annotations;
7using NaughtyAttributes;
8using UnityEngine;
9
10public class WeldAction : ActionObject, ISerializable
11{
12 private GameObject weldedGO;
13 private List<Transform> originalParents = new List<Transform>();
14 private List<Vector3> originalPositions = new List<Vector3>();
15 private List<Vector3> originalRotations = new List<Vector3>();
16 private GameObject[] cachedList;
17 private bool welded = false;
18
19 public override void Triggered(string id)
20 {
21 if (id == GetInput()) return;
22 base.Triggered(id);
23 var latereparent = true;
24 //Debug.Break();
25 if (latereparent)
26 {
27 StartCoroutine(LateReparent());
28 }
29 else
30 {
31 Reparent();
32 TriggerOutput(outPuts[0].name);
33 }
34 }
35
36 public override void Start()
37 {
38 base.Start();
39 originalParents = new List<Transform>();
40 originalPositions = new List<Vector3>();
41 originalRotations = new List<Vector3>();
42 if (cachedList == null) cachedList = GetInputGOs();
43 }
44
45 IEnumerator LateReparent()
46 {
47 yield return new WaitForEndOfFrame();
48 Reparent();
49 TriggerOutput(outPuts[0].name);
50 }
51
52 internal void Reparent() //Weld
53 {
54 var list = GetInputGOs();
55 cachedList = list;
56
57 if (welded/*originalParents.Count >= cachedList.Length*/)
58 {
59 Debug.LogError("Weld ERROR: double parenting!");
60 return;
61 }
62
63 if (list != null && list.Length > 1)
64 {
65 var parent = list[0].transform;
66 weldedGO = list[0];
67 for (var i = 1; i < list.Length; i++)
68 {
69 if (list[i] == null) continue;
70 originalParents.Add(list[i].transform.parent);
71 originalPositions.Add(list[i].transform.position);
72 originalRotations.Add(list[i].transform.eulerAngles);
73 list[i].transform.SetParent(parent, true);
74 }
75 }
76 welded = true;
77 }
78
79 public void RestoreWeld()
80 {
81 if (cachedList != null)
82 {
83 for (var i = 1; i < cachedList.Length; i++)
84 {
85 if (cachedList[i] == null) continue;
86 cachedList[i].transform.SetParent(originalParents[i - 1], true);
87 cachedList[i].transform.position = originalPositions[i - 1];
88 cachedList[i].transform.eulerAngles = originalRotations[i - 1];
89 }
90
91 originalParents.Clear();
92 originalPositions.Clear();
93 originalRotations.Clear();
94 cachedList = null;
95 welded = false;
96 }
97 }
98
99 [Button]
100 [CanBeNull]
101 GameObject[] GetInputGOs()
102 {
103 try
104 {
106 var retval = new List<GameObject>();
107 foreach (var s in list)
108 {
110 if (conn.toId == inPuts[1].name) continue;
111 var ao = Controller.Instance.GetActionByInOut(conn.fromId);
112 var goname = "UNKNOWN";
113 if (ao is GameObjectAction objectAction)
114 {
115 retval.Add(objectAction.GetGameObject());
116 goname = objectAction.GetGameObject().name;
117 }
118 if (ao is WeldAction weldAction)
119 {
120 if (weldAction == this)
121 continue;
122
123 retval.Add(weldAction.GetGameObject());
124 goname = weldAction.GetGameObject().name;
125 }
126
127 if (ao is ToolAction toolAction)
128 {
129 retval.Add(toolAction.GetGameObject());
130 goname = toolAction.GetGameObject().name;
131 }
132
133 Debug.Log($"Weld requested: \"{s}\" ({goname})");
134 }
135
136 return retval.ToArray();
137 }
138 catch (Exception e)
139 {
140 return null;
141 }
142 }
143
144 [CanBeNull]
145 public string GetInput()
146 {
147 try
148 {
149 return inIDs.Keys.ToList()[0];
150 }
151 catch (Exception)
152 {
153 return null;
154 }
155 }
156
157 public WeldAction(SerializationInfo info, StreamingContext context) : base(info, context)
158 {
159 }
160
161 [CanBeNull]
162 public GameObject GetGameObject()
163 {
164 if (!weldedGO) return GetInputGOs()?[0];
165 return weldedGO;
166 }
167}
UnityEngine.UI.Button Button
Definition: Pointer.cs:7
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
void TriggerOutput(string id)
List< GameObject > inPuts
Definition: ActionObject.cs:18
List< GameObject > outPuts
Definition: ActionObject.cs:19
List< string > GetConnectionsToObject(string id)
Definition: Controller.cs:876
Connection GetConnectionById(string id)
Definition: Controller.cs:871
ActionObject GetActionByInOut(string id)
Definition: Controller.cs:925
static Controller Instance
Definition: Controller.cs:16
GameObject GetGameObject()
Definition: WeldAction.cs:162
override void Start()
Definition: WeldAction.cs:36
void RestoreWeld()
Definition: WeldAction.cs:79
WeldAction(SerializationInfo info, StreamingContext context)
Definition: WeldAction.cs:157
string GetInput()
Definition: WeldAction.cs:145
override void Triggered(string id)
Definition: WeldAction.cs:19