private void Update() { currentOxygen += generationRate * Time.deltaTime; if (providingRadius == 0 || providingRate == 0.0f) { return; } List <OxygenTracker> trackers = new List <OxygenTracker>(); for (int x = -(int)providingRadius; x <= (int)providingRadius; x++) { for (int y = -(int)providingRadius; y <= (int)providingRadius; y++) { Vector2Int point = new Vector2Int(x, y); if (point.magnitude <= providingRadius) { foreach (GridElement e in WorldGrid.Instance.ElementsAtPosition(point + gridElement.position)) { OxygenTracker tracker = e.GetComponent <OxygenTracker>(); if (tracker != null && tracker != this && tracker.oxygenPriority > oxygenPriority) { trackers.Add(tracker); } } } } } foreach (OxygenTracker tracker in trackers) { float toGive = Mathf.Min(providingRate * Time.deltaTime, currentOxygen); float room = tracker.maxOxygen - tracker.currentOxygen; float given = Mathf.Min(toGive, room); tracker.currentOxygen += given; currentOxygen -= given; } }
// Update is called once per frame void Update() { switch (currentState) { case State.RefillingAtLung: OxygenTracker closestLung = null; float highestPriorityLungValue = Mathf.Infinity; foreach (OxygenTracker tracker in OxygenTracker.allTrackers) { if (tracker.oxygenPriority < oxygenTracker.oxygenPriority) { float distance = Vector2Int.Distance(oxygenTracker.gridElement.position, tracker.gridElement.position); float percentageFull = tracker.currentOxygen / tracker.maxOxygen; float lungPriority = percentageFull / (distance / 10 + 0.1f); if (percentageFull < 0.1f) { lungPriority = Mathf.NegativeInfinity; } if (lungPriority < highestPriorityLungValue) { highestPriorityLungValue = distance; closestLung = tracker; } } } if (closestLung != null) { navAgent.destination = closestLung.gridElement.position; } if (oxygenTracker.currentOxygen == oxygenTracker.maxOxygen) { currentState = State.Delivering; } break; case State.Delivering: if (oxygenTracker.currentOxygen == 0) { currentState = State.RefillingAtLung; } OxygenTracker highestPriority = null; float highestPriorityValue = Mathf.NegativeInfinity; foreach (OxygenTracker tracker in OxygenTracker.allTrackers) { if (tracker.oxygenPriority > oxygenTracker.oxygenPriority) { float distance = Vector2Int.Distance(oxygenTracker.gridElement.position, tracker.gridElement.position); float percentageFull = tracker.currentOxygen / tracker.maxOxygen; float priority = 1 / ((percentageFull + 0.1f) * (distance / 10.0f + 0.1f)); if (percentageFull == 1) { priority = Mathf.NegativeInfinity; } if (priority > highestPriorityValue) { highestPriorityValue = priority; highestPriority = tracker; } } } if (highestPriority != null) { navAgent.destination = highestPriority.gridElement.position; } break; } }