-
Notifications
You must be signed in to change notification settings - Fork 309
Expand file tree
/
Copy pathSecondUnitBrain.cs
More file actions
129 lines (104 loc) · 3.81 KB
/
SecondUnitBrain.cs
File metadata and controls
129 lines (104 loc) · 3.81 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
using GluonGui.Dialog;
using Model;
using Model.Runtime.Projectiles;
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
using Utilities;
using static Unity.IO.LowLevel.Unsafe.AsyncReadManagerMetrics;
namespace UnitBrains.Player
{
public class SecondUnitBrain : DefaultPlayerUnitBrain
{
public override string TargetUnitName => "Cobra Commando";
private const float OverheatTemperature = 3f;
private const float OverheatCooldown = 2f;
private float _temperature = 0f;
private float _cooldownTime = 0f;
static int Numbers = 0;
int UnitNumber = -1;
const int MaxTargets = 3;
private bool _overheated;
private List<Vector2Int> _moveTargets = new();
protected override void GenerateProjectiles(Vector2Int forTarget, List<BaseProjectile> intoList)
{
float overheatTemperature = OverheatTemperature;
if (GetTemperature() >= OverheatTemperature)
return;
IncreaseTemperature();
int projectileCount = GetTemperature();
for (int i = 0; i < projectileCount; i++)
{
var projectile = CreateProjectile(forTarget);
AddProjectileToList(projectile, intoList);
}
}
public override Vector2Int GetNextStep()
{
if (_moveTargets.Count == 0)
return unit.Pos;
Vector2Int target = _moveTargets[0];
if (IsTargetInRange(target))
return unit.Pos;
Vector2Int nextPosition = unit.Pos.CalcNextStepTowards(target);
return nextPosition;
}
protected override List<Vector2Int> SelectTargets()
{
if (UnitNumber == -1)
{
UnitNumber = Numbers;
Numbers++;
}
List<Vector2Int> result = new();
_moveTargets.Clear();
List<Vector2Int> allTargets = GetAllTargets().ToList();
if (allTargets.Count == 0)
{
Vector2Int enemyBase = runtimeModel.RoMap.Bases[
IsPlayerUnitBrain ? RuntimeModel.BotPlayerId : RuntimeModel.PlayerId
];
allTargets.Add(enemyBase);
}
SortByDistanceToOwnBase(allTargets);
int availableTargets = allTargets.Count < MaxTargets ? allTargets.Count : MaxTargets;
int targetIndex = UnitNumber % availableTargets;
Vector2Int selectedTarget = allTargets[targetIndex];
if (IsTargetInRange(selectedTarget))
result.Add(selectedTarget);
else
_moveTargets.Add(selectedTarget);
return result;
}
public override void Update(float deltaTime, float time)
{
if (_overheated)
{
_cooldownTime += Time.deltaTime;
float t = _cooldownTime / (OverheatCooldown/10);
_temperature = Mathf.Lerp(OverheatTemperature, 0, t);
if (t >= 1)
{
_cooldownTime = 0;
_overheated = false;
}
}
}
private int GetTemperature()
{
if(_overheated)
return (int) OverheatTemperature;
else
return (int)_temperature;
}
private void IncreaseTemperature()
{
_temperature += 1f;
if (_temperature >= OverheatTemperature)
_overheated = true;
}
}
}