public virtual void addGravity(bool addToChildren = false) { float massHeld = getExtent() / 10f; float distance = renderer.treeStructure.getHeightOf(this, new Type[] { typeof(Stump), typeof(Trunk) }) / 10f; float parentRotation = renderer.treeStructure.findTotalRotationOfChild(this) - rotation; Logger.log("Limb has a weight of " + massHeld + ", and a distance of " + distance); float rotationFactor = Math.Max((massHeld * (1 - renderer.species.limbStregnth)) - (distance * (1 - renderer.species.limbStregnth) * 0.5f), 0f); Logger.log("Rotation factor = " + rotationFactor + "; Max(" + massHeld + " * " + renderer.species.limbStregnth + ") - (" + distance + " * " + renderer.species.limbStregnth + " * 0.5), 0)"); //float newRot = ((rotationFactor * TreeRenderer.degreesToRadians(180f)) + rotation) / 2f - parentRotation; float rotDifference = TreeRenderer.degreesToRadians(180f) - (rotation + parentRotation); float newRot = (rotDifference * rotationFactor) + rotation; Logger.log("Rotating to " + TreeRenderer.radiansToDegrees(newRot) + " degrees..."); rotation = newRot; if (addToChildren) { foreach (TreePart child in children) { if (child is Limb) { (child as Limb).addGravity(addToChildren); } } } }
public void loadFromJSON(int treeType) { JObject treeJSON; try { treeJSON = Loader.load <JObject>("Trees/Tree_" + treeType + ".json", "Content/Trees/Tree_" + treeType + ".json") as JObject; } catch (Microsoft.Xna.Framework.Content.ContentLoadException e) { Logger.log("No tree json file found at Trees/Tree_" + treeType + "!", LogLevel.Error); if (treeType != 1) { Logger.log("Trying to default to Tree_1..."); loadFromJSON(1); return; } else { throw e; } } Logger.log("Successfully located the tree json file for Tree_" + treeType); if (!treeJSON.ContainsKey("Format") || !treeJSON.ContainsKey("Structure") || !treeJSON.ContainsKey("Graphics")) { throw new KeyNotFoundException("Tree json is missing a vital field! Missing:" + (treeJSON.ContainsKey("Format") ? " Format" : "") + (treeJSON.ContainsKey("Structure") ? " Structure" : "") + (treeJSON.ContainsKey("Graphics") ? " Graphics" : "")); } float format = Convert.ToSingle(treeJSON["Format"].ToString()); if (format != 1.0f) { throw new Exception("Format " + format + " not expected! No such format currently exists. Maybe you need to update Map Utilites?"); } JObject graphics = treeJSON["Graphics"] as JObject; string imageName = graphics["Sheet"].ToString(); try { treeSheet = Loader.load <Texture2D>("Trees/" + imageName, "Content/Trees/" + imageName + ".png") as Texture2D; } catch (Microsoft.Xna.Framework.Content.ContentLoadException e) { Logger.log("Could not find tree sprite sheet '" + imageName + "', as defined in Tree_" + treeType + ".json!", LogLevel.Error); throw e; } makeWeightedRectsFromJSON(graphics["Stump"] as JArray, ref weightedStumpRects, ref weightedStumpColors); makeWeightedRectsFromJSON(graphics["Trunk"] as JArray, ref weightedTrunkRects, ref weightedTrunkColors); makeWeightedRectsFromJSON(graphics["Limb"] as JArray, ref weightedLimbRects, ref weightedLimbColors); makeWeightedRectsFromJSON(graphics["Branch"] as JArray, ref weightedBranchRects, ref weightedBranchColors); makeWeightedRectsFromJSON(graphics["Leaf"] as JArray, ref weightedLeafRects, ref weightedLeafColors); JObject structure = treeJSON["Structure"] as JObject; if (structure.ContainsKey("Trunk")) { JObject trunk = (structure["Trunk"] as JObject); tryApplyInt(ref minHeight, "Min Height", trunk); tryApplyInt(ref heightVariation, "Height Variation", trunk); tryApplyFloat(ref maxTrunkWobble, "Max Angle", trunk); maxTrunkWobble = TreeRenderer.degreesToRadians(maxTrunkWobble); tryApplyBool(ref limbAlternate, "Alternating", trunk); if (trunk.ContainsKey("Form")) { switch (trunk["Form"].ToString().ToLower().Substring(0, 2)) { case "op": form = FORM_OPEN; break; case "co": form = FORM_CONICAL; break; case "sp": form = FORM_SPREADING; break; case "li": form = FORM_LINEAR; break; } } } if (structure.ContainsKey("Limb")) { JObject limb = (structure["Limb"] as JObject); tryApplyFloat(ref limbFrequency, "Frequency", limb); tryApplyFloat(ref limbGrouping, "Grouping", limb); tryApplyFloat(ref limbGroupVariation, "Grouping Variation", limb); tryApplyInt(ref minLimbHeight, "Distance", limb); tryApplyFloat(ref minLimbHeightVariation, "Distance Variation", limb); tryApplyFloat(ref averageLimbInterval, "Interval", limb); tryApplyFloat(ref limbIntervalVariation, "Interval Variation", limb); tryApplyFloat(ref limbAngle, "Angle", limb); limbAngle = TreeRenderer.degreesToRadians(limbAngle); tryApplyFloat(ref limbGrowth, "Length Factor", limb); tryApplyFloat(ref limbGrowthVariation, "Length Variation", limb); } if (structure.ContainsKey("Branch")) { JObject branch = (structure["Branch"] as JObject); tryApplyFloat(ref branchFrequency, "Frequency", branch); tryApplyInt(ref minBranchDistance, "Distance", branch); tryApplyFloat(ref minBranchDistanceVariation, "Distance Variation", branch); tryApplyFloat(ref branchWhorl, "Whorling", branch); tryApplyFloat(ref averageBranchLength, "Length Factor", branch); tryApplyFloat(ref branchLengthVariation, "Length Variation", branch); } }