-
Notifications
You must be signed in to change notification settings - Fork 309
Expand file tree
/
Copy pathBaseUnitBrain.cs
More file actions
167 lines (138 loc) · 5.5 KB
/
BaseUnitBrain.cs
File metadata and controls
167 lines (138 loc) · 5.5 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
using System.Collections.Generic;
using System.Linq;
using Model;
using Model.Runtime.Projectiles;
using Model.Runtime.ReadOnly;
using UnitBrains.Pathfinding;
using UnityEngine;
using Utilities;
using Unit = Model.Runtime.Unit;
namespace UnitBrains
{
public abstract class BaseUnitBrain
{
public virtual string TargetUnitName => string.Empty;
public virtual bool IsPlayerUnitBrain => true;
public virtual BaseUnitPath ActivePath => _activePath;
protected Unit unit { get; private set; }
protected IReadOnlyRuntimeModel runtimeModel => ServiceLocator.Get<IReadOnlyRuntimeModel>();
private BaseUnitPath _activePath = null;
private readonly Vector2[] _projectileShifts = new Vector2[]
{
new (0f, 0f),
new (0.15f, 0f),
new (0f, 0.15f),
new (0.15f, 0.15f),
new (0.15f, -0.15f),
new (-0.15f, 0.15f),
new (-0.15f, -0.15f),
};
public virtual Vector2Int GetNextStep()
{
if (HasTargetsInRange())
return unit.Pos;
var target = runtimeModel.RoMap.Bases[
IsPlayerUnitBrain ? RuntimeModel.BotPlayerId : RuntimeModel.PlayerId];
_activePath = new AstarUnitPath(runtimeModel, unit.Pos, target);
return _activePath.GetNextStepFrom(unit.Pos);
}
public List<BaseProjectile> GetProjectiles()
{
List<BaseProjectile> result = new ();
foreach (var target in SelectTargets())
{
GenerateProjectiles(target, result);
}
for (int i = 0; i < result.Count; i++)
{
var proj = result[i];
proj.AddStartShift(_projectileShifts[i % _projectileShifts.Length]);
}
return result;
}
public void SetUnit(Unit unit)
{
this.unit = unit;
}
public virtual void Update(float deltaTime, float time)
{
}
protected virtual void GenerateProjectiles(Vector2Int forTarget, List<BaseProjectile> intoList)
{
AddProjectileToList(CreateProjectile(forTarget), intoList);
}
protected virtual List<Vector2Int> SelectTargets()
{
var result = GetReachableTargets();
while (result.Count > 1)
result.RemoveAt(result.Count - 1);
return result;
}
protected BaseProjectile CreateProjectile(Vector2Int target) =>
BaseProjectile.Create(unit.Config.ProjectileType, unit, unit.Pos, target, unit.Config.Damage);
protected void AddProjectileToList(BaseProjectile projectile, List<BaseProjectile> list) =>
list.Add(projectile);
protected IReadOnlyUnit GetUnitAt(Vector2Int pos) =>
runtimeModel.RoUnits.FirstOrDefault(u => u.Pos == pos);
protected List<IReadOnlyUnit> GetUnitsInRadius(float radius, bool enemies)
{
var units = new List<IReadOnlyUnit>();
var pos = unit.Pos;
var distanceSqr = radius * radius;
foreach (var otherUnit in runtimeModel.RoUnits)
{
if (otherUnit == unit)
continue;
if (enemies != (otherUnit.Config.IsPlayerUnit == unit.Config.IsPlayerUnit))
continue;
var otherPos = otherUnit.Pos;
var diff = otherPos - pos;
var distance = diff.sqrMagnitude;
if (distance <= distanceSqr)
units.Add(otherUnit);
}
return units;
}
protected bool HasTargetsInRange()
{
var attackRangeSqr = unit.Config.AttackRange * unit.Config.AttackRange;
foreach (var possibleTarget in GetAllTargets())
{
var diff = possibleTarget - unit.Pos;
if (diff.sqrMagnitude < attackRangeSqr)
return true;
}
return false;
}
protected IEnumerable<IReadOnlyUnit> GetAllEnemyUnits()
{
return runtimeModel.RoUnits
.Where(u => u.Config.IsPlayerUnit != IsPlayerUnitBrain);
}
protected IEnumerable<Vector2Int> GetAllTargets()
{
return runtimeModel.RoUnits
.Where(u => u.Config.IsPlayerUnit != IsPlayerUnitBrain)
.Select(u => u.Pos)
.Append(runtimeModel.RoMap.Bases[IsPlayerUnitBrain ? RuntimeModel.BotPlayerId : RuntimeModel.PlayerId]);
}
protected bool IsTargetInRange(Vector2Int targetPos)
{
var attackRangeSqr = unit.Config.AttackRange * unit.Config.AttackRange;
var diff = targetPos - unit.Pos;
return diff.sqrMagnitude <= attackRangeSqr;
}
protected List<Vector2Int> GetReachableTargets()
{
var result = new List<Vector2Int>();
var attackRangeSqr = unit.Config.AttackRange * unit.Config.AttackRange;
foreach (var possibleTarget in GetAllTargets())
{
if (!IsTargetInRange(possibleTarget))
continue;
result.Add(possibleTarget);
}
return result;
}
}
}