-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPositionManager.cs
More file actions
140 lines (115 loc) · 3.97 KB
/
PositionManager.cs
File metadata and controls
140 lines (115 loc) · 3.97 KB
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
130
131
132
133
134
135
136
137
138
139
140
// //////////////////////////////
// Authors: Laurence (Git: SirLorrence)
// //////////////////////////////
using UnityEngine;
using System.Collections.Generic;
using System.Collections;
public class PositionManager : MonoBehaviour {
private static PositionManager _instance;
public static PositionManager Instance {
get {
if (_instance == null)
SetupInstance();
return _instance;
}
}
[SerializeField] private PositionScoring[] AIScoring;
[SerializeField] private Transform _target;
[SerializeField] private float rings;
private const int _initialPositions = 8;
private float _positions;
private Vector3 _pos;
private const float _radiusSpacing = .5f; //radius spacing
private PositioningPoint[] _points;
public PositioningPoint[] Points => _points;
public Transform Target => _target;
public float Rings => rings;
public void SetPositionAssignment(int index, bool value) {
Debug.Log("Called");
_points[index].AssignedStatus = value;
}
private static void SetupInstance() {
_instance = FindObjectOfType<PositionManager>();
if (_instance == null) {
GameObject gameObject = new GameObject();
gameObject.name = "Position Manager";
_instance = gameObject.AddComponent<PositionManager>();
DontDestroyOnLoad(gameObject);
}
}
private void Awake() {
// Lazy initialize
if (_instance == null) {
_instance = this;
DontDestroyOnLoad(gameObject);
}
else Destroy(gameObject);
}
private void Start() {
_points = InitializePoints();
AIScoring = GameObject.FindObjectsOfType<PositionScoring>();
if (AIScoring.Length > 0) {
StartCoroutine(UpdatePawns());
}
}
private void Update() {
UpdatePointLocation();
}
private PositioningPoint[] InitializePoints() {
// Using a Dynamic Array during the creation of the positions then using a static(sized) array
List<PositioningPoint> initialSetPosition = new List<PositioningPoint>();
//starting from 1 so the first ring doesn't spawn into the player
for (int i = 1; i <= rings; i++) {
_positions = _initialPositions * i;
for (int j = 0; j < _positions; j++) {
float radians = 2 * Mathf.PI / _positions * j;
Vector3 newPoint = new Vector3(Mathf.Sin(radians), 0, Mathf.Cos(radians));
float ringSpacing = i + _radiusSpacing;
Vector3 creationPoint = (newPoint * ringSpacing) + _target.position;
Vector3 vecAwayFromTarget = creationPoint - _target.position;
PositioningPoint point = new PositioningPoint {
CurrentPos = creationPoint,
OffsetPos = vecAwayFromTarget,
AssignedStatus = false
};
initialSetPosition.Add(point);
}
}
return initialSetPosition.ToArray();
}
private void UpdatePointLocation() {
for (int i = 0; i < _points.Length; i++) {
Vector3 updateLoc = _points[i].OffsetPos + _target.position;
updateLoc.y = 0;
_points[i].CurrentPos = updateLoc;
}
}
//TODO: Make this a job
private IEnumerator UpdatePawns() {
while (true) {
foreach (var pawn in AIScoring) {
yield return new WaitForSeconds(.25f); // to avoid position collision (data race when selecting a point)
// Debug.Log($"[Position Manager] Update Pawn Location");
pawn.Evaluate();
}
}
}
#if DEBUG
private void OnDrawGizmosSelected() {
//draw points
foreach (var positionPoint in _points) {
var point = positionPoint.CurrentPos;
Gizmos.color = (positionPoint.AssignedStatus) ? Color.red : Color.green;
Gizmos.DrawSphere(point, .25f);
}
}
#endif
}
public struct PositioningPoint {
public Vector3 CurrentPos { get; set; }
/// <summary>
/// Fixed Position from the target object
/// </summary>
public Vector3 OffsetPos { get; set; }
public bool AssignedStatus { get; set; }
}