-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMove.cs
112 lines (98 loc) · 3.91 KB
/
Move.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class Move : Command
{
public Vector3 movePosition;
public Move(Entity381 ent, Vector3 pos) : base(ent)
{
movePosition = pos;
}
public LineRenderer potentialLine;
public override void Init()
{
//Debug.Log("MoveInit:\tMoving to: " + movePosition);
line = LineMgr.inst.CreateMoveLine(entity.position, movePosition);
line.gameObject.SetActive(false);
potentialLine = LineMgr.inst.CreatePotentialLine(entity.position);
line.gameObject.SetActive(true);
}
public override void Tick()
{
DHDS dhds;
if (AIMgr.inst.isPotentialFieldsMovement)
dhds = ComputePotentialDHDS();
else
dhds = ComputeDHDS();
entity.desiredHeading = dhds.dh;
entity.desiredSpeed = dhds.ds;
line.SetPosition(1, movePosition);
}
public Vector3 diff = Vector3.positiveInfinity;
public float dhRadians;
public float dhDegrees;
public DHDS ComputeDHDS()
{
diff = movePosition - entity.position;
dhRadians = Mathf.Atan2(diff.x, diff.z);
dhDegrees = Utils.Degrees360(Mathf.Rad2Deg * dhRadians);
return new DHDS(dhDegrees, entity.maxSpeed);
}
public DHDS ComputePotentialDHDS()
{
Potential p;
repulsivePotential = Vector3.one; repulsivePotential.y = 0;
foreach (Entity381 ent in EntityMgr.inst.entities)
{
if (ent == entity) continue;
p = DistanceMgr.inst.GetPotential(entity, ent);
if (p.distance < AIMgr.inst.potentialDistanceThreshold)
{
repulsivePotential += p.direction * entity.mass *
AIMgr.inst.repulsiveCoefficient * Mathf.Pow(p.diff.magnitude, AIMgr.inst.repulsiveExponent);
//repulsivePotential += p.diff;
}
}
foreach (Obstacle ent in EntityMgr.inst.obstacles)
{
if (ent == entity) continue;
Vector3 point = ent.transform.position - entity.position;
if (point.magnitude < AIMgr.inst.potentialDistanceThreshold)
{
repulsivePotential += point.normalized * entity.mass *
AIMgr.inst.repulsiveCoefficient * Mathf.Pow(point.magnitude, AIMgr.inst.repulsiveExponent);
//repulsivePotential += p.diff;
}
}
//repulsivePotential *= repulsiveCoefficient * Mathf.Pow(repulsivePotential.magnitude, repulsiveExponent);
attractivePotential = movePosition - entity.position;
Vector3 tmp = attractivePotential.normalized;
attractivePotential = tmp *
AIMgr.inst.attractionCoefficient * Mathf.Pow(attractivePotential.magnitude, AIMgr.inst.attractiveExponent);
potentialSum = attractivePotential - repulsivePotential;
dh = Utils.Degrees360(Mathf.Rad2Deg * Mathf.Atan2(potentialSum.x, potentialSum.z));
angleDiff = Utils.Degrees360(Utils.AngleDiffPosNeg(dh, entity.heading));
cosValue = (Mathf.Cos(angleDiff * Mathf.Deg2Rad) + 1) / 2.0f; // makes it between 0 and 1
ds = entity.maxSpeed * cosValue;
return new DHDS(dh, ds);
}
public Vector3 attractivePotential = Vector3.zero;
public Vector3 potentialSum = Vector3.zero;
public Vector3 repulsivePotential = Vector3.zero;
public float dh;
public float angleDiff;
public float cosValue;
public float ds;
public float doneDistanceSq = 1000;
public override bool IsDone()
{
return ((entity.position - movePosition).sqrMagnitude < doneDistanceSq);
}
public override void Stop()
{
entity.desiredSpeed = 0;
LineMgr.inst.DestroyLR(line);
LineMgr.inst.DestroyLR(potentialLine);
}
}