public static void CreateSegment(Vector3 startPosTemp, Vector3 prevStartPosTemp, float angleTemp, Vector2 scaleTemp, int depthTemp, string name, GameObject curObj, GameObject treeMast, GameObject spawnObj, Tree_Segment_Script.TreeSegmentType treeinfo) { Quaternion rot = Quaternion.Euler(0, 0, angleTemp); Tree_Segment_Script curTreeSeg = curObj.GetComponent <Tree_Segment_Script>(); if (!curTreeSeg.GetComponent <Segment_GrowthState>().hitEnd&& !curTreeSeg.GetComponent <Tree_Segment_Script>().isMasterBranch&& curTreeSeg.GetComponent <Segment_GrowthState>().treeStages == Segment_GrowthState.TreeStage.GROWING) { curTreeSeg.masterSegment.GetComponent <Tree_Segment_Script>().masterSegGroup.GetComponent <Tree_Master_SegmentGroup_Info>().SegmentsList.Add(curTreeSeg.gameObject); curTreeSeg.transform.parent = curTreeSeg.masterSegment.GetComponent <Tree_Segment_Script>().masterSegGroup.transform; if (curTreeSeg.pos != null) { curTreeSeg.pos.GetComponent <PositionManager>().Explode(); curTreeSeg.pos = null; } Tree_Master.growthList.Remove(curTreeSeg.gameObject); } if (scaleTemp.y <= TreeParms.minScale && curObj.GetComponent <Segment_GrowthState>().treeStages == Segment_GrowthState.TreeStage.GROWING || scaleTemp.y >= curObj.GetComponent <Segment_GrowthState>().growthSplitPoint.y&& curObj.GetComponent <Segment_GrowthState>().treeStages == Segment_GrowthState.TreeStage.SPURT) { if (depthTemp >= maxDepth && curTreeSeg.treeSegmentTypes == Tree_Segment_Script.TreeSegmentType.BRANCH) { CreateLeaf(curTreeSeg.angle, curTreeSeg.transform.position, curObj); Tree_Master.growthList.Remove(curTreeSeg.gameObject); return; } CreateLeaf(curTreeSeg.angle, curTreeSeg.transform.position, curObj); curObj.GetComponent <Segment_GrowthState>().hitEnd = true; return; //done with this 'leaf'! } GameObject obj = (GameObject)GameObject.Instantiate(spawnObj, startPosTemp, rot); obj.name = name; GameObject anchrObj = (GameObject)GameObject.Instantiate(Tree_Master.anchorPoint, startPosTemp, rot); PositionManager anchor = anchrObj.GetComponent <PositionManager>(); if (depthTemp == 1 && treeinfo == Tree_Segment_Script.TreeSegmentType.BRANCH) { anchrObj.transform.name = "MASTER_Anchor" + depthTemp + " " + obj.name; } else { anchrObj.transform.name = "Anchor" + depthTemp + " " + obj.name; } Tree_Segment_Script segMan = obj.GetComponent <Tree_Segment_Script>(); anchor.startPosTemp = startPosTemp; anchor.angleTemp = angleTemp; anchor.prevSegment = curObj.transform; anchor.curChild = obj.transform; anchor.trunkTopOffset = trunkTopOffset; segMan.startPos = startPosTemp; segMan.angle = angleTemp; segMan.scale = new Vector2(scaleTemp.x, scaleTemp.y); segMan.depth = depthTemp; segMan.masterSegment = curObj.GetComponent <Tree_Segment_Script>().masterSegment; segMan.master = treeMast.GetComponent <Tree_Master>(); segMan.pos = anchrObj.transform; segMan.prevSeg = curObj.transform; segMan.GetComponentInChildren <SpriteRenderer>().sortingOrder = -depthTemp; //Current Branches Position and Rotation Point segMan.transform.parent = anchrObj.transform; anchrObj.transform.parent = Tree_Master.StartSeg.transform; if (treeinfo == Tree_Segment_Script.TreeSegmentType.BRANCH) { segMan.treeSegmentTypes = Tree_Segment_Script.TreeSegmentType.BRANCH; segMan.transform.name = "Branch" + "_" + depthTemp; } else if (treeinfo == Tree_Segment_Script.TreeSegmentType.TRUNK) { segMan.treeSegmentTypes = Tree_Segment_Script.TreeSegmentType.TRUNK; segMan.transform.name = "Trunk" + "_" + depthTemp; } // Tree_Master.AddSegment(obj); Tree_Master.growthList.Add(obj); Tree_Master.allTreeSegment.Add(obj); Tree_Master.anchorList.Add(anchrObj); //Control of Current Segments Growth Segment_GrowthState growthState = obj.GetComponent <Segment_GrowthState>(); growthState.growthSplitPoint = new Vector2(scaleTemp.x, scaleTemp.y); growthState.prevSeg = curObj.transform; growthState.depth = depthTemp; }
// Update is called once per frame void Update() { currentTime += Time.deltaTime; if (Tree_Master.growthList.Count != 0) { for (int i = 0; i < Tree_Master.growthList.Count; i++) { //Grabbing Current Tree Segment's Growth Information //Incriments Threw and Increases Scale According to Current Segment State //Spurt = Newly Created //Growth = Global Growth Rate For all Segments //Paused = Stops Growing Segment_GrowthState trunkGrowthState = Tree_Master.growthList[i].GetComponent <Segment_GrowthState>(); Tree_Segment_Script trunkSeginfo = Tree_Master.growthList[i].GetComponent <Tree_Segment_Script>(); Vector2 tempVector = new Vector2(trunkGrowthState.transform.localScale.x, trunkGrowthState.transform.localScale.y); if (currentTime >= spawnTime) { //========================================== //Creation of Master Branches at set intervals //========================================== curIndex = Random.Range(0, Tree_Master.treeTrunkList.Count - 1); if (curIndex > Tree_Master.treeTrunkList.Count - 1 || curIndex < 0) { curIndex = 0; } GameObject branchCreator = Tree_Master.treeTrunkList[curIndex].gameObject; branchCreator.GetComponent <Tree_Segment_Script>().createBranch = true; Tree_Master.creatorListMaster.Add(branchCreator); currentTime = 0; } if (SessionManager.instance != null) { if (currentTime >= SessionManager.instance.SessionTime_minutes * 60) { trunkGrowthState.treeStages = Segment_GrowthState.TreeStage.PUASED; } } else { } //controls splitting when a segment has reached a minimum scale if (trunkGrowthState.hitEnd && trunkGrowthState.transform.localScale.y >= trunkGrowthState.growthSplitPoint.y && !trunkSeginfo.isTree) { trunkSeginfo.nextSegmentMade = false; trunkGrowthState.hitEnd = false; Tree_Master.creatorListMaster.Add(trunkSeginfo.gameObject); } //This will just toggle between the first two results after the scale meets the condition, is this intended? if (trunkGrowthState.transform.localScale.y >= trunkGrowthState.growthSplitPoint.y && !trunkGrowthState.hitEnd && !trunkSeginfo.isTree) { Tree_Master.creatorListMaster.Add(trunkSeginfo.gameObject); trunkGrowthState.treeStages = Segment_GrowthState.TreeStage.GROWING; trunkGrowthState.growthSplitPoint = new Vector2(trunkGrowthState.growthSplitPoint.x, trunkGrowthState.growthSplitPoint.y / 2); } if (trunkGrowthState.growthSplitPoint.y < TreeParms.minScale && trunkGrowthState.treeStages == Segment_GrowthState.TreeStage.GROWING && !trunkSeginfo.isTree || trunkGrowthState.transform.localScale.y > trunkGrowthState.growthSplitPoint.y && trunkGrowthState.treeStages == Segment_GrowthState.TreeStage.GROWING && !trunkSeginfo.isTree) { } if (trunkGrowthState.treeStages == Segment_GrowthState.TreeStage.SPURT) { //If the Segment Hits its Current Split Point //It will Slow Down and Create a new Segment // trunkSeginfo.GetComponent<SpriteRenderer>().color = new Color (trunkSeginfo.GetComponent<SpriteRenderer>().color.r, // trunkSeginfo.GetComponent<SpriteRenderer>().color.g, // trunkSeginfo.GetComponent<SpriteRenderer>().color.b, // (float)tempVector.y * 2); tempVector.y = Mathf.Lerp(tempVector.y, tempVector.y + TreeParms.scaleChange, Time.deltaTime * TreeParms.growthRate.y); tempVector.x = Mathf.Lerp(tempVector.x, trunkGrowthState.transform.localScale.y, Time.deltaTime * TreeParms.growthRate.y); trunkGrowthState.transform.localScale = new Vector3(tempVector.x, tempVector.y, 1); } if (trunkSeginfo.isMasterBranch && trunkGrowthState.treeStages == Segment_GrowthState.TreeStage.GROWING) { Vector2 tempVector2 = new Vector2(trunkSeginfo.masterSegGroup.transform.localScale.x, trunkSeginfo.masterSegGroup.transform.localScale.y); tempVector2.y = Mathf.Lerp(tempVector2.y, tempVector2.y + TreeParms.scaleChange, Time.deltaTime * TreeParms.globalGrowth.y); tempVector2.x = Mathf.Lerp(tempVector2.x, trunkSeginfo.masterSegGroup.transform.localScale.y, (Time.deltaTime * TreeParms.globalGrowth.y) / 2); trunkSeginfo.masterSegGroup.transform.localScale = new Vector3(tempVector2.x, tempVector2.y, 1); } else { if (trunkGrowthState.treeStages == Segment_GrowthState.TreeStage.GROWING && trunkSeginfo.transform.parent != trunkSeginfo.masterSegment.GetComponent <Tree_Segment_Script>().masterSegGroup&& trunkGrowthState.hitEnd) { tempVector.y = Mathf.Lerp(tempVector.y, tempVector.y + TreeParms.scaleChange, Time.deltaTime * TreeParms.globalGrowth.y); tempVector.x = Mathf.Lerp(tempVector.x, trunkGrowthState.transform.localScale.y, Time.deltaTime * TreeParms.globalGrowth.y); trunkGrowthState.transform.localScale = new Vector3(tempVector.x, tempVector.y, 1); } } } } }
void Awake() { growthMan = GetComponent<Segment_GrowthState>(); growthMan.curSegment = this.gameObject; }
void Awake() { growthMan = GetComponent <Segment_GrowthState>(); growthMan.curSegment = this.gameObject; }