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.
- Listed in Crafting and making
- Inherits or implements
NetworkEntityBehaviour,ISavable<WoodcutTreeSaveData>,ISavableSave<WoodcutTreeSaveData>,ISavableSaveBase - Derived types
TreeEvent,WoodcutTreeEvent,WoodcutTreeSaveData - Nested types
MapNode - Documented in Farming, woodcutting and carpentry
Constructors
| Member | What it is |
|---|---|
WoodcutTree() | Unity makes components itself; use AddComponent or spawn a prefab instead of new. |
Fields
19 private and internal fields
| Member | What it is |
|---|---|
private WoodcutTreeEvent cut | Backing event for Cut. |
private Coroutine fallCoroutine | The running FallingWindow coroutine, stopped on the first ground hit. |
private float fallVelocity | Speed given to pieces as they tip after a split. 1 by default. |
private float fellChopMultiplier | Damage multiplier given to models on a non-kinematic (felled) tree. 1.5 by default. |
private float forceOffset | Up offset used by Spin for the angular push. 1 by default. |
private HealthObject healthObject | Backing field for HealthObject. Its max health is the species damage multiplier; on only while the root has no children. |
private ImpactDetector impactDetector | Detector shared by every model in the tree. Its hardness follows the species. |
private bool isInWindow | True while a ground hit counts as a thump. |
private static Logger logger | NLog logger for this class. |
private WoodcutTreeEvent onThump | Backing event for Thump. |
private float popVelocity | Speed the two parts are pushed apart with on a split. 1 by default. |
private Distribution spawnDistribution | Distribution a preset tree is picked from on first generation when spawnParticular is not set. |
private PresetTree spawnParticular | Backing field for SpawnParticular, the preset tree to build. Saved with the tree. |
private TreeSpecies species | Backing field for Species. Saved with the tree. |
private MethodSync sync | Method sync for the full tree layout. Sent to joining players and after splits. |
private MethodSync syncCut | Method sync that tells clients a cut happened. |
private float thumpMinimumFallTime | Seconds after initialize before a ground hit counts as a thump. 2 by default. |
private float thumpWindowTime | Seconds the thump window stays open. 6 by default. |
private Rigidbody treeRigidbody | Backing field for Rigidbody. Its mass follows the root node's mass. |
Properties
| Member | What 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
| Member | What it is |
|---|---|
event Action Chopped | Raised when a piece splits off this tree. |
event Action<WoodcutTree> Cut | Raised when a segment of this tree is chopped through. Also fires on clients. |
event Action<WoodcutTree, float> PartTookDamage | Server: raised with the damage when any segment takes damage. |
event Action ReadyForBake | Raised when the segment layout is ready or changed, so the baker can rebuild the visuals. |
event Action<WoodcutTree> Thump | Raised when a falling tree hits the ground layer within the thump time window. |
Methods
| Member | What 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
| Member | What 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:
PresetTree.GenerateEditorTreeOnExisting, PresetTree.GenerateRunTimeTree, PresetTree.GenerateRunTimeTreeOnExisting, PresetTree.GenerateRunTimeTreeWithoutParent, PresetTree.HandleEditorStep, PresetTree.HandleStep, PresetTree.SaveFrom, PresetTree.SpawnAndInitWoodcutTree, PresetTree.workingWithTree, TreeDecay.CheckCut, TreeDecay.PartTookDamage, TreeDecay.tree, TreeSpawner.CheckBaseCut, TreeSpawner.tree, WoodcutTree.MapNode.Tree, WoodcutTreeBaker.tree, WoodcutTreeModel.Tree, TreeChopDetector.tree2.