summaryrefslogtreecommitdiff
path: root/Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/ExampleScenes/Scenes/OldExamples/Example18_RTS/RTSHarvester.cs
blob: ac96d2dc71c95233f517853ba103ca1d480a7cad (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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
using UnityEngine;
using System.Collections;
using System.Linq;
using Pathfinding;
using Pathfinding.Examples.RTS;

namespace Pathfinding.Examples.RTS {
	[HelpURL("https://arongranberg.com/astar/documentation/stable/rtsharvester.html")]
	public class RTSHarvester : MonoBehaviour {
		RTSUnit unit;
		Animator animator;

		void Awake () {
			unit = GetComponent<RTSUnit>();
			animator = GetComponent<Animator>();

			ctx = new BTContext {
				animator = animator,
				transform = transform,
				unit = unit
			};
		}

		BTNode behave;
		BTContext ctx;
		// Use this for initialization
		void Start () {
			StartCoroutine(StateMachine());
			behave = Behaviors.HarvestBehavior();
		}

		RTSHarvestableResource FindFreeResource () {
			/*var resources = FindObjectsOfType<RTSHarvestableResource>().Where(c => c.reservedBy == null).ToArray();
			RTSHarvestableResource closest = null;
			var dist = float.PositiveInfinity;
			var point = transform.position;
			for (int i = 0; i < resources.Length; i++) {
			    var d = (resources[i].transform.position - point).sqrMagnitude;
			    if (d < dist) {
			        dist = d;
			        closest = resources[i];
			    }
			}
			return closest;*/
			return null;
		}

		void OnDestroy () {
			behave.Terminate(ctx);
		}

		IEnumerator StateMachine () {
			yield break;
		}

		// Update is called once per frame
		void Update () {
			behave.Tick(ctx);
		}
	}
}