Skip to main content

WoodcutTree

A class in Root.Township, Alta.Woodcutting.

The root of a choppable tree: a physics body holding a tree of WoodcutTreeModel segments joined by connections. The server generates it from a preset, splits off a new WoodcutTree when a segment is chopped through, and syncs the shape to clients.

Constructors​

MemberWhat it is
WoodcutTree()Unity makes components itself; use AddComponent or spawn a prefab instead of new.

Fields​

19 private and internal fields
MemberWhat it is
private WoodcutTreeEvent cutBacking event for Cut.
private Coroutine fallCoroutineThe running FallingWindow coroutine, stopped on the first ground hit.
private float fallVelocitySpeed given to pieces as they tip after a split. 1 by default.
private float fellChopMultiplierDamage multiplier given to models on a non-kinematic (felled) tree. 1.5 by default.
private float forceOffsetUp offset used by Spin for the angular push. 1 by default.
private HealthObject healthObjectBacking field for HealthObject. Its max health is the species damage multiplier; on only while the root has no children.
private ImpactDetector impactDetectorDetector shared by every model in the tree. Its hardness follows the species.
private bool isInWindowTrue while a ground hit counts as a thump.
private static Logger loggerNLog logger for this class.
private WoodcutTreeEvent onThumpBacking event for Thump.
private float popVelocitySpeed the two parts are pushed apart with on a split. 1 by default.
private Distribution spawnDistributionDistribution a preset tree is picked from on first generation when spawnParticular is not set.
private PresetTree spawnParticularBacking field for SpawnParticular, the preset tree to build. Saved with the tree.
private TreeSpecies speciesBacking field for Species. Saved with the tree.
private MethodSync syncMethod sync for the full tree layout. Sent to joining players and after splits.
private MethodSync syncCutMethod sync that tells clients a cut happened.
private float thumpMinimumFallTimeSeconds after initialize before a ground hit counts as a thump. 2 by default.
private float thumpWindowTimeSeconds the thump window stays open. 6 by default.
private Rigidbody treeRigidbodyBacking field for Rigidbody. Its mass follows the root node's mass.

Properties​

MemberWhat it is
HealthObject HealthObject { get; }Health of the last segment; enabled only when the root has no children. Its death destroys the tree.
float Mass { get; }Total mass of all segments, from the root node. 1 if there is no root.
Rigidbody Rigidbody { get; }The tree's rigidbody. Its mass follows Mass.
WoodcutTree.MapNode Root { get; private set; }Node of the bottom segment, or null before the tree is built.
PresetTree SpawnParticular { get; set; }Preset tree to generate. Saved with the tree; picked from the distribution if empty.
TreeSpecies Species { get; set; }Tree species. Setting it updates the impact hardness and the health.
float TotalSize { get; }Number of segments in the tree, from the root node.

Events​

MemberWhat it is
event Action ChoppedRaised when a piece splits off this tree.
event Action<WoodcutTree> CutRaised when a segment of this tree is chopped through. Also fires on clients.
event Action<WoodcutTree, float> PartTookDamageServer: raised with the damage when any segment takes damage.
event Action ReadyForBakeRaised when the segment layout is ready or changed, so the baker can rebuild the visuals.
event Action<WoodcutTree> ThumpRaised when a falling tree hits the ground layer within the thump time window.

Methods​

MemberWhat it is
WoodcutTree.MapNode Add(WoodcutTreeModel model, WoodcutConnection parentConnection, int rotation)Adds a segment model under a parent connection (or as the root), updating mass and size. Re-parenting can split a piece off another tree.
void EditorClear()Destroys the generated segments and clears Root.
void FixTree()Builds the node map from segment models already under the tree, if it has none.
override void Initialize()Sets up the sync messages and starts the fall window for the thump event.
override void InitializeAsServer(SpawnData spawnData)Server: builds the node map, or generates a tree from the preset or distribution on first spawn, then raises ReadyForBake.
override void OnConnected(Player player)Server: sends the tree's species and segment layout to a joining player.
void Rebake()Raises ReadyForBake again.
18 private and internal methods
MemberWhat it is
private void Awake()Hooks LastPieceChopped to the health object's Killed event.
private void BreakAttachments(WoodcutTree.MapNode node)Unroots every attachment on a node and all its children. Runs before a chopped piece splits off.
private void CheckAttachment(WoodcutTreeModel parent, Transform childTransform)Registers a child TreeAttachment found under a model as attached to that node and saved with it.
private bool EditorSetup()Editor: picks up the rigidbody and health object, caps depenetration velocity and turns the health object off.
private IEnumerator FallingWindow() (iterator)Coroutine that opens the thump window after thumpMinimumFallTime and closes it thumpWindowTime later. Stopped on dedicated servers.
private void HandleSplit(WoodcutTreeModel model, WoodcutTree newTree)Server: pushes the old and new trees apart and spins them to fall, split by their masses. Raises Chopped.
private void LastPieceChopped(DamageData impact)Server: when the health object dies and only the root piece is left, chops that piece and destroys the tree.
private void LookForChildren(WoodcutTreeModel parent)Walks a model's children, adding connected models to the map with their rotation and registering attachments.
private void ModelChopped(WoodcutTreeModel model)Server: when a piece is chopped through, spawns a new tree for it and everything above, then raises Cut and syncs it.
private void MoveTo(WoodcutTree.MapNode node, WoodcutTree newTree, bool isFirst = true)Moves a node and its children into another tree. The first call syncs the new tree and raises its ReadyForBake.
private void OnCollisionEnter(Collision collision)A hit on layer 8 in the thump window raises Thump. Server: strong impacts damage attachments and break points up the tree.
private void OnPartTookDamage(WoodcutTreeModel model, float damage)Passes a model's damage on through PartTookDamage.
private void Spin(Rigidbody rigidbody, Vector3 direction, float velocityMagnitude, float upOffset)Adds velocity in a direction plus angular velocity around an offset, so a piece tips over.
private void Sync(IPlayer player, Stream stream)Method sync handler for the species and the whole node map with attachments. Clients rebuild the tree from it.
private void Sync(WoodcutTree.MapNode node, WoodcutConnection parent, Stream stream)Method sync handler for the species and the whole node map with attachments. Clients rebuild the tree from it.
private void SyncAttachment(TreeAttachment attachment, WoodcutTree.MapNode parent, Stream stream)Reads or writes one attachment's entity ID. Clients add it to the node.
private void SyncCut(IPlayer player, Stream stream)Method sync handler: clients raise Cut when the server reports a cut.
private void TreeIsFell()Breaks the root model through its type. Runs when a piece that was a child becomes a root.

Used in​

Members of other types that take, return or hold a WoodcutTree: