-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnitAI.cs
129 lines (112 loc) · 3.96 KB
/
UnitAI.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UnitAI : MonoBehaviour
{
public Entity381 entity; //public only for ease of debugging
// Start is called before the first frame update
void Start()
{
entity = GetComponentInParent<Entity381>();
commands = new List<Command>();
intercepts = new List<Intercept>();
moves = new List<Move>();
}
public List<Move> moves;
public List<Command> commands;
public List<Intercept> intercepts;
// Update is called once per frame
void Update()
{
if (commands.Count > 0) {
if (commands[0].IsDone()) {
StopAndRemoveCommand(0);
} else {
commands[0].Tick();
commands[0].isRunning = true;
DecorateAll();
}
}
}
void StopAndRemoveCommand(int index)
{
commands[index].Stop();
commands.RemoveAt(index);
if(commands.Count == 0)
{
entity.desiredSpeed = 0;
}
}
public void StopAndRemoveAllCommands()
{
for(int i = commands.Count - 1; i >= 0; i--) {
StopAndRemoveCommand(i);
}
}
public void AddCommand(Command c)
{
//print("Adding command; " + c.ToString());
c.Init();
commands.Add(c);
if (c is Intercept)
intercepts.Add(c as Intercept);
else if (c is Follow) { }
else
moves.Add(c as Move);
}
public void SetCommand(Command c)
{
//print("Setting command: " + c.ToString());
StopAndRemoveAllCommands();
commands.Clear();
moves.Clear();
intercepts.Clear();
AddCommand(c);
}
//---------------------------------
public void DecorateAll()
{
Command prior = null;
foreach(Command c in commands) {
Decorate(prior, c);
prior = c;
}
}
//decoration logic (UI logic) in general is always convoluted. Ugh
public void Decorate(Command prior, Command current)
{
if (current.line != null) {
current.line.gameObject.SetActive(entity.isSelected);
if (prior == null)
current.line.SetPosition(0, entity.position);
else
current.line.SetPosition(0, prior.line.GetPosition(1));
if (current is Intercept) { //Most specific
Intercept intercept = current as Intercept;
if (intercept.isRunning)//
intercept.line.SetPosition(1, intercept.predictedMovePosition);
else
intercept.line.SetPosition(1, intercept.targetEntity.position);
intercept.line.SetPosition(2, intercept.targetEntity.position);
} else if (current is Follow) { // Less specific
Follow f = current as Follow;
f.line.SetPosition(1, f.targetEntity.position + f.offset);
f.line.SetPosition(2, f.targetEntity.position);
//f.line.SetPosition(1, f.predictedMovePosition);
}
//Moveposition never changes
}
//potential fields lines
if(!(current is Follow) && !(current is Intercept) && AIMgr.inst.isPotentialFieldsMovement){
Move m = current as Move;
m.potentialLine.SetPosition(0, entity.position);
Vector3 newpos = Vector3.zero;
newpos.x = Mathf.Sin(entity.desiredHeading * Mathf.Deg2Rad) * entity.desiredSpeed;
newpos.z = Mathf.Cos(entity.desiredHeading * Mathf.Deg2Rad) * entity.desiredSpeed;
newpos *= 20;
newpos.y = 1;
m.potentialLine.SetPosition(1, entity.position + newpos);
m.potentialLine.gameObject.SetActive(entity.isSelected);
}
}
}