public MicroBot(MicroBot parentBot, Transform botTransform) { this.parentBot = parentBot; this.botTransform = botTransform; childrenBots = new List <MicroBot>(); this.isRendering = false; }
private void Start() { // playerObject = GameObject.FindWithTag("Player").transform; allSphereBots = new List <MicroBot>(); renderedBots = new List <MicroBot>(); if (TryGetComponent(out Renderer _r)) { var newRoot = new MicroBot(null, transform, _r.material.color); allSphereBots = newRoot.GetBFS(); renderedBots = newRoot.GetRenderedBots(); } else { var newRoot = new MicroBot(null, transform); allSphereBots = newRoot.GetBFS(); renderedBots = newRoot.GetRenderedBots(); } Debug.Log($"Rendered bots count = {renderedBots.Count}"); originalRadius = Vector3.Distance(transform.position, renderedBots[renderedBots.Count - 1].botTransform.position); directions = new Vector3[renderedBots.Count]; for (int i = 0; i < renderedBots.Count; i++) { directions[i] = transform.position - renderedBots[i].botTransform.position; } }
public static List <MicroBot> ReverseOrderTraversal(this MicroBot rootBot) { List <MicroBot> finalBotList = new List <MicroBot>(); Stack <MicroBot> nodeStack = new Stack <MicroBot>(); Queue <MicroBot> nodeQueue = new Queue <MicroBot>(); nodeQueue.Enqueue(rootBot); while (nodeQueue.Count > 0) { var temp = nodeQueue.Dequeue(); nodeStack.Push(temp); foreach (var childBot in temp.childrenBots) { nodeQueue.Enqueue(childBot); } } while (nodeStack.Count > 0) { var temp = nodeStack.Pop(); finalBotList.Add(temp); } return(finalBotList); }
public static void FillTreeFromRoot(this MicroBot rootBot) { MicroBot[] tempChildNodes = new MicroBot[rootBot.botTransform.childCount]; //instant child count int i = 0; foreach (Transform directChildTransform in rootBot.botTransform) { MicroBot childNode; if (!directChildTransform.TryGetComponent(out Renderer _r)) { _r = directChildTransform.gameObject.AddComponent <MeshRenderer>(); _r.enabled = false; } if (_r.enabled) { childNode = new MicroBot(rootBot, directChildTransform, _r.sharedMaterial.color); //Init all child nodes with this transform as parent } else { childNode = new MicroBot(rootBot, directChildTransform); } tempChildNodes[i++] = childNode; childNode.FillTreeFromRoot(); //Start recursion } rootBot.childrenBots = tempChildNodes.ToList(); }
public MicroBot(MicroBot parentBot, Transform botTransform, Color botColor) { this.parentBot = parentBot; this.botTransform = botTransform; this.botColor = botColor; childrenBots = new List <MicroBot>(); this.isRendering = true; }
public static List <MicroBot> GetRenderedBots(this MicroBot root) { root.FillTreeFromRoot(); List <MicroBot> renderedBots = root.GetBFS(); Debug.Log($"Getting the rendered bots -- {renderedBots.Count}"); for (int i = renderedBots.Count - 1; i >= 0; i--) { if (renderedBots[i].botTransform.GetComponent <Renderer>().enabled == false) { renderedBots.RemoveAt(i++); } } Debug.Log($"Updated count -- {renderedBots.Count}"); return(renderedBots); }
// ReSharper disable once MemberCanBePrivate.Global public static MicroBot DeepCopyTree(MicroBot bot, MicroBot parent = null) { if (bot == null) { return(null); } var newBot = new MicroBot(parent, bot.botTransform, bot.botColor); foreach (var childBot in bot.childrenBots) { newBot.childrenBots.Add(DeepCopyTree(childBot, newBot)); } return(newBot); }
public static List <MicroBot> GetBFS(this MicroBot rootBot) { List <MicroBot> finalBotList = new List <MicroBot>(); Queue <MicroBot> botQueue = new Queue <MicroBot>(); botQueue.Enqueue(rootBot); finalBotList.Add(rootBot); while (botQueue.Count > 0) { var tempBot = botQueue.Dequeue(); foreach (var childBot in tempBot.childrenBots) { finalBotList.Add(childBot); botQueue.Enqueue(childBot); } } return(finalBotList); }
public void UpdateSO() { MicroBot rootBot; if (prefabRootObject.TryGetComponent(out Renderer _r)) { rootBot = new MicroBot(null, prefabRootObject.transform, prefabRootObject.GetComponent <Renderer>().sharedMaterial.color); } else { rootBot = new MicroBot(null, prefabRootObject.transform); } rootBot.FillTreeFromRoot(); targetSO.rootBot = rootBot; targetSO.botsInShape = prefabRootObject.GetComponentsInChildren <Renderer>().Length; targetSO.bonesInShape = prefabRootObject.GetComponentsInChildren <Transform>().Length - targetSO.botsInShape; }
public MicroBot GetRootNode() { var rootNode = new MicroBot(null, rootTransform, rootTransform.GetComponent <Renderer>().material.color); return(rootNode); }