summaryrefslogtreecommitdiff
path: root/Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/ExampleScenes/Scenes/OldExamples/Example18_RTS/RTSResourceDeterioration.cs
blob: 01160013d04878c20aa7dcbc4e839594fc1e0441 (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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Pathfinding.Examples.RTS {
	[HelpURL("https://arongranberg.com/astar/documentation/stable/rtsresourcedeterioration.html")]
	public class RTSResourceDeterioration : MonoBehaviour {
		RTSHarvestableResource resource;
		public Transform offsetRoot;
		public float maxOffset;
		float initialResources;

		void Start () {
			resource = GetComponent<RTSHarvestableResource>();
			initialResources = resource.value;
		}

		// Update is called once per frame
		void Update () {
			var offset = Mathf.Clamp((initialResources - resource.value) / Mathf.Max(1f, initialResources), 0.0f, 1.0f);

			if (resource.value > 0) {
				offset *= 0.8f;
			}

			offsetRoot.localPosition = Vector3.Lerp(offsetRoot.localPosition, Vector3.down * offset * maxOffset, 2 * Time.deltaTime);
		}
	}
}