[ad_1]
I’d resolve this by turning every chain right into a linked record with every node having a reference to the subsequent node within the chain. So yow will discover out it a node is related by following that chain of references till you discover a node which is both a hub (then it’s related) or a node which does not have a subsequent node (then it isn’t related).
That means we now have two sorts of nodes, ConnectionHub
and ConnectionNode
which each inherit from an summary base class Connectable
.
The summary base class for each would outline the tactic wanted to seek out out if there’s a hub connection:
public summary class Connectable : MonoBehavior {
public summary bool isConnectedToHub();
}
The common nodes have a public variable connectedTo
. This variable must be assigned to the subsequent Connectable, which generally is a hub, one other node or nothing if the node is disconnected. If you name the isConnectedToHub()
technique on an everyday node, it will return false if it is not related. If it’s related, it will delegate the response to the subsequent node up the chain:
public class ConnectionNode : Connectable {
public Connectable connectedTo;
public override bool isConnectedToHub() {
if (connectedTo == null) {
return false;
} else {
return connectedTo.isConnectedToHub();
}
}
}
The connection hub has no subsequent node. It would return true
. That outcome would then get handed down the decision chain and again to the unique caller:
public class ConnectionHub : Connectable {
public override bool isConnectedToHub() {
return true;
}
}
Alternative resolution:
When hyperlink modifications are comparatively uncommon occasions however checking if a node is related occurs rather a lot, then the above resolution may get unperformant. Especially when you have very lengthy chains and hold checking totally different members of the identical chain time and again. It can be higher if every node would know whether or not or not it’s related proper now, so that you needn’t reevaluate it on a regular basis.
In that case you can additionally do it the opposite means round: Nodes do not know their dad or mum, they know their descendant. When a node will get disconnected, then it propagates this standing down the chain to set all its descendants to disconnected.
When a node will get a brand new descendant, then it first disconnects the outdated descendant as beforehand described after which passes by itself connection standing (related or not) down the brand new descendant chain.
public class ConnectionNode : MonoBehaviour {
non-public ConnectionNode descendant;
non-public bool isConnectedToHub;
public void DisconnectDescendant() {
if (descendant != null) {
descendant.SetConnected(false);
descentant = null:
}
}
public void ConnectNewDescendant(ConnectionNode newDescendant) {
DisconnectDescendant();
descendant = newDescendant;
descendant.SetConnected(isConnectedToHub);
}
non-public void SetConnected(bool newConnectionStatus) {
isConnectedToHub = newConnectionStatus;
if (descendant != null) descendant.SetConnected(newConnectionStatus);
}
}
If you wish to flip a node right into a hub node, you simply have to name SetConnected(true)
on that node. That will make all its descendants related nodes and in addition trigger the node to mark any future descendants it receives as related.
[ad_2]