using Core.Extensions; #if UNITY_EDITOR using UnityEngine; #endif namespace TowerDefense.Nodes { /// /// Randomly selects the next node /// public class RandomNodeSelector : NodeSelector { /// /// The sum of all Node weights in m_LinkedNodes /// protected int m_WeightSum; /// /// Gets a random node in the list /// /// The randomly selected node public override Node GetNextNode() { if (linkedNodes == null) { return null; } int totalWeight = m_WeightSum; return linkedNodes.WeightedSelection(totalWeight, t => t.weight); } protected void Awake() { // cache the linked node weights m_WeightSum = TotalLinkedNodeWeights(); } #if UNITY_EDITOR protected override void OnDrawGizmos() { Gizmos.color = Color.cyan; base.OnDrawGizmos(); } #endif /// /// Sums up the weights of the linked nodes for random selection /// /// Weight Sum of Linked Nodes protected int TotalLinkedNodeWeights() { int totalWeight = 0; int count = linkedNodes.Count; for (int i = 0; i < count; i++) { totalWeight += linkedNodes[i].weight; } return totalWeight; } } }