-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathPlanarSteeringBehaviour.cs
144 lines (119 loc) · 5.14 KB
/
PlanarSteeringBehaviour.cs
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
141
142
143
144
using UnityEngine;
using Friedforfun.ContextSteering.Core;
using Friedforfun.ContextSteering.Utilities;
using Unity.Collections;
using Unity.Jobs;
using System;
namespace Friedforfun.ContextSteering.PlanarMovement
{
public abstract class PlanarSteeringBehaviour : CoreSteeringBehaviour<PlanarSteeringParameters>
{
[Tooltip("Is this behaviour attracted to this target or repulsed by it?")]
[SerializeField] protected SteerDirection Direction = SteerDirection.ATTRACT;
[Tooltip("How influential this behaviour is.")]
[SerializeField] protected float Weight = 1f;
[Tooltip("Does the behaviour scale its effect based on distance?")]
[SerializeField] protected bool ScaleOnDistance = false;
[Tooltip("If using scaling, set to true to make targets more important as they approach, false sets targets further away to be more significant.")]
[SerializeField] bool InvertScale = true;
protected float invertScalef { get { return InvertScale ? 1f : 0f; } }
protected float[] steeringMap = null; // The map of weights, each element represents our degree of interest in the direction that element corresponds to.
protected PlanarSteeringParameters steeringParameters;
[SerializeField] public PlanarMapVisualiserParameters MapDebugger;
private NativeArray<float> nextMap;
private NativeArray<Vector3> targetPositions;
//private bool rebuildMap = false;
/// <summary>
/// Instantiates the context map weights and computes the angle between each direction
/// </summary>
/// <param name="steeringParameters"></param>
public override void InstantiateContextMap(PlanarSteeringParameters steeringParameters)
{
this.steeringParameters = steeringParameters;
//this.steeringParameters.OnResolutionChange += MapResolutionChangeHandler;
steeringMap = new float[steeringParameters.ContextMapResolution];
nextMap = new NativeArray<float>(steeringParameters.ContextMapResolution, Allocator.Persistent);
}
/// <summary>
/// Runs when the context map resolution is changed at runtime
/// </summary>
private void MapResolutionChangeHandler(object sender, EventArgs e)
{
//rebuildMap = true;
Debug.Log("Resolution change");
}
/// <summary>
/// Return a context map where the index of the float defines the direction whose value is being inspected, the size of the scalar defines how much we want to move in a direction
/// </summary>
/// <returns></returns>
public virtual float[] GetContextMap()
{
return steeringMap;
}
/// <summary>
/// Get the job required to update the steering map, to be scheduled
/// </summary>
/// <returns>DotToVecJob ready to be scheduled</returns>
public DotToVecJob GetJob()
{
//if (rebuildMap)
//{
// rebuildMap = false;
// nextMap = new NativeArray<float>(steeringParameters.ContextMapResolution, Allocator.Persistent);
//}
Vector3[] targetArr = getPositionVectors();
targetPositions = new NativeArray<Vector3>(targetArr.Length, Allocator.Persistent);
for (int i = 0; i < targetArr.Length; i++)
{
targetPositions[i] = targetArr[i];
}
return new DotToVecJob()
{
targets = targetPositions,
my_position = transform.position,
range = Range,
weight = Weight,
angle = steeringParameters.ResolutionAngle,
Weights = nextMap,
direction = Direction,
scaled = ScaleOnDistance,
invertScale = invertScalef,
axis = steeringParameters.ContextMapRotationAxis
};
}
/// <summary>
/// swaps the completed job data into the steering map, run this when the jobs are complete before scheduling the next job
/// </summary>
public void Swap()
{
targetPositions.Dispose();
float[] next = new float[nextMap.Length];
for (int i = 0; i < nextMap.Length; i++)
{
next[i] = nextMap[i];
}
steeringMap = next;
}
protected abstract Vector3[] getPositionVectors();
/*protected virtual void OnDestroy()
{
steeringParameters.OnResolutionChange -= MapResolutionChangeHandler;
}*/
public virtual void OnDisable()
{
if (nextMap.IsCreated)
nextMap.Dispose();
if (targetPositions.IsCreated)
targetPositions.Dispose();
}
#if UNITY_EDITOR
protected virtual void OnDrawGizmos()
{
if (steeringMap != null && MapDebugger != null)
{
MapVisualiserPlanar.InDrawGizmos(MapDebugger, steeringMap, Range, steeringParameters.ResolutionAngle, transform);
}
}
#endif
}
}