summaryrefslogtreecommitdiff
path: root/Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/ExampleScenes/Scenes/OldExamples/Example18_RTS/RTSResourceView.cs
blob: 6c471330386502639eb856ad2af913651be4cb05 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
using UnityEngine;
using UnityEngine.UI;

namespace Pathfinding.Examples.RTS {
	[HelpURL("https://arongranberg.com/astar/documentation/stable/rtsresourceview.html")]
	public class RTSResourceView : VersionedMonoBehaviour {
		public float adjustmentSpeed = 10;

		[System.Serializable]
		public class Item {
			public RTSUnit.Type resource;
			public string name;
			public Text label;
			float smoothedValue;

			public void Tick (RTSPlayerResources resources, float adjustmentSpeed) {
				float val = resources.GetResource(resource);
				var diff = Mathf.Abs(val - smoothedValue);
				var dv = Mathf.Min(diff, Mathf.Max(diff * adjustmentSpeed * Time.deltaTime, 10f * adjustmentSpeed * Time.deltaTime));

				smoothedValue += dv * Mathf.Sign(val - smoothedValue);
				label.text = name + ": " + Mathf.Round(smoothedValue).ToString("0");
			}
		}

		public int team;
		public Item[] items;

		void Update () {
			var resources = RTSManager.instance.GetPlayer(team).resources;

			for (int i = 0; i < items.Length; i++) {
				items[i].Tick(resources, adjustmentSpeed);
			}
		}
	}
}