summaryrefslogtreecommitdiff
path: root/Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/ExampleScenes/Scenes/LocalAvoidance/GroupController.cs
blob: 35dc0d6eddbeb139cbf6bfc0e32380bbb9262ef6 (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
using UnityEngine;
using System.Collections.Generic;
using Pathfinding.RVO;
using Pathfinding.Util;

namespace Pathfinding.Examples {
	/// <summary>
	/// RVO Example Scene Unit Controller.
	/// Controls AIs and camera in the RVO example scene.
	/// </summary>
	[HelpURL("https://arongranberg.com/astar/documentation/stable/groupcontroller.html")]
	public class GroupController : MonoBehaviour {
		public GUIStyle selectionBox;
		public bool adjustCamera = true;

		Vector2 start, end;
		bool wasDown = false;


		List<RVOExampleAgent> selection = new List<RVOExampleAgent>();

		SimulatorBurst sim;

		Camera cam;

		public void Start () {
			cam = Camera.main;
			var simu = RVOSimulator.active;
			if (simu == null) {
				this.enabled = false;
				throw new System.Exception("No RVOSimulator in the scene. Please add one");
			}

			sim = simu.GetSimulator();
		}

		public void Update () {
			if (adjustCamera) {
				// Adjust camera
				var bounds = sim.AgentBounds;
				float max = Mathf.Max(Mathf.Max(bounds.xMax, -bounds.xMin), Mathf.Max(bounds.yMax, -bounds.yMin));

				float hh = max / Mathf.Tan((cam.fieldOfView*Mathf.Deg2Rad/2.0f));
				float hv = max / Mathf.Tan(Mathf.Atan(Mathf.Tan(cam.fieldOfView*Mathf.Deg2Rad/2.0f)*cam.aspect));

				var yCoord = Mathf.Max(hh, hv)*1.1f;
				yCoord = Mathf.Max(yCoord, 20);
				yCoord = Mathf.Min(yCoord, cam.farClipPlane - 1f);
				cam.transform.position = Vector3.Lerp(cam.transform.position, new Vector3(0, yCoord, 0), Time.smoothDeltaTime*2);
			}

			if (Input.GetKey(KeyCode.A) && Input.GetKeyDown(KeyCode.Mouse0)) {
				Order();
			}
		}

		// Update is called once per frame
		void OnGUI () {
			if (Event.current.type == EventType.MouseUp && Event.current.button == 0 && !Input.GetKey(KeyCode.A)) {
				Select(start, end);
				wasDown = false;
			}

			if (Event.current.type == EventType.MouseDrag && Event.current.button == 0) {
				end = Event.current.mousePosition;

				if (!wasDown) { start = end; wasDown = true; }
			}

			if (Input.GetKey(KeyCode.A)) wasDown = false;
			if (wasDown) {
				Rect r = Rect.MinMaxRect(Mathf.Min(start.x, end.x), Mathf.Min(start.y, end.y), Mathf.Max(start.x, end.x), Mathf.Max(start.y, end.y));
				if (r.width > 4 && r.height > 4)
					GUI.Box(r, "", selectionBox);
			}
		}

		public void Order () {
			Ray ray = cam.ScreenPointToRay(Input.mousePosition);
			RaycastHit hit;

			if (Physics.Raycast(ray, out hit)) {
				float radsum = 0;
				for (int i = 0; i < selection.Count; i++) radsum += selection[i].GetComponent<RVOController>().radius;

				float radius = radsum / (Mathf.PI);
				radius *= 2f;

				for (int i = 0; i < selection.Count; i++) {
					float deg = 2*Mathf.PI*i/selection.Count;
					Vector3 p = hit.point + new Vector3(Mathf.Cos(deg), 0, Mathf.Sin(deg))*radius;
					//Debug.DrawRay (p,Vector3.up*4,Color.cyan);
					//Debug.Break();
					selection[i].SetTarget(p);
					selection[i].SetColor(GetColor(deg));
					selection[i].RecalculatePath();
				}
			}
		}

		public void Select (Vector2 _start, Vector2 _end) {
			_start.y = Screen.height - _start.y;
			_end.y = Screen.height - _end.y;

			Vector2 start = Vector2.Min(_start, _end);
			Vector2 end = Vector2.Max(_start, _end);

			if ((end-start).sqrMagnitude < 4*4) return;

			selection.Clear();

			RVOExampleAgent[] rvo = UnityCompatibility.FindObjectsByTypeSorted<RVOExampleAgent>();
			for (int i = 0; i < rvo.Length; i++) {
				Vector2 sp = cam.WorldToScreenPoint(rvo[i].transform.position);
				if (sp.x > start.x && sp.y > start.y && sp.x < end.x && sp.y < end.y) {
					selection.Add(rvo[i]);
				}
			}
		}

		/// <summary>Radians to degrees constant</summary>
		const float rad2Deg = 360.0f/ ((float)System.Math.PI*2);

		/// <summary>Color from an angle</summary>
		public Color GetColor (float angle) {
			return AstarMath.HSVToRGB(angle * rad2Deg, 0.8f, 0.6f);
		}
	}
}