public void addBranchesToLimb(TreePart limb, int limbRemaining) { int branchSections = (int)(limbRemaining * species.averageBranchLength + (random.NextDouble() * species.branchLengthVariation - species.branchLengthVariation / 2)); Branch newBranch = new Branch(this, species.getRect(species.weightedBranchRects, random)); limb.addPart(newBranch); float initialRot = (float)(random.NextDouble() * (Math.PI * 2f)); newBranch.rotation = initialRot; newBranch.performSetup(); for (int i = 0; i < branchSections; i++) { Branch newSection = new Branch(this, species.getRect(species.weightedBranchRects, random)); newBranch.findAnyEnd().addPart(newSection); newSection.rotation = ((float)(random.NextDouble() * 0.5 - 0.25f)); newSection.performSetup(); } Rectangle leafSprite = species.getRect(species.weightedLeafRects, random); LeafCluster leafCluster = new LeafCluster(this, leafSprite, species.getColor(species.weightedLeafColors, leafSprite, random)); newBranch.findAnyEnd().addPart(leafCluster); leafCluster.depth -= (float)(random.NextDouble() + 0.5); leafCluster.performSetup(); Logger.log("Leaf cluster is on " + (leafCluster.left ? "left" : "right")); }
public virtual float findTotalRotationOfChild(TreePart part) { if (!this.isParentOf(part)) { return(0f); } return(findTotalRotationInternal(part)); }
public void addLimbToPart(TreePart part, bool left, int sectionsUp, int sectionsDown, float rotationOverride = float.MinValue) { Logger.log("Limb was on the " + (left ? "left" : "right")); Limb newLimb = new Limb(this, species.getRect(species.weightedLimbRects, random)); float initialRot; if (rotationOverride == float.MinValue) { initialRot = species.limbAngle * (left ? -1 : 1); } else { initialRot = rotationOverride; } float depth = (float)(random.NextDouble() * 2 + 1) * (random.Next(2) == 0 ? -1 : 1); newLimb.depth = depth; newLimb.rotation = initialRot; part.addPart(newLimb); newLimb.left = left; newLimb.performSetup(); int sectionsToGrow = (int)(sectionsDown * species.limbGrowth); Logger.log("Growing " + sectionsToGrow + " sections on limb..."); for (int i = 0; i < sectionsToGrow; i++) { Limb newSection = new Limb(this, species.getRect(species.weightedLimbRects, random)); newLimb.findAnyEnd(new Type[] { typeof(Limb) }).addPart(newSection); newSection.rotation = ((float)(random.NextDouble() * 0.5 - 0.25f)); newSection.performSetup(); if (i >= species.minBranchDistance && random.NextDouble() < species.branchFrequency) { Logger.log("Adding branch to limb section " + i); if (species.limbAlternate) { addBranchesToLimb(newSection, sectionsToGrow - i); } else { addBranchesToLimb(newSection, sectionsToGrow - i); addBranchesToLimb(newSection, sectionsToGrow - i); } //sectionsBetweenLimbs = (int)(averageLimbInterval + (random.NextDouble() * limbIntervalVariation) - (limbIntervalVariation / 2)); } } //newLimb.addGravity(true); addBranchesToLimb(newLimb.findAnyEnd(new Type[] { typeof(Limb) }), sectionsToGrow / 2); }
public virtual bool isParentOf(TreePart part) { if (this == part) { return(true); } foreach (TreePart child in children) { if (child.isParentOf(part)) { return(true); } } return(false); }
internal virtual float findTotalRotationInternal(TreePart part) { float totalChildRotation = 0f; if (part == this) { totalChildRotation += rotation; //Logger.log("Part found: total rotation now rotation of child: " + totalChildRotation); } foreach (TreePart child in children) { totalChildRotation += child.findTotalRotationInternal(part); //Logger.log("Total rotation now " + totalChildRotation); } if (totalChildRotation != 0f) { totalChildRotation += rotation; } return(totalChildRotation); }
public virtual void addPart(TreePart part) { if (children.Contains(part)) { Logger.log("Attempted to add already-existing part."); return; } if (this.isParentOf(part)) { Logger.log("Attempted to add a child to a parent that already contained it!"); return; } if (part.isParentOf(this)) { Logger.log("Attempted to add a parent as a child!"); return; } part.depth = depth; part.left = left; Logger.log("Part is on " + (part.left ? "left" : "right") + ", added to parent on " + (left ? "left" : "right")); children.Add(part); }
public virtual int getHeightOf(TreePart goal, Type[] ignoreTypes = null) { foreach (TreePart child in children) { int thisValue = 0; if (ignoreTypes == null || !ignoreTypes.Contains(this.GetType())) { //Logger.log("This part was " + this.GetType().ToString() + ", which was acceptable."); thisValue = 1; } if (child == goal) { //Logger.log("Child was goal, result is now " + (1 + thisValue)); return(1 + thisValue); } int childHeight = child.getHeightOf(goal, ignoreTypes); if (childHeight >= 1) { //Logger.log("Running total was greater than 0. Total is now " + (childHeight + thisValue)); return(childHeight + thisValue); } } return(-1); }
public void buildStructure() { treeStructure = new Stump(this, species.getRect(species.weightedStumpRects, random)); int trunkLength = species.minHeight + (seed % species.heightVariation); Logger.log("Tree is " + trunkLength + " units tall."); int firstLimbHeight = (int)(species.minLimbHeight + random.NextDouble() * species.minLimbHeightVariation); //int sectionsBetweenLimbs = 0; if (species.form == Species.FORM_OPEN) { for (int i = 0; i < trunkLength; i++) { Trunk newSection = new Trunk(this, species.getRect(species.weightedTrunkRects, random)); newSection.rotation = ((float)random.NextDouble() * 1f) - 0.5f; //newSection.rotation = 0.3f; treeStructure.findAnyEnd(new Type[] { typeof(Trunk), typeof(Stump) }).addPart(newSection); newSection.performSetup(); //&& i - firstLimbHeight >= sectionsBetweenLimbs if (i >= firstLimbHeight && random.NextDouble() <= species.limbFrequency) { Logger.log("Adding limbs to section " + i); if (species.limbAlternate) { addLimbToPart(newSection, random.Next(2) == 0, i, trunkLength - i); } else { addLimbToPart(newSection, true, i, trunkLength - i); addLimbToPart(newSection, false, i, trunkLength - i); } //sectionsBetweenLimbs = (int)(averageLimbInterval + (random.NextDouble() * limbIntervalVariation) - (limbIntervalVariation / 2)); } //else if (i >= firstLimbHeight) //{ // sectionsBetweenLimbs--; //} } addLimbToPart(treeStructure.findAnyEnd(new Type[] { typeof(Trunk), typeof(Stump) }), random.Next(2) == 0, trunkLength, species.minHeight / 2); } else if (species.form == Species.FORM_CONICAL) { for (int i = 0; i < trunkLength; i++) { if (i == trunkLength - 1) { Logger.log("Trunk has become vertical limb..."); addLimbToPart(treeStructure.findAnyEnd(new Type[] { typeof(Trunk), typeof(Stump) }), random.Next(2) == 0, trunkLength, 2, 0f); } else if (species.limbGrowth * (trunkLength - i) <= 1 || i >= trunkLength - 2) { Trunk newSection = new Trunk(this, species.getRect(species.weightedLimbRects, random)); newSection.rotation = ((float)random.NextDouble() * 1f) - 0.5f; treeStructure.findAnyEnd(new Type[] { typeof(Trunk), typeof(Stump) }).addPart(newSection); newSection.performSetup(); Logger.log("Adding branches to small section " + i); addBranchesToLimb(newSection, trunkLength - i); } else if (i >= firstLimbHeight && random.NextDouble() <= species.limbFrequency) { Trunk newSection = new Trunk(this, species.getRect(species.weightedTrunkRects, random)); newSection.rotation = ((float)random.NextDouble() * 1f) - 0.5f; treeStructure.findAnyEnd(new Type[] { typeof(Trunk), typeof(Stump) }).addPart(newSection); newSection.performSetup(); Logger.log("Adding limb to section " + i); if (species.limbAlternate) { addLimbToPart(newSection, random.Next(2) == 0, i, trunkLength - i); } else { addLimbToPart(newSection, true, i, trunkLength - i); addLimbToPart(newSection, false, i, trunkLength - i); } } } } else if (species.form == Species.FORM_LINEAR) { for (int i = 0; i < trunkLength; i++) { Trunk newSection = new Trunk(this, species.getRect(species.weightedTrunkRects, random)); newSection.rotation = ((float)random.NextDouble() * species.maxTrunkWobble) - (species.maxTrunkWobble / 2f); treeStructure.findAnyEnd(new Type[] { typeof(Trunk), typeof(Stump) }).addPart(newSection); newSection.performSetup(); if (i == trunkLength - 1) { Rectangle leafSprite = species.getRect(species.weightedLeafRects, random); LeafCluster leafCluster = new LeafCluster(this, leafSprite, species.getColor(species.weightedLeafColors, leafSprite, random)); leafCluster.left = random.NextDouble() >= 0.5; newSection.addPart(leafCluster); leafCluster.depth -= (float)(random.NextDouble() + 0.5); leafCluster.performSetup(); } } } else if (species.form == Species.FORM_SPREADING) { for (int i = 0; i < trunkLength; i++) { List <TreePart> currentEnds = treeStructure.findAllEnds(new Type[] { typeof(Trunk), typeof(Stump) }); foreach (TreePart currentEnd in currentEnds) { float splitChance = species.limbFrequency / currentEnds.Count; } } } }